From 393126a53bdb4a1cc08e86e27af5618de68f900a Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Wed, 16 Oct 2024 15:12:24 -0400 Subject: [PATCH] store new foundation address / miner fee --- explorer/types.go | 3 +++ persist/sqlite/init.sql | 5 +++- persist/sqlite/v2consensus.go | 9 +++++-- persist/sqlite/v2transactions.go | 42 ++++++++++++++++++++++++++++++-- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/explorer/types.go b/explorer/types.go index 57733697..798432be 100644 --- a/explorer/types.go +++ b/explorer/types.go @@ -171,6 +171,9 @@ type V2Transaction struct { ID types.TransactionID `json:"id"` ArbitraryData []byte `json:"arbitraryData,omitempty"` + NewFoundationAddress *types.Address `json:"newFoundationAddress,omitempty"` + MinerFee types.Currency `json:"minerFee"` + HostAnnouncements []chain.HostAnnouncement `json:"hostAnnouncements,omitempty"` } diff --git a/persist/sqlite/init.sql b/persist/sqlite/init.sql index a34074ca..2ba74761 100644 --- a/persist/sqlite/init.sql +++ b/persist/sqlite/init.sql @@ -255,7 +255,10 @@ CREATE INDEX transaction_file_contract_revisions_transaction_id_index ON transac CREATE TABLE v2_transactions ( id INTEGER PRIMARY KEY, - transaction_id BLOB UNIQUE NOT NULL + transaction_id BLOB UNIQUE NOT NULL, + + new_foundation_address BLOB, + miner_fee BLOB NOT NULL ); CREATE INDEX v2_transactions_transaction_id_index ON v2_transactions(transaction_id); diff --git a/persist/sqlite/v2consensus.go b/persist/sqlite/v2consensus.go index 43874134..42347644 100644 --- a/persist/sqlite/v2consensus.go +++ b/persist/sqlite/v2consensus.go @@ -29,7 +29,7 @@ func addV2Transactions(tx *txn, bid types.BlockID, txns []types.V2Transaction) ( } defer checkTransactionStmt.Close() - insertTransactionStmt, err := tx.Prepare(`INSERT INTO v2_transactions (transaction_id) VALUES (?)`) + insertTransactionStmt, err := tx.Prepare(`INSERT INTO v2_transactions (transaction_id, new_foundation_address, miner_fee) VALUES (?, ?, ?)`) if err != nil { return nil, fmt.Errorf("failed to prepare insert v2_transaction statement: %v", err) } @@ -52,7 +52,12 @@ func addV2Transactions(tx *txn, bid types.BlockID, txns []types.V2Transaction) ( } if !exist { - result, err := insertTransactionStmt.Exec(encode(txn.ID())) + var newFoundationAddress any + if txn.NewFoundationAddress != nil { + newFoundationAddress = encode(txn.NewFoundationAddress) + } + + result, err := insertTransactionStmt.Exec(encode(txn.ID()), newFoundationAddress, encode(txn.MinerFee)) if err != nil { return nil, fmt.Errorf("failed to insert into v2_transactions: %w", err) } diff --git a/persist/sqlite/v2transactions.go b/persist/sqlite/v2transactions.go index 666dee26..36657998 100644 --- a/persist/sqlite/v2transactions.go +++ b/persist/sqlite/v2transactions.go @@ -83,12 +83,46 @@ WHERE transaction_id IN (` + queryPlaceHolders(len(txnIDs)) + `)` return result, nil } +type v2OtherFields struct { + newFoundationAddress *types.Address + minerFee types.Currency +} + +// v2TransactionOtherFields returns the new foundation address and miner fee of a v2 +// transaction. +func v2TransactionOtherFields(tx *txn, txnIDs []int64) (map[int64]v2OtherFields, error) { + query := `SELECT id, new_foundation_address, miner_fee +FROM v2_transactions +WHERE id IN (` + queryPlaceHolders(len(txnIDs)) + `)` + rows, err := tx.Query(query, queryArgs(txnIDs)...) + if err != nil { + return nil, err + } + defer rows.Close() + + result := make(map[int64]v2OtherFields) + for rows.Next() { + var txnID int64 + var fields v2OtherFields + if err := rows.Scan(&txnID, decodeNull(&fields.newFoundationAddress), decode(&fields.minerFee)); err != nil { + return nil, fmt.Errorf("failed to scan new foundation address and miner fee: %w", err) + } + result[txnID] = fields + } + return result, nil +} + func getV2Transactions(tx *txn, idMap map[int64]transactionID) ([]explorer.V2Transaction, error) { dbIDs := make([]int64, len(idMap)) for order, id := range idMap { dbIDs[order] = id.dbID } + txnOtherFields, err := v2TransactionOtherFields(tx, dbIDs) + if err != nil { + return nil, fmt.Errorf("getV2Transactions: failed to get other fields: %w", err) + } + txnArbitraryData, err := v2TransactionArbitraryData(tx, dbIDs) if err != nil { return nil, fmt.Errorf("getV2Transactions: failed to get arbitrary data: %w", err) @@ -96,9 +130,13 @@ func getV2Transactions(tx *txn, idMap map[int64]transactionID) ([]explorer.V2Tra var results []explorer.V2Transaction for order, dbID := range dbIDs { + otherFields := txnOtherFields[dbID] + txn := explorer.V2Transaction{ - ID: idMap[int64(order)].id, - ArbitraryData: txnArbitraryData[dbID], + ID: idMap[int64(order)].id, + ArbitraryData: txnArbitraryData[dbID], + NewFoundationAddress: otherFields.newFoundationAddress, + MinerFee: otherFields.minerFee, } // for _, attestation := range txn.Attestations {