Skip to content

Commit

Permalink
list the blocks a transaction was on explorer.Transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Sep 6, 2024
1 parent 1a6471e commit c7209ce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions explorer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type FileContractRevision struct {
// A Transaction is a transaction that uses the wrapped types above.
type Transaction struct {
ID types.TransactionID `json:"id"`
ChainIndices []types.ChainIndex `json:"chainIndices,omitempty"`
SiacoinInputs []SiacoinInput `json:"siacoinInputs,omitempty"`
SiacoinOutputs []SiacoinOutput `json:"siacoinOutputs,omitempty"`
SiafundInputs []SiafundInput `json:"siafundInputs,omitempty"`
Expand Down
32 changes: 32 additions & 0 deletions persist/sqlite/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@ import (
"go.sia.tech/explored/explorer"
)

// transactionChainIndices returns the chain indices of the blocks the transaction
// was in.
func transactionChainIndices(tx *txn, txnIDs []int64) (map[int64][]types.ChainIndex, error) {
query := `SELECT bt.transaction_id, bt.block_id, b.height
FROM block_transactions bt
JOIN blocks b ON bt.block_id = b.id
WHERE bt.transaction_id IN (` + queryPlaceHolders(len(txnIDs)) + `)
ORDER BY bt.block_order ASC`
rows, err := tx.Query(query, queryArgs(txnIDs)...)
if err != nil {
return nil, err
}
defer rows.Close()

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

// transactionMinerFee returns the miner fees for each transaction.
func transactionMinerFee(tx *txn, txnIDs []int64) (map[int64][]types.Currency, error) {
query := `SELECT transaction_id, fee
Expand Down Expand Up @@ -458,6 +484,11 @@ func getTransactions(tx *txn, idMap map[int64]transactionID) ([]explorer.Transac
dbIDs[id.order] = dbID
}

txnChainIndices, err := transactionChainIndices(tx, dbIDs)
if err != nil {
return nil, fmt.Errorf("getTransactions: failed to get chain indices: %w", err)
}

txnArbitraryData, err := transactionArbitraryData(tx, dbIDs)
if err != nil {
return nil, fmt.Errorf("getTransactions: failed to get arbitrary data: %w", err)
Expand Down Expand Up @@ -512,6 +543,7 @@ func getTransactions(tx *txn, idMap map[int64]transactionID) ([]explorer.Transac
for _, dbID := range dbIDs {
txn := explorer.Transaction{
ID: idMap[dbID].id,
ChainIndices: txnChainIndices[dbID],
SiacoinInputs: txnSiacoinInputs[dbID],
SiacoinOutputs: txnSiacoinOutputs[dbID],
SiafundInputs: txnSiafundInputs[dbID],
Expand Down

0 comments on commit c7209ce

Please sign in to comment.