Skip to content

Commit

Permalink
add a constant ByteLengthForUint64 and fix the issue identified by co…
Browse files Browse the repository at this point in the history
…de comment
  • Loading branch information
TimmyExogenous committed Sep 18, 2024
1 parent d1497d8 commit c47545d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
5 changes: 3 additions & 2 deletions x/assets/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,16 @@ func (gs GenesisState) ValidateOperatorAssets(tokensTotalStaking map[string]math
// check that the asset is registered
// no need to check for the validity of the assetID, since
// an invalid assetID cannot be in the tokens map.
if _, ok := tokensTotalStaking[asset.AssetID]; !ok {
totalStaking, ok := tokensTotalStaking[asset.AssetID]
if !ok {
return errorsmod.Wrapf(
ErrInvalidGenesisData,
"unknown assetID for operator assets %s: %s",
assets.Operator, asset.AssetID,
)
}
// the sum amount of operators shouldn't be greater than the total staking amount of this asset
if asset.Info.TotalAmount.Add(asset.Info.PendingUndelegationAmount).GT(tokensTotalStaking[asset.AssetID]) {
if asset.Info.TotalAmount.Add(asset.Info.PendingUndelegationAmount).GT(totalStaking) {
return errorsmod.Wrapf(
ErrInvalidGenesisData,
"operator's sum amount exceeds the total staking amount for %s: %+v",
Expand Down
33 changes: 18 additions & 15 deletions x/operator/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const (
// AccAddressLength is used to parse the key, because the length isn't padded in the key
// This might be removed if the address length is padded in the key
AccAddressLength = 20

// ByteLengthForUint64 the type of chainID length is uint64, uint64 has 8 bytes.
ByteLengthForUint64 = 8
)

const (
Expand Down Expand Up @@ -133,7 +136,7 @@ func KeyForOperatorAndChainIDToConsKey(addr sdk.AccAddress, chainID string) []by
}

func ParseKeyForOperatorAndChainIDToConsKey(key []byte) (addr sdk.AccAddress, chainID string, err error) {
if len(key) < AccAddressLength+8 {
if len(key) < AccAddressLength+ByteLengthForUint64 {
return nil, "", xerrors.New("key length is too short to contain address and chainID length")
}
// Extract the address
Expand All @@ -143,13 +146,13 @@ func ParseKeyForOperatorAndChainIDToConsKey(key []byte) (addr sdk.AccAddress, ch
}

// Extract the chainID length
chainIDLen := sdk.BigEndianToUint64(key[AccAddressLength : AccAddressLength+8])
if len(key) != int(AccAddressLength+8+chainIDLen) {
return nil, "", xerrors.Errorf("invalid key length,expected:%d,got:%d", AccAddressLength+8+chainIDLen, len(key))
chainIDLen := sdk.BigEndianToUint64(key[AccAddressLength : AccAddressLength+ByteLengthForUint64])
if len(key) != int(AccAddressLength+ByteLengthForUint64+chainIDLen) {
return nil, "", xerrors.Errorf("invalid key length,expected:%d,got:%d", AccAddressLength+ByteLengthForUint64+chainIDLen, len(key))
}

// Extract the chainID
chainIDBytes := key[AccAddressLength+8:]
chainIDBytes := key[AccAddressLength+ByteLengthForUint64:]
chainID = string(chainIDBytes)

return addr, chainID, nil
Expand All @@ -164,22 +167,22 @@ func KeyForChainIDAndOperatorToPrevConsKey(chainID string, addr sdk.AccAddress)

func ParsePrevConsKey(key []byte) (chainID string, addr sdk.AccAddress, err error) {
// Check if the key has at least eight byte for the chainID length
if len(key) < 8 {
if len(key) < ByteLengthForUint64 {
return "", nil, xerrors.New("key length is too short to contain chainID length")
}

// Extract the chainID length
chainIDLen := sdk.BigEndianToUint64(key[0:8])
if len(key) < int(8+chainIDLen) {
chainIDLen := sdk.BigEndianToUint64(key[0:ByteLengthForUint64])
if len(key) < int(ByteLengthForUint64+chainIDLen) {
return "", nil, xerrors.New("key too short for chainID length")
}

// Extract the chainID
chainIDBytes := key[8 : 8+chainIDLen]
chainIDBytes := key[ByteLengthForUint64 : ByteLengthForUint64+chainIDLen]
chainID = string(chainIDBytes)

// Extract the address
addr = key[8+chainIDLen:]
addr = key[ByteLengthForUint64+chainIDLen:]
if len(addr) == 0 {
return "", nil, xerrors.New("missing address")
}
Expand Down Expand Up @@ -211,7 +214,7 @@ func KeyForOperatorKeyRemovalForChainID(addr sdk.AccAddress, chainID string) []b

func ParseKeyForOperatorKeyRemoval(key []byte) (addr sdk.AccAddress, chainID string, err error) {
// Check if the key has at least 20 byte for the operator and eight byte for the chainID length
if len(key) < AccAddressLength+8 {
if len(key) < AccAddressLength+ByteLengthForUint64 {
return nil, "", xerrors.New("key length is too short to contain operator address and chainID length")
}

Expand All @@ -222,13 +225,13 @@ func ParseKeyForOperatorKeyRemoval(key []byte) (addr sdk.AccAddress, chainID str
}

// Extract the chainID length
chainIDLen := sdk.BigEndianToUint64(key[AccAddressLength : AccAddressLength+8])
if len(key) != int(AccAddressLength+8+chainIDLen) {
return nil, "", xerrors.Errorf("invalid key length,expected:%d,got:%d", AccAddressLength+8+chainIDLen, len(key))
chainIDLen := sdk.BigEndianToUint64(key[AccAddressLength : AccAddressLength+ByteLengthForUint64])
if len(key) != int(AccAddressLength+ByteLengthForUint64+chainIDLen) {
return nil, "", xerrors.Errorf("invalid key length,expected:%d,got:%d", AccAddressLength+ByteLengthForUint64+chainIDLen, len(key))
}

// Extract the chainID
chainIDBytes := key[AccAddressLength+8:]
chainIDBytes := key[AccAddressLength+ByteLengthForUint64:]
chainID = string(chainIDBytes)

return addr, chainID, nil
Expand Down

0 comments on commit c47545d

Please sign in to comment.