Skip to content

Commit

Permalink
WIP: multi: add proof courier address to asset transfer table
Browse files Browse the repository at this point in the history
This commit persist a send package's proof courier address within the
chain porter's OutboundParcel persist/resume cycle.
  • Loading branch information
ffranr committed Aug 21, 2023
1 parent 6d43874 commit 3c26ba3
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 16 deletions.
12 changes: 12 additions & 0 deletions tapdb/assets_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"math"
"net/url"
"time"

"github.com/btcsuite/btcd/btcec/v2"
Expand Down Expand Up @@ -1930,6 +1931,7 @@ func (a *AssetStore) LogPendingParcel(ctx context.Context,
HeightHint: int32(spend.AnchorTxHeightHint),
AnchorTxid: newAnchorTXID[:],
TransferTimeUnix: spend.TransferTime,
ProofCourierAddr: []byte(spend.ProofCourierAddr.String()),
})
if err != nil {
return fmt.Errorf("unable to insert asset transfer: "+
Expand Down Expand Up @@ -2730,13 +2732,23 @@ func (a *AssetStore) QueryParcels(ctx context.Context,
"anchor tx: %w", err)
}

// Parse proof courier address.
proofCourierAddr, err := url.ParseRequestURI(
string(dbT.ProofCourierAddr),
)
if err != nil {
return fmt.Errorf("unable to parse proof courier address "+
"assigned to asset transfer: %w", err)
}

transfer := &tapfreighter.OutboundParcel{
AnchorTx: anchorTx,
AnchorTxHeightHint: uint32(dbT.HeightHint),
TransferTime: dbT.TransferTimeUnix.UTC(),
ChainFees: dbAnchorTx.ChainFees,
Inputs: inputs,
Outputs: outputs,
ProofCourierAddr: proofCourierAddr,
}
transfers = append(transfers, transfer)
}
Expand Down
2 changes: 2 additions & 0 deletions tapdb/assets_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"github.com/lightninglabs/taproot-assets/address"
"math/rand"
"sort"
"testing"
Expand Down Expand Up @@ -1267,6 +1268,7 @@ func TestAssetExportLog(t *testing.T) {
),
ProofSuffix: senderBlob,
}},
ProofCourierAddr: address.RandProofCourierAddr(t),
}
require.NoError(t, assetsStore.LogPendingParcel(
ctx, spendDelta, leaseOwner, leaseExpiry,
Expand Down
6 changes: 5 additions & 1 deletion tapdb/sqlc/migrations/000005_transfers.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ CREATE TABLE IF NOT EXISTS asset_transfers (

anchor_txn_id INTEGER NOT NULL REFERENCES chain_txns(txn_id),

transfer_time_unix TIMESTAMP NOT NULL
transfer_time_unix TIMESTAMP NOT NULL,

-- proof_courier_addr is the address of the proof courier that will be
-- used in distributing proofs associated with a particular tap address.
proof_courier_addr BLOB NOT NULL
);
CREATE INDEX IF NOT EXISTS transfer_time_idx
ON asset_transfers (transfer_time_unix);
Expand Down
1 change: 1 addition & 0 deletions tapdb/sqlc/models.go

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

7 changes: 4 additions & 3 deletions tapdb/sqlc/queries/transfers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ WITH target_txn(txn_id) AS (
WHERE txid = @anchor_txid
)
INSERT INTO asset_transfers (
height_hint, anchor_txn_id, transfer_time_unix
height_hint, anchor_txn_id, transfer_time_unix, proof_courier_addr
) VALUES (
@height_hint, (SELECT txn_id FROM target_txn), @transfer_time_unix
@height_hint, (SELECT txn_id FROM target_txn), @transfer_time_unix,
@proof_courier_addr
) RETURNING id;

-- name: InsertAssetTransferInput :exec
Expand All @@ -29,7 +30,7 @@ INSERT INTO asset_transfer_outputs (

-- name: QueryAssetTransfers :many
SELECT
id, height_hint, txns.txid, transfer_time_unix
id, height_hint, txns.txid, transfer_time_unix, proof_courier_addr
FROM asset_transfers transfers
JOIN chain_txns txns
ON transfers.anchor_txn_id = txns.txn_id
Expand Down
19 changes: 14 additions & 5 deletions tapdb/sqlc/transfers.sql.go

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

5 changes: 5 additions & 0 deletions tapfreighter/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tapfreighter
import (
"context"
"fmt"
"net/url"
"time"

"github.com/btcsuite/btcd/btcec/v2"
Expand Down Expand Up @@ -236,6 +237,10 @@ type OutboundParcel struct {
// Outputs represents the list of new assets that were created with this
// transfer.
Outputs []TransferOutput

// ProofCourierAddr is the address of the proof courier service that will be
// used in transferring the proof data to the recipient.
ProofCourierAddr *url.URL
}

// AssetConfirmEvent is used to mark a batched spend as confirmed on disk.
Expand Down
16 changes: 9 additions & 7 deletions tapfreighter/parcel.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ func (p *PendingParcel) pkg() *sendPackage {
// We set the send package state such that the send process will
// rebroadcast and then wait for the transfer to confirm.
return &sendPackage{
OutboundPkg: p.outboundPkg,
SendState: SendStateBroadcast,
OutboundPkg: p.outboundPkg,
SendState: SendStateBroadcast,
ProofCourierAddr: p.outboundPkg.ProofCourierAddr,
}
}

Expand Down Expand Up @@ -318,11 +319,12 @@ func (s *sendPackage) prepareForStorage(currentHeight uint32) (*OutboundParcel,
AnchorTx: s.AnchorTx.FinalTx,
AnchorTxHeightHint: currentHeight,
// TODO(bhandras): use clock.Clock instead.
TransferTime: time.Now(),
ChainFees: s.AnchorTx.ChainFees,
Inputs: make([]TransferInput, len(vPkt.Inputs)),
Outputs: make([]TransferOutput, len(vPkt.Outputs)),
PassiveAssets: s.PassiveAssets,
TransferTime: time.Now(),
ChainFees: s.AnchorTx.ChainFees,
Inputs: make([]TransferInput, len(vPkt.Inputs)),
Outputs: make([]TransferOutput, len(vPkt.Outputs)),
PassiveAssets: s.PassiveAssets,
ProofCourierAddr: s.ProofCourierAddr,
}

for idx := range vPkt.Inputs {
Expand Down

0 comments on commit 3c26ba3

Please sign in to comment.