Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ type Store interface {
BestTip(height uint64) (types.ChainIndex, error)
MerkleProof(leafIndex uint64) ([]types.Hash256, error)
Transactions(ids []types.TransactionID) ([]Transaction, error)
UnspentSiacoinOutputs(address types.Address, offset, limit uint64) ([]SiacoinOutput, error)
UnspentSiafundOutputs(address types.Address, offset, limit uint64) ([]SiafundOutput, error)
AddressTransactions(addr types.Address, limit, offset uint64) (results []types.TransactionID, err error)
UnspentSiacoinOutputs(address types.Address, limit, offset uint64) ([]SiacoinOutput, error)
UnspentSiafundOutputs(address types.Address, limit, offset uint64) ([]SiafundOutput, error)
Balance(address types.Address) (sc types.Currency, immatureSC types.Currency, sf uint64, err error)
Contracts(ids []types.FileContractID) (result []FileContract, err error)
AddressEvents(address types.Address, offset, limit int) (events []Event, err error)
Expand Down Expand Up @@ -136,6 +137,11 @@ func (e *Explorer) Transactions(ids []types.TransactionID) ([]Transaction, error
return e.s.Transactions(ids)
}

// AddressTransactions returns the transactions involving the address.
func (e *Explorer) AddressTransactions(addr types.Address, limit, offset uint64) (results []types.TransactionID, err error) {
return e.s.AddressTransactions(addr, limit, offset)
}

// UnspentSiacoinOutputs returns the unspent siacoin outputs owned by the
// specified address.
func (e *Explorer) UnspentSiacoinOutputs(address types.Address, offset, limit uint64) ([]SiacoinOutput, error) {
Expand Down
54 changes: 51 additions & 3 deletions persist/sqlite/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,55 @@
return nil
}

func (ut *updateTx) addArbitraryData(id int64, txn types.Transaction) error {
stmt, err := ut.tx.Prepare(`INSERT INTO transaction_arbitrary_data(transaction_id, transaction_order, data) VALUES (?, ?, ?)`)
func (s *Store) addTransactionAddresses(dbTxn txn, id int64, txn types.Transaction) error {
m := make(map[types.Address]struct{})
for _, sci := range txn.SiacoinInputs {
m[sci.UnlockConditions.UnlockHash()] = struct{}{}
}
for _, sco := range txn.SiacoinOutputs {
m[sco.Address] = struct{}{}
}
for _, sfi := range txn.SiafundInputs {
m[sfi.UnlockConditions.UnlockHash()] = struct{}{}
}
for _, sfo := range txn.SiafundOutputs {
m[sfo.Address] = struct{}{}
}
for _, fc := range txn.FileContracts {
for _, vpo := range fc.ValidProofOutputs {
m[vpo.Address] = struct{}{}
}
for _, mpo := range fc.MissedProofOutputs {
m[mpo.Address] = struct{}{}
}
m[types.Address(fc.UnlockHash)] = struct{}{}
}
for _, fcr := range txn.FileContractRevisions {
for _, vpo := range fcr.FileContract.ValidProofOutputs {
m[vpo.Address] = struct{}{}
}
for _, mpo := range fcr.FileContract.MissedProofOutputs {
m[mpo.Address] = struct{}{}
}
m[fcr.UnlockConditions.UnlockHash()] = struct{}{}
}

stmt, err := dbTxn.Prepare(`INSERT INTO transaction_addresses(transaction_id, address) VALUES (?, ?);`)
if err != nil {
return fmt.Errorf("addTransactionAddresses: failed to prepare statement: %w", err)
}
defer stmt.Close()

for addr := range m {
if _, err := stmt.Exec(id, dbEncode(addr)); err != nil {

Check failure on line 86 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

undefined: dbEncode

Check failure on line 86 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

undefined: dbEncode

Check failure on line 86 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

undefined: dbEncode

Check failure on line 86 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

undefined: dbEncode

Check failure on line 86 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22)

undefined: dbEncode

Check failure on line 86 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22)

undefined: dbEncode
return fmt.Errorf("addTransactionAddresses: failed to execute statement: %w", err)
}
}
return nil
}

func (s *Store) addArbitraryData(dbTxn txn, id int64, txn types.Transaction) error {
stmt, err := dbTxn.Prepare(`INSERT INTO transaction_arbitrary_data(transaction_id, transaction_order, data) VALUES (?, ?, ?)`)
if err != nil {
return fmt.Errorf("addArbitraryData: failed to prepare statement: %w", err)
}
Expand Down Expand Up @@ -243,7 +289,9 @@

if _, err := blockTransactionsStmt.Exec(encode(bid), txnID, i); err != nil {
return fmt.Errorf("failed to insert into block_transactions: %w", err)
} else if err := ut.addArbitraryData(txnID, txn); err != nil {
} else if err := s.addTransactionAddresses(dbTxn, txnID, txn); err != nil {

Check failure on line 292 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

undefined: dbTxn

Check failure on line 292 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

undefined: s

Check failure on line 292 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

undefined: dbTxn

Check failure on line 292 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

undefined: s

Check failure on line 292 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22)

undefined: dbTxn

Check failure on line 292 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22)

undefined: s
return fmt.Errorf("failed to add transaction addresses: %w", err)
} else if err := s.addArbitraryData(dbTxn, txnID, txn); err != nil {

Check failure on line 294 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

undefined: dbTxn

Check failure on line 294 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

undefined: s

Check failure on line 294 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

undefined: dbTxn

Check failure on line 294 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

undefined: s

Check failure on line 294 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22)

undefined: dbTxn

Check failure on line 294 in persist/sqlite/consensus.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22)

undefined: s
return fmt.Errorf("failed to add arbitrary data: %w", err)
} else if err := ut.addSiacoinInputs(txnID, txn); err != nil {
return fmt.Errorf("failed to add siacoin inputs: %w", err)
Expand Down
24 changes: 24 additions & 0 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sqlite_test

import (
"errors"
"math"
"math/bits"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -572,6 +573,29 @@ func TestSendTransactions(t *testing.T) {
check(t, "value", e.sfValue, sfo.SiafundOutput.Value)
}
}

{
addr1Txns, err := db.AddressTransactions(addr1, math.MaxInt64, 0)
if err != nil {
panic(err)
}
// miner payment plus any of the transactions from this loop
check(t, "addr1 txns", 1+1+i, len(addr1Txns))

addr2Txns, err := db.AddressTransactions(addr2, math.MaxInt64, 0)
if err != nil {
panic(err)
}
// should only have transactions generated in this loop
check(t, "addr2 txns", 1+i, len(addr2Txns))

addr3Txns, err := db.AddressTransactions(addr3, math.MaxInt64, 0)
if err != nil {
panic(err)
}
// should only have transactions generated in this loop
check(t, "addr3 txns", 1+i, len(addr3Txns))
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions persist/sqlite/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ CREATE TABLE block_transactions (

CREATE INDEX block_transactions_block_id_index ON block_transactions(block_id);

CREATE TABLE transaction_addresses (
transaction_id INTEGER REFERENCES transactions(id) ON DELETE CASCADE NOT NULL,
address BLOB NOT NULL,
UNIQUE(transaction_id, address)
);

CREATE INDEX transaction_addresses_transaction_id_index ON transaction_addresses(transaction_id);

CREATE TABLE transaction_arbitrary_data (
transaction_id INTEGER REFERENCES transactions(id) ON DELETE CASCADE NOT NULL,
transaction_order INTEGER NOT NULL,
Expand Down
29 changes: 29 additions & 0 deletions persist/sqlite/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,32 @@
})
return
}

// AddressTransactions implements explorer.Store.
func (s *Store) AddressTransactions(addr types.Address, limit, offset uint64) (results []types.TransactionID, err error) {
err = s.transaction(func(tx txn) error {

Check failure on line 423 in persist/sqlite/transactions.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

cannot use func(tx txn) error {…} (value of type func(tx txn) error) as func(*txn) error value in argument to s.transaction

Check failure on line 423 in persist/sqlite/transactions.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

cannot use func(tx txn) error {…} (value of type func(tx txn) error) as func(*txn) error value in argument to s.transaction

Check failure on line 423 in persist/sqlite/transactions.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22)

cannot use func(tx txn) error {…} (value of type func(tx txn) error) as func(*txn) error value in argument to s.transaction
query := `SELECT transactions.transaction_id
FROM transaction_addresses
INNER JOIN transactions ON (transactions.id = transaction_addresses.transaction_id)
WHERE transaction_addresses.address = ?
ORDER BY transactions.transaction_id ASC
LIMIT ? OFFSET ?
`

rows, err := tx.Query(query, dbEncode(addr), limit, offset)

Check failure on line 432 in persist/sqlite/transactions.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

undefined: dbEncode

Check failure on line 432 in persist/sqlite/transactions.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

undefined: dbEncode

Check failure on line 432 in persist/sqlite/transactions.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22)

undefined: dbEncode
if err != nil {
return fmt.Errorf("failed to get txn IDs: %w", err)
}
defer rows.Close()

for rows.Next() {
var id types.TransactionID
if err := rows.Scan(dbDecode(&id)); err != nil {

Check failure on line 440 in persist/sqlite/transactions.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21)

undefined: dbDecode (typecheck)

Check failure on line 440 in persist/sqlite/transactions.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

undefined: dbDecode (typecheck)

Check failure on line 440 in persist/sqlite/transactions.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22)

undefined: dbDecode (typecheck)
return fmt.Errorf("failed to scan txn ID: %w", err)
}
results = append(results, id)
}
return nil
})
return
}
Loading