Skip to content

Commit 473ea54

Browse files
committed
tapdb: add tests for shouldInsertPreCommit and upsertSupplyPreCommit
Add unit tests for the shouldInsertPreCommit and upsertSupplyPreCommit functions to verify correct logic for determining insert behavior and upserting supply pre-commit entries in the database.
1 parent cb15607 commit 473ea54

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed

tapdb/universe_test.go

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/btcsuite/btcd/btcec/v2"
14+
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1415
"github.com/btcsuite/btcd/wire"
1516
"github.com/lightninglabs/taproot-assets/asset"
1617
"github.com/lightninglabs/taproot-assets/fn"
@@ -19,6 +20,7 @@ import (
1920
"github.com/lightninglabs/taproot-assets/proof"
2021
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
2122
"github.com/lightninglabs/taproot-assets/universe"
23+
"github.com/lightninglabs/taproot-assets/universe/supplycommit"
2224
"github.com/stretchr/testify/require"
2325
)
2426

@@ -1097,3 +1099,213 @@ func TestMultiverseRootSum(t *testing.T) {
10971099
})
10981100
}
10991101
}
1102+
1103+
// TestShouldInsertPreCommit tests the shouldInsertPreCommit function with
1104+
// various combinations of proof types, asset groups, and meta reveals.
1105+
func TestShouldInsertPreCommit(t *testing.T) {
1106+
t.Parallel()
1107+
1108+
// Create test data.
1109+
groupKey := test.RandPubKey(t)
1110+
assetWithGroup := asset.RandAsset(t, asset.Normal)
1111+
assetWithGroup.GroupKey = &asset.GroupKey{
1112+
GroupPubKey: *groupKey,
1113+
}
1114+
assetWithoutGroup := asset.RandAsset(t, asset.Normal)
1115+
assetWithoutGroup.GroupKey = nil
1116+
1117+
delegationKey := test.RandPubKey(t)
1118+
1119+
testCases := []struct {
1120+
name string
1121+
proofType universe.ProofType
1122+
asset *asset.Asset
1123+
metaReveal *proof.MetaReveal
1124+
expected bool
1125+
description string
1126+
}{
1127+
{
1128+
name: "transfer proof type",
1129+
proofType: universe.ProofTypeTransfer,
1130+
asset: assetWithGroup,
1131+
metaReveal: &proof.MetaReveal{
1132+
UniverseCommitments: true,
1133+
DelegationKey: fn.Some(*delegationKey),
1134+
},
1135+
expected: false,
1136+
description: "Transfer proofs should not insert " +
1137+
"pre-commits",
1138+
},
1139+
{
1140+
name: "issuance proof without group key",
1141+
proofType: universe.ProofTypeIssuance,
1142+
asset: assetWithoutGroup,
1143+
metaReveal: nil,
1144+
expected: false,
1145+
description: "Assets without group key should not " +
1146+
"insert pre-commits",
1147+
},
1148+
{
1149+
name: "issuance proof with group key but no " +
1150+
"meta reveal",
1151+
proofType: universe.ProofTypeIssuance,
1152+
asset: assetWithGroup,
1153+
metaReveal: nil,
1154+
expected: false,
1155+
description: "Missing meta reveal should not insert " +
1156+
"pre-commits",
1157+
},
1158+
{
1159+
name: "issuance proof with group key but no " +
1160+
"universe commitments",
1161+
proofType: universe.ProofTypeIssuance,
1162+
asset: assetWithGroup,
1163+
metaReveal: &proof.MetaReveal{
1164+
UniverseCommitments: false,
1165+
DelegationKey: fn.Some(*delegationKey),
1166+
},
1167+
expected: false,
1168+
description: "Meta reveal without universe " +
1169+
"commitments should not insert pre-commits",
1170+
},
1171+
{
1172+
name: "issuance proof with group key but no " +
1173+
"delegation key",
1174+
proofType: universe.ProofTypeIssuance,
1175+
asset: assetWithGroup,
1176+
metaReveal: &proof.MetaReveal{
1177+
UniverseCommitments: true,
1178+
DelegationKey: fn.None[btcec.PublicKey](),
1179+
},
1180+
expected: false,
1181+
description: "Meta reveal without delegation key " +
1182+
"should not insert pre-commits",
1183+
},
1184+
{
1185+
name: "valid issuance proof with all requirements",
1186+
proofType: universe.ProofTypeIssuance,
1187+
asset: assetWithGroup,
1188+
metaReveal: &proof.MetaReveal{
1189+
UniverseCommitments: true,
1190+
DelegationKey: fn.Some(*delegationKey),
1191+
},
1192+
expected: true,
1193+
description: "Valid issuance proof with all " +
1194+
"requirements should insert pre-commits",
1195+
},
1196+
{
1197+
name: "unspecified proof type",
1198+
proofType: universe.ProofTypeUnspecified,
1199+
asset: assetWithGroup,
1200+
metaReveal: &proof.MetaReveal{
1201+
UniverseCommitments: true,
1202+
DelegationKey: fn.Some(*delegationKey),
1203+
},
1204+
expected: false,
1205+
description: "Unspecified proof type should not " +
1206+
"insert pre-commits",
1207+
},
1208+
}
1209+
1210+
for _, tc := range testCases {
1211+
t.Run(tc.name, func(t *testing.T) {
1212+
// Create a proof with the test asset.
1213+
testProof := randProof(t, tc.asset)
1214+
1215+
result := shouldInsertPreCommit(
1216+
tc.proofType, *testProof, tc.metaReveal,
1217+
)
1218+
1219+
require.Equal(t, tc.expected, result, tc.description)
1220+
})
1221+
}
1222+
}
1223+
1224+
// TestUpsertSupplyPreCommit tests the upsertSupplyPreCommit function with
1225+
// various scenarios including new inserts and updates.
1226+
func TestUpsertSupplyPreCommit(t *testing.T) {
1227+
t.Parallel()
1228+
1229+
ctx := context.Background()
1230+
db := NewTestDB(t)
1231+
1232+
// Create test data.
1233+
groupKey := test.RandPubKey(t)
1234+
internalKey, _ := test.RandKeyDesc(t)
1235+
mintingTx := wire.NewMsgTx(2)
1236+
mintingTx.AddTxOut(&wire.TxOut{Value: 1000})
1237+
blockHeight := uint32(100)
1238+
1239+
preCommit := supplycommit.PreCommitment{
1240+
GroupPubKey: *groupKey,
1241+
InternalKey: internalKey,
1242+
MintingTxn: mintingTx,
1243+
OutIdx: 0,
1244+
BlockHeight: blockHeight,
1245+
}
1246+
1247+
t.Run("successful insert", func(t *testing.T) {
1248+
err := upsertSupplyPreCommit(ctx, db, preCommit)
1249+
require.NoError(t, err)
1250+
1251+
// Verify the pre-commit was inserted by fetching unspent
1252+
// pre-commits for this group key.
1253+
groupKeyBytes := schnorr.SerializePubKey(&preCommit.GroupPubKey)
1254+
rows, err := db.FetchUnspentSupplyPreCommits(ctx, groupKeyBytes)
1255+
require.NoError(t, err)
1256+
require.Len(t, rows, 1)
1257+
require.Equal(t, groupKeyBytes, rows[0].GroupKey)
1258+
require.Equal(t,
1259+
preCommit.InternalKey.PubKey.SerializeCompressed(),
1260+
rows[0].TaprootInternalKey,
1261+
)
1262+
})
1263+
1264+
t.Run("successful update", func(t *testing.T) {
1265+
// Update with a different group key.
1266+
newGroupKey := test.RandPubKey(t)
1267+
updatedPreCommit := preCommit
1268+
updatedPreCommit.GroupPubKey = *newGroupKey
1269+
1270+
err := upsertSupplyPreCommit(ctx, db, updatedPreCommit)
1271+
require.NoError(t, err)
1272+
1273+
// Verify the pre-commit was updated by fetching pre-commits for
1274+
// the new group key.
1275+
newGroupKeyBytes := schnorr.SerializePubKey(newGroupKey)
1276+
rows, err := db.FetchUnspentSupplyPreCommits(
1277+
ctx, newGroupKeyBytes,
1278+
)
1279+
require.NoError(t, err)
1280+
require.Len(t, rows, 1)
1281+
require.Equal(t, newGroupKeyBytes, rows[0].GroupKey)
1282+
})
1283+
1284+
t.Run("existing chain tx with block height", func(t *testing.T) {
1285+
// First, insert a chain tx with a block height.
1286+
txBytes, err := fn.Serialize(mintingTx)
1287+
require.NoError(t, err)
1288+
1289+
_, err = db.UpsertChainTx(ctx, ChainTxParams{
1290+
Txid: fn.ByteSlice(mintingTx.TxHash()),
1291+
RawTx: txBytes,
1292+
BlockHeight: sqlInt32(200),
1293+
})
1294+
require.NoError(t, err)
1295+
1296+
// Create a new pre-commit with different outpoint.
1297+
newPreCommit := preCommit
1298+
newPreCommit.OutIdx = 1
1299+
1300+
err = upsertSupplyPreCommit(ctx, db, newPreCommit)
1301+
require.NoError(t, err)
1302+
1303+
// Verify it was inserted successfully by fetching pre-commits.
1304+
groupKeyBytes := schnorr.SerializePubKey(
1305+
&newPreCommit.GroupPubKey,
1306+
)
1307+
rows, err := db.FetchUnspentSupplyPreCommits(ctx, groupKeyBytes)
1308+
require.NoError(t, err)
1309+
require.Len(t, rows, 1)
1310+
})
1311+
}

0 commit comments

Comments
 (0)