Skip to content

Commit

Permalink
store miner fees
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed May 15, 2024
1 parent bc3cda4 commit 1c705ee
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions explorer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Transaction struct {
SiafundOutputs []SiafundOutput `json:"siafundOutputs,omitempty"`
FileContracts []FileContract `json:"fileContracts,omitempty"`
FileContractRevisions []FileContractRevision `json:"fileContractRevisions,omitempty"`
MinerFees []types.Currency `json:"minerFees,omitempty"`
ArbitraryData [][]byte `json:"arbitraryData,omitempty"`
}

Expand Down
19 changes: 18 additions & 1 deletion persist/sqlite/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ func addMinerPayouts(tx *txn, bid types.BlockID, scos []types.SiacoinOutput, dbI
return nil
}

func addMinerFees(tx *txn, id int64, txn types.Transaction) error {
stmt, err := tx.Prepare(`INSERT INTO transaction_miner_fees(transaction_id, transaction_order, fee) VALUES (?, ?, ?)`)
if err != nil {
return fmt.Errorf("addMinerFees: failed to prepare statement: %w", err)
}
defer stmt.Close()

for i, fee := range txn.MinerFees {
if _, err := stmt.Exec(id, i, encode(fee)); err != nil {
return fmt.Errorf("addMinerFees: failed to execute statement: %w", err)
}
}
return nil
}

func addArbitraryData(tx *txn, id int64, txn types.Transaction) error {
stmt, err := tx.Prepare(`INSERT INTO transaction_arbitrary_data(transaction_id, transaction_order, data) VALUES (?, ?, ?)`)

Expand Down Expand Up @@ -262,7 +277,9 @@ func addTransactions(tx *txn, bid types.BlockID, txns []types.Transaction, scDBI
if exist {
continue
}
if err := addArbitraryData(tx, txnID, txn); err != nil {
if err := addMinerFees(tx, txnID, txn); err != nil {
return fmt.Errorf("failed to add miner fees: %w", err)
} else if err := addArbitraryData(tx, txnID, txn); err != nil {
return fmt.Errorf("failed to add arbitrary data: %w", err)
} else if err := addSiacoinInputs(tx, txnID, txn); err != nil {
return fmt.Errorf("failed to add siacoin inputs: %w", err)
Expand Down
9 changes: 9 additions & 0 deletions persist/sqlite/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ CREATE TABLE transaction_arbitrary_data (

CREATE INDEX transaction_arbitrary_data_transaction_id_index ON transaction_arbitrary_data(transaction_id);

CREATE TABLE transaction_miner_fees (
transaction_id INTEGER REFERENCES transactions(id) ON DELETE CASCADE NOT NULL,
transaction_order INTEGER NOT NULL,
fee BLOB NOT NULL,
UNIQUE(transaction_id, transaction_order)
);

CREATE INDEX transaction_miner_fees_transaction_id_index ON transaction_miner_fees(transaction_id);

CREATE TABLE transaction_siacoin_inputs (
transaction_id INTEGER REFERENCES transactions(id) ON DELETE CASCADE NOT NULL,
transaction_order INTEGER NOT NULL,
Expand Down
32 changes: 31 additions & 1 deletion persist/sqlite/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ import (
"go.sia.tech/explored/explorer"
)

// transactionMinerFee returns the miner fees for each transaction.
func transactionMinerFee(tx *txn, txnIDs []int64) (map[int64][]types.Currency, error) {
query := `SELECT transaction_id, fee
FROM transaction_miner_fees
WHERE transaction_id IN (` + queryPlaceHolders(len(txnIDs)) + `)
ORDER BY transaction_order ASC`
rows, err := tx.Query(query, queryArgs(txnIDs)...)
if err != nil {
return nil, err
}
defer rows.Close()

result := make(map[int64][]types.Currency)
for rows.Next() {
var txnID int64
var fee types.Currency
if err := rows.Scan(&txnID, decode(&fee)); err != nil {
return nil, fmt.Errorf("failed to scan arbitrary data: %w", err)
}
result[txnID] = append(result[txnID], fee)
}
return result, nil
}

// transactionArbitraryData returns the arbitrary data for each transaction.
func transactionArbitraryData(tx *txn, txnIDs []int64) (map[int64][][]byte, error) {
query := `SELECT transaction_id, data
Expand Down Expand Up @@ -353,6 +377,11 @@ func (s *Store) getTransactions(tx *txn, dbIDs []int64) ([]explorer.Transaction,
return nil, fmt.Errorf("getTransactions: failed to get arbitrary data: %w", err)
}

txnMinerFees, err := transactionMinerFee(tx, dbIDs)
if err != nil {
return nil, fmt.Errorf("getTransactions: failed to get miner fees: %w", err)
}

txnSiacoinInputs, err := transactionSiacoinInputs(tx, dbIDs)
if err != nil {
return nil, fmt.Errorf("getTransactions: failed to get siacoin inputs: %w", err)
Expand Down Expand Up @@ -389,13 +418,14 @@ func (s *Store) getTransactions(tx *txn, dbIDs []int64) ([]explorer.Transaction,
var results []explorer.Transaction
for _, dbID := range dbIDs {
txn := explorer.Transaction{
ArbitraryData: txnArbitraryData[dbID],
SiacoinInputs: txnSiacoinInputs[dbID],
SiacoinOutputs: txnSiacoinOutputs[dbID],
SiafundInputs: txnSiafundInputs[dbID],
SiafundOutputs: txnSiafundOutputs[dbID],
FileContracts: txnFileContracts[dbID],
FileContractRevisions: txnFileContractRevisions[dbID],
MinerFees: txnMinerFees[dbID],
ArbitraryData: txnArbitraryData[dbID],
}
results = append(results, txn)
}
Expand Down

0 comments on commit 1c705ee

Please sign in to comment.