Skip to content

Commit

Permalink
store new foundation address / miner fee
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Oct 16, 2024
1 parent 4d34627 commit 393126a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
3 changes: 3 additions & 0 deletions explorer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
5 changes: 4 additions & 1 deletion persist/sqlite/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 7 additions & 2 deletions persist/sqlite/v2consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
42 changes: 40 additions & 2 deletions persist/sqlite/v2transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,60 @@ 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)
}

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 {
Expand Down

0 comments on commit 393126a

Please sign in to comment.