Skip to content

Commit

Permalink
[TRA-98] Update upper limit of subaccount number constraint (#1164)
Browse files Browse the repository at this point in the history
Signed-off-by: Shrenuj Bansal <[email protected]>
  • Loading branch information
shrenujb authored Mar 11, 2024
1 parent 340801a commit 525bb6f
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export interface SubaccountId {
/** The address of the wallet that owns this subaccount. */
owner: string;
/**
* < 128 Since 128 should be enough to start and it fits within
* 1 Byte (1 Bit needed to indicate that the first byte is the last).
* The unique number of this subaccount for the owner.
* Currently limited to 128*1000 subaccounts per owner.
*/

number: number;
Expand All @@ -20,8 +20,8 @@ export interface SubaccountIdSDKType {
/** The address of the wallet that owns this subaccount. */
owner: string;
/**
* < 128 Since 128 should be enough to start and it fits within
* 1 Byte (1 Bit needed to indicate that the first byte is the last).
* The unique number of this subaccount for the owner.
* Currently limited to 128*1000 subaccounts per owner.
*/

number: number;
Expand Down
4 changes: 2 additions & 2 deletions proto/dydxprotocol/subaccounts/subaccount.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ option go_package = "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/typ
message SubaccountId {
// The address of the wallet that owns this subaccount.
string owner = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
// < 128 Since 128 should be enough to start and it fits within
// 1 Byte (1 Bit needed to indicate that the first byte is the last).
// The unique number of this subaccount for the owner.
// Currently limited to 128*1000 subaccounts per owner.
uint32 number = 2;
}

Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/types/message_cancel_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMsgCancelOrder_ValidateBasic(t *testing.T) {
OrderId: OrderId{
SubaccountId: satypes.SubaccountId{
Owner: sample.AccAddress(),
Number: uint32(9999),
Number: uint32(999_999),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/types/message_place_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestMsgPlaceOrder_ValidateBasic(t *testing.T) {
OrderId: OrderId{
SubaccountId: satypes.SubaccountId{
Owner: sample.AccAddress(),
Number: uint32(9999),
Number: uint32(999_999),
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/sending/types/message_create_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestMsgCreateTransfer_ValidateBasic(t *testing.T) {
Transfer: &types.Transfer{
Sender: satypes.SubaccountId{
Owner: sample.AccAddress(),
Number: uint32(9999),
Number: uint32(999_999),
},
Recipient: constants.Carl_Num0,
},
Expand All @@ -63,7 +63,7 @@ func TestMsgCreateTransfer_ValidateBasic(t *testing.T) {
Sender: constants.Carl_Num0,
Recipient: satypes.SubaccountId{
Owner: sample.AccAddress(),
Number: uint32(9999),
Number: uint32(999_999),
},
},
},
Expand Down
7 changes: 5 additions & 2 deletions protocol/x/subaccounts/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"reflect"
"testing"

"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"

"github.com/dydxprotocol/v4-chain/protocol/app/module"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -132,11 +135,11 @@ func TestAppModuleBasic_ValidateGenesisErrBadState_Number(t *testing.T) {

cdc := codec.NewProtoCodec(module.InterfaceRegistry)

msg := fmt.Sprintf(`{"subaccounts": [{ "id": {"owner": "%s", "number": 128 } }]}`, sample.AccAddress())
msg := fmt.Sprintf(`{"subaccounts": [{ "id": {"owner": "%s", "number": 128001 } }]}`, sample.AccAddress())
h := json.RawMessage(msg)

err := am.ValidateGenesis(cdc, nil, h)
require.EqualError(t, err, "subaccount id number cannot exceed 127")
require.EqualError(t, err, "subaccount id number cannot exceed "+lib.IntToString(types.MaxSubaccountIdNumber))
}

func TestAppModuleBasic_ValidateGenesis(t *testing.T) {
Expand Down
15 changes: 11 additions & 4 deletions protocol/x/subaccounts/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package types

// DONTCOVER

import errorsmod "cosmossdk.io/errors"
import (
errorsmod "cosmossdk.io/errors"
"github.com/dydxprotocol/v4-chain/protocol/lib"
)

// x/subaccounts module sentinel errors
var (
Expand All @@ -18,9 +21,13 @@ var (
ErrProductPositionNotUpdatable = errorsmod.Register(ModuleName, 103, "product position is not updatable")

// 200 - 299: subaccount id related.
ErrInvalidSubaccountIdNumber = errorsmod.Register(ModuleName, 200, "subaccount id number cannot exceed 127")
ErrInvalidSubaccountIdOwner = errorsmod.Register(ModuleName, 201, "subaccount id owner is an invalid address")
ErrDuplicateSubaccountIds = errorsmod.Register(ModuleName, 202, "duplicate subaccount id found in genesis")
ErrInvalidSubaccountIdNumber = errorsmod.Register(
ModuleName,
200,
"subaccount id number cannot exceed "+lib.IntToString(MaxSubaccountIdNumber),
)
ErrInvalidSubaccountIdOwner = errorsmod.Register(ModuleName, 201, "subaccount id owner is an invalid address")
ErrDuplicateSubaccountIds = errorsmod.Register(ModuleName, 202, "duplicate subaccount id found in genesis")

// 300 - 399: asset position related.
ErrAssetPositionsOutOfOrder = errorsmod.Register(ModuleName, 300, "asset positions are out of order")
Expand Down
7 changes: 4 additions & 3 deletions protocol/x/subaccounts/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package types_test

import (
errorsmod "cosmossdk.io/errors"
"testing"

errorsmod "cosmossdk.io/errors"

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
"github.com/dydxprotocol/v4-chain/protocol/testutil/sample"
"github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
Expand Down Expand Up @@ -85,13 +86,13 @@ func TestGenesisState_Validate(t *testing.T) {
},
expectedError: types.ErrInvalidSubaccountIdOwner,
},
"invalid: id number is greater than 127": {
"invalid: id number is greater than 128_000": {
genState: &types.GenesisState{
Subaccounts: []types.Subaccount{
{
Id: &types.SubaccountId{
Owner: sample.AccAddress(),
Number: uint32(128),
Number: uint32(128_001),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/subaccounts/types/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
MaxSubaccountIdNumber = 127 // 0 ... 127 are valid numbers.
MaxSubaccountIdNumber = 128_000 // 0 ... 128,000 are valid numbers.
)

// BaseQuantums is used to represent an amount in base quantums.
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/subaccounts/types/subaccount.pb.go

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

2 changes: 1 addition & 1 deletion protocol/x/subaccounts/types/subaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestSubaccountIdValidate(t *testing.T) {
},
"invalid number": {
owner: sample.AccAddress(),
number: 128,
number: 128_001,
expectedError: types.ErrInvalidSubaccountIdNumber,
},
}
Expand Down

0 comments on commit 525bb6f

Please sign in to comment.