Skip to content

Commit 5995896

Browse files
committed
universe: create new LeafKey interface
This is a prep for fixing an issue with Universe keys that have an identical 2-tuple, but distinct 3-tuple.
1 parent 430614b commit 5995896

File tree

14 files changed

+94
-60
lines changed

14 files changed

+94
-60
lines changed

rpcserver.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5148,11 +5148,11 @@ func (r *rpcServer) DeleteAssetRoot(ctx context.Context,
51485148
func marshalLeafKey(leafKey universe.LeafKey) *unirpc.AssetKey {
51495149
return &unirpc.AssetKey{
51505150
Outpoint: &unirpc.AssetKey_OpStr{
5151-
OpStr: leafKey.OutPoint.String(),
5151+
OpStr: leafKey.LeafOutPoint().String(),
51525152
},
51535153
ScriptKey: &unirpc.AssetKey_ScriptKeyBytes{
51545154
ScriptKeyBytes: schnorr.SerializePubKey(
5155-
leafKey.ScriptKey.PubKey,
5155+
leafKey.LeafScriptKey().PubKey,
51565156
),
51575157
},
51585158
}
@@ -5287,7 +5287,7 @@ func (r *rpcServer) AssetLeaves(ctx context.Context,
52875287

52885288
// unmarshalLeafKey un-marshals a leaf key from the RPC form.
52895289
func unmarshalLeafKey(key *unirpc.AssetKey) (universe.LeafKey, error) {
5290-
var leafKey universe.LeafKey
5290+
var leafKey universe.BaseLeafKey
52915291

52925292
switch {
52935293
case key.GetScriptKeyBytes() != nil:
@@ -5537,7 +5537,7 @@ func unmarshalUniverseKey(key *unirpc.UniverseKey) (universe.Identifier,
55375537

55385538
var (
55395539
uniID = universe.Identifier{}
5540-
uniKey = universe.LeafKey{}
5540+
uniKey = universe.BaseLeafKey{}
55415541
err error
55425542
)
55435543

@@ -5550,12 +5550,12 @@ func unmarshalUniverseKey(key *unirpc.UniverseKey) (universe.Identifier,
55505550
return uniID, uniKey, err
55515551
}
55525552

5553-
uniKey, err = unmarshalLeafKey(key.LeafKey)
5553+
leafKey, err := unmarshalLeafKey(key.LeafKey)
55545554
if err != nil {
55555555
return uniID, uniKey, err
55565556
}
55575557

5558-
return uniID, uniKey, nil
5558+
return uniID, leafKey, nil
55595559
}
55605560

55615561
// unmarshalAssetLeaf unmarshals an asset leaf from the RPC form.

tapdb/burn_tree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func decodeAndBuildAuthBurnLeaf(dbLeaf UniverseLeaf) (
229229
}
230230
scriptKey := asset.NewScriptKey(scriptPub)
231231

232-
leafKey := universe.LeafKey{
232+
leafKey := universe.BaseLeafKey{
233233
OutPoint: burnProof.OutPoint(),
234234
ScriptKey: &scriptKey,
235235
}

tapdb/burn_tree_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func createBurnLeaf(t *testing.T) *universe.BurnLeaf {
4242
scriptKey := asset.RandScriptKey(t)
4343

4444
return &universe.BurnLeaf{
45-
UniverseKey: universe.LeafKey{
45+
UniverseKey: universe.BaseLeafKey{
4646
OutPoint: burnProof.OutPoint(),
4747
ScriptKey: &scriptKey,
4848
},
@@ -189,7 +189,7 @@ func TestBurnUniverseTreeInsertBurns(t *testing.T) {
189189
}
190190

191191
nonBurnLeaf := &universe.BurnLeaf{
192-
UniverseKey: universe.LeafKey{
192+
UniverseKey: universe.BaseLeafKey{
193193
OutPoint: op,
194194
ScriptKey: &a.ScriptKey,
195195
},
@@ -270,10 +270,10 @@ func TestBurnUniverseTreeInsertBurns(t *testing.T) {
270270
count := 0
271271
for _, leaf := range queryLeaves {
272272
//nolint:lll
273-
if leaf.BurnLeaf.UniverseKey.OutPoint ==
274-
burnLeaf.UniverseKey.OutPoint &&
275-
leaf.BurnLeaf.UniverseKey.ScriptKey.PubKey.IsEqual(
276-
burnLeaf.UniverseKey.ScriptKey.PubKey,
273+
if leaf.BurnLeaf.UniverseKey.LeafOutPoint() ==
274+
burnLeaf.UniverseKey.LeafOutPoint() &&
275+
leaf.BurnLeaf.UniverseKey.LeafScriptKey().PubKey.IsEqual(
276+
burnLeaf.UniverseKey.LeafScriptKey().PubKey,
277277
) {
278278

279279
count++

tapdb/multiverse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ func (b *MultiverseStore) FetchProof(ctx context.Context,
698698
ProofType: universe.ProofTypeTransfer,
699699
}
700700
scriptKey := asset.NewScriptKey(&loc.ScriptKey)
701-
leafKey := universe.LeafKey{
701+
leafKey := universe.BaseLeafKey{
702702
ScriptKey: &scriptKey,
703703
}
704704
if loc.OutPoint != nil {
@@ -1028,7 +1028,7 @@ func (b *MultiverseStore) RegisterSubscriber(
10281028
ProofType: universe.ProofTypeTransfer,
10291029
}
10301030
scriptKey := asset.NewScriptKey(&loc.ScriptKey)
1031-
key := universe.LeafKey{
1031+
key := universe.BaseLeafKey{
10321032
ScriptKey: &scriptKey,
10331033
}
10341034

tapdb/sqlutils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (d *DbHandler) AddUniProofLeaf(t *testing.T, testAsset *asset.Asset,
191191
// populate the universe root and universe leaves tables.
192192
uniId := universe.NewUniIDFromAsset(*testAsset)
193193

194-
leafKey := universe.LeafKey{
194+
leafKey := universe.BaseLeafKey{
195195
OutPoint: annotatedProof.AssetSnapshot.OutPoint,
196196
ScriptKey: &testAsset.ScriptKey,
197197
}

tapdb/universe.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ func universeUpsertProofLeaf(ctx context.Context, dbTx BaseUniverseStore,
619619
groupKeyBytes = schnorr.SerializePubKey(id.GroupKey)
620620
}
621621

622-
mintingPointBytes, err := encodeOutpoint(key.OutPoint)
622+
mintingPointBytes, err := encodeOutpoint(key.LeafOutPoint())
623623
if err != nil {
624624
return nil, err
625625
}
@@ -675,7 +675,8 @@ func universeUpsertProofLeaf(ctx context.Context, dbTx BaseUniverseStore,
675675
return nil, err
676676
}
677677

678-
scriptKeyBytes := schnorr.SerializePubKey(key.ScriptKey.PubKey)
678+
scriptKey := key.LeafScriptKey()
679+
scriptKeyBytes := schnorr.SerializePubKey(scriptKey.PubKey)
679680
err = dbTx.UpsertUniverseLeaf(ctx, UpsertUniverseLeaf{
680681
AssetGenesisID: assetGenID,
681682
ScriptKeyBytes: scriptKeyBytes,
@@ -846,13 +847,14 @@ func universeFetchProofLeaf(ctx context.Context,
846847
// Depending on the universeKey, we'll either be fetching the details of
847848
// a specific issuance, or each issuance for that minting outpoint.
848849
var targetScriptKey []byte
849-
if universeKey.ScriptKey != nil {
850+
scriptKey, hasScriptKey := universeKey.(universe.BaseLeafKey)
851+
if hasScriptKey && scriptKey.ScriptKey != nil {
850852
targetScriptKey = schnorr.SerializePubKey(
851-
universeKey.ScriptKey.PubKey,
853+
scriptKey.ScriptKey.PubKey,
852854
)
853855
}
854856

855-
mintingPointBytes, err := encodeOutpoint(universeKey.OutPoint)
857+
mintingPointBytes, err := encodeOutpoint(universeKey.LeafOutPoint())
856858
if err != nil {
857859
return nil, err
858860
}
@@ -902,11 +904,11 @@ func universeFetchProofLeaf(ctx context.Context,
902904

903905
// Next, we'll fetch the leaf node from the tree and also obtain
904906
// a merkle proof for the leaf alongside it.
905-
universeKey := universe.LeafKey{
906-
OutPoint: universeKey.OutPoint,
907+
leafKey := universe.BaseLeafKey{
908+
OutPoint: universeKey.LeafOutPoint(),
907909
ScriptKey: &scriptKey,
908910
}
909-
smtKey := universeKey.UniverseKey()
911+
smtKey := leafKey.UniverseKey()
910912
leafProof, err := universeTree.MerkleProof(ctx, smtKey)
911913
if err != nil {
912914
return err
@@ -1008,7 +1010,7 @@ func mintingKeys(ctx context.Context, dbTx BaseUniverseStore,
10081010
return err
10091011
}
10101012

1011-
leafKeys = append(leafKeys, universe.LeafKey{
1013+
leafKeys = append(leafKeys, universe.BaseLeafKey{
10121014
OutPoint: genPoint,
10131015
ScriptKey: &scriptKey,
10141016
})

tapdb/universe_federation.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,15 @@ func (u *UniverseFederationDB) UpsertFederationProofSyncLog(
319319

320320
// Encode the leaf key outpoint as bytes. We'll use this to look up the
321321
// leaf ID in the DB.
322-
leafKeyOutpointBytes, err := encodeOutpoint(leafKey.OutPoint)
322+
leafKeyOutpointBytes, err := encodeOutpoint(leafKey.LeafOutPoint())
323323
if err != nil {
324324
return 0, err
325325
}
326326

327327
// Encode the leaf script key pub key as bytes. We'll use this to look
328328
// up the leaf ID in the DB.
329329
scriptKeyPubKeyBytes := schnorr.SerializePubKey(
330-
leafKey.ScriptKey.PubKey,
330+
leafKey.LeafScriptKey().PubKey,
331331
)
332332

333333
var (
@@ -369,15 +369,15 @@ func (u *UniverseFederationDB) QueryFederationProofSyncLog(
369369

370370
// Encode the leaf key outpoint as bytes. We'll use this to look up the
371371
// leaf ID in the DB.
372-
leafKeyOutpointBytes, err := encodeOutpoint(leafKey.OutPoint)
372+
leafKeyOutpointBytes, err := encodeOutpoint(leafKey.LeafOutPoint())
373373
if err != nil {
374374
return nil, err
375375
}
376376

377377
// Encode the leaf script key pub key as bytes. We'll use this to look
378378
// up the leaf ID in the DB.
379379
scriptKeyPubKeyBytes := schnorr.SerializePubKey(
380-
leafKey.ScriptKey.PubKey,
380+
leafKey.LeafScriptKey().PubKey,
381381
)
382382

383383
var (
@@ -531,7 +531,7 @@ func fetchProofSyncLogEntry(ctx context.Context, entry ProofSyncLogEntry,
531531
return nil, err
532532
}
533533

534-
leafKey := universe.LeafKey{
534+
leafKey := universe.BaseLeafKey{
535535
OutPoint: outPoint,
536536
ScriptKey: &scriptKey,
537537
}

tapdb/universe_federation_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,10 @@ func assertProofSyncLogLeafKey(t *testing.T, actualLeafKey universe.LeafKey,
332332
// We can safely ignore the tweaked script key as it is the derivation
333333
// information for the script key. It is only ever known to the owner of
334334
// the asset and is never serialized in a proof
335-
actualLeafKey.ScriptKey.TweakedScriptKey = nil
335+
baseKey, ok := actualLeafKey.(universe.BaseLeafKey)
336+
require.True(t, ok)
337+
baseKey.ScriptKey.TweakedScriptKey = nil
338+
actualLeafKey = baseKey
336339
require.Equal(t, actualLeafKey, logLeafKey)
337340
}
338341

tapdb/universe_test.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func TestUniverseEmptyTree(t *testing.T) {
156156
}
157157

158158
func randLeafKey(t *testing.T) universe.LeafKey {
159-
return universe.LeafKey{
159+
return universe.BaseLeafKey{
160160
OutPoint: test.RandOp(t),
161161
ScriptKey: fn.Ptr(asset.NewScriptKey(test.RandPubKey(t))),
162162
}
@@ -649,12 +649,13 @@ func TestUniverseLeafQuery(t *testing.T) {
649649

650650
// We'll create three new leaves, all of them will share the exact same
651651
// minting outpoint, but will have distinct script keys.
652-
rootMintingPoint := randLeafKey(t).OutPoint
652+
rootMintingPoint := randLeafKey(t).LeafOutPoint()
653653

654654
leafToScriptKey := make(map[asset.SerializedKey]universe.Leaf)
655655
for i := 0; i < numLeafs; i++ {
656-
targetKey := randLeafKey(t)
657-
targetKey.OutPoint = rootMintingPoint
656+
baseKey := randLeafKey(t).(universe.BaseLeafKey)
657+
baseKey.OutPoint = rootMintingPoint
658+
targetKey := baseKey
658659

659660
leaf := randMintingLeaf(t, assetGen, id.GroupKey)
660661
if id.GroupKey != nil {
@@ -673,7 +674,9 @@ func TestUniverseLeafQuery(t *testing.T) {
673674
}
674675
}
675676

676-
scriptKey := asset.ToSerialized(targetKey.ScriptKey.PubKey)
677+
scriptKey := asset.ToSerialized(
678+
targetKey.LeafScriptKey().PubKey,
679+
)
677680

678681
leafToScriptKey[scriptKey] = leaf
679682

@@ -685,9 +688,11 @@ func TestUniverseLeafQuery(t *testing.T) {
685688

686689
// If we query for only the minting point, then all three leaves should
687690
// be returned.
688-
proofs, err := baseUniverse.FetchIssuanceProof(ctx, universe.LeafKey{
689-
OutPoint: rootMintingPoint,
690-
})
691+
proofs, err := baseUniverse.FetchIssuanceProof(
692+
ctx, universe.BaseLeafKey{
693+
OutPoint: rootMintingPoint,
694+
},
695+
)
691696
require.NoError(t, err)
692697
require.Len(t, proofs, numLeafs)
693698

@@ -698,12 +703,14 @@ func TestUniverseLeafQuery(t *testing.T) {
698703
scriptKey, err := btcec.ParsePubKey(scriptKeyBytes[:])
699704
require.NoError(t, err)
700705

701-
p, err := baseUniverse.FetchIssuanceProof(ctx, universe.LeafKey{
702-
OutPoint: rootMintingPoint,
703-
ScriptKey: &asset.ScriptKey{
704-
PubKey: scriptKey,
706+
p, err := baseUniverse.FetchIssuanceProof(
707+
ctx, universe.BaseLeafKey{
708+
OutPoint: rootMintingPoint,
709+
ScriptKey: &asset.ScriptKey{
710+
PubKey: scriptKey,
711+
},
705712
},
706-
})
713+
)
707714
require.NoError(t, err)
708715
require.Len(t, p, 1)
709716

tapgarden/caretaker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ func (b *BatchCaretaker) storeMintingProof(ctx context.Context,
12191219
// The base key is the set of bytes that keys into the universe, this'll
12201220
// be the outpoint where it was created at and the script key for that
12211221
// asset.
1222-
leafKey := universe.LeafKey{
1222+
leafKey := universe.BaseLeafKey{
12231223
OutPoint: wire.OutPoint{
12241224
Hash: mintTxHash,
12251225
Index: b.anchorOutputIndex,

0 commit comments

Comments
 (0)