Skip to content

Commit 7dcb1e1

Browse files
committed
rpcserver: return decimal display as option
When attempting to fetch the decimal display in a non strict way, we directly return fn.None() if the meta data isn't valid JSON.
1 parent 4a6dcf6 commit 7dcb1e1

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

itest/assertions.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ func AssetVersionCheck(version taprpc.AssetVersion) AssetCheck {
233233
// non-nil decimal display value.
234234
func AssetDecimalDisplayCheck(decDisplay uint32) AssetCheck {
235235
return func(a *taprpc.Asset) error {
236+
// If we didn't set a decimal display in the mint request, we
237+
// don't expect one to be set.
238+
if decDisplay == 0 &&
239+
(a.DecimalDisplay == nil ||
240+
a.DecimalDisplay.DecimalDisplay == 0) {
241+
242+
return nil
243+
}
244+
236245
if a.DecimalDisplay == nil {
237246
return fmt.Errorf("asset decimal display is nil")
238247
}

rpcserver.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ func (r *rpcServer) MarshalChainAsset(ctx context.Context, a *asset.ChainAsset,
10521052
keyRing taprpc.KeyLookup) (*taprpc.Asset, error) {
10531053

10541054
var (
1055-
decDisplay uint32
1055+
decDisplay fn.Option[uint32]
10561056
err error
10571057
)
10581058

@@ -1064,14 +1064,12 @@ func (r *rpcServer) MarshalChainAsset(ctx context.Context, a *asset.ChainAsset,
10641064
default:
10651065
decDisplay, err = r.DecDisplayForAssetID(ctx, a.ID())
10661066
}
1067-
10681067
if err != nil {
10691068
return nil, err
10701069
}
10711070

10721071
rpcAsset, err := taprpc.MarshalAsset(
1073-
ctx, a.Asset, a.IsSpent, withWitness, keyRing,
1074-
fn.Some(decDisplay),
1072+
ctx, a.Asset, a.IsSpent, withWitness, keyRing, decDisplay,
10751073
)
10761074
if err != nil {
10771075
return nil, err
@@ -5011,7 +5009,7 @@ func (r *rpcServer) AssetLeaves(ctx context.Context,
50115009
}
50125010

50135011
resp.Leaves[i], err = r.marshalAssetLeaf(
5014-
ctx, &assetLeaf, fn.Some(decDisplay),
5012+
ctx, &assetLeaf, decDisplay,
50155013
)
50165014
if err != nil {
50175015
return nil, err
@@ -5114,9 +5112,7 @@ func (r *rpcServer) marshalUniverseProofLeaf(ctx context.Context,
51145112
return nil, err
51155113
}
51165114

5117-
assetLeaf, err := r.marshalAssetLeaf(
5118-
ctx, proof.Leaf, fn.Some(decDisplay),
5119-
)
5115+
assetLeaf, err := r.marshalAssetLeaf(ctx, proof.Leaf, decDisplay)
51205116
if err != nil {
51215117
return nil, err
51225118
}
@@ -5533,7 +5529,7 @@ func (r *rpcServer) marshalUniverseDiff(ctx context.Context,
55335529
}
55345530

55355531
leaves[i], err = r.marshalAssetLeaf(
5536-
ctx, leaf, fn.Some(decDisplay),
5532+
ctx, leaf, decDisplay,
55375533
)
55385534
if err != nil {
55395535
return err
@@ -5617,8 +5613,8 @@ func marshalUniverseServer(
56175613
// of the local Universe server. These servers are used to push out new proofs,
56185614
// and also periodically call sync new proofs from the remote server.
56195615
func (r *rpcServer) ListFederationServers(ctx context.Context,
5620-
_ *unirpc.ListFederationServersRequest,
5621-
) (*unirpc.ListFederationServersResponse, error) {
5616+
_ *unirpc.ListFederationServersRequest) (
5617+
*unirpc.ListFederationServersResponse, error) {
56225618

56235619
uniServers, err := r.cfg.FederationDB.UniverseServers(ctx)
56245620
if err != nil {
@@ -6805,14 +6801,14 @@ func encodeVirtualPackets(packets []*tappsbt.VPacket) ([][]byte, error) {
68056801
// DecDisplayForAssetID attempts to fetch the meta reveal for a specific asset
68066802
// ID and extract the decimal display value from it.
68076803
func (r *rpcServer) DecDisplayForAssetID(ctx context.Context,
6808-
id asset.ID) (uint32, error) {
6804+
id asset.ID) (fn.Option[uint32], error) {
68096805

68106806
meta, err := r.cfg.AssetStore.FetchAssetMetaForAsset(
68116807
ctx, id,
68126808
)
68136809
if err != nil {
6814-
return 0, fmt.Errorf("unable to fetch asset meta "+
6815-
"for asset_id=%v :%v", id, err)
6810+
return fn.None[uint32](), fmt.Errorf("unable to fetch asset "+
6811+
"meta for asset_id=%v :%v", id, err)
68166812
}
68176813

68186814
return getDecimalDisplayNonStrict(meta)
@@ -6821,21 +6817,27 @@ func (r *rpcServer) DecDisplayForAssetID(ctx context.Context,
68216817
// getDecimalDisplayNonStrict attempts to decode a decimal display value from
68226818
// metadata. If no custom decimal display value is decoded, the default value of
68236819
// 0 is returned without error.
6824-
func getDecimalDisplayNonStrict(meta *proof.MetaReveal) (uint32, error) {
6820+
func getDecimalDisplayNonStrict(
6821+
meta *proof.MetaReveal) (fn.Option[uint32], error) {
6822+
68256823
_, decDisplay, err := meta.GetDecDisplay()
68266824
switch {
68276825
// If it isn't JSON, or doesn't have a dec display, we'll just return 0
68286826
// below.
68296827
case errors.Is(err, proof.ErrNotJSON):
68306828
fallthrough
6829+
case errors.Is(err, proof.ErrInvalidJSON):
6830+
fallthrough
68316831
case errors.Is(err, proof.ErrDecDisplayMissing):
68326832
fallthrough
68336833
case errors.Is(err, proof.ErrDecDisplayInvalidType):
6834-
break
6834+
// We can't determine if there is a decimal display value set.
6835+
return fn.None[uint32](), nil
6836+
68356837
case err != nil:
6836-
return 0, fmt.Errorf("unable to extract decimal display: %v",
6837-
err)
6838+
return fn.None[uint32](), fmt.Errorf("unable to extract "+
6839+
"decimal display: %v", err)
68386840
}
68396841

6840-
return decDisplay, nil
6842+
return fn.Some(decDisplay), nil
68416843
}

0 commit comments

Comments
 (0)