Skip to content

dlog v1 finalization: unit-tests coverage above 80% - token #1035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions token/core/fabtoken/v1/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type TokensService struct {
}

func NewTokensService(pp *setup.PublicParams, identityDeserializer driver.Deserializer) (*TokensService, error) {
supportedTokens, err := SupportedTokenFormat(pp.QuantityPrecision)
supportedTokens, err := ComputeTokenFormat(pp.QuantityPrecision)
if err != nil {
return nil, errors.WithMessagef(err, "failed getting supported token types")
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (s *TokensUpgradeService) CheckUpgradeProof(ch driver.TokensUpgradeChalleng
return false, errors.New("not supported")
}

func SupportedTokenFormat(precision uint64) (token2.Format, error) {
func ComputeTokenFormat(precision uint64) (token2.Format, error) {
hasher := utils.NewSHA256Hasher()
if err := errors2.Join(
hasher.AddInt32(fabtoken.Type),
Expand Down
2 changes: 1 addition & 1 deletion token/core/zkatdlog/nogh/v1/crypto/upgrade/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewService(
// compute supported tokens
var upgradeSupportedTokenFormatList []token.Format
for _, precision := range []uint64{16, 32, 64} {
format, err := v1.SupportedTokenFormat(precision)
format, err := v1.ComputeTokenFormat(precision)
if err != nil {
return nil, errors.Wrapf(err, "failed computing fabtoken token format with precision [%d]", precision)
}
Expand Down
4 changes: 2 additions & 2 deletions token/core/zkatdlog/nogh/v1/crypto/upgrade/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestTokensService_GenUpgradeProof(t *testing.T) {
}
fabtokenOutputRaw, err := fabtokenOutput.Serialize()
assert.NoError(t, err)
formatFabtoken16, err := v1.SupportedTokenFormat(16)
formatFabtoken16, err := v1.ComputeTokenFormat(16)
assert.NoError(t, err)
validTokens := []token.LedgerToken{{
ID: token.ID{TxId: "tx1", Index: 1},
Expand Down Expand Up @@ -178,7 +178,7 @@ func TestTokensService_CheckUpgradeProof(t *testing.T) {
}
fabtokenOutputRaw, err := fabtokenOutput.Serialize()
assert.NoError(t, err)
formatFabtoken16, err := v1.SupportedTokenFormat(16)
formatFabtoken16, err := v1.ComputeTokenFormat(16)
assert.NoError(t, err)
validTokens := []token.LedgerToken{{
ID: token.ID{TxId: "tx1", Index: 1},
Expand Down
2 changes: 1 addition & 1 deletion token/core/zkatdlog/nogh/v1/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (d *Driver) NewTokenService(tmsID driver.TMSID, publicParams []byte) (drive

metricsProvider := metrics.NewTMSProvider(tmsConfig.ID(), d.metricsProvider)
driverMetrics := v1.NewMetrics(metricsProvider)
tokensService, err := token3.NewTokensService(logger, ppm, deserializer)
tokensService, err := token3.NewTokensService(logger, ppm.PublicParams(), deserializer)
if err != nil {
return nil, errors.Wrapf(err, "failed to initiliaze token service for [%s:%s]", tmsID.Network, tmsID.Namespace)
}
Expand Down
10 changes: 6 additions & 4 deletions token/core/zkatdlog/nogh/v1/token/fabtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ package token

import (
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/actions"
fabtoken "github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/actions"
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
)

func ParseFabtokenToken(tok []byte, precision uint64, maxPrecision uint64) (*actions.Output, uint64, error) {
if precision < maxPrecision {
// ParseFabtokenToken unmarshals tok as a fabtoken.Output using precision to parse the quantity.
// If precision is larger than maxPrecision, it returns an error
func ParseFabtokenToken(tok []byte, precision uint64, maxPrecision uint64) (*fabtoken.Output, uint64, error) {
if precision > maxPrecision {
return nil, 0, errors.Errorf("unsupported precision [%d], max [%d]", precision, maxPrecision)
}

output := &actions.Output{}
output := &fabtoken.Output{}
err := output.Deserialize(tok)
if err != nil {
return nil, 0, errors.Wrap(err, "failed to unmarshal fabtoken")
Expand Down
113 changes: 113 additions & 0 deletions token/core/zkatdlog/nogh/v1/token/fabtoken_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package token

import (
"testing"

"github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/actions"
"github.com/stretchr/testify/assert"
)

func TestParseFabtokenToken(t *testing.T) {
nilGetTokFunc := func() (*actions.Output, []byte, error) {
return nil, nil, nil
}
tests := []struct {
name string
tok func() (*actions.Output, []byte, error)
precision uint64
maxPrecision uint64
wantErr bool
expectedError string
expectedQuantity uint64
}{
{
name: "precision is langer than maxPrecision",
tok: nilGetTokFunc,
precision: 10,
maxPrecision: 5,
wantErr: true,
expectedError: "unsupported precision [10], max [5]",
},
{
name: "invalid tok",
tok: nilGetTokFunc,
precision: 5,
maxPrecision: 10,
wantErr: true,
expectedError: "failed to unmarshal fabtoken: failed deserializing token: failed unmarshalling token: failed to unmarshal to TypedToken: asn1: syntax error: sequence truncated",
},
{
name: "invalid tok 2",
tok: func() (*actions.Output, []byte, error) {
return nil, []byte{}, nil
},
precision: 5,
maxPrecision: 10,
wantErr: true,
expectedError: "failed to unmarshal fabtoken: failed deserializing token: failed unmarshalling token: failed to unmarshal to TypedToken: asn1: syntax error: sequence truncated",
},
{
name: "invalid tok 3",
tok: func() (*actions.Output, []byte, error) {
return nil, []byte{0, 1, 2}, nil
},
precision: 5,
maxPrecision: 10,
wantErr: true,
expectedError: "failed to unmarshal fabtoken: failed deserializing token: failed unmarshalling token: failed to unmarshal to TypedToken: asn1: structure error: tags don't match (16 vs {class:0 tag:0 length:1 isCompound:false}) {optional:false explicit:false application:false private:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} TypedToken @2",
},
{
name: "invalid quantity",
tok: func() (*actions.Output, []byte, error) {
output := &actions.Output{
Owner: nil,
Type: "",
Quantity: "",
}
raw, err := output.Serialize()
return output, raw, err
},
precision: 5,
maxPrecision: 10,
wantErr: true,
expectedError: "failed to create quantity: invalid input [,5]",
},
{
name: "success",
tok: func() (*actions.Output, []byte, error) {
output := &actions.Output{
Owner: nil,
Type: "",
Quantity: "10",
}
raw, err := output.Serialize()
return output, raw, err
},
precision: 5,
maxPrecision: 10,
wantErr: false,
expectedQuantity: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tok, tokBytes, err := tt.tok()
assert.NoError(t, err)
output, quantity, err := ParseFabtokenToken(tokBytes, tt.precision, tt.maxPrecision)
if tt.wantErr {
assert.Error(t, err)
assert.EqualError(t, err, tt.expectedError)
} else {
assert.NoError(t, err)
assert.Equal(t, tok, output)
assert.Equal(t, tt.expectedQuantity, quantity)
}
})
}
}
117 changes: 117 additions & 0 deletions token/core/zkatdlog/nogh/v1/token/mock/id.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading