Skip to content

Commit fe562a1

Browse files
committed
WIP: debug x,[y, -y] group key serialization
I noticed that the universe ID group key returned in the new root of the sync diff event sometimes differs from the group key I use when upserting the pre-commit output (which is taken from the issuance leaf proof). The only difference is the prefix, for example: 039bd899fe00ce28741cc81473920f918356af0656a9759f6067a84f26d4951e21 029bd899fe00ce28741cc81473920f918356af0656a9759f6067a84f26d4951e21 I do not understand why the leaves and the universe ID would have different group keys (y component differs). It is flaky, not always the case. I assume I should just use schnorr.SerializePubKey, which strips the prefix with [1:], but I do not see why that should be necessary. I guess we should only ever store 32 byte schnorr.SerializePubKey in the db? I think that's what we do for the universe and multiverse SQL tables. But then for the new supply commit tables we store the 33 byte compressed, which doesn't seem stable to me anymore. The y component of a secp256k1 public key can be either -y or y for the same private key. An "odd" y is messing things up somewhere.
1 parent 060429e commit fe562a1

File tree

6 files changed

+86
-58
lines changed

6 files changed

+86
-58
lines changed

itest/supply_commit_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,10 @@ func testSupplyCommitIgnoreAsset(t *harnessTest) {
591591
t.syncUniverseState(t.universeServer.service, secondTapd, 0)
592592
t.Log("Attempting to fetch supply commit from secondary node")
593593

594-
fetchResp = nil
594+
var peerFetchResp *unirpc.FetchSupplyCommitResponse
595595
require.Eventually(t.t, func() bool {
596596
// nolint: lll
597-
fetchResp, err = secondTapd.FetchSupplyCommit(
597+
peerFetchResp, err = secondTapd.FetchSupplyCommit(
598598
ctxb, &unirpc.FetchSupplyCommitRequest{
599599
GroupKey: &unirpc.FetchSupplyCommitRequest_GroupKeyBytes{
600600
GroupKeyBytes: groupKeyBytes,
@@ -612,15 +612,15 @@ func testSupplyCommitIgnoreAsset(t *harnessTest) {
612612
// If the fetch response has no block height or hash,
613613
// it means that the supply commitment transaction has not
614614
// been mined yet, so we should retry.
615-
if fetchResp.ChainData.BlockHeight == 0 ||
616-
len(fetchResp.ChainData.BlockHash) == 0 {
615+
if peerFetchResp.ChainData.BlockHeight == 0 ||
616+
len(peerFetchResp.ChainData.BlockHash) == 0 {
617617

618618
return false
619619
}
620620

621621
// Once the ignore tree includes the ignored asset outpoint, we
622622
// know that the supply commitment has been updated.
623-
if fetchResp.IgnoreSubtreeRoot == nil {
623+
if peerFetchResp.IgnoreSubtreeRoot == nil {
624624
return false
625625
}
626626

tapdb/supply_commit.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1414
"github.com/btcsuite/btcd/chaincfg/chainhash"
1515
"github.com/btcsuite/btcd/wire"
16+
"github.com/davecgh/go-spew/spew"
1617
"github.com/lightninglabs/taproot-assets/asset"
1718
"github.com/lightninglabs/taproot-assets/fn"
1819
"github.com/lightninglabs/taproot-assets/mssmt"
@@ -358,6 +359,9 @@ func (s *SupplyCommitMachine) UnspentPrecommits(ctx context.Context,
358359

359360
// No pre-commits found where we were the issuer. So now
360361
// we'll query for pre-commits from other issuers.
362+
log.Debugf("Fetching non-issuer supply pre-commit: group_key_bytes=%x", groupKeyBytes)
363+
log.Debugf("Fetching non-issuer supply pre-commit: group_key=%s", spew.Sdump(groupKey))
364+
361365
rows, err := db.FetchUnspentSupplyPreCommits(
362366
ctx, schnorr.SerializePubKey(groupKey),
363367
)

tapdb/universe.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/btcsuite/btcd/btcec/v2"
1111
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1212
"github.com/btcsuite/btcd/wire"
13+
"github.com/davecgh/go-spew/spew"
1314
"github.com/lightninglabs/taproot-assets/asset"
1415
"github.com/lightninglabs/taproot-assets/fn"
1516
"github.com/lightninglabs/taproot-assets/mssmt"
@@ -703,6 +704,9 @@ func maybeUpsertSupplyPreCommit(ctx context.Context, dbTx UpsertAssetStore,
703704
return err
704705
}
705706

707+
log.Debugf("Upserting supply pre-commit: group_key_bytes=%x", groupKeyBytes)
708+
log.Debugf("Upserting supply pre-commit: group_key=%s", spew.Sdump(issuanceProof.Asset.GroupKey.GroupPubKey))
709+
706710
_, err = dbTx.UpsertSupplyPreCommit(
707711
ctx, sqlc.UpsertSupplyPreCommitParams{
708712
TaprootInternalKey: taprootInternalKeyBytes,
@@ -949,6 +953,11 @@ func universeUpsertProofLeaf(ctx context.Context, dbTx BaseUniverseStore,
949953
// If the asset group supports supply commitments and this is an
950954
// issuance proof, then we may need to log the supply pre-commitment
951955
// output.
956+
if leaf.GroupKey != nil && groupKey != nil {
957+
log.Debugf("leaf group key: %x", leaf.GroupKey.GroupPubKey.SerializeCompressed())
958+
log.Debugf("upsert arg groupKey: %x", groupKey.SerializeCompressed())
959+
}
960+
952961
err = maybeUpsertSupplyPreCommit(
953962
ctx, dbTx, proofType, leafProof, metaReveal,
954963
)

universe/supplycommit/transitions.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ func newRootCommitment(ctx context.Context,
454454
logger.WhenSome(func(l btclog.Logger) {
455455
l.Infof("Creating new root commitment, spending %v "+
456456
"pre-commits", len(unspentPreCommits))
457+
458+
for _, pre := range unspentPreCommits {
459+
l.Infof("pre-commit outpoint: %s", pre.OutPoint())
460+
}
457461
})
458462

459463
newCommitTx := wire.NewMsgTx(2)
@@ -494,10 +498,10 @@ func newRootCommitment(ctx context.Context,
494498
// commitment that was broadcast.
495499
var spentCommitOp fn.Option[wire.OutPoint]
496500
oldCommitment.WhenSome(func(r RootCommitment) {
497-
logger.WhenSome(func(l btclog.Logger) {
498-
l.Infof("Re-using prior commitment as outpoint=%v: %v",
499-
r.CommitPoint(), limitSpewer.Sdump(r))
500-
})
501+
//logger.WhenSome(func(l btclog.Logger) {
502+
// l.Infof("Re-using prior commitment as outpoint=%v: %v",
503+
// r.CommitPoint(), limitSpewer.Sdump(r))
504+
//})
501505

502506
newCommitTx.AddTxIn(r.TxIn())
503507

@@ -602,10 +606,10 @@ func newRootCommitment(ctx context.Context,
602606
SpentCommitment: spentCommitOp,
603607
}
604608

605-
logger.WhenSome(func(l btclog.Logger) {
606-
l.Infof("Created new root commitment: %v",
607-
limitSpewer.Sdump(newSupplyCommit))
608-
})
609+
//logger.WhenSome(func(l btclog.Logger) {
610+
// l.Infof("Created new root commitment: %v",
611+
// limitSpewer.Sdump(newSupplyCommit))
612+
//})
609613

610614
commitPkt, err := psbt.NewFromUnsignedTx(newCommitTx)
611615
if err != nil {
@@ -836,8 +840,8 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
836840
"commitment tx: %w", err)
837841
}
838842

839-
prefixedLog.Infof("Signed supply "+
840-
"commitment txn: %v", limitSpewer.Sdump(signedPsbt))
843+
//prefixedLog.Infof("Signed supply "+
844+
// "commitment txn: %v", limitSpewer.Sdump(signedPsbt))
841845

842846
err = psbt.MaybeFinalizeAll(signedPsbt)
843847
if err != nil {
@@ -930,10 +934,10 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
930934
return nil, fmt.Errorf("commitment transaction is nil")
931935
}
932936

933-
commitTxid := c.SupplyTransition.NewCommitment.Txn.TxHash()
934-
prefixedLog.Infof("Broadcasting supply commitment "+
935-
"txn (txid=%v): %v", commitTxid,
936-
limitSpewer.Sdump(c.SupplyTransition.NewCommitment.Txn))
937+
//commitTxid := c.SupplyTransition.NewCommitment.Txn.TxHash()
938+
//prefixedLog.Infof("Broadcasting supply commitment "+
939+
// "txn (txid=%v): %v", commitTxid,
940+
// limitSpewer.Sdump(c.SupplyTransition.NewCommitment.Txn))
937941

938942
commitTx := c.SupplyTransition.NewCommitment.Txn
939943

@@ -1022,10 +1026,10 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
10221026
TxIndex: newEvent.TxIndex,
10231027
})
10241028

1025-
prefixedLog.Infof("Supply commitment txn confirmed "+
1026-
"in block %d (hash=%v): %v",
1027-
newEvent.BlockHeight, newEvent.Block.Header.BlockHash(),
1028-
limitSpewer.Sdump(c.SupplyTransition.NewCommitment.Txn))
1029+
//prefixedLog.Infof("Supply commitment txn confirmed "+
1030+
// "in block %d (hash=%v): %v",
1031+
// newEvent.BlockHeight, newEvent.Block.Header.BlockHash(),
1032+
// limitSpewer.Sdump(c.SupplyTransition.NewCommitment.Txn))
10291033

10301034
// The commitment has been confirmed, so we'll transition to the
10311035
// finalize state, but also log on disk that we no longer need

universe/supplyverifier/manager.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ func (m *Manager) handleUniSyncEvent(event fn.Event) error {
256256
return nil
257257
}
258258

259-
// If the sync diff is not a new issuance, we
260-
// disregard it.
259+
// If the sync diff is not a new issuance, we disregard it.
261260
universeID := syncDiffEvent.SyncDiff.NewUniverseRoot.ID
262261
if universeID.ProofType != universe.ProofTypeIssuance {
263262
return nil
@@ -268,7 +267,24 @@ func (m *Manager) handleUniSyncEvent(event fn.Event) error {
268267
if universeID.GroupKey == nil {
269268
return nil
270269
}
271-
assetSpec := asset.NewSpecifierFromGroupKey(*universeID.GroupKey)
270+
271+
// If there are no new leaf proofs, we disregard the sync event.
272+
if len(syncDiffEvent.SyncDiff.NewLeafProofs) == 0 {
273+
return nil
274+
}
275+
276+
// Get genesis asset ID from the first synced leaf and formulate an
277+
// asset specifier.
278+
//
279+
// TODO(ffranr): Revisit this. We select any asset ID to aid in metdata
280+
// retrieval, but we should be able to do this with just the group key.
281+
// However, QueryAssetGroupByGroupKey currently fails for the asset
282+
// group.
283+
assetID := syncDiffEvent.SyncDiff.NewLeafProofs[0].Genesis.ID()
284+
285+
assetSpec := asset.NewSpecifierOptionalGroupPubKey(
286+
assetID, universeID.GroupKey,
287+
)
272288

273289
// Check that the asset group supports supply
274290
// commitments.
@@ -436,6 +452,9 @@ func (m *Manager) fetchStateMachine(assetSpec asset.Specifier) (*StateMachine,
436452
// the cache with a new running instance.
437453
}
438454

455+
log.Debugf("Creating new supply verifier state machine for "+
456+
"group: %x", groupKey.SerializeCompressed())
457+
439458
ctx, cancel := m.WithCtxQuitNoTimeout()
440459
defer cancel()
441460

universe/supplyverifier/transitions.go

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,35 @@ func (s *InitState) ProcessEvent(event Event,
2323
case *InitEvent:
2424
ctx := context.Background()
2525

26-
// First, we'll query local db for the latest verified supply
27-
// commitment.
26+
// Retrieve the set of unspent pre-commitments for the asset
27+
// group. We will need these later to watch their spends.
28+
preCommits, err := env.SupplyCommitView.UnspentPrecommits(
29+
ctx, env.AssetSpec, false,
30+
).Unpack()
31+
if err != nil {
32+
return nil, fmt.Errorf("unable to fetch unspent "+
33+
"pre-commitments: %w", err)
34+
}
35+
36+
// Query local db for the latest verified supply commitment.
2837
latestCommit, err := env.SupplyCommitView.FetchLatestCommitment(
2938
ctx, env.AssetSpec,
3039
)
3140
switch {
3241
case errors.Is(err, ErrCommitmentNotFound):
33-
// If we don't have a supply commitment in our local db,
34-
// then we will kick things off by syncing supply
35-
// commitment proofs to catch up.
36-
return &StateTransition{
37-
NextState: &SyncVerifyState{},
38-
NewEvents: lfn.Some(FsmEvent{
39-
InternalEvent: []Event{
40-
&SyncVerifyEvent{},
41-
},
42-
}),
43-
}, nil
42+
// Continue without the latest commitment.
4443

4544
case err != nil:
4645
return nil, fmt.Errorf("unable to fetch latest "+
4746
"verified commitment from db: %w", err)
4847
}
4948

50-
// If a prior verified commitment exists, construct a watch
51-
// event for its spend along with any other unspent
52-
// pre-commitments. This code path also handles catching up on
53-
// supply commitments missed while offline.
54-
preCommits, err := env.SupplyCommitView.UnspentPrecommits(
55-
ctx, env.AssetSpec, false,
56-
).Unpack()
57-
if err != nil {
58-
return nil, fmt.Errorf("unable to fetch unspent "+
59-
"pre-commitments: %w", err)
49+
// If at this point we don't have any pre-commitments or a
50+
// verified supply commitment, then we'll have to raise an
51+
// error. Something went wrong before this point.
52+
if latestCommit == nil && len(preCommits) == 0 {
53+
return nil, fmt.Errorf("no pre-commitments or " +
54+
"verified supply commitment found")
6055
}
6156

6257
return &StateTransition{
@@ -319,26 +314,23 @@ func (s *WatchOutputsState) ProcessEvent(event Event,
319314
for idx := range preCommits {
320315
preCommit := preCommits[idx]
321316

322-
outpoint := wire.OutPoint{
323-
Hash: preCommit.MintingTxn.TxHash(),
324-
Index: preCommit.OutIdx,
325-
}
326-
txOut := preCommit.MintingTxn.TxOut[preCommit.OutIdx]
317+
log.Debugf("watching pre-commitment: %s", preCommit.OutPoint().String())
327318

328-
pc := preCommit
319+
txOut := preCommit.MintingTxn.TxOut[preCommit.OutIdx]
329320
mapper := func(spend *chainntnfs.SpendDetail) Event {
330321
// nolint: lll
331322
return &SpendEvent{
332323
SpendDetail: spend,
333-
SpentPreCommitment: &pc,
324+
SpentPreCommitment: &preCommit,
334325
PreCommitments: preCommits,
335326
WatchStartTimestamp: watchStartTimestamp,
336327
}
337328
}
338329

339330
events = append(events, &protofsm.RegisterSpend[Event]{
340-
OutPoint: outpoint,
341-
PkScript: txOut.PkScript,
331+
OutPoint: preCommit.OutPoint(),
332+
PkScript: txOut.PkScript,
333+
HeightHint: preCommit.BlockHeight,
342334
PostSpendEvent: lfn.Some(
343335
protofsm.SpendMapper[Event](mapper),
344336
),

0 commit comments

Comments
 (0)