Skip to content

Commit 473ae23

Browse files
committed
tapdb: extract unit test func for populating asset and proof in db
This commit adds a unit test db handler helper method which we will use in a new test (in a subsequent commit) to populate a unit test db with an asset and its corresponding proof.
1 parent b76189f commit 473ae23

File tree

2 files changed

+141
-97
lines changed

2 files changed

+141
-97
lines changed

tapdb/assets_store_test.go

Lines changed: 11 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -246,105 +246,17 @@ func assertAssetEqual(t *testing.T, a, b *asset.Asset) {
246246
func TestImportAssetProof(t *testing.T) {
247247
t.Parallel()
248248

249-
// First, we'll create a new instance of the database.
250-
_, assetStore, db := newAssetStore(t)
251-
252-
// Next, we'll make a new random asset that also has a few inputs with
253-
// dummy witness information.
254-
testAsset := randAsset(t)
255-
256-
assetRoot, err := commitment.NewAssetCommitment(testAsset)
257-
require.NoError(t, err)
258-
259-
taprootAssetRoot, err := commitment.NewTapCommitment(assetRoot)
260-
require.NoError(t, err)
261-
262-
// With our asset created, we can now create the AnnotatedProof we use
263-
// to import assets into the database.
264-
var blockHash chainhash.Hash
265-
_, err = rand.Read(blockHash[:])
266-
require.NoError(t, err)
249+
var (
250+
ctxb = context.Background()
267251

268-
anchorTx := wire.NewMsgTx(2)
269-
anchorTx.AddTxIn(&wire.TxIn{})
270-
anchorTx.AddTxOut(&wire.TxOut{
271-
PkScript: bytes.Repeat([]byte{0x01}, 34),
272-
Value: 10,
273-
})
252+
dbHandle = NewDbHandle(t)
253+
assetStore = dbHandle.AssetStore
254+
)
274255

256+
// Add a random asset and corresponding proof into the database.
257+
testAsset, testProof := dbHandle.AddRandomAssetProof(t)
275258
assetID := testAsset.ID()
276-
anchorPoint := wire.OutPoint{
277-
Hash: anchorTx.TxHash(),
278-
Index: 0,
279-
}
280-
initialBlob := bytes.Repeat([]byte{0x0}, 100)
281-
updatedBlob := bytes.Repeat([]byte{0x77}, 100)
282-
testProof := &proof.AnnotatedProof{
283-
Locator: proof.Locator{
284-
AssetID: &assetID,
285-
ScriptKey: *testAsset.ScriptKey.PubKey,
286-
},
287-
Blob: initialBlob,
288-
AssetSnapshot: &proof.AssetSnapshot{
289-
Asset: testAsset,
290-
OutPoint: anchorPoint,
291-
AnchorBlockHash: blockHash,
292-
AnchorBlockHeight: test.RandInt[uint32](),
293-
AnchorTxIndex: test.RandInt[uint32](),
294-
AnchorTx: anchorTx,
295-
OutputIndex: 0,
296-
InternalKey: test.RandPubKey(t),
297-
ScriptRoot: taprootAssetRoot,
298-
},
299-
}
300-
if testAsset.GroupKey != nil {
301-
testProof.GroupKey = &testAsset.GroupKey.GroupPubKey
302-
}
303-
304-
// We'll now insert the internal key information as well as the script
305-
// key ahead of time to reflect the address creation that happens
306-
// elsewhere.
307-
ctxb := context.Background()
308-
_, err = db.UpsertInternalKey(ctxb, InternalKey{
309-
RawKey: testProof.InternalKey.SerializeCompressed(),
310-
KeyFamily: test.RandInt[int32](),
311-
KeyIndex: test.RandInt[int32](),
312-
})
313-
require.NoError(t, err)
314-
rawScriptKeyID, err := db.UpsertInternalKey(ctxb, InternalKey{
315-
RawKey: testAsset.ScriptKey.RawKey.PubKey.SerializeCompressed(),
316-
KeyFamily: int32(testAsset.ScriptKey.RawKey.Family),
317-
KeyIndex: int32(testAsset.ScriptKey.RawKey.Index),
318-
})
319-
require.NoError(t, err)
320-
_, err = db.UpsertScriptKey(ctxb, NewScriptKey{
321-
InternalKeyID: rawScriptKeyID,
322-
TweakedScriptKey: testAsset.ScriptKey.PubKey.SerializeCompressed(),
323-
Tweak: nil,
324-
})
325-
require.NoError(t, err)
326-
327-
// We'll add the chain transaction of the proof now to simulate a
328-
// batched transfer on a higher layer.
329-
var anchorTxBuf bytes.Buffer
330-
err = testProof.AnchorTx.Serialize(&anchorTxBuf)
331-
require.NoError(t, err)
332-
anchorTXID := testProof.AnchorTx.TxHash()
333-
_, err = db.UpsertChainTx(ctxb, ChainTxParams{
334-
Txid: anchorTXID[:],
335-
RawTx: anchorTxBuf.Bytes(),
336-
BlockHeight: sqlInt32(testProof.AnchorBlockHeight),
337-
BlockHash: testProof.AnchorBlockHash[:],
338-
TxIndex: sqlInt32(testProof.AnchorTxIndex),
339-
})
340-
require.NoError(t, err, "unable to insert chain tx: %w", err)
341-
342-
// With all our test data constructed, we'll now attempt to import the
343-
// asset into the database.
344-
require.NoError(t, assetStore.ImportProofs(
345-
ctxb, proof.MockHeaderVerifier, proof.MockGroupVerifier, false,
346-
testProof,
347-
))
259+
initialBlob := testProof.Blob
348260

349261
// We should now be able to retrieve the set of all assets inserted on
350262
// disk.
@@ -371,7 +283,7 @@ func TestImportAssetProof(t *testing.T) {
371283
ScriptKey: *testAsset.ScriptKey.PubKey,
372284
})
373285
require.NoError(t, err)
374-
require.Equal(t, initialBlob, []byte(currentBlob))
286+
require.Equal(t, initialBlob, currentBlob)
375287

376288
// We should also be able to fetch the created asset above based on
377289
// either the asset ID, or key group via the main coin selection
@@ -391,6 +303,8 @@ func TestImportAssetProof(t *testing.T) {
391303

392304
// We'll now attempt to overwrite the proof with one that has different
393305
// block information (simulating a re-org).
306+
updatedBlob := bytes.Repeat([]byte{0x77}, 100)
307+
394308
testProof.AnchorBlockHash = chainhash.Hash{12, 34, 56}
395309
testProof.AnchorBlockHeight = 1234
396310
testProof.AnchorTxIndex = 5678

tapdb/sqlutils_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package tapdb
22

33
import (
4+
"bytes"
5+
"context"
46
"database/sql"
7+
"math/rand"
58
"testing"
69
"time"
710

11+
"github.com/btcsuite/btcd/chaincfg/chainhash"
12+
"github.com/btcsuite/btcd/wire"
13+
"github.com/lightninglabs/taproot-assets/asset"
14+
"github.com/lightninglabs/taproot-assets/commitment"
15+
"github.com/lightninglabs/taproot-assets/internal/test"
16+
"github.com/lightninglabs/taproot-assets/proof"
817
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
918
"github.com/lightningnetwork/lnd/clock"
19+
"github.com/stretchr/testify/require"
1020
)
1121

1222
// DbHandler is a helper struct that contains all the database stores.
@@ -28,6 +38,126 @@ type DbHandler struct {
2838
DirectQuery sqlc.Querier
2939
}
3040

41+
// AddRandomAssetProof generates a random asset and corresponding proof and
42+
// inserts them into the given test database.
43+
func (d *DbHandler) AddRandomAssetProof(t *testing.T) (*asset.Asset,
44+
*proof.AnnotatedProof) {
45+
46+
var (
47+
ctx = context.Background()
48+
49+
assetStore = d.AssetStore
50+
db = d.DirectQuery
51+
)
52+
53+
// Next, we'll make a new random asset that also has a few inputs with
54+
// dummy witness information.
55+
testAsset := randAsset(t)
56+
57+
assetRoot, err := commitment.NewAssetCommitment(testAsset)
58+
require.NoError(t, err)
59+
60+
taprootAssetRoot, err := commitment.NewTapCommitment(assetRoot)
61+
require.NoError(t, err)
62+
63+
// With our asset created, we can now create the AnnotatedProof we use
64+
// to import assets into the database.
65+
var blockHash chainhash.Hash
66+
_, err = rand.Read(blockHash[:])
67+
require.NoError(t, err)
68+
69+
anchorTx := wire.NewMsgTx(2)
70+
anchorTx.AddTxIn(&wire.TxIn{})
71+
anchorTx.AddTxOut(&wire.TxOut{
72+
PkScript: bytes.Repeat([]byte{0x01}, 34),
73+
Value: 10,
74+
})
75+
76+
assetID := testAsset.ID()
77+
anchorPoint := wire.OutPoint{
78+
Hash: anchorTx.TxHash(),
79+
Index: 0,
80+
}
81+
82+
// Generate a random proof and encode it into a proof blob.
83+
testProof := randProof(t, testAsset)
84+
85+
var proofBlobBuffer bytes.Buffer
86+
err = testProof.Encode(&proofBlobBuffer)
87+
require.NoError(t, err)
88+
89+
proofBlob := proofBlobBuffer.Bytes()
90+
scriptKey := testAsset.ScriptKey
91+
92+
annotatedProof := &proof.AnnotatedProof{
93+
Locator: proof.Locator{
94+
AssetID: &assetID,
95+
ScriptKey: *scriptKey.PubKey,
96+
},
97+
Blob: proofBlob,
98+
AssetSnapshot: &proof.AssetSnapshot{
99+
Asset: testAsset,
100+
OutPoint: anchorPoint,
101+
AnchorBlockHash: blockHash,
102+
AnchorBlockHeight: test.RandInt[uint32](),
103+
AnchorTxIndex: test.RandInt[uint32](),
104+
AnchorTx: anchorTx,
105+
OutputIndex: 0,
106+
InternalKey: test.RandPubKey(t),
107+
ScriptRoot: taprootAssetRoot,
108+
},
109+
}
110+
if testAsset.GroupKey != nil {
111+
annotatedProof.GroupKey = &testAsset.GroupKey.GroupPubKey
112+
}
113+
114+
// We'll now insert the internal key information as well as the script
115+
// key ahead of time to reflect the address creation that happens
116+
// elsewhere.
117+
_, err = db.UpsertInternalKey(ctx, InternalKey{
118+
RawKey: annotatedProof.InternalKey.SerializeCompressed(),
119+
KeyFamily: test.RandInt[int32](),
120+
KeyIndex: test.RandInt[int32](),
121+
})
122+
require.NoError(t, err)
123+
rawScriptKeyID, err := db.UpsertInternalKey(ctx, InternalKey{
124+
RawKey: scriptKey.RawKey.PubKey.SerializeCompressed(),
125+
KeyFamily: int32(testAsset.ScriptKey.RawKey.Family),
126+
KeyIndex: int32(testAsset.ScriptKey.RawKey.Index),
127+
})
128+
require.NoError(t, err)
129+
_, err = db.UpsertScriptKey(ctx, NewScriptKey{
130+
InternalKeyID: rawScriptKeyID,
131+
TweakedScriptKey: scriptKey.PubKey.SerializeCompressed(),
132+
Tweak: nil,
133+
})
134+
require.NoError(t, err)
135+
136+
// We'll add the chain transaction of the proof now to simulate a
137+
// batched transfer on a higher layer.
138+
var anchorTxBuf bytes.Buffer
139+
err = annotatedProof.AnchorTx.Serialize(&anchorTxBuf)
140+
require.NoError(t, err)
141+
anchorTXID := annotatedProof.AnchorTx.TxHash()
142+
_, err = db.UpsertChainTx(ctx, ChainTxParams{
143+
Txid: anchorTXID[:],
144+
RawTx: anchorTxBuf.Bytes(),
145+
BlockHeight: sqlInt32(annotatedProof.AnchorBlockHeight),
146+
BlockHash: annotatedProof.AnchorBlockHash[:],
147+
TxIndex: sqlInt32(annotatedProof.AnchorTxIndex),
148+
})
149+
require.NoError(t, err, "unable to insert chain tx: %w", err)
150+
151+
// With all our test data constructed, we'll now attempt to import the
152+
// asset into the database.
153+
require.NoError(t, assetStore.ImportProofs(
154+
ctx, proof.MockHeaderVerifier, proof.MockGroupVerifier, false,
155+
annotatedProof,
156+
))
157+
158+
return testAsset, annotatedProof
159+
}
160+
31161
// NewDbHandle creates a new store and query handle to the test database.
32162
func NewDbHandle(t *testing.T) *DbHandler {
33163
// Create a new test database.

0 commit comments

Comments
 (0)