Skip to content

Commit

Permalink
[temp]: debug checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Oct 3, 2023
1 parent f498871 commit 45a5f46
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 31 deletions.
22 changes: 20 additions & 2 deletions itest/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/proof"
Expand Down Expand Up @@ -115,7 +116,10 @@ func AssetScriptKeyIsBurnCheck(isBurn bool) AssetCheck {
// AssetVersionCheck returns a check function that tests an asset's version.
func AssetVersionCheck(version taprpc.AssetVersion) AssetCheck {
return func(a *taprpc.Asset) error {
if a.Version != a.Version {
fmt.Printf("expected version: %v, actual version: %v\n",
version, a.Version)

if a.Version != version {
return fmt.Errorf("unexpected asset version, got %v "+
"wanted %v", a.Version, version)
}
Expand Down Expand Up @@ -153,6 +157,9 @@ func AssertAssetState(t *testing.T, assets map[string][]*taprpc.Asset,
if bytes.Equal(rpcGen.MetaHash, metaHash[:]) {
a = rpcAsset

fmt.Printf("doing asserts on: %v\n",
spew.Sdump(formatProtoJSON(a)))

for _, check := range assetChecks {
err := check(rpcAsset)
require.NoError(t, err)
Expand Down Expand Up @@ -1237,14 +1244,25 @@ func AssertAssetsMinted(t *testing.T,
require.NoError(t, err)
confirmedAssets := GroupAssetsByName(listRespConfirmed.Assets)

listResp, err := formatProtoJSON(listRespConfirmed)
require.NoError(t, err)
t.Logf("assets created: %v", listResp)

for _, assetRequest := range assetRequests {
assetRequest := assetRequest

metaHash := (&proof.MetaReveal{
Data: assetRequest.Asset.AssetMeta.Data,
}).MetaHash()

t.Logf("ASSERT VERSION: %v", assetRequest.Asset.AssetVersion)

mintedAsset := AssertAssetState(
t, confirmedAssets, assetRequest.Asset.Name,
metaHash[:], AssetAnchorCheck(mintTXID, blockHash),
metaHash[:],
AssetAnchorCheck(mintTXID, blockHash),
AssetScriptKeyIsLocalCheck(true),
AssetVersionCheck(assetRequest.Asset.AssetVersion),
func(a *taprpc.Asset) error {
anchor := a.ChainAnchor

Expand Down
10 changes: 6 additions & 4 deletions itest/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func testBasicSendUnidirectional(t *harnessTest) {
// send it around a few times.
rpcAssets := MintAssetsConfirmBatch(
t.t, t.lndHarness.Miner.Client, t.tapd,
[]*mintrpc.MintAssetRequest{simpleAssets[0]},
[]*mintrpc.MintAssetRequest{issuableAssets[0]},
)

genInfo := rpcAssets[0].AssetGenesis
Expand All @@ -98,12 +98,13 @@ func testBasicSendUnidirectional(t *harnessTest) {

// Next, we'll attempt to complete two transfers with distinct
// addresses from our main node to Bob.
currentUnits := simpleAssets[0].Asset.Amount
currentUnits := issuableAssets[0].Asset.Amount

// Issue a single address which will be reused for each send.
bobAddr, err := secondTapd.NewAddr(ctxb, &taprpc.NewAddrRequest{
AssetId: genInfo.AssetId,
Amt: numUnits,
AssetId: genInfo.AssetId,
Amt: numUnits,
AssetVersion: rpcAssets[0].Version,
})
require.NoError(t.t, err)

Expand All @@ -114,6 +115,7 @@ func testBasicSendUnidirectional(t *harnessTest) {

AssertAddrCreated(t.t, secondTapd, rpcAssets[0], bobAddr)

t.Logf("sending attempt %v", i)
sendResp := sendAssetsToAddr(t, t.tapd, bobAddr)

ConfirmAndAssertOutboundTransfer(
Expand Down
2 changes: 2 additions & 0 deletions itest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ func MintAssetUnconfirmed(t *testing.T, minerClient *rpcclient.Client,

unconfirmedAssets := GroupAssetsByName(listRespUnconfirmed.Assets)
for _, assetRequest := range assetRequests {
assetRequest := assetRequest

metaHash := (&proof.MetaReveal{
Data: assetRequest.Asset.AssetMeta.Data,
}).MetaHash()
Expand Down
4 changes: 4 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ func (r *rpcServer) MintAsset(ctx context.Context,
EnableEmission: req.EnableEmission,
}

rpcsLog.Infof("[MintAsset]: version=%v, type=%v, name=%v, amt=%v, "+
"issuance=%v", seedling.AssetVersion, seedling.AssetType,
seedling.AssetName, seedling.Amount, seedling.EnableEmission)

// If a group key is provided, parse the provided group public key
// before creating the asset seedling.
if specificGroupKey {
Expand Down
4 changes: 3 additions & 1 deletion tapdb/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ func (t *TapAddressBook) InsertAddrs(ctx context.Context,
// internal keys, then use those returned primary key IDs to
// returned to insert the address itself.
for _, addr := range addrs {
addr := addr

// The asset genesis should already be known at this
// point, so we'll just fetch it so we can obtain the
// genAssetID.
Expand Down Expand Up @@ -584,11 +586,11 @@ func fetchAddr(ctx context.Context, db AddrBook, params *address.ChainParams,
address.Version(dbAddr.Version), genesis, groupKey,
groupSig, *scriptKey, *internalKey, uint64(dbAddr.Amount),
tapscriptSibling, params, *proofCourierAddr,
address.WithAssetVersion(asset.Version(dbAddr.AssetVersion)),
)
if err != nil {
return nil, fmt.Errorf("unable to make addr: %w", err)
}
tapAddr.AssetVersion = asset.Version(dbAddr.AssetVersion)

return &address.AddrWithKeyInfo{
Tap: tapAddr,
Expand Down
85 changes: 65 additions & 20 deletions tapdb/asset_minting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,37 @@ func TestAddSproutsToBatch(t *testing.T) {
// We also expect that for each of the assets we created above, we're
// able to obtain the asset meta for them all.
require.Len(t, mintingBatches[0].AssetMetas, numSeedlings)

// The number of assets in the batch should match the seedlings we
// inserted.
allAssets := mintingBatches[0].RootAssetCommitment.CommittedAssets()
require.Equal(t, numSeedlings, len(allAssets))

// For each inserted asset, the asset version should match the
// corresponding seedling.
require.True(t, fn.All(allAssets, func(a *asset.Asset) bool {
seedling, ok := mintingBatch.Seedlings[a.Genesis.Tag]
if !ok {
// We should find the seedlings.
return false
}

return a.Version == seedling.AssetVersion
}))
}

type randAssetCtx struct {
batchKey *btcec.PublicKey
groupKey *btcec.PublicKey
groupGenAmt uint64
genesisPkt *tapgarden.FundedPsbt
scriptRoot []byte
assetRoot *commitment.TapCommitment
mintingBatch *tapgarden.MintingBatch
}

func addRandAssets(t *testing.T, ctx context.Context,
assetStore *AssetMintingStore,
numAssets int) (*btcec.PublicKey, *btcec.PublicKey, uint64,
*tapgarden.FundedPsbt, []byte, *commitment.TapCommitment) {
assetStore *AssetMintingStore, numAssets int) randAssetCtx {

mintingBatch := tapgarden.RandSeedlingMintingBatch(t, numAssets)
genAmt, seedlingGroups, group := addRandGroupToBatch(
Expand All @@ -578,8 +603,16 @@ func addRandAssets(t *testing.T, ctx context.Context,
))

scriptRoot := assetRoot.TapscriptRoot(nil)
return batchKey, &group.GroupKey.GroupPubKey, genAmt,
genesisPacket, scriptRoot[:], assetRoot

return randAssetCtx{
batchKey: batchKey,
groupKey: &group.GroupKey.GroupPubKey,
groupGenAmt: genAmt,
genesisPkt: genesisPacket,
scriptRoot: scriptRoot[:],
assetRoot: assetRoot,
mintingBatch: mintingBatch,
}
}

// TestCommitBatchChainActions tests that we're able to properly write a signed
Expand All @@ -596,21 +629,23 @@ func TestCommitBatchChainActions(t *testing.T) {

// First, we'll create a new batch, then add some sample seedlings, and
// then those seedlings as assets.
batchKey, groupKey, groupGenAmt, genesisPkt, scriptRoot, assetRoot :=
addRandAssets(t, ctx, assetStore, numSeedlings)
//
// nolint:lll
randAssetCtx := addRandAssets(t, ctx, assetStore, numSeedlings)

// The packet needs to be finalized, so we'll insert a fake
// FinalScriptSig. The FinalScriptSig doesn't need to be well formed,
// so we get by w/ this.
//
// TODO(roasbeef): move the tx extraction up one layer?
genesisPkt.Pkt.Inputs[0].FinalScriptSig = []byte{}
randAssetCtx.genesisPkt.Pkt.Inputs[0].FinalScriptSig = []byte{}

// With our assets inserted, we'll now commit the signed genesis packet
// to disk, along with the Taproot Asset script root that's stored
// alongside any managed UTXOs.
require.NoError(t, assetStore.CommitSignedGenesisTx(
ctx, batchKey, genesisPkt, 2, scriptRoot,
ctx, randAssetCtx.batchKey, randAssetCtx.genesisPkt, 2,
randAssetCtx.scriptRoot,
))

// The batch updated above should be found, with the batch state
Expand All @@ -620,10 +655,12 @@ func TestCommitBatchChainActions(t *testing.T) {
assertBatchState(
t, mintingBatches[0], tapgarden.BatchStateBroadcast,
)
assertPsbtEqual(t, genesisPkt, mintingBatches[0].GenesisPacket)
assertPsbtEqual(
t, randAssetCtx.genesisPkt, mintingBatches[0].GenesisPacket,
)

var rawTxBytes bytes.Buffer
rawGenTx, err := psbt.Extract(genesisPkt.Pkt)
rawGenTx, err := psbt.Extract(randAssetCtx.genesisPkt.Pkt)
require.NoError(t, err)
require.NoError(t, rawGenTx.Serialize(&rawTxBytes))

Expand All @@ -636,7 +673,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)
require.Equal(t, randAssetCtx.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 All @@ -645,7 +682,7 @@ func TestCommitBatchChainActions(t *testing.T) {
TxnID: sqlInt32(dbGenTx.TxnID),
})
require.NoError(t, err)
require.Equal(t, scriptRoot, managedUTXO.MerkleRoot)
require.Equal(t, randAssetCtx.scriptRoot, managedUTXO.MerkleRoot)

// Next, we'll confirm that all the assets inserted previously now are
// able to be queried according to the anchor UTXO primary key.
Expand All @@ -662,7 +699,7 @@ func TestCommitBatchChainActions(t *testing.T) {

// For each asset created above, we'll make a fake proof file for it.
assetProofs := make(proof.AssetBlobs)
for _, a := range assetRoot.CommittedAssets() {
for _, a := range randAssetCtx.assetRoot.CommittedAssets() {
blob := make([]byte, 100)
_, err := rand.Read(blob[:])
require.NoError(t, err)
Expand All @@ -676,8 +713,8 @@ func TestCommitBatchChainActions(t *testing.T) {
blockHeight := uint32(20)
txIndex := uint32(5)
require.NoError(t, assetStore.MarkBatchConfirmed(
ctx, batchKey, &fakeBlockHash, blockHeight, txIndex,
assetProofs,
ctx, randAssetCtx.batchKey, &fakeBlockHash, blockHeight,
txIndex, assetProofs,
))

// We'll now fetch the chain transaction again, to confirm that all the
Expand All @@ -698,9 +735,17 @@ func TestCommitBatchChainActions(t *testing.T) {
require.NoError(t, err)
require.Equal(t, numSeedlings, len(assets))

// All the assets returned should have the genesis prev ID set up.
mintingBatch := randAssetCtx.mintingBatch
for _, dbAsset := range assets {
// All the assets returned should have the genesis prev ID set up.
require.True(t, dbAsset.HasGenesisWitness())

// All the assets should also have a matching asset version as
// the seedlings we created.
seedling, ok := mintingBatch.Seedlings[dbAsset.Genesis.Tag]
require.True(t, ok)

require.Equal(t, seedling.AssetVersion, dbAsset.Version)
}

// Now that the batch has been committed on disk, we should be able to
Expand All @@ -723,7 +768,7 @@ func TestCommitBatchChainActions(t *testing.T) {
require.NoError(t, err)
require.Equal(t, assetProofs, diskProofs)

mintedAssets := assetRoot.CommittedAssets()
mintedAssets := randAssetCtx.assetRoot.CommittedAssets()

// We'll now query for the set of balances to ensure they all line up
// with the assets we just created, including the group genesis asset.
Expand Down Expand Up @@ -753,7 +798,7 @@ func TestCommitBatchChainActions(t *testing.T) {
)
require.NoError(t, err)
require.Equal(t, numKeyGroups, len(assetBalancesByGroup))
existingGroupKey := asset.ToSerialized(groupKey)
existingGroupKey := asset.ToSerialized(randAssetCtx.groupKey)

for _, newAsset := range mintedAssets {
if newAsset.GroupKey == nil {
Expand All @@ -768,7 +813,7 @@ func TestCommitBatchChainActions(t *testing.T) {
// of the group genesis asset must be deducted from the group
// balance before comparing to the minted asset.
if bytes.Equal(groupKey[:], existingGroupKey[:]) {
assetBalance.Balance -= groupGenAmt
assetBalance.Balance -= randAssetCtx.groupGenAmt
}

require.Equal(t, newAsset.Amount, assetBalance.Balance)
Expand Down
4 changes: 4 additions & 0 deletions tapdb/assets_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ func upsertAssetsWithGenesis(ctx context.Context, q UpsertAssetStore,
// itself.
assetIDs := make([]int32, len(assets))
for idx, a := range assets {
a := a

// First, we make sure the genesis asset information exists in
// the database.
genAssetID, err := upsertGenesis(
Expand Down Expand Up @@ -199,6 +201,8 @@ func upsertAssetsWithGenesis(ctx context.Context, q UpsertAssetStore,
anchorUtxoID = anchorUtxoIDs[idx]
}

log.Infof("proof asset version insert: ", a.Version)

// With all the dependent data inserted, we can now insert the
// base asset information itself.
assetIDs[idx], err = q.InsertNewAsset(
Expand Down
4 changes: 4 additions & 0 deletions tapdb/assets_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/commitment"
"github.com/lightninglabs/taproot-assets/fn"
Expand Down Expand Up @@ -665,6 +666,7 @@ func (a *AssetStore) dbAssetsToChainAssets(dbAssets []ConfirmedAsset,
},
}

log.Infof("db sprout version: ", sprout.Version)
assetSprout, err := asset.New(
assetGenesis, amount, lockTime, relativeLocktime,
scriptKey, groupKey,
Expand Down Expand Up @@ -861,6 +863,7 @@ func fetchAssetsWithWitness(ctx context.Context, q ActiveAssetsStore,
if err != nil {
return nil, nil, fmt.Errorf("unable to read db assets: %v", err)
}
log.Infof("query assets resp: %v", spew.Sdump(dbAssets))

assetIDs := fMap(dbAssets, func(a ConfirmedAsset) int32 {
return a.AssetPrimaryKey
Expand Down Expand Up @@ -1439,6 +1442,7 @@ func (a *AssetStore) importAssetFromProof(ctx context.Context,
}

// Insert/update the asset information in the database now.
log.Infof("proof asset import version: ", newAsset.Version)
_, assetIDs, err := upsertAssetsWithGenesis(
ctx, db, newAsset.Genesis.FirstPrevOut,
[]*asset.Asset{newAsset}, []sql.NullInt32{sqlInt32(utxoID)},
Expand Down
Loading

0 comments on commit 45a5f46

Please sign in to comment.