Skip to content

Commit

Permalink
move arbitrary data to v2_transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Oct 21, 2024
1 parent 54cf1d9 commit ec5ed36
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 53 deletions.
11 changes: 2 additions & 9 deletions persist/sqlite/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ CREATE TABLE v2_transactions (
transaction_id BLOB UNIQUE NOT NULL,

new_foundation_address BLOB,
miner_fee BLOB NOT NULL
miner_fee BLOB NOT NULL,
arbitrary_data BLOB
);
CREATE INDEX v2_transactions_transaction_id_index ON v2_transactions(transaction_id);

Expand All @@ -271,14 +272,6 @@ CREATE TABLE v2_block_transactions (
CREATE INDEX v2_block_transactions_block_id_index ON v2_block_transactions(block_id);
CREATE INDEX v2_block_transactions_transaction_id_block_id ON v2_block_transactions(transaction_id, block_id);

CREATE TABLE v2_transaction_arbitrary_data (
transaction_id INTEGER REFERENCES v2_transactions(id) ON DELETE CASCADE NOT NULL,
data BLOB,
UNIQUE(transaction_id)
);

CREATE INDEX v2_transaction_arbitrary_data_transaction_id_index ON v2_transaction_arbitrary_data(transaction_id);

CREATE TABLE state_tree (
row INTEGER NOT NULL,
column INTEGER NOT NULL,
Expand Down
22 changes: 2 additions & 20 deletions persist/sqlite/v2consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,14 @@ import (
"go.sia.tech/explored/explorer"
)

func addV2ArbitraryData(tx *txn, id int64, txn types.V2Transaction) error {
stmt, err := tx.Prepare(`INSERT INTO v2_transaction_arbitrary_data(transaction_id, data) VALUES (?, ?)`)

if err != nil {
return fmt.Errorf("addV2ArbitraryData: failed to prepare statement: %w", err)
}
defer stmt.Close()

if _, err := stmt.Exec(id, txn.ArbitraryData); err != nil {
return fmt.Errorf("addV2ArbitraryData: failed to execute statement: %w", err)
}
return nil
}

func addV2Transactions(tx *txn, bid types.BlockID, txns []types.V2Transaction) (map[types.TransactionID]txnDBId, error) {
checkTransactionStmt, err := tx.Prepare(`SELECT id FROM v2_transactions WHERE transaction_id = ?`)
if err != nil {
return nil, fmt.Errorf("failed to prepare check v2_transaction statement: %v", err)
}
defer checkTransactionStmt.Close()

insertTransactionStmt, err := tx.Prepare(`INSERT INTO v2_transactions (transaction_id, new_foundation_address, miner_fee) VALUES (?, ?, ?)`)
insertTransactionStmt, err := tx.Prepare(`INSERT INTO v2_transactions (transaction_id, new_foundation_address, miner_fee, arbitrary_data) VALUES (?, ?, ?, ?)`)
if err != nil {
return nil, fmt.Errorf("failed to prepare insert v2_transaction statement: %v", err)
}
Expand Down Expand Up @@ -57,7 +43,7 @@ func addV2Transactions(tx *txn, bid types.BlockID, txns []types.V2Transaction) (
newFoundationAddress = encode(txn.NewFoundationAddress)
}

result, err := insertTransactionStmt.Exec(encode(txn.ID()), newFoundationAddress, encode(txn.MinerFee))
result, err := insertTransactionStmt.Exec(encode(txn.ID()), newFoundationAddress, encode(txn.MinerFee), txn.ArbitraryData)
if err != nil {
return nil, fmt.Errorf("failed to insert into v2_transactions: %w", err)
}
Expand Down Expand Up @@ -86,10 +72,6 @@ func addV2TransactionFields(tx *txn, txns []types.V2Transaction, scDBIds map[typ
if dbID.exist {
continue
}

if err := addV2ArbitraryData(tx, dbID.id, txn); err != nil {
return fmt.Errorf("failed to add arbitrary data: %w", err)
}
}

return nil
Expand Down
27 changes: 3 additions & 24 deletions persist/sqlite/v2transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,18 @@ WHERE block_id = ? ORDER BY block_order ASC`, encode(blockID))
// getV2Transactions fetches v2 transactions in the correct order using
// prepared statements.
func getV2Transactions(tx *txn, ids []types.TransactionID) ([]explorer.V2Transaction, error) {
dbIDs, txns, err := getV2TransactionBase(tx, ids)
_, txns, err := getV2TransactionBase(tx, ids)
if err != nil {
return nil, fmt.Errorf("getV2Transactions: failed to get base transactions: %w", err)
}
if err := fillV2TransactionArbitraryData(tx, dbIDs, txns); err != nil {
return nil, fmt.Errorf("getV2Transactions: failed to get arbitrary data: %w", err)
}

return txns, nil
}

// getV2TransactionBase fetches the base transaction data for a given list of
// transaction IDs.
func getV2TransactionBase(tx *txn, txnIDs []types.TransactionID) (dbIDs []int64, txns []explorer.V2Transaction, err error) {
stmt, err := tx.Prepare(`SELECT id, transaction_id, new_foundation_address, miner_fee FROM v2_transactions WHERE transaction_id = ?`)
stmt, err := tx.Prepare(`SELECT id, transaction_id, new_foundation_address, miner_fee, arbitrary_data FROM v2_transactions WHERE transaction_id = ?`)
if err != nil {
return nil, nil, fmt.Errorf("getV2TransactionBase: failed to prepare statement: %w", err)
}
Expand All @@ -86,7 +83,7 @@ func getV2TransactionBase(tx *txn, txnIDs []types.TransactionID) (dbIDs []int64,
for _, id := range txnIDs {
var txn explorer.V2Transaction
var newFoundationAddress types.Address
if err := stmt.QueryRow(encode(id)).Scan(&dbID, decode(&txn.ID), decodeNull(&newFoundationAddress), decode(&txn.MinerFee)); err != nil {
if err := stmt.QueryRow(encode(id)).Scan(&dbID, decode(&txn.ID), decodeNull(&newFoundationAddress), decode(&txn.MinerFee), &txn.ArbitraryData); err != nil {
return nil, nil, fmt.Errorf("failed to scan base transaction: %w", err)
}
if (newFoundationAddress != types.Address{}) {
Expand All @@ -99,24 +96,6 @@ func getV2TransactionBase(tx *txn, txnIDs []types.TransactionID) (dbIDs []int64,
return dbIDs, txns, nil
}

// fillV2TransactionArbitraryData fills in the arbitrary data for each transaction using prepared statements.
func fillV2TransactionArbitraryData(tx *txn, dbIDs []int64, txns []explorer.V2Transaction) error {
stmt, err := tx.Prepare(`SELECT data FROM v2_transaction_arbitrary_data WHERE transaction_id = ?`)
if err != nil {
return fmt.Errorf("failed to prepare arbitrary data statement: %w", err)
}
defer stmt.Close()

for i, dbID := range dbIDs {
var data []byte
if err := stmt.QueryRow(dbID).Scan(&data); err != nil {
return fmt.Errorf("failed to scan arbitrary data for txn %d: %w", dbID, err)
}
txns[i].ArbitraryData = data
}
return nil
}

// V2Transactions implements explorer.Store.
func (s *Store) V2Transactions(ids []types.TransactionID) (results []explorer.V2Transaction, err error) {
err = s.transaction(func(tx *txn) error {
Expand Down

0 comments on commit ec5ed36

Please sign in to comment.