diff --git a/app/apptesting/test_suite.go b/app/apptesting/test_suite.go index 1fc34da9b..1c7b4f569 100644 --- a/app/apptesting/test_suite.go +++ b/app/apptesting/test_suite.go @@ -4,12 +4,12 @@ import ( "strings" "github.com/cometbft/cometbft/libs/rand" - "github.com/cosmos/cosmos-sdk/crypto/types" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" bankutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" + "github.com/dymensionxyz/sdk-utils/utils/urand" "github.com/stretchr/testify/suite" "github.com/dymensionxyz/dymension/v3/app" @@ -31,7 +31,7 @@ type KeeperTestHelper struct { } func (s *KeeperTestHelper) CreateDefaultRollappWithProposer() (string, string) { - return s.CreateRollappWithNameWithProposer(rand.Str(8)) + return s.CreateRollappWithNameWithProposer(urand.RollappID()) } func (s *KeeperTestHelper) CreateRollappWithNameWithProposer(name string) (string, string) { diff --git a/app/upgrades/v4/upgrade_test.go b/app/upgrades/v4/upgrade_test.go index 259707b1e..840f71ea2 100644 --- a/app/upgrades/v4/upgrade_test.go +++ b/app/upgrades/v4/upgrade_test.go @@ -172,7 +172,7 @@ func (s *UpgradeTestSuite) validateRollappsMigration(numRoll int) error { s.Require().Len(rollapps, len(expectRollapps)) for _, rollapp := range rollapps { - rollappID, _ := rollapptypes.NewChainID(rollapp.RollappId) + rollappID := rollapptypes.MustNewChainID(rollapp.RollappId) // check that the rollapp can be retrieved by EIP155 key if _, ok := s.App.RollappKeeper.GetRollappByEIP155(s.Ctx, rollappID.GetEIP155ID()); !ok { return fmt.Errorf("rollapp by EIP155 not found") diff --git a/go.mod b/go.mod index f7295c396..8bb2f38b6 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/decred/dcrd/dcrec/edwards v1.0.0 github.com/dustin/go-humanize v1.0.1 github.com/dymensionxyz/gerr-cosmos v1.0.0 - github.com/dymensionxyz/sdk-utils v0.2.2 + github.com/dymensionxyz/sdk-utils v0.2.7-0.20240807091416-0717365f0f61 github.com/ethereum/go-ethereum v1.10.26 github.com/evmos/ethermint v0.22.0 github.com/gogo/protobuf v1.3.3 diff --git a/go.sum b/go.sum index 55b52cee6..d10f609d6 100644 --- a/go.sum +++ b/go.sum @@ -503,8 +503,8 @@ github.com/dymensionxyz/osmosis/osmomath v0.0.6-dym-v0.0.1 h1:59ZE3Ocrn04MUpb5VJ github.com/dymensionxyz/osmosis/osmomath v0.0.6-dym-v0.0.1/go.mod h1:2idySYJxP5YfMAEeSeqD8e7fSchfsI4jn7XFHJgNUsM= github.com/dymensionxyz/osmosis/v15 v15.2.1-0.20240627111157-f2243f47cdb3 h1:4VD23Jv5d8hqXEhLNNcLXlpSDJCWAGYJLF0kisJtkIk= github.com/dymensionxyz/osmosis/v15 v15.2.1-0.20240627111157-f2243f47cdb3/go.mod h1:2rsnXAdjYfXtyEw0mNwAdOiAccALYjAPvINGUf9Qg7Y= -github.com/dymensionxyz/sdk-utils v0.2.2 h1:RoANEXbR4nkEMaRGWCLT1P9A7KBCmzJ2U9wAZnNbszo= -github.com/dymensionxyz/sdk-utils v0.2.2/go.mod h1:ywr7+EEhHyuXCPUyLsktF3R4Mr31uCiviIjqwWNbQ84= +github.com/dymensionxyz/sdk-utils v0.2.7-0.20240807091416-0717365f0f61 h1:L9gxOTR+ZGd3+7Ycm3QVRHEYnE+rkIXRchlZMMAbsAc= +github.com/dymensionxyz/sdk-utils v0.2.7-0.20240807091416-0717365f0f61/go.mod h1:it9owYOpnIe17+ftTATQNDN4z+mBQx20/2Jm8SK15Rk= github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= diff --git a/x/incentives/client/cli/query_test.go b/x/incentives/client/cli/query_test.go index eba4b807f..0d0326b50 100644 --- a/x/incentives/client/cli/query_test.go +++ b/x/incentives/client/cli/query_test.go @@ -6,21 +6,18 @@ import ( "testing" "time" + tmrand "github.com/cometbft/cometbft/libs/rand" cometbftproto "github.com/cometbft/cometbft/proto/tendermint/types" + "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/dymensionxyz/sdk-utils/utils/urand" "github.com/stretchr/testify/suite" - tmrand "github.com/cometbft/cometbft/libs/rand" - "github.com/dymensionxyz/dymension/v3/app/apptesting" "github.com/dymensionxyz/dymension/v3/x/incentives/keeper" "github.com/dymensionxyz/dymension/v3/x/incentives/types" - - "github.com/cosmos/cosmos-sdk/baseapp" - rollapp "github.com/dymensionxyz/dymension/v3/x/rollapp/keeper" rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types" - - sdk "github.com/cosmos/cosmos-sdk/types" ) type QueryTestSuite struct { @@ -35,7 +32,7 @@ func (suite *QueryTestSuite) CreateDefaultRollapp() string { msgCreateRollapp := rollapptypes.MsgCreateRollapp{ Creator: alice.String(), - RollappId: tmrand.Str(8), + RollappId: urand.RollappID(), Bech32Prefix: strings.ToLower(tmrand.Str(3)), Alias: strings.ToLower(tmrand.Str(3)), VmType: rollapptypes.Rollapp_EVM, diff --git a/x/incentives/keeper/suite_test.go b/x/incentives/keeper/suite_test.go index 2becaae29..1c4cb6f6a 100644 --- a/x/incentives/keeper/suite_test.go +++ b/x/incentives/keeper/suite_test.go @@ -7,14 +7,13 @@ import ( "time" tmrand "github.com/cometbft/cometbft/libs/rand" - + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/dymensionxyz/sdk-utils/utils/urand" lockuptypes "github.com/osmosis-labs/osmosis/v15/x/lockup/types" "github.com/dymensionxyz/dymension/v3/x/incentives/types" rollapp "github.com/dymensionxyz/dymension/v3/x/rollapp/keeper" rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types" - - sdk "github.com/cosmos/cosmos-sdk/types" ) var ( @@ -211,7 +210,7 @@ func (suite *KeeperTestSuite) CreateDefaultRollapp(addr sdk.AccAddress) string { msgCreateRollapp := rollapptypes.MsgCreateRollapp{ Creator: addr.String(), - RollappId: tmrand.Str(8), + RollappId: urand.RollappID(), Bech32Prefix: strings.ToLower(tmrand.Str(3)), GenesisChecksum: "checksum", InitialSequencer: addr.String(), diff --git a/x/rollapp/genesis_test.go b/x/rollapp/genesis_test.go index dda3c583c..6c610afa6 100644 --- a/x/rollapp/genesis_test.go +++ b/x/rollapp/genesis_test.go @@ -3,45 +3,51 @@ package rollapp_test import ( "testing" + "github.com/stretchr/testify/require" + keepertest "github.com/dymensionxyz/dymension/v3/testutil/keeper" "github.com/dymensionxyz/dymension/v3/testutil/nullify" "github.com/dymensionxyz/dymension/v3/x/rollapp" "github.com/dymensionxyz/dymension/v3/x/rollapp/types" - "github.com/stretchr/testify/require" ) func TestInitExportGenesis(t *testing.T) { + const ( + rollappID1 = "rollapp_1234-1" + rollappID2 = "rollupp_1235-2" + ) + genesisState := types.GenesisState{ Params: types.DefaultParams(), RollappList: []types.Rollapp{ { - RollappId: "0", + RollappId: rollappID1, }, { - RollappId: "1", + RollappId: rollappID2, }, }, StateInfoList: []types.StateInfo{ { StateInfoIndex: types.StateInfoIndex{ - RollappId: "0", + RollappId: rollappID1, Index: 0, }, }, { StateInfoIndex: types.StateInfoIndex{ - RollappId: "1", + RollappId: rollappID2, Index: 1, }, }, }, LatestStateInfoIndexList: []types.StateInfoIndex{ { - RollappId: "0", + RollappId: rollappID1, }, { - RollappId: "1", + RollappId: rollappID2, }, }, BlockHeightToFinalizationQueueList: []types.BlockHeightToFinalizationQueue{ @@ -52,7 +58,6 @@ func TestInitExportGenesis(t *testing.T) { CreationHeight: 1, }, }, - // this line is used by starport scaffolding # genesis/test/state } k, ctx := keepertest.RollappKeeper(t) @@ -67,5 +72,4 @@ func TestInitExportGenesis(t *testing.T) { require.ElementsMatch(t, genesisState.StateInfoList, got.StateInfoList) require.ElementsMatch(t, genesisState.LatestStateInfoIndexList, got.LatestStateInfoIndexList) require.ElementsMatch(t, genesisState.BlockHeightToFinalizationQueueList, got.BlockHeightToFinalizationQueueList) - // this line is used by starport scaffolding # genesis/test/assert } diff --git a/x/rollapp/keeper/block_height_to_finalization_queue_test.go b/x/rollapp/keeper/block_height_to_finalization_queue_test.go index edfecbbda..62cc5f5e6 100644 --- a/x/rollapp/keeper/block_height_to_finalization_queue_test.go +++ b/x/rollapp/keeper/block_height_to_finalization_queue_test.go @@ -131,12 +131,12 @@ func (suite *RollappTestSuite) TestFinalizeRollapps() { fields: fields{ rollappStateUpdates: []rollappStateUpdate{ { - rollappId: "rollapp1", + rollappId: "rollapp_1234-1", stateUpdates: []stateUpdate{{ blockHeight: initialHeight, startHeight: 1, numOfBlocks: 10, }}, }, { - rollappId: "rollapp2", + rollappId: "rollappa_2345-1", stateUpdates: []stateUpdate{{ blockHeight: initialHeight, startHeight: 1, numOfBlocks: 10}}, }, @@ -153,11 +153,11 @@ func (suite *RollappTestSuite) TestFinalizeRollapps() { fields: fields{ rollappStateUpdates: []rollappStateUpdate{ { - rollappId: "rollapp1", + rollappId: "rollapp_1234-1", stateUpdates: []stateUpdate{{ blockHeight: initialHeight, startHeight: 1, numOfBlocks: 10}}, }, { - rollappId: "rollapp2", + rollappId: "rollappa_2345-1", stateUpdates: []stateUpdate{{ blockHeight: initialHeight, startHeight: 1, numOfBlocks: 10, fail: true, }}, @@ -168,7 +168,7 @@ func (suite *RollappTestSuite) TestFinalizeRollapps() { wantNumFinalized: 1, wantQueue: []queue{{ rollappsLeft: []rollappQueue{{ - rollappId: "rollapp2", + rollappId: "rollappa_2345-1", index: 1, }}}}, }, @@ -179,24 +179,24 @@ func (suite *RollappTestSuite) TestFinalizeRollapps() { fields: fields{ rollappStateUpdates: []rollappStateUpdate{ { - rollappId: "rollapp1", + rollappId: "rollapp_1234-1", stateUpdates: []stateUpdate{{ blockHeight: initialHeight, startHeight: 1, numOfBlocks: 10, }, { blockHeight: initialHeight, startHeight: 11, numOfBlocks: 20, }}, }, { - rollappId: "rollapp2", + rollappId: "rollappa_2345-1", stateUpdates: []stateUpdate{{ blockHeight: initialHeight, startHeight: 1, numOfBlocks: 10, }}, }, { - rollappId: "rollapp3", + rollappId: "rollappe_3456-1", stateUpdates: []stateUpdate{{ blockHeight: initialHeight, startHeight: 1, numOfBlocks: 10, }}, }, { - rollappId: "rollapp4", + rollappId: "rollappi_4567-1", stateUpdates: []stateUpdate{{ blockHeight: initialHeight, startHeight: 1, numOfBlocks: 10, fail: true, @@ -205,7 +205,7 @@ func (suite *RollappTestSuite) TestFinalizeRollapps() { fail: true, }}, }, { - rollappId: "rollapp5", + rollappId: "rollappo_5678-1", stateUpdates: []stateUpdate{{ blockHeight: initialHeight, startHeight: 1, numOfBlocks: 10, fail: true, @@ -219,15 +219,15 @@ func (suite *RollappTestSuite) TestFinalizeRollapps() { wantQueue: []queue{{ rollappsLeft: []rollappQueue{ { - rollappId: "rollapp4", + rollappId: "rollappi_4567-1", index: 1, }, { - rollappId: "rollapp5", + rollappId: "rollappo_5678-1", index: 1, }}}, { rollappsLeft: []rollappQueue{ { - rollappId: "rollapp4", + rollappId: "rollappi_4567-1", index: 2, }, }}, @@ -236,17 +236,17 @@ func (suite *RollappTestSuite) TestFinalizeRollapps() { // second finalization: 1 state finalized from first finalization, 2 states left wantNumFinalized: 1, recovers: map[types.StateInfoIndex]struct{}{ - {RollappId: "rollapp4", Index: 1}: {}, + {RollappId: "rollappi_4567-1", Index: 1}: {}, }, wantQueue: []queue{{ rollappsLeft: []rollappQueue{ { - rollappId: "rollapp5", + rollappId: "rollappo_5678-1", index: 1, }}}, { rollappsLeft: []rollappQueue{ { - rollappId: "rollapp4", + rollappId: "rollappi_4567-1", index: 2, }, }}, @@ -255,12 +255,12 @@ func (suite *RollappTestSuite) TestFinalizeRollapps() { // third finalization: 1 state finalized from first finalization, 1 state left wantNumFinalized: 1, recovers: map[types.StateInfoIndex]struct{}{ - {RollappId: "rollapp5", Index: 1}: {}, + {RollappId: "rollappo_5678-1", Index: 1}: {}, }, wantQueue: []queue{{ rollappsLeft: []rollappQueue{ { - rollappId: "rollapp4", + rollappId: "rollappi_4567-1", index: 2, }, }}, @@ -269,7 +269,7 @@ func (suite *RollappTestSuite) TestFinalizeRollapps() { // fourth finalization: 1 state finalized from first finalization, 0 states left wantNumFinalized: 1, recovers: map[types.StateInfoIndex]struct{}{ - {RollappId: "rollapp4", Index: 2}: {}, + {RollappId: "rollappi_4567-1", Index: 2}: {}, }, wantQueue: nil, }, @@ -414,22 +414,22 @@ func (suite *RollappTestSuite) TestKeeperFinalizePending() { { CreationHeight: 1, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 1}, - {RollappId: "rollapp2", Index: 2}, - {RollappId: "rollapp1", Index: 2}, - {RollappId: "rollapp3", Index: 1}, - {RollappId: "rollapp2", Index: 3}, - {RollappId: "rollapp3", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 2}, + {RollappId: "rollappe_3456-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 3}, + {RollappId: "rollappe_3456-1", Index: 2}, }, }, { CreationHeight: 2, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 3}, - {RollappId: "rollapp2", Index: 4}, - {RollappId: "rollapp1", Index: 4}, - {RollappId: "rollapp3", Index: 3}, - {RollappId: "rollapp2", Index: 5}, - {RollappId: "rollapp3", Index: 4}, + {RollappId: "rollapp_1234-1", Index: 3}, + {RollappId: "rollappa_2345-1", Index: 4}, + {RollappId: "rollapp_1234-1", Index: 4}, + {RollappId: "rollappe_3456-1", Index: 3}, + {RollappId: "rollappa_2345-1", Index: 5}, + {RollappId: "rollappe_3456-1", Index: 4}, }, }, }, @@ -441,23 +441,23 @@ func (suite *RollappTestSuite) TestKeeperFinalizePending() { { CreationHeight: 1, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 1}, - {RollappId: "rollapp2", Index: 2}, - {RollappId: "rollapp1", Index: 2}, - {RollappId: "rollapp3", Index: 1}, - {RollappId: "rollapp1", Index: 3}, - {RollappId: "rollapp3", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 2}, + {RollappId: "rollappe_3456-1", Index: 1}, + {RollappId: "rollapp_1234-1", Index: 3}, + {RollappId: "rollappe_3456-1", Index: 2}, }, }, }, - errFinalizeIndices: []types.StateInfoIndex{{"rollapp1", 2}, {"rollapp3", 2}}, + errFinalizeIndices: []types.StateInfoIndex{{"rollapp_1234-1", 2}, {"rollappe_3456-1", 2}}, expectQueueAfter: []types.BlockHeightToFinalizationQueue{ { CreationHeight: 1, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 2}, - {RollappId: "rollapp1", Index: 3}, - {RollappId: "rollapp3", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 3}, + {RollappId: "rollappe_3456-1", Index: 2}, }, }, }, @@ -467,29 +467,29 @@ func (suite *RollappTestSuite) TestKeeperFinalizePending() { { CreationHeight: 1, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 1}, - {RollappId: "rollapp2", Index: 1}, + {RollappId: "rollapp_1234-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 1}, }, }, { CreationHeight: 2, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 2}, - {RollappId: "rollapp2", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 2}, + {RollappId: "rollappa_2345-1", Index: 2}, }, }, }, - errFinalizeIndices: []types.StateInfoIndex{{"rollapp1", 1}, {"rollapp2", 2}}, + errFinalizeIndices: []types.StateInfoIndex{{"rollapp_1234-1", 1}, {"rollappa_2345-1", 2}}, expectQueueAfter: []types.BlockHeightToFinalizationQueue{ { CreationHeight: 1, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 1}, + {RollappId: "rollapp_1234-1", Index: 1}, }, }, { CreationHeight: 2, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 2}, - {RollappId: "rollapp2", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 2}, + {RollappId: "rollappa_2345-1", Index: 2}, }, }, }, @@ -499,38 +499,38 @@ func (suite *RollappTestSuite) TestKeeperFinalizePending() { { CreationHeight: 1, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 1}, - {RollappId: "rollapp2", Index: 2}, - {RollappId: "rollapp1", Index: 2}, - {RollappId: "rollapp3", Index: 1}, - {RollappId: "rollapp2", Index: 3}, - {RollappId: "rollapp3", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 2}, + {RollappId: "rollappe_3456-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 3}, + {RollappId: "rollappe_3456-1", Index: 2}, }, }, { CreationHeight: 2, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 3}, - {RollappId: "rollapp2", Index: 4}, - {RollappId: "rollapp1", Index: 4}, - {RollappId: "rollapp3", Index: 3}, - {RollappId: "rollapp2", Index: 5}, - {RollappId: "rollapp3", Index: 4}, + {RollappId: "rollapp_1234-1", Index: 3}, + {RollappId: "rollappa_2345-1", Index: 4}, + {RollappId: "rollapp_1234-1", Index: 4}, + {RollappId: "rollappe_3456-1", Index: 3}, + {RollappId: "rollappa_2345-1", Index: 5}, + {RollappId: "rollappe_3456-1", Index: 4}, }, }, }, - errFinalizeIndices: []types.StateInfoIndex{{"rollapp1", 2}, {"rollapp3", 4}}, + errFinalizeIndices: []types.StateInfoIndex{{"rollapp_1234-1", 2}, {"rollappe_3456-1", 4}}, expectQueueAfter: []types.BlockHeightToFinalizationQueue{ { CreationHeight: 1, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 2}, }, }, { CreationHeight: 2, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 3}, - {RollappId: "rollapp1", Index: 4}, - {RollappId: "rollapp3", Index: 4}, + {RollappId: "rollapp_1234-1", Index: 3}, + {RollappId: "rollapp_1234-1", Index: 4}, + {RollappId: "rollappe_3456-1", Index: 4}, }, }, }, @@ -540,50 +540,50 @@ func (suite *RollappTestSuite) TestKeeperFinalizePending() { { CreationHeight: 1, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 1}, - {RollappId: "rollapp2", Index: 2}, - {RollappId: "rollapp1", Index: 2}, - {RollappId: "rollapp3", Index: 1}, - {RollappId: "rollapp2", Index: 3}, - {RollappId: "rollapp3", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 2}, + {RollappId: "rollappe_3456-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 3}, + {RollappId: "rollappe_3456-1", Index: 2}, }, }, { CreationHeight: 2, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 3}, - {RollappId: "rollapp2", Index: 4}, - {RollappId: "rollapp1", Index: 4}, - {RollappId: "rollapp3", Index: 3}, - {RollappId: "rollapp2", Index: 5}, - {RollappId: "rollapp3", Index: 4}, + {RollappId: "rollapp_1234-1", Index: 3}, + {RollappId: "rollappa_2345-1", Index: 4}, + {RollappId: "rollapp_1234-1", Index: 4}, + {RollappId: "rollappe_3456-1", Index: 3}, + {RollappId: "rollappa_2345-1", Index: 5}, + {RollappId: "rollappe_3456-1", Index: 4}, }, }, }, errFinalizeIndices: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 1}, - {RollappId: "rollapp2", Index: 2}, - {RollappId: "rollapp3", Index: 1}, + {RollappId: "rollapp_1234-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 2}, + {RollappId: "rollappe_3456-1", Index: 1}, }, expectQueueAfter: []types.BlockHeightToFinalizationQueue{ { CreationHeight: 1, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 1}, - {RollappId: "rollapp2", Index: 2}, - {RollappId: "rollapp1", Index: 2}, - {RollappId: "rollapp3", Index: 1}, - {RollappId: "rollapp2", Index: 3}, - {RollappId: "rollapp3", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 2}, + {RollappId: "rollapp_1234-1", Index: 2}, + {RollappId: "rollappe_3456-1", Index: 1}, + {RollappId: "rollappa_2345-1", Index: 3}, + {RollappId: "rollappe_3456-1", Index: 2}, }, }, { CreationHeight: 2, FinalizationQueue: []types.StateInfoIndex{ - {RollappId: "rollapp1", Index: 3}, - {RollappId: "rollapp2", Index: 4}, - {RollappId: "rollapp1", Index: 4}, - {RollappId: "rollapp3", Index: 3}, - {RollappId: "rollapp2", Index: 5}, - {RollappId: "rollapp3", Index: 4}, + {RollappId: "rollapp_1234-1", Index: 3}, + {RollappId: "rollappa_2345-1", Index: 4}, + {RollappId: "rollapp_1234-1", Index: 4}, + {RollappId: "rollappe_3456-1", Index: 3}, + {RollappId: "rollappa_2345-1", Index: 5}, + {RollappId: "rollappe_3456-1", Index: 4}, }, }, }, diff --git a/x/rollapp/keeper/grpc_query_get_state_info_by_height_test.go b/x/rollapp/keeper/grpc_query_get_state_info_by_height_test.go index 0f1b1bab2..b919259c0 100644 --- a/x/rollapp/keeper/grpc_query_get_state_info_by_height_test.go +++ b/x/rollapp/keeper/grpc_query_get_state_info_by_height_test.go @@ -7,11 +7,13 @@ import ( errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/dymensionxyz/sdk-utils/utils/urand" + "github.com/stretchr/testify/require" + keepertest "github.com/dymensionxyz/dymension/v3/testutil/keeper" "github.com/dymensionxyz/dymension/v3/testutil/nullify" "github.com/dymensionxyz/dymension/v3/x/rollapp/keeper" "github.com/dymensionxyz/dymension/v3/x/rollapp/types" - "github.com/stretchr/testify/require" ) // Prevent strconv unused error @@ -57,7 +59,7 @@ func createNStateInfoAndIndex(keeper *keeper.Keeper, ctx sdk.Context, n int, rol func TestStateInfoByHeightLatestStateInfoIndex(t *testing.T) { keeper, ctx := keepertest.RollappKeeper(t) wctx := sdk.WrapSDKContext(ctx) - rollappId := "rollappId" + rollappId := "rollappid_1234-1" keeper.SetRollapp(ctx, types.Rollapp{ RollappId: rollappId, }) @@ -73,7 +75,7 @@ func TestStateInfoByHeightMissingStateInfo(t *testing.T) { keeper, ctx := keepertest.RollappKeeper(t) wctx := sdk.WrapSDKContext(ctx) - rollappId := "rollappId" + rollappId := urand.RollappID() keeper.SetRollapp(ctx, types.Rollapp{ RollappId: rollappId, }) @@ -92,14 +94,14 @@ func TestStateInfoByHeightMissingStateInfo(t *testing.T) { } func TestStateInfoByHeightMissingStateInfo1(t *testing.T) { - keeper, ctx := keepertest.RollappKeeper(t) + k, ctx := keepertest.RollappKeeper(t) wctx := sdk.WrapSDKContext(ctx) - rollappId := "rollappId" - keeper.SetRollapp(ctx, types.Rollapp{ + rollappId := urand.RollappID() + k.SetRollapp(ctx, types.Rollapp{ RollappId: rollappId, }) - keeper.SetLatestStateInfoIndex(ctx, types.StateInfoIndex{ + k.SetLatestStateInfoIndex(ctx, types.StateInfoIndex{ RollappId: rollappId, Index: uint64(60), }) @@ -107,12 +109,12 @@ func TestStateInfoByHeightMissingStateInfo1(t *testing.T) { RollappId: rollappId, Height: 70, } - keeper.SetStateInfo(ctx, types.StateInfo{ + k.SetStateInfo(ctx, types.StateInfo{ StateInfoIndex: types.StateInfoIndex{RollappId: rollappId, Index: 60}, StartHeight: 71, NumBlocks: 1, }) - _, err := keeper.StateInfo(wctx, request) + _, err := k.StateInfo(wctx, request) require.EqualError(t, err, errorsmod.Wrapf(types.ErrNotFound, "StateInfo wasn't found for rollappId=%s, index=%d", rollappId, 1).Error()) @@ -121,7 +123,8 @@ func TestStateInfoByHeightMissingStateInfo1(t *testing.T) { func TestStateInfoByHeightErr(t *testing.T) { keeper, ctx := keepertest.RollappKeeper(t) wctx := sdk.WrapSDKContext(ctx) - msgs := createNStateInfoAndIndex(keeper, ctx, 4, "rollappId") + rollappID := urand.RollappID() + msgs := createNStateInfoAndIndex(keeper, ctx, 4, rollappID) for _, tc := range []struct { desc string request *types.QueryGetStateInfoRequest @@ -131,11 +134,11 @@ func TestStateInfoByHeightErr(t *testing.T) { { desc: "StateInfoByHeight", request: &types.QueryGetStateInfoRequest{ - RollappId: "rollappId", + RollappId: rollappID, Height: msgs[3].StartHeight + 1, }, response: &types.QueryGetStateInfoResponse{StateInfo: types.StateInfo{ - StateInfoIndex: types.StateInfoIndex{RollappId: "rollappId", Index: 4}, + StateInfoIndex: types.StateInfoIndex{RollappId: rollappID, Index: 4}, StartHeight: msgs[3].StartHeight, NumBlocks: msgs[3].NumBlocks, }}, @@ -143,11 +146,11 @@ func TestStateInfoByHeightErr(t *testing.T) { { desc: "StateInfoByHeight_firstBlockInBatch", request: &types.QueryGetStateInfoRequest{ - RollappId: "rollappId", + RollappId: rollappID, Height: msgs[2].StartHeight, }, response: &types.QueryGetStateInfoResponse{StateInfo: types.StateInfo{ - StateInfoIndex: types.StateInfoIndex{RollappId: "rollappId", Index: 3}, + StateInfoIndex: types.StateInfoIndex{RollappId: rollappID, Index: 3}, StartHeight: msgs[2].StartHeight, NumBlocks: msgs[2].NumBlocks, }}, @@ -155,11 +158,11 @@ func TestStateInfoByHeightErr(t *testing.T) { { desc: "StateInfoByHeight_lastBlockInBatch", request: &types.QueryGetStateInfoRequest{ - RollappId: "rollappId", + RollappId: rollappID, Height: msgs[2].StartHeight + msgs[2].NumBlocks - 1, }, response: &types.QueryGetStateInfoResponse{StateInfo: types.StateInfo{ - StateInfoIndex: types.StateInfoIndex{RollappId: "rollappId", Index: 3}, + StateInfoIndex: types.StateInfoIndex{RollappId: rollappID, Index: 3}, StartHeight: msgs[2].StartHeight, NumBlocks: msgs[2].NumBlocks, }}, @@ -175,7 +178,7 @@ func TestStateInfoByHeightErr(t *testing.T) { { desc: "StateInfoByHeight_invalidHeight", request: &types.QueryGetStateInfoRequest{ - RollappId: "rollappId", + RollappId: rollappID, Height: 10000000, }, err: types.ErrStateNotExists, @@ -200,12 +203,13 @@ func TestStateInfoByHeightValidIncreasingBlockBatches(t *testing.T) { keeper, ctx := keepertest.RollappKeeper(t) wctx := sdk.WrapSDKContext(ctx) numOfMsg := 20 - msgs := createNStateInfoAndIndex(keeper, ctx, numOfMsg, "rollappId") + rollappID := urand.RollappID() + msgs := createNStateInfoAndIndex(keeper, ctx, numOfMsg, rollappID) for i := 0; i < numOfMsg; i += 1 { for height := msgs[i].StartHeight; height < msgs[i].StartHeight+msgs[i].NumBlocks; height += 1 { request := &types.QueryGetStateInfoRequest{ - RollappId: "rollappId", + RollappId: rollappID, Height: height, } response, err := keeper.StateInfo(wctx, request) @@ -222,12 +226,13 @@ func TestStateInfoByHeightValidDecreasingBlockBatches(t *testing.T) { keeper, ctx := keepertest.RollappKeeper(t) wctx := sdk.WrapSDKContext(ctx) numOfMsg := 20 - msgs := createNStateInfoAndIndex(keeper, ctx, numOfMsg, "rollappId") + rollappID := urand.RollappID() + msgs := createNStateInfoAndIndex(keeper, ctx, numOfMsg, rollappID) for i := 0; i < numOfMsg; i += 1 { for height := msgs[i].StartHeight; height < msgs[i].StartHeight+msgs[i].NumBlocks; height += 1 { request := &types.QueryGetStateInfoRequest{ - RollappId: "rollappId", + RollappId: rollappID, Height: height, } response, err := keeper.StateInfo(wctx, request) diff --git a/x/rollapp/keeper/invariants.go b/x/rollapp/keeper/invariants.go index cb7a1d6d6..bab646d53 100644 --- a/x/rollapp/keeper/invariants.go +++ b/x/rollapp/keeper/invariants.go @@ -4,6 +4,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + commontypes "github.com/dymensionxyz/dymension/v3/x/common/types" "github.com/dymensionxyz/dymension/v3/x/rollapp/types" ) @@ -62,11 +63,6 @@ func RollappByEIP155KeyInvariant(k Keeper) sdk.Invariant { continue } - // not breaking invariant, as eip155 format is not required - if !rollappID.IsEIP155() { - continue - } - _, found := k.GetRollappByEIP155(ctx, rollappID.GetEIP155ID()) if !found { msg += fmt.Sprintf("rollapp (%s) have no eip155 key\n", rollapp.RollappId) diff --git a/x/rollapp/keeper/invariants_test.go b/x/rollapp/keeper/invariants_test.go index 96e91422c..856901a90 100644 --- a/x/rollapp/keeper/invariants_test.go +++ b/x/rollapp/keeper/invariants_test.go @@ -31,7 +31,7 @@ func (suite *RollappTestSuite) TestInvariants() { rollapp, seqaddr := suite.CreateRollappWithNameWithProposer("dym_1100-1") seqPerRollapp[rollapp] = seqaddr - rollapp, seqaddr = suite.CreateRollappWithNameWithProposer("dym_1100") + rollapp, seqaddr = suite.CreateRollappWithNameWithProposer("dymd_1101-2") seqPerRollapp[rollapp] = seqaddr // send state updates @@ -59,7 +59,7 @@ func (suite *RollappTestSuite) TestInvariants() { func (suite *RollappTestSuite) TestRollappFinalizedStateInvariant() { suite.SetupTest() ctx := suite.Ctx - rollapp1, rollapp2, rollapp3 := "rollapp1", "rollapp2", "rollapp3" + rollapp1, rollapp2, rollapp3 := "rollapp_1234-1", "unrollapp_2345-1", "trollapp_3456-1" cases := []struct { name string rollappId string @@ -70,7 +70,7 @@ func (suite *RollappTestSuite) TestRollappFinalizedStateInvariant() { }{ { "successful invariant check", - "rollapp1", + rollapp1, &types.StateInfo{ StateInfoIndex: types.StateInfoIndex{ RollappId: rollapp1, diff --git a/x/rollapp/keeper/msg_server_create_rollapp_test.go b/x/rollapp/keeper/msg_server_create_rollapp_test.go index 3dcb8db15..5419a2901 100644 --- a/x/rollapp/keeper/msg_server_create_rollapp_test.go +++ b/x/rollapp/keeper/msg_server_create_rollapp_test.go @@ -6,6 +6,7 @@ import ( "github.com/cometbft/cometbft/libs/rand" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/dymensionxyz/sdk-utils/utils/urand" "github.com/dymensionxyz/dymension/v3/testutil/sample" "github.com/dymensionxyz/dymension/v3/x/rollapp/types" @@ -25,24 +26,70 @@ func (suite *RollappTestSuite) TestCreateRollappAlreadyExists() { suite.SetupTest() goCtx := sdk.WrapSDKContext(suite.Ctx) - // rollapp is the rollapp to create rollapp := types.MsgCreateRollapp{ Creator: alice, - RollappId: "rollapp1", + RollappId: "rollapp_1234-1", InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", GenesisChecksum: "checksum", Alias: "Rollapp", VmType: types.Rollapp_EVM, } + _, err := suite.msgServer.CreateRollapp(goCtx, &rollapp) - suite.Require().Nil(err) + suite.Require().NoError(err) - _, err = suite.msgServer.CreateRollapp(goCtx, &rollapp) - suite.ErrorIs(err, types.ErrRollappIDExists) + tests := []struct { + name string + rollappId string + expErr error + malleate func() + }{ + { + name: "same ID", + rollappId: "rollapp_1234-1", + expErr: types.ErrRollappExists, + }, { + name: "same EIP155, different name", + rollappId: "trollapp_1234-1", + expErr: types.ErrRollappExists, + }, { + name: "same name, different EIP155", + rollappId: "rollapp_2345-1", + expErr: types.ErrRollappExists, + }, { + name: "same ID, forked", + rollappId: "rollapp_1234-2", + malleate: func() { + r := rollapp.GetRollapp() + r.Frozen = true + suite.App.RollappKeeper.SetRollapp(suite.Ctx, r) + }, + expErr: nil, + }, + } + for _, test := range tests { + suite.Run(test.name, func() { + newRollapp := types.MsgCreateRollapp{ + Creator: alice, + RollappId: test.rollappId, + Bech32Prefix: "rol", + VmType: types.Rollapp_EVM, + Alias: strings.ToLower(rand.Str(3)), + } + + if test.malleate != nil { + test.malleate() + } + + _, err := suite.msgServer.CreateRollapp(goCtx, &newRollapp) + suite.Require().ErrorIs(err, test.expErr) + }) + } } func (suite *RollappTestSuite) TestCreateRollappAliasAlreadyExists() { + suite.T().Skip() // TODO: delete test suite.SetupTest() goCtx := sdk.WrapSDKContext(suite.Ctx) @@ -50,7 +97,7 @@ func (suite *RollappTestSuite) TestCreateRollappAliasAlreadyExists() { rollapp := types.MsgCreateRollapp{ Creator: alice, - RollappId: "rollapp1", + RollappId: urand.RollappID(), InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", GenesisChecksum: "checksum", @@ -62,7 +109,7 @@ func (suite *RollappTestSuite) TestCreateRollappAliasAlreadyExists() { rollapp = types.MsgCreateRollapp{ Creator: alice, - RollappId: "rollapp2", + RollappId: urand.RollappID(), InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", GenesisChecksum: "checksum", @@ -75,141 +122,60 @@ func (suite *RollappTestSuite) TestCreateRollappAliasAlreadyExists() { func (suite *RollappTestSuite) TestCreateRollappId() { suite.SetupTest() - goCtx := sdk.WrapSDKContext(suite.Ctx) tests := []struct { name string rollappId string - eip bool - valid bool + revision uint64 + expErr error }{ { name: "default is valid", rollappId: "rollapp_1234-1", - eip: true, - valid: true, - }, - { - name: "valid non-eip155", - rollappId: "testChain3", - eip: false, - valid: true, + revision: 1, + expErr: nil, }, { name: "too long id", - rollappId: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", - eip: false, - valid: false, + rollappId: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz_1234-1", + expErr: types.ErrInvalidRollappID, }, { name: "wrong EIP155", rollappId: "rollapp_ea2413-1", - eip: false, - valid: true, + expErr: types.ErrInvalidRollappID, }, { name: "no EIP155 with revision", rollappId: "rollapp-1", - eip: false, - valid: true, + expErr: types.ErrInvalidRollappID, }, { name: "starts with dash", rollappId: "-1234", - eip: false, - valid: false, - }, - } - for _, test := range tests { - suite.Run(test.name, func() { - alias := strings.NewReplacer("_", "", "-", "").Replace(test.rollappId) // reuse rollapp ID to avoid alias conflicts - rollapp := types.MsgCreateRollapp{ - Creator: alice, - RollappId: test.rollappId, - InitialSequencer: sample.AccAddress(), - Bech32Prefix: "rol", - GenesisChecksum: "checksum", - Alias: alias, - VmType: types.Rollapp_EVM, - Metadata: &mockRollappMetadata, - } - - _, err := suite.msgServer.CreateRollapp(goCtx, &rollapp) - if test.valid { - suite.Require().NoError(err) - id, err := types.NewChainID(test.rollappId) - suite.Require().NoError(err) - if test.eip { - suite.Require().True(id.IsEIP155()) - } else { - suite.Require().False(id.IsEIP155()) - } - } else { - suite.Require().ErrorIs(err, types.ErrInvalidRollappID) - } - }) - } -} - -func (suite *RollappTestSuite) TestCreateRollappIdRevisionNumber() { - suite.SetupTest() - goCtx := sdk.WrapSDKContext(suite.Ctx) - - tests := []struct { - name string - rollappId string - revision uint64 - valid bool - }{ - { - name: "revision set with eip155", - rollappId: "rollapp_1234-1", - revision: 1, - valid: true, + expErr: types.ErrInvalidRollappID, }, { name: "revision set without eip155", rollappId: "rollapp-3", - revision: 3, - valid: true, + expErr: types.ErrInvalidRollappID, }, { name: "revision not set", rollappId: "rollapp", - revision: 0, - valid: true, + expErr: types.ErrInvalidRollappID, }, { name: "invalid revision", rollappId: "rollapp-1-1", - revision: 0, - valid: false, + expErr: types.ErrInvalidRollappID, }, } for _, test := range tests { suite.Run(test.name, func() { - alias := strings.NewReplacer("_", "", "-", "").Replace(test.rollappId) // reuse rollapp ID to avoid alias conflicts - rollapp := types.MsgCreateRollapp{ - Creator: alice, - RollappId: test.rollappId, - InitialSequencer: sample.AccAddress(), - Bech32Prefix: "rol", - GenesisChecksum: "checksum", - Alias: alias, - VmType: types.Rollapp_EVM, - } - - _, err := suite.msgServer.CreateRollapp(goCtx, &rollapp) - - if test.valid { - suite.Require().NoError(err) - id, err := types.NewChainID(test.rollappId) - suite.Require().NoError(err) - suite.Require().Equal(test.revision, id.GetRevisionNumber()) - - } else { - suite.Require().ErrorIs(err, types.ErrInvalidRollappID) - } + id, err := types.NewChainID(test.rollappId) + suite.Require().ErrorIs(err, test.expErr) + suite.Require().Equal(test.revision, id.GetRevisionNumber()) }) } } @@ -227,12 +193,6 @@ func (suite *RollappTestSuite) TestForkChainId() { newRollappId: "rollapp_1234-2", valid: true, }, - { - name: "valid non-eip155 id", - rollappId: "rollapp", - newRollappId: "rollapp-2", - valid: true, - }, { name: "non-valid eip155 id", rollappId: "rollapp_1234-1", @@ -324,21 +284,15 @@ func (suite *RollappTestSuite) TestOverwriteEIP155Key() { _, err := suite.msgServer.CreateRollapp(goCtx, &rollapp) suite.Require().NoError(err) - // get eip155 key id, err := types.NewChainID(test.rollappId) suite.Require().NoError(err) suite.Require().NotEqual(0, id.GetEIP155ID()) - eip155key := id.GetEIP155ID() - // eip155 key registers to correct roll app - rollappFromEip1155, found := suite.App.RollappKeeper.GetRollappByEIP155(suite.Ctx, eip155key) + // eip155 key registers to correct rollapp + rollappFromEip1155, found := suite.App.RollappKeeper.GetRollappByEIP155(suite.Ctx, id.GetEIP155ID()) suite.Require().True(found) suite.Require().Equal(rollappFromEip1155.RollappId, rollapp.RollappId) - rollappFromAlias, found := suite.App.RollappKeeper.GetRollappByAlias(suite.Ctx, rollapp.Alias) - suite.Require().True(found) - suite.Require().Equal(rollappFromAlias.RollappId, rollapp.RollappId) - // create bad rollapp badRollapp := types.MsgCreateRollapp{ Creator: alice, @@ -351,7 +305,7 @@ func (suite *RollappTestSuite) TestOverwriteEIP155Key() { } _, err = suite.msgServer.CreateRollapp(goCtx, &badRollapp) // it should not be possible to register rollapp name with extra space - suite.Require().ErrorIs(err, types.ErrRollappIDExists) + suite.Require().ErrorIs(err, types.ErrRollappExists) }) } } @@ -391,7 +345,7 @@ func (suite *RollappTestSuite) createRollappWithCreatorAndVerify(expectedErr err rollapp := types.MsgCreateRollapp{ Creator: creator, - RollappId: rollappID, + RollappId: urand.RollappID(), InitialSequencer: address, Bech32Prefix: "rol", GenesisChecksum: "checksum", diff --git a/x/rollapp/keeper/msg_server_update_state_test.go b/x/rollapp/keeper/msg_server_update_state_test.go index e0afb3343..2ea71cb3f 100644 --- a/x/rollapp/keeper/msg_server_update_state_test.go +++ b/x/rollapp/keeper/msg_server_update_state_test.go @@ -4,6 +4,7 @@ import ( abci "github.com/cometbft/cometbft/abci/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/query" + "github.com/dymensionxyz/sdk-utils/utils/urand" "github.com/dymensionxyz/dymension/v3/testutil/sample" common "github.com/dymensionxyz/dymension/v3/x/common/types" @@ -19,7 +20,7 @@ func (suite *RollappTestSuite) TestFirstUpdateState() { // set rollapp rollapp := types.Rollapp{ - RollappId: "rollapp1", + RollappId: urand.RollappID(), Creator: alice, InitialSequencer: sample.AccAddress(), GenesisChecksum: "checksum", @@ -77,7 +78,7 @@ func (suite *RollappTestSuite) TestUpdateState() { // set rollapp rollapp := types.Rollapp{ - RollappId: "rollapp1", + RollappId: urand.RollappID(), Creator: alice, InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", @@ -186,7 +187,7 @@ func (suite *RollappTestSuite) TestUpdateStateUnknownRollappId() { // update state of unknown rollapp updateState := types.MsgUpdateState{ Creator: bob, - RollappId: "rollapp1", + RollappId: urand.RollappID(), StartHeight: 1, NumBlocks: 3, DAPath: "", @@ -204,7 +205,7 @@ func (suite *RollappTestSuite) TestUpdateStateUnknownSequencer() { // set rollapp rollapp := types.Rollapp{ - RollappId: "rollapp1", + RollappId: urand.RollappID(), Creator: alice, InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", @@ -233,7 +234,7 @@ func (suite *RollappTestSuite) TestUpdateStateSequencerRollappMismatch() { // set rollapp rollapp := types.Rollapp{ - RollappId: "rollapp1", + RollappId: urand.RollappID(), Creator: alice, InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", @@ -244,7 +245,7 @@ func (suite *RollappTestSuite) TestUpdateStateSequencerRollappMismatch() { // set sequencer sequencer := sequencertypes.Sequencer{ Address: bob, - RollappId: "rollapp2", + RollappId: urand.RollappID(), Status: sequencertypes.Bonded, Proposer: true, } @@ -268,9 +269,11 @@ func (suite *RollappTestSuite) TestUpdateStateErrLogicUnpermissioned() { suite.SetupTest() goCtx := sdk.WrapSDKContext(suite.Ctx) + rollappID := urand.RollappID() + // set rollapp rollapp := types.Rollapp{ - RollappId: "rollapp1", + RollappId: rollappID, Creator: alice, InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", @@ -281,7 +284,7 @@ func (suite *RollappTestSuite) TestUpdateStateErrLogicUnpermissioned() { // set unpermissioned sequencer sequencer := sequencertypes.Sequencer{ Address: rollapp.InitialSequencer, - RollappId: "rollapp1", + RollappId: rollappID, Status: sequencertypes.Bonded, Proposer: true, } @@ -301,13 +304,14 @@ func (suite *RollappTestSuite) TestUpdateStateErrLogicUnpermissioned() { suite.ErrorIs(err, sequencertypes.ErrUnknownSequencer) } -func (suite *RollappTestSuite) TestFirstUpdateStateGensisHightGreaterThanZero() { +func (suite *RollappTestSuite) TestFirstUpdateStateGensisHeightGreaterThanZero() { suite.SetupTest() goCtx := sdk.WrapSDKContext(suite.Ctx) // set rollapp + rollappID := urand.RollappID() rollapp := types.Rollapp{ - RollappId: "rollapp1", + RollappId: rollappID, Creator: alice, InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", @@ -318,7 +322,7 @@ func (suite *RollappTestSuite) TestFirstUpdateStateGensisHightGreaterThanZero() // set sequencer sequencer := sequencertypes.Sequencer{ Address: bob, - RollappId: "rollapp1", + RollappId: rollappID, Status: sequencertypes.Bonded, Proposer: true, } @@ -343,8 +347,9 @@ func (suite *RollappTestSuite) TestUpdateStateErrWrongBlockHeight() { _ = sdk.WrapSDKContext(suite.Ctx) // set rollapp + rollappID := urand.RollappID() rollapp := types.Rollapp{ - RollappId: "rollapp1", + RollappId: rollappID, Creator: alice, InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", @@ -355,7 +360,7 @@ func (suite *RollappTestSuite) TestUpdateStateErrWrongBlockHeight() { // set sequencer sequencer := sequencertypes.Sequencer{ Address: bob, - RollappId: "rollapp1", + RollappId: rollappID, Status: sequencertypes.Bonded, Proposer: true, } @@ -363,11 +368,11 @@ func (suite *RollappTestSuite) TestUpdateStateErrWrongBlockHeight() { // set initial latestStateInfoIndex & StateInfo latestStateInfoIndex := types.StateInfoIndex{ - RollappId: "rollapp1", + RollappId: rollappID, Index: 1, } stateInfo := types.StateInfo{ - StateInfoIndex: types.StateInfoIndex{RollappId: "rollapp1", Index: 1}, + StateInfoIndex: types.StateInfoIndex{RollappId: rollappID, Index: 1}, Sequencer: sequencer.Address, StartHeight: 1, NumBlocks: 3, @@ -402,8 +407,9 @@ func (suite *RollappTestSuite) TestUpdateStateErrLogicMissingStateInfo() { goCtx := sdk.WrapSDKContext(suite.Ctx) // set rollapp + rollappID := urand.RollappID() rollapp := types.Rollapp{ - RollappId: "rollapp1", + RollappId: rollappID, Creator: alice, InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", @@ -414,7 +420,7 @@ func (suite *RollappTestSuite) TestUpdateStateErrLogicMissingStateInfo() { // set sequencer sequencer := sequencertypes.Sequencer{ Address: bob, - RollappId: "rollapp1", + RollappId: rollappID, Status: sequencertypes.Bonded, Proposer: true, } @@ -422,7 +428,7 @@ func (suite *RollappTestSuite) TestUpdateStateErrLogicMissingStateInfo() { // set initial latestStateInfoIndex latestStateInfoIndex := types.StateInfoIndex{ - RollappId: "rollapp1", + RollappId: rollappID, Index: 1, } suite.App.RollappKeeper.SetLatestStateInfoIndex(suite.Ctx, latestStateInfoIndex) @@ -447,8 +453,9 @@ func (suite *RollappTestSuite) TestUpdateStateErrNotActiveSequencer() { goCtx := sdk.WrapSDKContext(suite.Ctx) // set rollapp + rollappID := urand.RollappID() rollapp := types.Rollapp{ - RollappId: "rollapp1", + RollappId: rollappID, Creator: alice, InitialSequencer: sample.AccAddress(), Bech32Prefix: "rol", @@ -459,7 +466,7 @@ func (suite *RollappTestSuite) TestUpdateStateErrNotActiveSequencer() { // set sequencer sequencer := sequencertypes.Sequencer{ Address: bob, - RollappId: "rollapp1", + RollappId: rollappID, Status: sequencertypes.Bonded, } suite.App.SequencerKeeper.SetSequencer(suite.Ctx, sequencer) diff --git a/x/rollapp/keeper/rollapp.go b/x/rollapp/keeper/rollapp.go index c877b9050..c08179bf6 100644 --- a/x/rollapp/keeper/rollapp.go +++ b/x/rollapp/keeper/rollapp.go @@ -14,16 +14,14 @@ import ( ) func (k Keeper) RegisterRollapp(ctx sdk.Context, rollapp types.Rollapp) error { - if err := rollapp.ValidateBasic(); err != nil { - return fmt.Errorf("validate rollapp: %w", err) - } - - rollappId, _ := types.NewChainID(rollapp.RollappId) + // Already validated chain id in ValidateBasic, so we assume it's valid + rollappId := types.MustNewChainID(rollapp.RollappId) if err := k.checkIfRollappExists(ctx, rollappId, rollapp.Alias); err != nil { return err } - creator, _ := sdk.AccAddressFromBech32(rollapp.Creator) + // Already validated chain id in ValidateBasic, so we assume it's valid + creator := sdk.MustAccAddressFromBech32(rollapp.Creator) registrationFee := sdk.NewCoins(k.RegistrationFee(ctx)) if !registrationFee.IsZero() { @@ -118,31 +116,31 @@ func (k Keeper) canUpdateAlias( return updateAlias, nil } -// checkIfRollappExists checks if a rollapp with the same ID, EIP155ID (if supported) or alias already exists in the store. -// An exception is made for EIP155ID when the rollapp is frozen, in which case it is allowed to replace the existing rollapp. +// checkIfRollappExists checks if a rollapp with the same ID or alias already exists in the store. +// An exception is made for when the rollapp is frozen, in which case it is allowed to replace the existing rollapp (forking). func (k Keeper) checkIfRollappExists(ctx sdk.Context, rollappId types.ChainID, alias string) error { // check to see if the RollappId has been registered before if _, isFound := k.GetRollapp(ctx, rollappId.GetChainID()); isFound { - return types.ErrRollappIDExists + return types.ErrRollappExists } if _, isFound := k.GetRollappByAlias(ctx, alias); isFound { return types.ErrRollappAliasExists } - if !rollappId.IsEIP155() { - return nil - } - // check to see if the RollappId has been registered before with same key existingRollapp, isFound := k.GetRollappByEIP155(ctx, rollappId.GetEIP155ID()) // allow replacing EIP155 only when forking (previous rollapp is frozen) if !isFound { + // if not forking, check to see if the Rollapp has been registered before with same name + if _, isFound = k.GetRollappByName(ctx, rollappId.GetName()); isFound { + return types.ErrRollappExists + } return nil } if !existingRollapp.Frozen { - return types.ErrRollappIDExists + return types.ErrRollappExists } - existingRollappChainId, _ := types.NewChainID(existingRollapp.RollappId) + existingRollappChainId := types.MustNewChainID(existingRollapp.RollappId) if rollappId.GetName() != existingRollappChainId.GetName() { return errorsmod.Wrapf(types.ErrInvalidRollappID, "rollapp name should be %s", existingRollappChainId.GetName()) @@ -169,13 +167,9 @@ func (k Keeper) SetRollapp(ctx sdk.Context, rollapp types.Rollapp) { rollapp.GetAlias(), ), []byte(rollapp.RollappId)) - // check if chain-id is EVM compatible. no err check as rollapp is already validated - rollappID, _ := types.NewChainID(rollapp.RollappId) - if !rollappID.IsEIP155() { - return - } + // no err check as rollapp is already validated + rollappID := types.MustNewChainID(rollapp.RollappId) - // In case the chain id is EVM compatible, we store it by EIP155 id, to be retrievable by EIP155 id key store = prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.RollappByEIP155KeyPrefix)) store.Set(types.RollappByEIP155Key( rollappID.GetEIP155ID(), @@ -198,7 +192,7 @@ func (k Keeper) SealRollapp(ctx sdk.Context, rollappId string) error { return nil } -// GetRollappByEIP155 returns a rollapp from its EIP155 id (https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md) for EVM compatible rollapps +// GetRollappByEIP155 returns a rollapp from its EIP155 id (https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md) func (k Keeper) GetRollappByEIP155(ctx sdk.Context, eip155 uint64) (val types.Rollapp, found bool) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.RollappByEIP155KeyPrefix)) id := store.Get(types.RollappByEIP155Key( @@ -241,6 +235,24 @@ func (k Keeper) GetRollapp( return val, true } +func (k Keeper) GetRollappByName( + ctx sdk.Context, + name string, +) (val types.Rollapp, found bool) { + name = name + "_" + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.RollappKeyPrefix)) + iterator := sdk.KVStorePrefixIterator(store, []byte(name)) + + defer iterator.Close() // nolint: errcheck + + if !iterator.Valid() { + return val, false + } + + k.cdc.MustUnmarshal(iterator.Value(), &val) + return val, true +} + func (k Keeper) MustGetRollapp(ctx sdk.Context, rollappId string) types.Rollapp { ret, found := k.GetRollapp(ctx, rollappId) if !found { diff --git a/x/rollapp/keeper/rollapp_suite_test.go b/x/rollapp/keeper/rollapp_suite_test.go index a8df7f803..73d53c879 100644 --- a/x/rollapp/keeper/rollapp_suite_test.go +++ b/x/rollapp/keeper/rollapp_suite_test.go @@ -9,6 +9,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/dymensionxyz/sdk-utils/utils/urand" "github.com/stretchr/testify/suite" "github.com/dymensionxyz/dymension/v3/app/apptesting" @@ -67,7 +68,7 @@ func createNRollapp(keeper *keeper.Keeper, ctx sdk.Context, n int) (items []type items, rollappSummaries = make([]types.Rollapp, n), make([]types.RollappSummary, n) for i := range items { - items[i].RollappId = strconv.Itoa(i) + items[i].RollappId = urand.RollappID() keeper.SetRollapp(ctx, items[i]) rollappSummaries[i] = types.RollappSummary{ diff --git a/x/rollapp/keeper/rollapp_test.go b/x/rollapp/keeper/rollapp_test.go index ee2b382f9..0951199ed 100644 --- a/x/rollapp/keeper/rollapp_test.go +++ b/x/rollapp/keeper/rollapp_test.go @@ -4,20 +4,22 @@ import ( "strconv" "testing" + "github.com/stretchr/testify/require" + keepertest "github.com/dymensionxyz/dymension/v3/testutil/keeper" "github.com/dymensionxyz/dymension/v3/testutil/nullify" - "github.com/stretchr/testify/require" + "github.com/dymensionxyz/dymension/v3/x/rollapp/types" ) // Prevent strconv unused error var _ = strconv.IntSize func TestRollappGet(t *testing.T) { - keeper, ctx := keepertest.RollappKeeper(t) - items, _ := createNRollapp(keeper, ctx, 10) + k, ctx := keepertest.RollappKeeper(t) + items, _ := createNRollapp(k, ctx, 10) for _, item := range items { item := item - rst, found := keeper.GetRollapp(ctx, + rst, found := k.GetRollapp(ctx, item.RollappId, ) require.True(t, found) @@ -29,13 +31,13 @@ func TestRollappGet(t *testing.T) { } func TestRollappRemove(t *testing.T) { - keeper, ctx := keepertest.RollappKeeper(t) - items, _ := createNRollapp(keeper, ctx, 10) + k, ctx := keepertest.RollappKeeper(t) + items, _ := createNRollapp(k, ctx, 10) for _, item := range items { - keeper.RemoveRollapp(ctx, + k.RemoveRollapp(ctx, item.RollappId, ) - _, found := keeper.GetRollapp(ctx, + _, found := k.GetRollapp(ctx, item.RollappId, ) require.False(t, found) @@ -43,10 +45,45 @@ func TestRollappRemove(t *testing.T) { } func TestRollappGetAll(t *testing.T) { - keeper, ctx := keepertest.RollappKeeper(t) - items, _ := createNRollapp(keeper, ctx, 10) + k, ctx := keepertest.RollappKeeper(t) + items, _ := createNRollapp(k, ctx, 10) require.ElementsMatch(t, nullify.Fill(items), - nullify.Fill(keeper.GetAllRollapps(ctx)), + nullify.Fill(k.GetAllRollapps(ctx)), ) } + +func TestKeeper_FindRollappByName(t *testing.T) { + const rollappID = "rollapp_1234-1" + + k, ctx := keepertest.RollappKeeper(t) + k.SetRollapp(ctx, types.Rollapp{ + RollappId: rollappID, + }) + + tests := []struct { + name string + queryRollappName string + wantFound bool + }{ + { + name: "rollapp found: same name", + queryRollappName: "rollapp", + wantFound: true, + }, { + name: "rollapp not found: different name", + queryRollappName: "rollupp", + wantFound: false, + }, { + name: "rollapp not found: partial name match 1", + queryRollappName: "rollap", + wantFound: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, gotFound := k.GetRollappByName(ctx, tt.queryRollappName) + require.Equal(t, tt.wantFound, gotFound) + }) + } +} diff --git a/x/rollapp/types/chain_id.go b/x/rollapp/types/chain_id.go index 3e4fa2257..a4766c6af 100644 --- a/x/rollapp/types/chain_id.go +++ b/x/rollapp/types/chain_id.go @@ -40,66 +40,39 @@ func NewChainID(id string) (ChainID, error) { } if len(chainID) > types.MaxChainIDLen { - return ChainID{}, errorsmod.Wrapf(ErrInvalidRollappID, "exceeds 48 chars: %s: len: %d", chainID, len(chainID)) + return ChainID{}, errorsmod.Wrapf(ErrInvalidRollappID, "exceeds %d chars: %s: len: %d", types.MaxChainIDLen, chainID, len(chainID)) } - eip155, err := getEIP155ID(chainID) - if err != nil { - return ChainID{}, err - } - revision, err := getRevisionNumber(chainID) - if err != nil { - return ChainID{}, err - } - matches := strings.Split(chainID, "-") - - if matches[0] == "" { - return ChainID{}, errorsmod.Wrapf(ErrInvalidRollappID, "empty chain id before '-'") - } - - return ChainID{ - chainID: chainID, - eip155ID: eip155, - revision: revision, - name: matches[0], - }, nil -} - -// getEIP155ID parses a string chain identifier's epoch to an Ethereum-compatible -// chain-id in *big.Int format. The function returns an error if the chain-id has an invalid format -func getEIP155ID(chainID string) (*big.Int, error) { matches := ethermintChainID.FindStringSubmatch(chainID) if matches == nil || len(matches) != 4 || matches[1] == "" { - return nil, nil + return ChainID{}, ErrInvalidRollappID } // verify that the chain-id entered is a base 10 integer chainIDInt, ok := new(big.Int).SetString(matches[2], 10) if !ok { - return nil, errorsmod.Wrapf(ErrInvalidRollappID, "epoch %s must be base-10 integer format", matches[2]) + return ChainID{}, errorsmod.Wrapf(ErrInvalidRollappID, "EIP155 part %s must be base-10 integer format", matches[2]) } - return chainIDInt, nil -} - -// getRevisionNumber parses a string chain identifier and returns the revision number -func getRevisionNumber(chainID string) (uint64, error) { - matches := strings.Split(chainID, "-") - if len(matches) == 1 { - return 0, nil - } - if len(matches) != 2 { - return 0, errorsmod.Wrapf(ErrInvalidRollappID, "unable to parse revision number") - } - revision, err := strconv.ParseUint(matches[1], 0, 64) + revision, err := strconv.ParseUint(matches[3], 0, 64) if err != nil { - return 0, errorsmod.Wrapf(ErrInvalidRollappID, "unable to parse revision number: error: %v", err) + return ChainID{}, errorsmod.Wrapf(ErrInvalidRollappID, "parse revision number: error: %v", err) } - return revision, nil + + return ChainID{ + chainID: chainID, + eip155ID: chainIDInt, + revision: revision, + name: matches[1], + }, nil } -func (c *ChainID) IsEIP155() bool { - return c.eip155ID != nil +func MustNewChainID(id string) ChainID { + chainID, err := NewChainID(id) + if err != nil { + panic(err) + } + return chainID } func (c *ChainID) GetChainID() string { diff --git a/x/rollapp/types/errors.go b/x/rollapp/types/errors.go index 88503d22e..bf819ede1 100644 --- a/x/rollapp/types/errors.go +++ b/x/rollapp/types/errors.go @@ -9,7 +9,7 @@ import ( // x/rollapp module sentinel errors var ( - ErrRollappIDExists = errorsmod.Register(ModuleName, 1000, "rollapp already exists for this rollapp-id; must use new rollapp-id") + ErrRollappExists = errorsmod.Register(ModuleName, 1000, "rollapp already exists") ErrInvalidInitialSequencer = errorsmod.Register(ModuleName, 1001, "empty initial sequencer") ErrInvalidCreatorAddress = errorsmod.Register(ModuleName, 1002, "invalid creator address") ErrInvalidBech32Prefix = errorsmod.Register(ModuleName, 1003, "invalid Bech32 prefix") diff --git a/x/sequencer/keeper/msg_server_create_sequencer_test.go b/x/sequencer/keeper/msg_server_create_sequencer_test.go index fa4283ff6..9d50a919d 100644 --- a/x/sequencer/keeper/msg_server_create_sequencer_test.go +++ b/x/sequencer/keeper/msg_server_create_sequencer_test.go @@ -9,6 +9,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/query" bankutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" + "github.com/dymensionxyz/sdk-utils/utils/urand" "github.com/dymensionxyz/dymension/v3/testutil/sample" rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types" @@ -111,10 +112,12 @@ func (suite *SequencerTestSuite) TestCreateSequencer() { } rollappSequencersExpect := make(map[rollappSequencersExpectKey]string) + const numRollapps = 3 + rollappIDs := make([]string, numRollapps) // for 3 rollapps, test 10 sequencers creations - for j := 0; j < 3; j++ { + for j := 0; j < numRollapps; j++ { rollapp := rollapptypes.Rollapp{ - RollappId: fmt.Sprintf("%s%d", "rollapp", j), + RollappId: urand.RollappID(), Creator: alice, Bech32Prefix: bech32Prefix, GenesisChecksum: "1234567890abcdefg", @@ -132,6 +135,7 @@ func (suite *SequencerTestSuite) TestCreateSequencer() { suite.App.RollappKeeper.SetRollapp(suite.Ctx, rollapp) rollappId := rollapp.GetRollappId() + rollappIDs[j] = rollappId for i := 0; i < 10; i++ { pubkey := ed25519.GenPrivKey().PubKey() @@ -191,8 +195,8 @@ func (suite *SequencerTestSuite) TestCreateSequencer() { totalFound := 0 // check query by rollapp - for j := 0; j < 3; j++ { - rollappId := fmt.Sprintf("%s%d", "rollapp", j) + for i := 0; i < numRollapps; i++ { + rollappId := rollappIDs[i] queryAllResponse, err := suite.queryClient.SequencersByRollapp(goCtx, &types.QueryGetSequencersByRollappRequest{RollappId: rollappId}) suite.Require().Nil(err) diff --git a/x/sequencer/keeper/sequencer_suite_test.go b/x/sequencer/keeper/sequencer_suite_test.go index 01a09a4c5..8741b9606 100644 --- a/x/sequencer/keeper/sequencer_suite_test.go +++ b/x/sequencer/keeper/sequencer_suite_test.go @@ -3,7 +3,6 @@ package keeper_test import ( "testing" - "github.com/cometbft/cometbft/libs/rand" cometbftproto "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/baseapp" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -11,6 +10,7 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" bankutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" + "github.com/dymensionxyz/sdk-utils/utils/urand" "github.com/stretchr/testify/suite" "github.com/dymensionxyz/dymension/v3/app/apptesting" @@ -52,7 +52,7 @@ func (suite *SequencerTestSuite) CreateDefaultRollapp() (string, cryptotypes.Pub func (suite *SequencerTestSuite) CreateRollappWithInitialSequencer(initSeq string) string { rollapp := rollapptypes.Rollapp{ - RollappId: rand.Str(8), + RollappId: urand.RollappID(), Creator: sample.AccAddress(), GenesisChecksum: "checksum", InitialSequencer: initSeq,