Skip to content

Commit

Permalink
Merge pull request #184 from positiveblue/fix-144
Browse files Browse the repository at this point in the history
tarodb: add `chain_fees` to `chain_txns`
  • Loading branch information
Roasbeef authored Nov 4, 2022
2 parents e17b324 + 257cbae commit 19af433
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 24 deletions.
2 changes: 1 addition & 1 deletion rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,6 @@ func (r *rpcServer) SendAsset(ctx context.Context,
PrevInputs: prevInputs,
NewOutputs: newOutputs,
},
TotalFeeSats: 0,
TotalFeeSats: int64(resp.TotalFees),
}, nil
}
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 @@ -811,6 +811,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 @@ -849,6 +851,7 @@ func TestAssetExportLog(t *testing.T) {
ReceiverAssetProof: receiverBlob,
},
},
ChainFees: int64(chainFees),
}
require.NoError(t, assetsStore.LogPendingParcel(ctx, spendDelta))

Expand Down Expand Up @@ -941,6 +944,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 @@ -331,9 +331,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.

7 changes: 7 additions & 0 deletions tarofreighter/chain_porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,12 @@ func (p *ChainPorter) stateStep(currentPkg sendPackage) (*sendPackage, error) {
return nil, err
}

chainFees, err := tarogarden.GetTxFee(currentPkg.SendPkt)
if err != nil {
return nil, fmt.Errorf("unable to get on-chain fees "+
"for psbt: %w", err)
}

// Before we broadcast, we'll write to disk that we have a
// pending outbound parcel. If we crash before this point,
// we'll start all over. Otherwise, we'll come back to this
Expand Down Expand Up @@ -965,6 +971,7 @@ func (p *ChainPorter) stateStep(currentPkg sendPackage) (*sendPackage, error) {
TapscriptSibling: currentPkg.InputAsset.TapscriptSibling,
// TODO(bhandras): use clock.Clock instead.
TransferTime: time.Now(),
ChainFees: chainFees,
}

// Don't allow shutdown while we're attempting to store proofs.
Expand Down
4 changes: 4 additions & 0 deletions tarofreighter/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ type OutboundParcelDelta struct {

// TransferTime holds the timestamp of the outbound spend.
TransferTime time.Time

// ChainFees is the amount in sats paid in on-chain fees for the
// anchor transaction.
ChainFees int64
}

// AssetConfirmEvent is used to mark a batched spend as confirmed on disk.
Expand Down
2 changes: 1 addition & 1 deletion tarofreighter/parcel.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,6 @@ func (s *sendPackage) deliverResponse(respChan chan<- *PendingParcel) {
},
},
},
TotalFees: 0,
TotalFees: btcutil.Amount(s.OutboundPkg.ChainFees),
}
}
23 changes: 23 additions & 0 deletions tarogarden/caretaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,14 @@ func (b *BatchCaretaker) stateStep(currentState BatchState) (BatchState, error)
}
b.cfg.Batch.GenesisPacket.Pkt = signedPkt

// Populate how much this tx paid in on-chain fees.
chainFees, err := GetTxFee(signedPkt)
if err != nil {
return 0, fmt.Errorf("unable to get on-chain fees "+
"for psbt: %w", err)
}
b.cfg.Batch.GenesisPacket.ChainFees = chainFees

log.Infof("BatchCaretaker(%x): GenesisPacket finalized: %v",
b.batchKey[:], spew.Sdump(signedPkt))

Expand Down Expand Up @@ -739,3 +747,18 @@ func (b *BatchCaretaker) stateStep(currentState BatchState) (BatchState, error)
return 0, fmt.Errorf("unknown state: %v", currentState)
}
}

// GetTxFee returns the value of the on-chain fees paid by a finalized PSBT.
func GetTxFee(pkt *psbt.Packet) (int64, error) {
inputValue, err := psbt.SumUtxoInputValues(pkt)
if err != nil {
return 0, fmt.Errorf("unable to sum input values: %v", err)
}

outputValue := int64(0)
for _, out := range pkt.UnsignedTx.TxOut {
outputValue += out.Value
}

return inputValue - outputValue, nil
}
4 changes: 4 additions & 0 deletions tarogarden/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ type FundedPsbt struct {
// Taro commitment (the non-change output).
ChangeOutputIndex uint32

// ChainFees is the amount in sats paid in on-chain fees for this
// transaction.
ChainFees int64

// LockedUTXOs is the set of UTXOs that were locked to create the PSBT
// packet.
LockedUTXOs []wire.OutPoint
Expand Down

0 comments on commit 19af433

Please sign in to comment.