Skip to content

Commit

Permalink
tarodb: store on-chain fees for chain tx
Browse files Browse the repository at this point in the history
Store on-chain fees as a separate column so we can directly execute queries
in our database using this value.

Not all chain tx will have this field populated. We will store the fees
only for the tx that we create (minting and trasfer operations but not
importing).
  • Loading branch information
positiveblue committed Nov 4, 2022
1 parent 5b492df commit 257cbae
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 22 deletions.
5 changes: 3 additions & 2 deletions tarodb/asset_minting.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,9 @@ func (a *AssetMintingStore) CommitSignedGenesisTx(ctx context.Context,
// chain transaction, as that chain transaction will be
// referenced by the managed UTXO.
chainTXID, err := q.UpsertChainTx(ctx, ChainTx{
Txid: genTXID[:],
RawTx: txBuf.Bytes(),
Txid: genTXID[:],
RawTx: txBuf.Bytes(),
ChainFees: genesisPkt.ChainFees,
})
if err != nil {
return fmt.Errorf("unable to insert chain tx: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions tarodb/asset_minting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func randGenesisPacket(t *testing.T) *tarogarden.FundedPsbt {
return &tarogarden.FundedPsbt{
Pkt: psbt,
ChangeOutputIndex: 1,
ChainFees: 100,
}
}

Expand Down Expand Up @@ -454,6 +455,7 @@ func TestCommitBatchChainActions(t *testing.T) {
require.NoError(t, err)
require.Equal(t, genTXID[:], dbGenTx.Txid[:])
require.Equal(t, rawTxBytes.Bytes(), dbGenTx.RawTx)
require.Equal(t, genesisPkt.ChainFees, dbGenTx.ChainFees)

// Now that we have the primary key for the chain transaction inserted
// above, we'll use that to confirm that the managed UTXO has been
Expand Down
6 changes: 4 additions & 2 deletions tarodb/assets_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,8 +1263,9 @@ func (a *AssetStore) LogPendingParcel(ctx context.Context,
// Next, we'll insert the new transaction that anchors the new
// anchor point (commits to the set of new outputs).
txnID, err := q.UpsertChainTx(ctx, ChainTx{
Txid: newAnchorTXID[:],
RawTx: anchorTxBytes,
Txid: newAnchorTXID[:],
RawTx: anchorTxBytes,
ChainFees: spend.ChainFees,
})
if err != nil {
return fmt.Errorf("unable to insert new chain "+
Expand Down Expand Up @@ -1640,6 +1641,7 @@ func (a *AssetStore) QueryParcels(ctx context.Context,
AnchorTx: anchorTx,
AssetSpendDeltas: spendDeltas,
TransferTime: xfer.TransferTimeUnix,
ChainFees: xfer.ChainFees,
})
}

Expand Down
4 changes: 4 additions & 0 deletions tarodb/assets_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ func TestAssetExportLog(t *testing.T) {
SplitCommitment: nil,
}

chainFees := int64(100)

// With the assets inserted, we'll now construct the struct we'll used
// to commit a new spend on disk.
anchorTxHash := newAnchorTx.TxHash()
Expand Down Expand Up @@ -784,6 +786,7 @@ func TestAssetExportLog(t *testing.T) {
ReceiverAssetProof: receiverBlob,
},
},
ChainFees: int64(chainFees),
}
require.NoError(t, assetsStore.LogPendingParcel(ctx, spendDelta))

Expand Down Expand Up @@ -876,6 +879,7 @@ func TestAssetExportLog(t *testing.T) {
t, uint32(blockHeight), extractSqlInt32[uint32](anchorTx.BlockHeight),
)
require.Equal(t, uint32(txIndex), extractSqlInt32[uint32](anchorTx.TxIndex))
require.Equal(t, chainFees, anchorTx.ChainFees)

// At this point, there should be no more pending parcels.
parcels, err = assetsStore.PendingParcels(ctx)
Expand Down
9 changes: 6 additions & 3 deletions tarodb/sqlite/assets.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tarodb/sqlite/migrations/000002_assets.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ CREATE TABLE IF NOT EXISTS chain_txns (

txid BLOB UNIQUE NOT NULL,

chain_fees BIGINT NOT NULL,

raw_tx BLOB NOT NULL,

block_height INTEGER,

block_hash BLOB,

tx_index INTEGER
tx_index INTEGER
);

-- genesis_points stores all genesis_points relevant to tardo, which is the
Expand Down
1 change: 1 addition & 0 deletions tarodb/sqlite/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tarodb/sqlite/queries/assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ WHERE batch_id in (SELECT batch_id FROM target_batch);

-- name: UpsertChainTx :one
INSERT INTO chain_txns (
txid, raw_tx, block_height, block_hash, tx_index
txid, raw_tx, chain_fees, block_height, block_hash, tx_index
) VALUES (
?, ?, sqlc.narg('block_height'), sqlc.narg('block_hash'),
?, ?, ?, sqlc.narg('block_height'), sqlc.narg('block_hash'),
sqlc.narg('tx_index')
) ON CONFLICT (txid)
-- Not a NOP but instead update any nullable fields that aren't null in the
Expand Down
12 changes: 6 additions & 6 deletions tarodb/sqlite/queries/transfers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ INSERT INTO transfer_proofs (
-- name: QueryAssetTransfers :many
SELECT
asset_transfers.old_anchor_point, utxos.outpoint AS new_anchor_point,
utxos.taro_root, utxos.tapscript_sibling, utxos.utxo_id AS new_anchor_utxo_id,
txns.raw_tx AS anchor_tx_bytes, txns.txid AS anchor_txid,
txns.txn_id AS anchor_tx_primary_key, transfer_time_unix,
keys.raw_key AS internal_key_bytes, keys.key_family AS internal_key_fam,
keys.key_index AS internal_key_index, id AS transfer_id,
transfer_time_unix
utxos.taro_root, utxos.tapscript_sibling,
utxos.utxo_id AS new_anchor_utxo_id, txns.raw_tx AS anchor_tx_bytes,
txns.txid AS anchor_txid, txns.txn_id AS anchor_tx_primary_key,
txns.chain_fees, transfer_time_unix, keys.raw_key AS internal_key_bytes,
keys.key_family AS internal_key_fam, keys.key_index AS internal_key_index,
id AS transfer_id, transfer_time_unix
FROM asset_transfers
JOIN internal_keys keys
ON asset_transfers.new_internal_key = keys.key_id
Expand Down
14 changes: 8 additions & 6 deletions tarodb/sqlite/transfers.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 257cbae

Please sign in to comment.