Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,14 @@ func (r *RPCServer) MintAsset(ctx context.Context,
"and group internal key descriptor")
}

assetType, err := rpcutils.UnmarshalAssetType(req.Asset.AssetType)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exercise the caller paths too. Assert that MintAsset rejects an unknown type before queueing and that group key request decoding rejects an unknown anchor asset type.

if err != nil {
return nil, err
}

seedling := &tapgarden.Seedling{
AssetVersion: assetVersion,
AssetType: asset.Type(req.Asset.AssetType),
AssetType: assetType,
AssetName: req.Asset.Name,
Amount: req.Asset.Amount,
EnableEmission: req.Asset.NewGroupedAsset,
Expand Down Expand Up @@ -6888,6 +6893,41 @@ func unmarshalUniSortDirection(
}
}

// unmarshalUniSyncStatsSort parses the RPC asset query sort field into
// the native sync stats sort counterpart.
func unmarshalUniSyncStatsSort(
rpcSort unirpc.AssetQuerySort) (universe.SyncStatsSort, error) {

switch rpcSort {
case unirpc.AssetQuerySort_SORT_BY_NONE:
return universe.SortByNone, nil

case unirpc.AssetQuerySort_SORT_BY_ASSET_NAME:
return universe.SortByAssetName, nil

case unirpc.AssetQuerySort_SORT_BY_ASSET_ID:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use rows whose ID and type orders differ to test both sort modes through QueryAssetStats. Also assert that an unknown sort fails before querying the store.

return universe.SortByAssetID, nil

case unirpc.AssetQuerySort_SORT_BY_ASSET_TYPE:
return universe.SortByAssetType, nil

case unirpc.AssetQuerySort_SORT_BY_TOTAL_SYNCS:
return universe.SortByTotalSyncs, nil

case unirpc.AssetQuerySort_SORT_BY_TOTAL_PROOFS:
return universe.SortByTotalProofs, nil

case unirpc.AssetQuerySort_SORT_BY_GENESIS_HEIGHT:
return universe.SortByGenesisHeight, nil

case unirpc.AssetQuerySort_SORT_BY_TOTAL_SUPPLY:
return universe.SortByTotalSupply, nil

default:
return 0, fmt.Errorf("unknown asset query sort: %v", rpcSort)
}
}

// validatePage validates the pagination parameters for universe
// queries.
func validatePage(offset, limit int32) error {
Expand Down Expand Up @@ -9016,6 +9056,11 @@ func (r *RPCServer) QueryAssetStats(ctx context.Context,
limit = universe.RequestPageSize
}

sortBy, err := unmarshalUniSyncStatsSort(req.SortBy)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unknown AssetTypeFilter silently turns this into an unfiltered query. Handle FILTER_ASSET_NONE explicitly and reject unknown values.

if err != nil {
return nil, err
}

assetStats, err := r.cfg.UniverseStats.QuerySyncStats(
ctx, universe.SyncStatsQuery{
AssetNameFilter: req.AssetNameFilter,
Expand All @@ -9034,7 +9079,7 @@ func (r *RPCServer) QueryAssetStats(ctx context.Context,
AssetIDFilter: fn.ToArray[asset.ID](
req.AssetIdFilter,
),
SortBy: universe.SyncStatsSort(req.SortBy),
SortBy: sortBy,
SortDirection: unmarshalUniSortDirection(req.Direction),
Offset: int(req.Offset),
Limit: int(limit + 1),
Expand Down
21 changes: 20 additions & 1 deletion rpcutils/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ func MarshalAssetVersion(version asset.Version) (taprpc.AssetVersion, error) {
}
}

// UnmarshalAssetType parses an asset type from the RPC variant.
func UnmarshalAssetType(rpcType taprpc.AssetType) (asset.Type, error) {
switch rpcType {
case taprpc.AssetType_NORMAL:
return asset.Normal, nil

case taprpc.AssetType_COLLECTIBLE:
return asset.Collectible, nil

default:
return 0, fmt.Errorf("unknown asset type: %v", rpcType)
}
}

// MarshalGenesisInfo marshals the native asset genesis into the RPC
// counterpart.
func MarshalGenesisInfo(gen *asset.Genesis,
Expand Down Expand Up @@ -256,12 +270,17 @@ func UnmarshalGenesisInfo(rpcGen *taprpc.GenesisInfo) (*asset.Genesis, error) {
sha256.Size)
}

assetType, err := UnmarshalAssetType(rpcGen.AssetType)
if err != nil {
return nil, err
}

return &asset.Genesis{
FirstPrevOut: *firstPrevOut,
Tag: rpcGen.Name,
MetaHash: fn.ToArray[[32]byte](rpcGen.MetaHash),
OutputIndex: rpcGen.OutputIndex,
Type: asset.Type(rpcGen.AssetType),
Type: assetType,
}, nil
}

Expand Down
48 changes: 48 additions & 0 deletions rpcutils/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package rpcutils
import (
"testing"

"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -83,3 +85,49 @@ func TestParseDerivationPath(t *testing.T) {
})
}
}

func TestUnmarshalAssetType(t *testing.T) {
testCases := []struct {
name string
rpcType taprpc.AssetType
expectedErr string
expectedResult asset.Type
}{{
name: "normal",
rpcType: taprpc.AssetType_NORMAL,
expectedResult: asset.Normal,
}, {
name: "collectible",
rpcType: taprpc.AssetType_COLLECTIBLE,
expectedResult: asset.Collectible,
}, {
name: "unknown positive value",
rpcType: taprpc.AssetType(2),
expectedErr: "unknown asset type",
}, {
name: "value 256",
rpcType: taprpc.AssetType(256),
expectedErr: "unknown asset type",
}, {
name: "negative value",
rpcType: taprpc.AssetType(-1),
expectedErr: "unknown asset type",
}}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
result, err := UnmarshalAssetType(tc.rpcType)

if tc.expectedErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedErr)
return
}

require.NoError(t, err)
require.Equal(t, tc.expectedResult, result)
})
}
}
Loading