Skip to content

Commit

Permalink
change some naming, add validate basic tests, cmdline test
Browse files Browse the repository at this point in the history
  • Loading branch information
nullpointer0x00 committed Jan 22, 2024
1 parent 5c2c45c commit 42c157c
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 160 deletions.
2 changes: 1 addition & 1 deletion docs/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -8279,7 +8279,7 @@ MsgWriteScopeRequest is the request type for the Msg/WriteScope RPC method.
| `signers` | [string](#string) | repeated | signers is the list of address of those signing this request. |
| `scope_uuid` | [string](#string) | | scope_uuid is an optional uuid string, e.g. "91978ba2-5f35-459a-86a7-feca1b0512e0" If provided, it will be used to generate the MetadataAddress for the scope which will override the scope_id in the provided scope. If not provided (or it is an empty string), nothing special happens. If there is a value in scope.scope_id that is different from the one created from this uuid, an error is returned. |
| `spec_uuid` | [string](#string) | | spec_uuid is an optional scope specification uuid string, e.g. "dc83ea70-eacd-40fe-9adf-1cf6148bf8a2" If provided, it will be used to generate the MetadataAddress for the scope specification which will override the specification_id in the provided scope. If not provided (or it is an empty string), nothing special happens. If there is a value in scope.specification_id that is different from the one created from this uuid, an error is returned. |
| `usd_mils` | [uint64](#uint64) | | usd_mils value of scope in usd mils (1234 = $1.234) used for net asset value |
| `usd_mills` | [uint64](#uint64) | | usd_mills value of scope in usd mills (1234 = $1.234) used for net asset value |
| `volume` | [uint64](#uint64) | | volume value of scope used for net asset value |


Expand Down
4 changes: 2 additions & 2 deletions proto/provenance/metadata/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ message MsgWriteScopeRequest {
// returned.
string spec_uuid = 4 [(gogoproto.moretags) = "yaml:\"spec_uuid\""];

// usd_mils value of scope in usd mils (1234 = $1.234) used for net asset value
uint64 usd_mils = 12;
// usd_mills value of scope in usd mills (1234 = $1.234) used for net asset value
uint64 usd_mills = 12;
// volume value of scope used for net asset value
uint64 volume = 13;
}
Expand Down
57 changes: 57 additions & 0 deletions x/metadata/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3859,3 +3859,60 @@ func (s *IntegrationCLITestSuite) TestCountAuthorizationIntactTxCommands() {

runTxCmdTestCases(s, testCases)
}

func (s *IntegrationCLITestSuite) TestGetCmdAddNetAssetValues() {
scopeID := "scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel"
argsWStdFlags := func(args ...string) []string {
return append(args,
fmt.Sprintf("--%s=%s", flags.FlagFrom, s.testnet.Validators[0].Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
)
}

tests := []struct {
name string
args []string
expErr string
incLog bool // set to true to log the output regardless of failure
}{
{
name: "invalid net asset string",
args: argsWStdFlags(scopeID, "invalid"),
expErr: "invalid net asset value, expected coin,volume",
},
{
name: "address not meta address",
args: argsWStdFlags("notmetaaddress", "1usd,1"),
expErr: `invalid metadata address "notmetaaddress": decoding bech32 failed: invalid separator index -1`,
},
{
name: "address not a scope address",
args: argsWStdFlags("session1qxge0zaztu65tx5x5llv5xc9zts9sqlch3sxwn44j50jzgt8rshvqyfrjcr", "1usd,1"),
expErr: "metadata address is not scope address: session1qxge0zaztu65tx5x5llv5xc9zts9sqlch3sxwn44j50jzgt8rshvqyfrjcr",
},
{
name: "successful",
args: argsWStdFlags(scopeID, "1usd,1"),
},
}

for _, tc := range tests {
s.Run(tc.name, func() {
cmd := cli.GetCmdAddNetAssetValues()

clientCtx := s.testnet.Validators[0].ClientCtx
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
outBz := out.Bytes()
outStr := string(outBz)

if len(tc.expErr) > 0 {
s.Require().EqualError(err, tc.expErr, "GetCmdAddNetAssetValues error")
s.Require().Contains(outStr, tc.expErr, "GetCmdAddNetAssetValues output")
} else {
s.Require().NoError(err, "GetCmdAddNetAssetValues error")
}
})
}
}
2 changes: 2 additions & 0 deletions x/metadata/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func NewTxCmd() *cobra.Command {
RemoveRecordCmd(),

SetAccountDataCmd(),

GetCmdAddNetAssetValues(),
)

return txCmd
Expand Down
5 changes: 4 additions & 1 deletion x/metadata/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) {
if err != nil {
panic(err)
}
k.SetNetAssetValue(ctx, address, types.NewNetAssetValue(nav.Price, nav.Volume), types.ModuleName)
err = k.SetNetAssetValue(ctx, address, types.NewNetAssetValue(nav.Price, nav.Volume), types.ModuleName)
if err != nil {
panic(err)
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions x/metadata/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,14 @@ func (msg MsgAddNetAssetValuesRequest) ValidateBasic() error {
return fmt.Errorf("net asset value list cannot be empty")
}

scopeID, err := MetadataAddressFromBech32(msg.ScopeId)
if err != nil {
return fmt.Errorf("invalid metadata address %q: %w", msg.ScopeId, err)
}
if !scopeID.IsScopeAddress() {
return fmt.Errorf("metadata address is not scope address: %v", scopeID.String())
}

seen := make(map[string]bool)
for _, nav := range msg.NetAssetValues {
if err := nav.Validate(); err != nil {
Expand Down
69 changes: 68 additions & 1 deletion x/metadata/types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ volume: 0

bz, err = ModuleCdc.MarshalJSON(msg)
require.NoError(t, err, "ModuleCdc.MarshalJSON(msg)")
assert.Equal(t, "{\"scope\":{\"scope_id\":\"scope1qzxcpvj6czy5g354dews3nlruxjsahhnsp\",\"specification_id\":\"scopespec1qs30c9axgrw5669ft0kffe6h9gysfe58v3\",\"owners\":[{\"address\":\"data_owner\",\"role\":\"PARTY_TYPE_OWNER\",\"optional\":false}],\"data_access\":[\"data_accessor\"],\"value_owner_address\":\"value_owner\",\"require_party_rollup\":false},\"signers\":[],\"scope_uuid\":\"\",\"spec_uuid\":\"\",\"usd_mils\":\"0\",\"volume\":\"0\"}", string(bz))
assert.Equal(t, "{\"scope\":{\"scope_id\":\"scope1qzxcpvj6czy5g354dews3nlruxjsahhnsp\",\"specification_id\":\"scopespec1qs30c9axgrw5669ft0kffe6h9gysfe58v3\",\"owners\":[{\"address\":\"data_owner\",\"role\":\"PARTY_TYPE_OWNER\",\"optional\":false}],\"data_access\":[\"data_accessor\"],\"value_owner_address\":\"value_owner\",\"require_party_rollup\":false},\"signers\":[],\"scope_uuid\":\"\",\"spec_uuid\":\"\",\"usd_mills\":\"0\",\"volume\":\"0\"}", string(bz))
}

func TestWriteScopeValidation(t *testing.T) {
Expand Down Expand Up @@ -1058,3 +1058,70 @@ func TestRegisterInterfaces(t *testing.T) {
})
}
}

func TestMsgAddNetAssetValueValidateBasic(t *testing.T) {
addr := sdk.AccAddress("addr________________").String()
scopeID := "scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel"
sessionID := "session1qxge0zaztu65tx5x5llv5xc9zts9sqlch3sxwn44j50jzgt8rshvqyfrjcr"
netAssetValue1 := NetAssetValue{Price: sdk.NewInt64Coin("jackthecat", 100), Volume: uint64(100)}
netAssetValue2 := NetAssetValue{Price: sdk.NewInt64Coin("hotdog", 100), Volume: uint64(100)}
invalidNetAssetValue := NetAssetValue{Price: sdk.NewInt64Coin("hotdog", 100), Volume: uint64(0)}
invalidNetAssetValue2 := NetAssetValue{Price: sdk.NewInt64Coin("hotdog", 100), Volume: uint64(1), UpdatedBlockHeight: 1}

tests := []struct {
name string
msg MsgAddNetAssetValuesRequest
expErr string
}{
{
name: "should succeed",
msg: MsgAddNetAssetValuesRequest{ScopeId: scopeID, NetAssetValues: []NetAssetValue{netAssetValue1, netAssetValue2}, Signers: []string{addr}},
},
{
name: "block height is set",
msg: MsgAddNetAssetValuesRequest{ScopeId: scopeID, NetAssetValues: []NetAssetValue{invalidNetAssetValue2}, Signers: []string{addr}},
expErr: "marker net asset value must not have update height set",
},
{
name: "validation of net asset value failure",
msg: MsgAddNetAssetValuesRequest{ScopeId: scopeID, NetAssetValues: []NetAssetValue{invalidNetAssetValue}, Signers: []string{addr}},
expErr: "marker net asset value volume must be positive value",
},
{
name: "duplicate net asset values",
msg: MsgAddNetAssetValuesRequest{ScopeId: scopeID, NetAssetValues: []NetAssetValue{netAssetValue1, netAssetValue2, netAssetValue2}, Signers: []string{addr}},
expErr: "list of net asset values contains duplicates",
},
{
name: "incorrect meta address",
msg: MsgAddNetAssetValuesRequest{ScopeId: "", NetAssetValues: []NetAssetValue{netAssetValue1, netAssetValue2, netAssetValue2}, Signers: []string{addr}},
expErr: `invalid metadata address "": empty address string is not allowed`,
},
{
name: "not scope meta address",
msg: MsgAddNetAssetValuesRequest{ScopeId: sessionID, NetAssetValues: []NetAssetValue{netAssetValue1, netAssetValue2, netAssetValue2}, Signers: []string{addr}},
expErr: "metadata address is not scope address: session1qxge0zaztu65tx5x5llv5xc9zts9sqlch3sxwn44j50jzgt8rshvqyfrjcr",
},
{
name: "invalid administrator address",
msg: MsgAddNetAssetValuesRequest{ScopeId: scopeID, NetAssetValues: []NetAssetValue{netAssetValue1, netAssetValue2}, Signers: []string{"invalid"}},
expErr: "decoding bech32 failed: invalid bech32 string length 7",
},
{
name: "empty net asset list",
msg: MsgAddNetAssetValuesRequest{ScopeId: scopeID, NetAssetValues: []NetAssetValue{}, Signers: []string{addr}},
expErr: "net asset value list cannot be empty",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := tc.msg.ValidateBasic()
if len(tc.expErr) > 0 {
require.EqualErrorf(t, err, tc.expErr, "ValidateBasic error")
} else {
require.NoError(t, err, "ValidateBasic error")
}
})
}
}
Loading

0 comments on commit 42c157c

Please sign in to comment.