Skip to content

Commit

Permalink
fix(assets): check deposit and asset chain ids
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxMustermann2 committed Apr 3, 2024
1 parent f44e12e commit df2a01e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
20 changes: 16 additions & 4 deletions x/assets/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,21 @@ func (gs GenesisState) Validate() error {
for _, depositByStaker := range gs.Deposits {
stakerID := depositByStaker.StakerID
// validate the stakerID
var id uint64
var stakerClientChainID uint64
var err error
if _, id, err = ValidateID(stakerID, true); err != nil {
if _, stakerClientChainID, err = ValidateID(stakerID, true); err != nil {
return errorsmod.Wrapf(
ErrInvalidGenesisData,
"invalid stakerID: %s",
stakerID,
)
}
// check that the chain is registered
if _, ok := lzIDs[id]; !ok {
if _, ok := lzIDs[stakerClientChainID]; !ok {
return errorsmod.Wrapf(
ErrInvalidGenesisData,
"unknown LayerZeroChainID for staker %s: %d",
stakerID, id,
stakerID, stakerClientChainID,
)
}
// check that it is not a duplicate
Expand All @@ -153,6 +153,18 @@ func (gs GenesisState) Validate() error {
stakerID, assetID,
)
}
// #nosec G703 // if it's invalid, we will not reach here.
_, assetClientChainID, _ := ParseID(assetID)
if assetClientChainID != stakerClientChainID {
// we can reach here if there are multiple chains
// and it tries to deposit assets from one chain
// under a staker from another chain.
return errorsmod.Wrapf(
ErrInvalidGenesisData,
"mismatched client chain IDs for staker %s and asset %s",
stakerID, assetID,
)
}
// check that it is not a duplicate
if _, ok := tokensForStaker[assetID]; ok {
return errorsmod.Wrapf(
Expand Down
49 changes: 47 additions & 2 deletions x/assets/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types_test

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -308,6 +307,52 @@ func (suite *GenesisTestSuite) TestValidateGenesis() {
gs.Deposits[0].Deposits[0].AssetID = assetID
},
},
{
name: "invalid genesis due to different chain ids for asset and staker",
genState: &types.GenesisState{
Params: types.DefaultParams(),
ClientChains: []types.ClientChainInfo{
ethClientChain, ethClientChain,
},
Tokens: []types.StakingAssetInfo{
stakingInfo, stakingInfo,
},
Deposits: []types.DepositsByStaker{genesisDeposit, genesisDeposit},
},
expPass: false,
malleate: func(gs *types.GenesisState) {
// new chain with different layer zero chain id
gs.ClientChains[1].LayerZeroChainID += 1
// new asset (old asset is a pointer so can't alter that)
tokenAddress := utiltx.GenerateAddress().String()
usdcClientChainAsset := types.AssetInfo{
Name: "Circle USD",
Symbol: "USDC",
Address: tokenAddress,
Decimals: 18,
LayerZeroChainID: ethClientChain.LayerZeroChainID + 1,
MetaInfo: "Circle USD token",
TotalSupply: math.NewInt(500000000),
}
stakingInfo := types.StakingAssetInfo{
AssetBasicInfo: &usdcClientChainAsset,
StakingTotalAmount: math.NewInt(0),
}
gs.Tokens[1] = stakingInfo
stakerID, _ := types.GetStakeIDAndAssetIDFromStr(
usdtClientChainAsset.LayerZeroChainID+1,
ethAddress.String(), usdtClientChainAsset.Address,
)
// change stakerID to be that of the second chain
gs.Deposits[1].StakerID = stakerID
// but keep the assetID the same
},
unmalleate: func(gs *types.GenesisState) {
gs.ClientChains[1].LayerZeroChainID -= 1
gs.Tokens[1].AssetBasicInfo.LayerZeroChainID -= 1
gs.Deposits[1].StakerID = stakerID
},
},
{
name: "invalid genesis due to duplicate asset id for staker",
genState: &types.GenesisState{
Expand Down Expand Up @@ -485,6 +530,6 @@ func (suite *GenesisTestSuite) TestValidateGenesis() {
if tc.unmalleate != nil {
tc.unmalleate(tc.genState)
}
fmt.Println(tc.name, ",", err)
// fmt.Println(tc.name, ",", err)
}
}

0 comments on commit df2a01e

Please sign in to comment.