Skip to content

Commit

Permalink
Merge pull request #540 from lightninglabs/uni-namespace-proof-type
Browse files Browse the repository at this point in the history
Add proof type to universe identifer
  • Loading branch information
Roasbeef authored Oct 11, 2023
2 parents 9649775 + 911e2b1 commit 951c80e
Show file tree
Hide file tree
Showing 21 changed files with 1,286 additions and 572 deletions.
5 changes: 5 additions & 0 deletions asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,11 @@ func (a *Asset) HasGenesisWitnessForGroup() bool {
return *witness.PrevID == ZeroPrevID
}

// IsGenesisAsset returns true if an asset is a genesis asset.
func (a *Asset) IsGenesisAsset() bool {
return a.HasGenesisWitness() || a.HasGenesisWitnessForGroup()
}

// HasSplitCommitmentWitness returns true if an asset has a split commitment
// witness.
func (a *Asset) HasSplitCommitmentWitness() bool {
Expand Down
5 changes: 5 additions & 0 deletions itest/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,11 @@ func AssertUniverseRoot(t *testing.T, client unirpc.UniverseClient,
}

func AssertUniverseRootEqual(a, b *unirpc.UniverseRoot) bool {
// Basic RPC form sanity checks.
if (a.Id != nil && b.Id == nil) || (a.Id == nil && b.Id != nil) {
return false
}

// The ids should batch exactly.
if !reflect.DeepEqual(a.Id.Id, b.Id.Id) {
return false
Expand Down
43 changes: 32 additions & 11 deletions itest/universe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
tap "github.com/lightninglabs/taproot-assets"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/mssmt"
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
unirpc "github.com/lightninglabs/taproot-assets/taprpc/universerpc"
"github.com/lightninglabs/taproot-assets/universe"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
Expand Down Expand Up @@ -110,7 +112,12 @@ func testUniverseSync(t *harnessTest) {
}
}()

srcRoot, ok := universeRoots.UniverseRoots[uniKey]
// Construct universe namespace.
proofType, err := tap.UnmarshalUniProofType(newRoot.Id.ProofType)
require.NoError(t.t, err)
uniNamespace := fmt.Sprintf("%s-%s", proofType, uniKey)

srcRoot, ok := universeRoots.UniverseRoots[uniNamespace]
require.True(t.t, ok)
require.True(t.t, AssertUniverseRootEqual(srcRoot, newRoot))
}
Expand All @@ -130,8 +137,7 @@ func testUniverseSync(t *harnessTest) {
uniRoots := maps.Values(universeRoots.UniverseRoots)
uniIDs := fn.Map(uniRoots, func(root *unirpc.UniverseRoot) *unirpc.ID {
return root.Id
},
)
})
AssertUniverseKeysEqual(t.t, uniIDs, t.tapd, bob)
AssertUniverseLeavesEqual(t.t, uniIDs, t.tapd, bob)

Expand Down Expand Up @@ -211,6 +217,7 @@ func testUniverseSync(t *harnessTest) {
Id: &unirpc.ID_AssetId{
AssetId: firstAssetID,
},
ProofType: unirpc.ProofType_PROOF_TYPE_ISSUANCE,
},
})
require.NoError(t.t, err)
Expand Down Expand Up @@ -282,21 +289,29 @@ func testUniverseREST(t *harnessTest) {

// Simple assets are keyed by their asset ID.
for _, simpleAsset := range rpcSimpleAssets {
assetID := hex.EncodeToString(simpleAsset.AssetGenesis.AssetId)
require.Contains(t.t, roots.UniverseRoots, assetID)
// Ensure that the universe root set contains issuance roots for
// all of our assets.
var assetID asset.ID
copy(assetID[:], simpleAsset.AssetGenesis.AssetId)
uniID := universe.Identifier{
AssetID: assetID,
ProofType: universe.ProofTypeIssuance,
}
uniIDStr := uniID.String()
require.Contains(t.t, roots.UniverseRoots, uniIDStr)

require.Equal(
t.t, simpleAsset.AssetGenesis.Name,
roots.UniverseRoots[assetID].AssetName,
roots.UniverseRoots[uniIDStr].AssetName,
)

// Query the specific root to make sure we get the same result.
assetRoot, err := getJSON[*unirpc.QueryRootResponse](
assetRoots, err := getJSON[*unirpc.QueryRootResponse](
fmt.Sprintf("%s/roots/asset-id/%s", urlPrefix, assetID),
)
require.NoError(t.t, err)
require.True(t.t, AssertUniverseRootEqual(
roots.UniverseRoots[assetID], assetRoot.AssetRoot,
roots.UniverseRoots[uniIDStr], assetRoots.IssuanceRoot,
))
}

Expand All @@ -309,7 +324,12 @@ func testUniverseREST(t *harnessTest) {
groupKey := issuableAsset.AssetGroup.TweakedGroupKey
groupKeyHash := sha256.Sum256(groupKey[1:])
groupKeyID := hex.EncodeToString(groupKeyHash[:])
require.Contains(t.t, roots.UniverseRoots, groupKeyID)

// Construct universe namespace using the group key ID.
namespace := fmt.Sprintf(
"%s-%s", universe.ProofTypeIssuance, groupKeyID,
)
require.Contains(t.t, roots.UniverseRoots, namespace)

// Query the specific root to make sure we get the same result.
// Rather than use the hash above, the API exposes the
Expand All @@ -321,9 +341,10 @@ func testUniverseREST(t *harnessTest) {
assetRoot, err := getJSON[*unirpc.QueryRootResponse](queryURI)
require.NoError(t.t, err)

uniRoot, foundRoot := roots.UniverseRoots[namespace]
require.True(t.t, foundRoot)
require.True(t.t, AssertUniverseRootEqual(
roots.UniverseRoots[groupKeyID],
assetRoot.AssetRoot,
uniRoot, assetRoot.IssuanceRoot,
))
}
}
Expand Down
4 changes: 1 addition & 3 deletions proof/courier.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,9 +1072,7 @@ func (c *UniverseRpcCourier) ReceiveProof(ctx context.Context,
// Break if we've reached the genesis point (the asset is the
// genesis asset).
asset := transitionProof.Asset
if asset.HasGenesisWitness() ||
asset.HasGenesisWitnessForGroup() {

if asset.IsGenesisAsset() {
break
}

Expand Down
3 changes: 1 addition & 2 deletions proof/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,7 @@ func (p *Proof) Verify(ctx context.Context, prev *AssetSnapshot,
// 5. If this is a genesis asset, start by verifying the
// genesis reveal, which should be present for genesis assets.
// Non-genesis assets must not have a genesis or meta reveal.
isGenesisAsset := p.Asset.HasGenesisWitness() ||
p.Asset.HasGenesisWitnessForGroup()
isGenesisAsset := p.Asset.IsGenesisAsset()
hasGenesisReveal := p.GenesisReveal != nil
hasMetaReveal := p.MetaReveal != nil

Expand Down
Loading

0 comments on commit 951c80e

Please sign in to comment.