diff --git a/cmd/node/config/enableEpochs.toml b/cmd/node/config/enableEpochs.toml index e5b6efe99f3..6a9384c8490 100644 --- a/cmd/node/config/enableEpochs.toml +++ b/cmd/node/config/enableEpochs.toml @@ -258,7 +258,7 @@ AutoBalanceDataTriesEnableEpoch = 1 # MigrateDataTrieEnableEpoch represents the epoch when the data tries migration is enabled - MigrateDataTrieEnableEpoch = 999999 + MigrateDataTrieEnableEpoch = 2 # KeepExecOrderOnCreatedSCRsEnableEpoch represents the epoch when the execution order of created SCRs is ensured KeepExecOrderOnCreatedSCRsEnableEpoch = 1 diff --git a/go.mod b/go.mod index 135f5d22821..fbd61b07d8d 100644 --- a/go.mod +++ b/go.mod @@ -12,19 +12,20 @@ require ( github.com/gogo/protobuf v1.3.2 github.com/google/gops v0.3.18 github.com/gorilla/websocket v1.5.0 + github.com/klauspost/cpuid/v2 v2.2.5 github.com/mitchellh/mapstructure v1.5.0 github.com/multiversx/mx-chain-communication-go v1.0.12 github.com/multiversx/mx-chain-core-go v1.2.18 github.com/multiversx/mx-chain-crypto-go v1.2.9 github.com/multiversx/mx-chain-es-indexer-go v1.4.18 github.com/multiversx/mx-chain-logger-go v1.0.13 - github.com/multiversx/mx-chain-scenario-go v1.2.1 + github.com/multiversx/mx-chain-scenario-go v1.3.0 github.com/multiversx/mx-chain-storage-go v1.0.14 - github.com/multiversx/mx-chain-vm-common-go v1.5.10 - github.com/multiversx/mx-chain-vm-go v1.5.24 - github.com/multiversx/mx-chain-vm-v1_2-go v1.2.64 - github.com/multiversx/mx-chain-vm-v1_3-go v1.3.65 - github.com/multiversx/mx-chain-vm-v1_4-go v1.4.92 + github.com/multiversx/mx-chain-vm-common-go v1.5.11 + github.com/multiversx/mx-chain-vm-go v1.5.26 + github.com/multiversx/mx-chain-vm-v1_2-go v1.2.65 + github.com/multiversx/mx-chain-vm-v1_3-go v1.3.66 + github.com/multiversx/mx-chain-vm-v1_4-go v1.4.94 github.com/pelletier/go-toml v1.9.3 github.com/pkg/errors v0.9.1 github.com/shirou/gopsutil v3.21.11+incompatible @@ -91,7 +92,6 @@ require ( github.com/jbenet/goprocess v0.1.4 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.16.5 // indirect - github.com/klauspost/cpuid/v2 v2.2.5 // indirect github.com/koron/go-ssdp v0.0.4 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/libp2p/go-buffer-pool v0.1.0 // indirect diff --git a/go.sum b/go.sum index 7940ab735e0..b7cb342ed43 100644 --- a/go.sum +++ b/go.sum @@ -395,20 +395,20 @@ github.com/multiversx/mx-chain-es-indexer-go v1.4.18 h1:rCA+/mn/77MyB7c8FjtabdQe github.com/multiversx/mx-chain-es-indexer-go v1.4.18/go.mod h1:maraR9xXcfi0lLifhGMc+DVHpw1IOPX/c89HVckF1Js= github.com/multiversx/mx-chain-logger-go v1.0.13 h1:eru/TETo0MkO4ZTnXsQDKf4PBRpAXmqjT02klNT/JnY= github.com/multiversx/mx-chain-logger-go v1.0.13/go.mod h1:MZJhTAtZTJxT+yK2EHc4ZW3YOHUc1UdjCD0iahRNBZk= -github.com/multiversx/mx-chain-scenario-go v1.2.1 h1:9eC6VcOEAKRRKZ7EbSWPLzCdNIMWwuNBtAZlgR4cSMA= -github.com/multiversx/mx-chain-scenario-go v1.2.1/go.mod h1:EuZY7DpNFHVNSxJR8dKE1z2I8gBYfEFFPSwNUOXptqE= +github.com/multiversx/mx-chain-scenario-go v1.3.0 h1:Vm6jYipJuIcJt8kamgXkePtKkwXaF5Lv5DglZjE5o8I= +github.com/multiversx/mx-chain-scenario-go v1.3.0/go.mod h1:Sdgz8nzA9Ki/pdY/e2a4k90LALBKQB1Eo8HeCV3Bkzo= github.com/multiversx/mx-chain-storage-go v1.0.14 h1:h0acoqPS3FKJ4S3cKBEriTU0OabSQnpxai5WKhi1YCs= github.com/multiversx/mx-chain-storage-go v1.0.14/go.mod h1:sJ2q49tgjxNpMpsHysjABqCAB0FLBmDblbjBkQ8XfmA= -github.com/multiversx/mx-chain-vm-common-go v1.5.10 h1:VoqVt9yX1nQUa0ZujMpdT3J3pKSnQcB6WCQLvIW4sqw= -github.com/multiversx/mx-chain-vm-common-go v1.5.10/go.mod h1:sqkKMCnwkWl8DURdb9q7pctK8IANghdHY1KJLE0ox2c= -github.com/multiversx/mx-chain-vm-go v1.5.24 h1:6RhMvf84Ys8DksDovms+su7w6j9TWz3Rtm/PpgV12Yw= -github.com/multiversx/mx-chain-vm-go v1.5.24/go.mod h1:T03t+in5jqeTuFZKDt2wH/Sl9MSRczvWhmG+tQEIfec= -github.com/multiversx/mx-chain-vm-v1_2-go v1.2.64 h1:3BEpSxEQibMMi4LXBjpo2y5vUa1LS7olDC2eDkmUfFQ= -github.com/multiversx/mx-chain-vm-v1_2-go v1.2.64/go.mod h1:MUO2E4aEIu3siDkvjraO/WaBh/FxVeQyPWfsrZE+MTU= -github.com/multiversx/mx-chain-vm-v1_3-go v1.3.65 h1:H0Duuoz6lR6KapqLqMspWTojaVtQRiLA5lIm6XV9H04= -github.com/multiversx/mx-chain-vm-v1_3-go v1.3.65/go.mod h1:IZCHU3j/OSKVzdXu+5uZZSq2pVJrAS/KKAvnGrA/IKM= -github.com/multiversx/mx-chain-vm-v1_4-go v1.4.92 h1:8ZcqnUQoIeM5k1F2IHvqbFzCumGwB4oVilWGuwurxpo= -github.com/multiversx/mx-chain-vm-v1_4-go v1.4.92/go.mod h1:NyGULyeuEFe7Tb3gavT3Mti2oIFZJiMIf8VJIQnL4E8= +github.com/multiversx/mx-chain-vm-common-go v1.5.11 h1:rAQR00ALKOmWAuNA8XW8hR02M9gmrAE4WZwyJH4dMMU= +github.com/multiversx/mx-chain-vm-common-go v1.5.11/go.mod h1:T04rZt/VdwjPXcXVxltMmDmrhxNXsqlByYbWNWcNLYA= +github.com/multiversx/mx-chain-vm-go v1.5.26 h1:ZjUJTG9cO2h5WNRIZ50ZSZNsTEPqXXPGS9Y/SAGyC2A= +github.com/multiversx/mx-chain-vm-go v1.5.26/go.mod h1:gNZ/s4Z6OHg6ZeBsW6aDxWQduXsRS0Bsv4pfkmHeRzs= +github.com/multiversx/mx-chain-vm-v1_2-go v1.2.65 h1:TxFjQH0dXC/ACQxlIgJbO7pVoh00rcqeKSnIjWTDMxg= +github.com/multiversx/mx-chain-vm-v1_2-go v1.2.65/go.mod h1:UUUxIU7mlRkz+Jz4GWV2GkgJt2mKd+j1kky++RNYc9s= +github.com/multiversx/mx-chain-vm-v1_3-go v1.3.66 h1:xgrXfHKa0Za4xjFj5W0FcYEivjrQIhLvGEFXXa1uQZU= +github.com/multiversx/mx-chain-vm-v1_3-go v1.3.66/go.mod h1:mx6IOAqo7zjSinYd8D2YqlpMWsuqFoYXJ6bntnTOeQg= +github.com/multiversx/mx-chain-vm-v1_4-go v1.4.94 h1:MZFEBjDmfwLGB0cZb/pvlLx+qRv/9tO83bEgHUk34is= +github.com/multiversx/mx-chain-vm-v1_4-go v1.4.94/go.mod h1:uuSbZGe0UwOWQyHA4EeJWhs8UeDdhtmMwlhNaX9ppx0= github.com/multiversx/mx-components-big-int v1.0.0 h1:Wkr8lSzK2nDqixOrrBa47VNuqdhV1m/aJhaP1EMaiS8= github.com/multiversx/mx-components-big-int v1.0.0/go.mod h1:maIEMgHlNE2u78JaDD0oLzri+ShgU4okHfzP3LWGdQM= github.com/multiversx/protobuf v1.3.2 h1:RaNkxvGTGbA0lMcnHAN24qE1G1i+Xs5yHA6MDvQ4mSM= diff --git a/integrationTests/multiShard/endOfEpoch/startInEpoch/startInEpoch_test.go b/integrationTests/multiShard/endOfEpoch/startInEpoch/startInEpoch_test.go index b9492592bd3..86d2070814b 100644 --- a/integrationTests/multiShard/endOfEpoch/startInEpoch/startInEpoch_test.go +++ b/integrationTests/multiShard/endOfEpoch/startInEpoch/startInEpoch_test.go @@ -216,7 +216,7 @@ func testNodeStartsInEpoch(t *testing.T, shardID uint32, expectedHighestRound ui cryptoComponents.BlKeyGen = &mock.KeyGenMock{} cryptoComponents.TxKeyGen = &mock.KeyGenMock{} - coreComponents := integrationTests.GetDefaultCoreComponents() + coreComponents := integrationTests.GetDefaultCoreComponents(integrationTests.CreateEnableEpochsConfig()) coreComponents.InternalMarshalizerField = integrationTests.TestMarshalizer coreComponents.TxMarshalizerField = integrationTests.TestMarshalizer coreComponents.HasherField = integrationTests.TestHasher diff --git a/integrationTests/multiShard/hardFork/hardFork_test.go b/integrationTests/multiShard/hardFork/hardFork_test.go index 0f4f6140854..4172deb9462 100644 --- a/integrationTests/multiShard/hardFork/hardFork_test.go +++ b/integrationTests/multiShard/hardFork/hardFork_test.go @@ -386,7 +386,7 @@ func hardForkImport( defaults.FillGasMapInternal(gasSchedule, 1) log.Warn("started import process") - coreComponents := integrationTests.GetDefaultCoreComponents() + coreComponents := integrationTests.GetDefaultCoreComponents(integrationTests.CreateEnableEpochsConfig()) coreComponents.InternalMarshalizerField = integrationTests.TestMarshalizer coreComponents.TxMarshalizerField = integrationTests.TestMarshalizer coreComponents.HasherField = integrationTests.TestHasher @@ -558,7 +558,7 @@ func createHardForkExporter( returnedConfigs[node.ShardCoordinator.SelfId()] = append(returnedConfigs[node.ShardCoordinator.SelfId()], exportConfig) returnedConfigs[node.ShardCoordinator.SelfId()] = append(returnedConfigs[node.ShardCoordinator.SelfId()], keysConfig) - coreComponents := integrationTests.GetDefaultCoreComponents() + coreComponents := integrationTests.GetDefaultCoreComponents(integrationTests.CreateEnableEpochsConfig()) coreComponents.InternalMarshalizerField = integrationTests.TestMarshalizer coreComponents.TxMarshalizerField = integrationTests.TestTxSignMarshalizer coreComponents.HasherField = integrationTests.TestHasher diff --git a/integrationTests/node/getAccount/getAccount_test.go b/integrationTests/node/getAccount/getAccount_test.go index c3123a41b29..8f24706fff5 100644 --- a/integrationTests/node/getAccount/getAccount_test.go +++ b/integrationTests/node/getAccount/getAccount_test.go @@ -37,7 +37,7 @@ func TestNode_GetAccountAccountDoesNotExistsShouldRetEmpty(t *testing.T) { accDB, _ := integrationTests.CreateAccountsDB(0, trieStorage) rootHash, _ := accDB.Commit() - coreComponents := integrationTests.GetDefaultCoreComponents() + coreComponents := integrationTests.GetDefaultCoreComponents(integrationTests.CreateEnableEpochsConfig()) coreComponents.AddressPubKeyConverterField = integrationTests.TestAddressPubkeyConverter dataComponents := integrationTests.GetDefaultDataComponents() @@ -77,7 +77,7 @@ func TestNode_GetAccountAccountExistsShouldReturn(t *testing.T) { testPubkey := integrationTests.CreateAccount(accDB, testNonce, testBalance) rootHash, _ := accDB.Commit() - coreComponents := integrationTests.GetDefaultCoreComponents() + coreComponents := integrationTests.GetDefaultCoreComponents(integrationTests.CreateEnableEpochsConfig()) coreComponents.AddressPubKeyConverterField = testscommon.RealWorldBech32PubkeyConverter dataComponents := integrationTests.GetDefaultDataComponents() diff --git a/integrationTests/state/stateTrie/stateTrie_test.go b/integrationTests/state/stateTrie/stateTrie_test.go index f8a7bfae8c5..3bc5184767b 100644 --- a/integrationTests/state/stateTrie/stateTrie_test.go +++ b/integrationTests/state/stateTrie/stateTrie_test.go @@ -2,6 +2,7 @@ package stateTrie import ( "bytes" + "context" "encoding/base64" "encoding/binary" "encoding/hex" @@ -24,11 +25,13 @@ import ( "github.com/multiversx/mx-chain-core-go/hashing/sha256" crypto "github.com/multiversx/mx-chain-crypto-go" "github.com/multiversx/mx-chain-go/common" + "github.com/multiversx/mx-chain-go/common/errChan" "github.com/multiversx/mx-chain-go/config" "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/epochStart" "github.com/multiversx/mx-chain-go/integrationTests" "github.com/multiversx/mx-chain-go/integrationTests/mock" + esdtCommon "github.com/multiversx/mx-chain-go/integrationTests/vm/esdt" "github.com/multiversx/mx-chain-go/sharding" "github.com/multiversx/mx-chain-go/state" "github.com/multiversx/mx-chain-go/state/factory" @@ -2342,6 +2345,221 @@ func Test_SnapshotStateRemovesLastSnapshotStartedAfterSnapshotFinished(t *testin assert.NotNil(t, err) } +func TestMigrateDataTrieBuiltinFunc(t *testing.T) { + if testing.Short() { + t.Skip("this is not a short test") + } + + t.Run("migrate shard 0 system account", func(t *testing.T) { + shardId := byte(0) + nodes, idxProposers, nonce, round := startNodesAndIssueToken(t, 2, shardId) + defer func() { + for _, n := range nodes { + n.Close() + } + }() + + valuesBeforeMigration := getValuesFromAccount(t, nodes[shardId].AccntState, core.SystemAccountAddress) + migrateDataTrieBuiltInFunc(t, nodes, shardId, core.SystemAccountAddress, nonce, round, idxProposers) + valuesAfterMigration := getValuesFromAccount(t, nodes[shardId].AccntState, core.SystemAccountAddress) + + require.Equal(t, len(valuesBeforeMigration), len(valuesAfterMigration)) + require.True(t, len(valuesAfterMigration) > 0) + for i := range valuesBeforeMigration { + require.Equal(t, valuesBeforeMigration[i], valuesAfterMigration[i]) + } + }) + t.Run("migrate shard 0 user account", func(t *testing.T) { + shardId := byte(0) + nodes, idxProposers, nonce, round := startNodesAndIssueToken(t, 2, shardId) + defer func() { + for _, n := range nodes { + n.Close() + } + }() + + migrationAddress := nodes[shardId].OwnAccount.Address + valuesBeforeMigration := getValuesFromAccount(t, nodes[shardId].AccntState, migrationAddress) + migrateDataTrieBuiltInFunc(t, nodes, shardId, migrationAddress, nonce, round, idxProposers) + valuesAfterMigration := getValuesFromAccount(t, nodes[shardId].AccntState, migrationAddress) + + require.Equal(t, len(valuesBeforeMigration), len(valuesAfterMigration)) + require.True(t, len(valuesAfterMigration) > 0) + for i := range valuesBeforeMigration { + require.Equal(t, valuesBeforeMigration[i], valuesAfterMigration[i]) + } + }) + t.Run("migrate shard 1 system account", func(t *testing.T) { + shardId := byte(1) + nodes, idxProposers, nonce, round := startNodesAndIssueToken(t, 2, shardId) + defer func() { + for _, n := range nodes { + n.Close() + } + }() + + valuesBeforeMigration := getValuesFromAccount(t, nodes[shardId].AccntState, core.SystemAccountAddress) + migrateDataTrieBuiltInFunc(t, nodes, shardId, core.SystemAccountAddress, nonce, round, idxProposers) + valuesAfterMigration := getValuesFromAccount(t, nodes[shardId].AccntState, core.SystemAccountAddress) + + require.Equal(t, len(valuesBeforeMigration), len(valuesAfterMigration)) + require.True(t, len(valuesAfterMigration) > 0) + for i := range valuesBeforeMigration { + require.Equal(t, valuesBeforeMigration[i], valuesAfterMigration[i]) + } + }) + t.Run("migrate shard 1 user account", func(t *testing.T) { + shardId := byte(1) + nodes, idxProposers, nonce, round := startNodesAndIssueToken(t, 2, shardId) + defer func() { + for _, n := range nodes { + n.Close() + } + }() + + migrationAddress := nodes[shardId].OwnAccount.Address + valuesBeforeMigration := getValuesFromAccount(t, nodes[shardId].AccntState, migrationAddress) + migrateDataTrieBuiltInFunc(t, nodes, shardId, nodes[shardId].OwnAccount.Address, nonce, round, idxProposers) + valuesAfterMigration := getValuesFromAccount(t, nodes[shardId].AccntState, migrationAddress) + + require.Equal(t, len(valuesBeforeMigration), len(valuesAfterMigration)) + require.True(t, len(valuesAfterMigration) > 0) + for i := range valuesBeforeMigration { + require.Equal(t, valuesBeforeMigration[i], valuesAfterMigration[i]) + } + }) +} + +func getValuesFromAccount(t *testing.T, adb state.AccountsAdapter, address []byte) [][]byte { + account, err := adb.GetExistingAccount(address) + require.Nil(t, err) + + chLeaves := &common.TrieIteratorChannels{ + LeavesChan: make(chan core.KeyValueHolder, common.TrieLeavesChannelDefaultCapacity), + ErrChan: errChan.NewErrChanWrapper(), + } + err = account.(state.UserAccountHandler).GetAllLeaves(chLeaves, context.Background()) + require.Nil(t, err) + + values := make([][]byte, 0) + for leaf := range chLeaves.LeavesChan { + values = append(values, leaf.Value()) + } + + err = chLeaves.ErrChan.ReadFromChanNonBlocking() + require.Nil(t, err) + + return values +} + +func migrateDataTrieBuiltInFunc( + t *testing.T, + nodes []*integrationTests.TestProcessorNode, + shardId byte, + migrationAddress []byte, + nonce uint64, + round uint64, + idxProposers []int, +) { + require.True(t, nodes[shardId].EnableEpochsHandler.IsAutoBalanceDataTriesEnabled()) + isMigrated := getAddressMigrationStatus(t, nodes[shardId].AccntState, migrationAddress) + require.False(t, isMigrated) + + integrationTests.CreateAndSendTransactionWithSenderAccount(nodes[shardId], nodes, big.NewInt(0), nodes[shardId].OwnAccount, getDestAccountAddress(migrationAddress, shardId), core.BuiltInFunctionMigrateDataTrie, 1000000) + + time.Sleep(time.Second) + nrRoundsToPropagate := 5 + _, _ = integrationTests.WaitOperationToBeDone(t, nodes, nrRoundsToPropagate, nonce, round, idxProposers) + + isMigrated = getAddressMigrationStatus(t, nodes[shardId].AccntState, migrationAddress) + require.True(t, isMigrated) +} + +func startNodesAndIssueToken( + t *testing.T, + numOfShards int, + issuerShardId byte, +) ([]*integrationTests.TestProcessorNode, []int, uint64, uint64) { + nodesPerShard := 1 + numMetachainNodes := 1 + + enableEpochs := config.EnableEpochs{ + GlobalMintBurnDisableEpoch: integrationTests.UnreachableEpoch, + BuiltInFunctionOnMetaEnableEpoch: integrationTests.UnreachableEpoch, + OptimizeGasUsedInCrossMiniBlocksEnableEpoch: integrationTests.UnreachableEpoch, + ScheduledMiniBlocksEnableEpoch: integrationTests.UnreachableEpoch, + MiniBlockPartialExecutionEnableEpoch: integrationTests.UnreachableEpoch, + StakingV2EnableEpoch: integrationTests.UnreachableEpoch, + AutoBalanceDataTriesEnableEpoch: 1, + } + nodes := integrationTests.CreateNodesWithEnableEpochs( + numOfShards, + nodesPerShard, + numMetachainNodes, + enableEpochs, + ) + + roundsPerEpoch := uint64(5) + for _, node := range nodes { + node.EpochStartTrigger.SetRoundsPerEpoch(roundsPerEpoch) + } + + idxProposers := make([]int, numOfShards+1) + for i := 0; i < numOfShards; i++ { + idxProposers[i] = i * nodesPerShard + } + idxProposers[numOfShards] = numOfShards * nodesPerShard + + integrationTests.DisplayAndStartNodes(nodes) + + initialVal := int64(10000000000) + integrationTests.MintAllNodes(nodes, big.NewInt(initialVal)) + + round := uint64(0) + nonce := uint64(0) + round = integrationTests.IncrementAndPrintRound(round) + nonce++ + + // send token issue + initialSupply := int64(10000000000) + ticker := "TCK" + esdtCommon.IssueTestTokenWithIssuerAccount(nodes, nodes[issuerShardId].OwnAccount, initialSupply, ticker) + + time.Sleep(time.Second) + nrRoundsToPropagate := 8 + nonce, round = integrationTests.WaitOperationToBeDone(t, nodes, nrRoundsToPropagate, nonce, round, idxProposers) + time.Sleep(time.Second) + + tokenIdentifier := string(integrationTests.GetTokenIdentifier(nodes, []byte(ticker))) + + esdtCommon.CheckAddressHasTokens(t, nodes[issuerShardId].OwnAccount.Address, nodes, []byte(tokenIdentifier), 0, initialSupply) + + return nodes, idxProposers, nonce, round +} + +func getDestAccountAddress(migrationAddress []byte, shardId byte) []byte { + if bytes.Equal(migrationAddress, core.SystemAccountAddress) && shardId == 0 { + systemAccountAddress := bytes.Repeat([]byte{255}, 30) + systemAccountAddress = append(systemAccountAddress, []byte{0, 0}...) + return systemAccountAddress + } + + return migrationAddress +} + +func getAddressMigrationStatus(t *testing.T, adb state.AccountsAdapter, address []byte) bool { + account, err := adb.LoadAccount(address) + require.Nil(t, err) + + userAccount, ok := account.(state.UserAccountHandler) + require.True(t, ok) + + isMigrated, err := userAccount.DataTrie().IsMigratedToLatestVersion() + require.Nil(t, err) + + return isMigrated +} + func addDataTriesForAccountsStartingWithIndex( startIndex uint32, nbAccounts uint32, diff --git a/integrationTests/testConsensusNode.go b/integrationTests/testConsensusNode.go index f56720fd0a3..b6c223e6138 100644 --- a/integrationTests/testConsensusNode.go +++ b/integrationTests/testConsensusNode.go @@ -234,7 +234,7 @@ func (tcn *TestConsensusNode) initNode(args ArgsTestConsensusNode) { tcn.initAccountsDB() - coreComponents := GetDefaultCoreComponents() + coreComponents := GetDefaultCoreComponents(CreateEnableEpochsConfig()) coreComponents.SyncTimerField = syncer coreComponents.RoundHandlerField = roundHandler coreComponents.InternalMarshalizerField = TestMarshalizer diff --git a/integrationTests/testInitializer.go b/integrationTests/testInitializer.go index 6a72d118a50..340f6786985 100644 --- a/integrationTests/testInitializer.go +++ b/integrationTests/testInitializer.go @@ -643,7 +643,7 @@ func CreateFullGenesisBlocks( gasSchedule := wasmConfig.MakeGasMapForTests() defaults.FillGasMapInternal(gasSchedule, 1) - coreComponents := GetDefaultCoreComponents() + coreComponents := GetDefaultCoreComponents(enableEpochsConfig) coreComponents.InternalMarshalizerField = TestMarshalizer coreComponents.TxMarshalizerField = TestTxSignMarshalizer coreComponents.HasherField = TestHasher @@ -759,7 +759,7 @@ func CreateGenesisMetaBlock( gasSchedule := wasmConfig.MakeGasMapForTests() defaults.FillGasMapInternal(gasSchedule, 1) - coreComponents := GetDefaultCoreComponents() + coreComponents := GetDefaultCoreComponents(enableEpochsConfig) coreComponents.InternalMarshalizerField = marshalizer coreComponents.HasherField = hasher coreComponents.Uint64ByteSliceConverterField = uint64Converter @@ -2215,7 +2215,7 @@ func generateValidTx( _ = accnts.SaveAccount(acc) _, _ = accnts.Commit() - coreComponents := GetDefaultCoreComponents() + coreComponents := GetDefaultCoreComponents(CreateEnableEpochsConfig()) coreComponents.InternalMarshalizerField = TestMarshalizer coreComponents.TxMarshalizerField = TestTxSignMarshalizer coreComponents.VmMarshalizerField = TestMarshalizer diff --git a/integrationTests/testProcessorNode.go b/integrationTests/testProcessorNode.go index 875f2bb3cec..cb23b90ca8c 100644 --- a/integrationTests/testProcessorNode.go +++ b/integrationTests/testProcessorNode.go @@ -1247,7 +1247,7 @@ func (tpn *TestProcessorNode) initInterceptors(heartbeatPk string) { tpn.EpochStartNotifier = notifier.NewEpochStartSubscriptionHandler() } - coreComponents := GetDefaultCoreComponents() + coreComponents := GetDefaultCoreComponents(CreateEnableEpochsConfig()) coreComponents.InternalMarshalizerField = TestMarshalizer coreComponents.TxMarshalizerField = TestTxSignMarshalizer coreComponents.HasherField = TestHasher @@ -2166,7 +2166,7 @@ func (tpn *TestProcessorNode) initBlockProcessor(stateCheckpointModulus uint) { accountsDb[state.UserAccountsState] = tpn.AccntState accountsDb[state.PeerAccountsState] = tpn.PeerState - coreComponents := GetDefaultCoreComponents() + coreComponents := GetDefaultCoreComponents(CreateEnableEpochsConfig()) coreComponents.InternalMarshalizerField = TestMarshalizer coreComponents.HasherField = TestHasher coreComponents.Uint64ByteSliceConverterField = TestUint64Converter @@ -2439,7 +2439,7 @@ func (tpn *TestProcessorNode) initNode() { AppStatusHandlerField: tpn.AppStatusHandler, } - coreComponents := GetDefaultCoreComponents() + coreComponents := GetDefaultCoreComponents(CreateEnableEpochsConfig()) coreComponents.InternalMarshalizerField = TestMarshalizer coreComponents.VmMarshalizerField = TestVmMarshalizer coreComponents.TxMarshalizerField = TestTxSignMarshalizer @@ -3237,10 +3237,9 @@ func CreateEnableEpochsConfig() config.EnableEpochs { } // GetDefaultCoreComponents - -func GetDefaultCoreComponents() *mock.CoreComponentsStub { - enableEpochsCfg := CreateEnableEpochsConfig() +func GetDefaultCoreComponents(enableEpochsConfig config.EnableEpochs) *mock.CoreComponentsStub { genericEpochNotifier := forking.NewGenericEpochNotifier() - enableEpochsHandler, _ := enablers.NewEnableEpochsHandler(enableEpochsCfg, genericEpochNotifier) + enableEpochsHandler, _ := enablers.NewEnableEpochsHandler(enableEpochsConfig, genericEpochNotifier) return &mock.CoreComponentsStub{ InternalMarshalizerField: TestMarshalizer, diff --git a/integrationTests/testSyncNode.go b/integrationTests/testSyncNode.go index 1dfea4958b2..d617a4b7a56 100644 --- a/integrationTests/testSyncNode.go +++ b/integrationTests/testSyncNode.go @@ -44,7 +44,7 @@ func (tpn *TestProcessorNode) initBlockProcessorWithSync() { accountsDb[state.UserAccountsState] = tpn.AccntState accountsDb[state.PeerAccountsState] = tpn.PeerState - coreComponents := GetDefaultCoreComponents() + coreComponents := GetDefaultCoreComponents(CreateEnableEpochsConfig()) coreComponents.InternalMarshalizerField = TestMarshalizer coreComponents.HasherField = TestHasher coreComponents.Uint64ByteSliceConverterField = TestUint64Converter diff --git a/integrationTests/vm/txsFee/migrateDataTrie_test.go b/integrationTests/vm/txsFee/migrateDataTrie_test.go index a4bc4ad1e0f..9c62a4f30fd 100644 --- a/integrationTests/vm/txsFee/migrateDataTrie_test.go +++ b/integrationTests/vm/txsFee/migrateDataTrie_test.go @@ -215,7 +215,8 @@ func generateDataTrie( for i := 1; i < numLeaves; i++ { key := keyGenerator(i) - err := tr.UpdateWithVersion(key, key, core.NotSpecified) + value := getValWithAppendedData(key, key, accAddr) + err := tr.UpdateWithVersion(key, value, core.NotSpecified) require.Nil(t, err) keys[i] = key @@ -226,6 +227,13 @@ func generateDataTrie( return rootHash, keys } +func getValWithAppendedData(key, val, address []byte) []byte { + suffix := append(key, address...) + val = append(val, suffix...) + + return val +} + func initDataTrie( t *testing.T, testContext *vm.VMTestContext, diff --git a/integrationTests/vm/wasm/wasmvm/mockContracts.go b/integrationTests/vm/wasm/wasmvm/mockContracts.go index 21c6e6cae55..e8478768cbc 100644 --- a/integrationTests/vm/wasm/wasmvm/mockContracts.go +++ b/integrationTests/vm/wasm/wasmvm/mockContracts.go @@ -17,9 +17,9 @@ import ( "github.com/multiversx/mx-chain-go/testscommon/hashingMocks" "github.com/multiversx/mx-chain-go/testscommon/marshallerMock" "github.com/multiversx/mx-chain-go/testscommon/txDataBuilder" + "github.com/multiversx/mx-chain-scenario-go/worldmock" "github.com/multiversx/mx-chain-vm-go/executor" contextmock "github.com/multiversx/mx-chain-vm-go/mock/context" - worldmock "github.com/multiversx/mx-chain-vm-go/mock/world" "github.com/multiversx/mx-chain-vm-go/testcommon" "github.com/multiversx/mx-chain-vm-go/vmhost" "github.com/stretchr/testify/require" diff --git a/integrationTests/vm/wasm/wasmvm/scenariosConverter/scenariosConverter.go b/integrationTests/vm/wasm/wasmvm/scenariosConverter/scenariosConverter.go index 64a8bde201f..36a4fb8e51b 100644 --- a/integrationTests/vm/wasm/wasmvm/scenariosConverter/scenariosConverter.go +++ b/integrationTests/vm/wasm/wasmvm/scenariosConverter/scenariosConverter.go @@ -10,15 +10,15 @@ import ( "github.com/multiversx/mx-chain-go/process/factory" "github.com/multiversx/mx-chain-go/state" "github.com/multiversx/mx-chain-go/testscommon/txDataBuilder" - mge "github.com/multiversx/mx-chain-scenario-go/scenario-exporter" - mgutil "github.com/multiversx/mx-chain-scenario-go/util" + "github.com/multiversx/mx-chain-scenario-go/scenario/exporter" + scenmodel "github.com/multiversx/mx-chain-scenario-go/scenario/model" vmcommon "github.com/multiversx/mx-chain-vm-common-go" ) var errReturnCodeNotOk = errors.New("returnCode is not 0(Ok)") // CreateAccountsFromScenariosAccs uses scenariosAccounts to populate the AccountsAdapter -func CreateAccountsFromScenariosAccs(tc *vm.VMTestContext, scenariosUserAccounts []*mge.TestAccount) error { +func CreateAccountsFromScenariosAccs(tc *vm.VMTestContext, scenariosUserAccounts []*exporter.TestAccount) error { for _, scenariosAcc := range scenariosUserAccounts { acc, err := tc.Accounts.LoadAccount(scenariosAcc.GetAddress()) if err != nil { @@ -60,7 +60,7 @@ func CreateAccountsFromScenariosAccs(tc *vm.VMTestContext, scenariosUserAccounts } // CreateTransactionsFromScenariosTxs converts scenarios transactions intro trasnsactions that can be processed by the txProcessor -func CreateTransactionsFromScenariosTxs(scenariosTxs []*mge.Transaction) (transactions []*transaction.Transaction) { +func CreateTransactionsFromScenariosTxs(scenariosTxs []*exporter.Transaction) (transactions []*transaction.Transaction) { var data []byte transactions = make([]*transaction.Transaction, 0) @@ -70,7 +70,7 @@ func CreateTransactionsFromScenariosTxs(scenariosTxs []*mge.Transaction) (transa endpointName := scenariosTx.GetCallFunction() args := scenariosTx.GetCallArguments() if len(esdtTransfers) != 0 { - data = mgutil.CreateMultiTransferData(scenariosTx.GetReceiverAddress(), esdtTransfers, endpointName, args) + data = scenmodel.CreateMultiTransferData(scenariosTx.GetReceiverAddress(), esdtTransfers, endpointName, args) } else { data = createData(endpointName, args) } @@ -92,7 +92,7 @@ func CreateTransactionsFromScenariosTxs(scenariosTxs []*mge.Transaction) (transa } // DeploySCsFromScenariosDeployTxs deploys all smartContracts correspondent to "scDeploy" in a scenarios test, then replaces with the correct computed address in all the transactions. -func DeploySCsFromScenariosDeployTxs(testContext *vm.VMTestContext, deployScenariosTxs []*mge.Transaction) ([][]byte, error) { +func DeploySCsFromScenariosDeployTxs(testContext *vm.VMTestContext, deployScenariosTxs []*exporter.Transaction) ([][]byte, error) { newScAddresses := make([][]byte, 0) for _, deployScenariosTransaction := range deployScenariosTxs { deployedScAddress, err := deploySC(testContext, deployScenariosTransaction) @@ -105,7 +105,7 @@ func DeploySCsFromScenariosDeployTxs(testContext *vm.VMTestContext, deployScenar } // ReplaceScenariosScAddressesWithNewScAddresses corrects the Scenarios SC Addresses, with the new Addresses obtained from deploying the SCs -func ReplaceScenariosScAddressesWithNewScAddresses(deployedScAccounts []*mge.TestAccount, newScAddresses [][]byte, scenariosTxs []*mge.Transaction) { +func ReplaceScenariosScAddressesWithNewScAddresses(deployedScAccounts []*exporter.TestAccount, newScAddresses [][]byte, scenariosTxs []*exporter.Transaction) { for _, newScAddr := range newScAddresses { addressToBeReplaced := deployedScAccounts[0].GetAddress() for _, scenariosTx := range scenariosTxs { @@ -126,7 +126,7 @@ func createData(functionName string, arguments [][]byte) []byte { return builder.ToBytes() } -func deploySC(testContext *vm.VMTestContext, deployScenariosTx *mge.Transaction) (scAddress []byte, err error) { +func deploySC(testContext *vm.VMTestContext, deployScenariosTx *exporter.Transaction) (scAddress []byte, err error) { gasLimit, gasPrice := deployScenariosTx.GetGasLimitAndPrice() ownerAddr := deployScenariosTx.GetSenderAddress() deployData := deployScenariosTx.GetDeployData() diff --git a/integrationTests/vm/wasm/wasmvm/scenariosConverter/scenariosConverterUtils.go b/integrationTests/vm/wasm/wasmvm/scenariosConverter/scenariosConverterUtils.go index a701d090e95..2d3d15f681d 100644 --- a/integrationTests/vm/wasm/wasmvm/scenariosConverter/scenariosConverterUtils.go +++ b/integrationTests/vm/wasm/wasmvm/scenariosConverter/scenariosConverterUtils.go @@ -8,8 +8,8 @@ import ( "github.com/multiversx/mx-chain-go/integrationTests/vm" "github.com/multiversx/mx-chain-go/state" logger "github.com/multiversx/mx-chain-logger-go" - mge "github.com/multiversx/mx-chain-scenario-go/scenario-exporter" - mgutil "github.com/multiversx/mx-chain-scenario-go/util" + "github.com/multiversx/mx-chain-scenario-go/scenario/exporter" + scenmodel "github.com/multiversx/mx-chain-scenario-go/scenario/model" vmcommon "github.com/multiversx/mx-chain-vm-common-go" "github.com/stretchr/testify/require" ) @@ -17,7 +17,7 @@ import ( var log = logger.GetOrCreate("scenariosConverter") // CheckAccounts will verify if scenariosAccounts correspond to AccountsAdapter accounts -func CheckAccounts(t *testing.T, accAdapter state.AccountsAdapter, scenariosAccounts []*mge.TestAccount) { +func CheckAccounts(t *testing.T, accAdapter state.AccountsAdapter, scenariosAccounts []*exporter.TestAccount) { for _, scenariosAcc := range scenariosAccounts { accHandler, err := accAdapter.LoadAccount(scenariosAcc.GetAddress()) require.Nil(t, err) @@ -56,7 +56,7 @@ func CheckStorage(t *testing.T, dataTrie state.UserAccountHandler, scenariosAccS } // CheckTransactions checks if the transactions correspond with the scenariosTransactions -func CheckTransactions(t *testing.T, transactions []*transaction.Transaction, scenariosTransactions []*mge.Transaction) { +func CheckTransactions(t *testing.T, transactions []*transaction.Transaction, scenariosTransactions []*exporter.Transaction) { expectedLength := len(scenariosTransactions) require.Equal(t, expectedLength, len(transactions)) for i := 0; i < expectedLength; i++ { @@ -77,7 +77,7 @@ func CheckTransactions(t *testing.T, transactions []*transaction.Transaction, sc var expectedData []byte if len(expectedEsdtTransfers) != 0 { - expectedData = mgutil.CreateMultiTransferData(expectedReceiver, expectedEsdtTransfers, expectedCallFunction, expectedCallArguments) + expectedData = scenmodel.CreateMultiTransferData(expectedReceiver, expectedEsdtTransfers, expectedCallFunction, expectedCallArguments) require.Equal(t, expectedSender, transactions[i].GetRcvAddr()) } else { require.Equal(t, expectedReceiver, transactions[i].GetRcvAddr()) @@ -97,7 +97,7 @@ func BenchmarkScenariosSpecificTx(b *testing.B, scenariosTestPath string) { return } defer testContext.Close() - if benchmarkTxPos == mge.InvalidBenchmarkTxPos { + if benchmarkTxPos == exporter.InvalidBenchmarkTxPos { log.Trace("no transactions marked for benchmarking") } if len(transactions) > 1 { @@ -115,21 +115,21 @@ func BenchmarkScenariosSpecificTx(b *testing.B, scenariosTestPath string) { // SetStateFromScenariosTest recieves path to scenariosTest, returns a VMTestContext with the specified accounts, an array with the specified transactions and an error func SetStateFromScenariosTest(scenariosTestPath string) (testContext *vm.VMTestContext, transactions []*transaction.Transaction, bechmarkTxPos int, err error) { - stateAndBenchmarkInfo, err := mge.GetAccountsAndTransactionsFromScenarios(scenariosTestPath) + stateAndBenchmarkInfo, err := exporter.GetAccountsAndTransactionsFromScenarios(scenariosTestPath) if err != nil { - return nil, nil, mge.InvalidBenchmarkTxPos, err + return nil, nil, exporter.InvalidBenchmarkTxPos, err } testContext, err = vm.CreatePreparedTxProcessorWithVMs(config.EnableEpochs{}) if err != nil { - return nil, nil, mge.InvalidBenchmarkTxPos, err + return nil, nil, exporter.InvalidBenchmarkTxPos, err } err = CreateAccountsFromScenariosAccs(testContext, stateAndBenchmarkInfo.Accs) if err != nil { - return nil, nil, mge.InvalidBenchmarkTxPos, err + return nil, nil, exporter.InvalidBenchmarkTxPos, err } newAddresses, err := DeploySCsFromScenariosDeployTxs(testContext, stateAndBenchmarkInfo.DeployTxs) if err != nil { - return nil, nil, mge.InvalidBenchmarkTxPos, err + return nil, nil, exporter.InvalidBenchmarkTxPos, err } ReplaceScenariosScAddressesWithNewScAddresses(stateAndBenchmarkInfo.DeployedAccs, newAddresses, stateAndBenchmarkInfo.Txs) transactions = CreateTransactionsFromScenariosTxs(stateAndBenchmarkInfo.Txs) @@ -138,7 +138,7 @@ func SetStateFromScenariosTest(scenariosTestPath string) (testContext *vm.VMTest // CheckConverter - func CheckConverter(t *testing.T, scenariosTestPath string) { - stateAndBenchmarkInfo, err := mge.GetAccountsAndTransactionsFromScenarios(scenariosTestPath) + stateAndBenchmarkInfo, err := exporter.GetAccountsAndTransactionsFromScenarios(scenariosTestPath) require.Nil(t, err) testContext, err := vm.CreatePreparedTxProcessorWithVMs(config.EnableEpochs{}) require.Nil(t, err) diff --git a/state/accountsDB_test.go b/state/accountsDB_test.go index 43c82853c65..95785e9c231 100644 --- a/state/accountsDB_test.go +++ b/state/accountsDB_test.go @@ -41,6 +41,7 @@ import ( "github.com/multiversx/mx-chain-go/trie" "github.com/multiversx/mx-chain-go/trie/hashesHolder" vmcommon "github.com/multiversx/mx-chain-vm-common-go" + "github.com/multiversx/mx-chain-vm-common-go/dataTrieMigrator" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -97,19 +98,20 @@ func generateAddressAccountAccountsDB(trie common.Trie) ([]byte, *stateMock.Acco func getDefaultTrieAndAccountsDb() (common.Trie, *state.AccountsDB) { checkpointHashesHolder := hashesHolder.NewCheckpointHashesHolder(10000000, testscommon.HashSize) - adb, tr, _ := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock()) + adb, tr, _ := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock(), &enableEpochsHandlerMock.EnableEpochsHandlerStub{}) return tr, adb } func getDefaultTrieAndAccountsDbWithCustomDB(db common.BaseStorer) (common.Trie, *state.AccountsDB) { checkpointHashesHolder := hashesHolder.NewCheckpointHashesHolder(10000000, testscommon.HashSize) - adb, tr, _ := getDefaultStateComponents(checkpointHashesHolder, db) + adb, tr, _ := getDefaultStateComponents(checkpointHashesHolder, db, &enableEpochsHandlerMock.EnableEpochsHandlerStub{}) return tr, adb } func getDefaultStateComponents( hashesHolder trie.CheckpointHashesHolder, db common.BaseStorer, + enableEpochsHandler common.EnableEpochsHandler, ) (*state.AccountsDB, common.Trie, common.StorageManager) { generalCfg := config.TrieStorageManagerConfig{ PruningBufferLen: 1000, @@ -123,7 +125,7 @@ func getDefaultStateComponents( args.MainStorer = db args.CheckpointHashesHolder = hashesHolder trieStorage, _ := trie.NewTrieStorageManager(args) - tr, _ := trie.NewTrie(trieStorage, marshaller, hasher, &enableEpochsHandlerMock.EnableEpochsHandlerStub{}, 5) + tr, _ := trie.NewTrie(trieStorage, marshaller, hasher, enableEpochsHandler, 5) ewlArgs := evictionWaitingList.MemoryEvictionWaitingListArgs{ RootHashesSize: 100, HashesSize: 10000, @@ -133,7 +135,7 @@ func getDefaultStateComponents( argsAccCreator := factory.ArgsAccountCreator{ Hasher: hasher, Marshaller: marshaller, - EnableEpochsHandler: &enableEpochsHandlerMock.EnableEpochsHandlerStub{}, + EnableEpochsHandler: enableEpochsHandler, } accCreator, _ := factory.NewAccountCreator(argsAccCreator) @@ -2056,7 +2058,7 @@ func TestAccountsDB_CommitAddsDirtyHashesToCheckpointHashesHolder(t *testing.T) }, } - adb, tr, _ := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock()) + adb, tr, _ := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock(), &enableEpochsHandlerMock.EnableEpochsHandlerStub{}) accountsAddresses := generateAccounts(t, 3, adb) newHashes, _ = tr.GetDirtyHashes() @@ -2099,7 +2101,7 @@ func TestAccountsDB_CommitSetsStateCheckpointIfCheckpointHashesHolderIsFull(t *t }, } - adb, tr, trieStorage := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock()) + adb, tr, trieStorage := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock(), &enableEpochsHandlerMock.EnableEpochsHandlerStub{}) accountsAddresses := generateAccounts(t, 3, adb) newHashes = modifyDataTries(t, accountsAddresses, adb) @@ -2129,7 +2131,7 @@ func TestAccountsDB_SnapshotStateCleansCheckpointHashesHolder(t *testing.T) { return false }, } - adb, tr, trieStorage := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock()) + adb, tr, trieStorage := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock(), &enableEpochsHandlerMock.EnableEpochsHandlerStub{}) _ = trieStorage.Put([]byte(common.ActiveDBKey), []byte(common.ActiveDBVal)) accountsAddresses := generateAccounts(t, 3, adb) @@ -2150,7 +2152,7 @@ func TestAccountsDB_SetStateCheckpointCommitsOnlyMissingData(t *testing.T) { t.Parallel() checkpointHashesHolder := hashesHolder.NewCheckpointHashesHolder(100000, testscommon.HashSize) - adb, tr, trieStorage := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock()) + adb, tr, trieStorage := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock(), &enableEpochsHandlerMock.EnableEpochsHandlerStub{}) accountsAddresses := generateAccounts(t, 3, adb) rootHash, _ := tr.RootHash() @@ -2227,7 +2229,7 @@ func TestAccountsDB_CheckpointHashesHolderReceivesOnly32BytesData(t *testing.T) return false }, } - adb, _, _ := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock()) + adb, _, _ := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock(), &enableEpochsHandlerMock.EnableEpochsHandlerStub{}) accountsAddresses := generateAccounts(t, 3, adb) _ = modifyDataTries(t, accountsAddresses, adb) @@ -2248,7 +2250,7 @@ func TestAccountsDB_PruneRemovesDataFromCheckpointHashesHolder(t *testing.T) { removeCalled++ }, } - adb, tr, _ := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock()) + adb, tr, _ := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock(), &enableEpochsHandlerMock.EnableEpochsHandlerStub{}) accountsAddresses := generateAccounts(t, 3, adb) newHashes, _ = tr.GetDirtyHashes() @@ -3221,6 +3223,55 @@ func testAccountMethodsConcurrency( wg.Wait() } +func TestAccountsDB_MigrateDataTrieWithFunc(t *testing.T) { + t.Parallel() + + checkpointHashesHolder := hashesHolder.NewCheckpointHashesHolder(10000000, testscommon.HashSize) + enabeEpochsHandler := &enableEpochsHandlerMock.EnableEpochsHandlerStub{ + IsAutoBalanceDataTriesEnabledField: false, + } + adb, _, _ := getDefaultStateComponents(checkpointHashesHolder, testscommon.NewSnapshotPruningStorerMock(), enabeEpochsHandler) + + addr := []byte("addr") + acc, _ := adb.LoadAccount(addr) + value := []byte("value") + _ = acc.(state.UserAccountHandler).SaveKeyValue([]byte("key"), value) + _ = acc.(state.UserAccountHandler).SaveKeyValue([]byte("key2"), value) + _ = adb.SaveAccount(acc) + + enabeEpochsHandler.IsAutoBalanceDataTriesEnabledField = true + acc, _ = adb.LoadAccount(addr) + + isMigrated, err := acc.(state.AccountHandlerWithDataTrieMigrationStatus).IsDataTrieMigrated() + assert.Nil(t, err) + assert.False(t, isMigrated) + + accWithMigrate := acc.(vmcommon.UserAccountHandler).AccountDataHandler() + dataTrieMig := dataTrieMigrator.NewDataTrieMigrator(dataTrieMigrator.ArgsNewDataTrieMigrator{ + GasProvided: 100000000, + DataTrieGasCost: dataTrieMigrator.DataTrieGasCost{ + TrieLoadPerNode: 1, + TrieStorePerNode: 1, + }, + }) + err = accWithMigrate.MigrateDataTrieLeaves(vmcommon.ArgsMigrateDataTrieLeaves{ + OldVersion: core.NotSpecified, + NewVersion: core.AutoBalanceEnabled, + TrieMigrator: dataTrieMig, + }) + assert.Nil(t, err) + _ = adb.SaveAccount(acc) + + acc, _ = adb.LoadAccount(addr) + retrievedVal, _, err := acc.(state.UserAccountHandler).RetrieveValue([]byte("key")) + assert.Equal(t, value, retrievedVal) + assert.Nil(t, err) + + isMigrated, err = acc.(state.AccountHandlerWithDataTrieMigrationStatus).IsDataTrieMigrated() + assert.Nil(t, err) + assert.True(t, isMigrated) +} + func BenchmarkAccountsDB_GetMethodsInParallel(b *testing.B) { _, adb := getDefaultTrieAndAccountsDb() diff --git a/state/export_test.go b/state/export_test.go index 43810db3749..b9fc6b2f4cd 100644 --- a/state/export_test.go +++ b/state/export_test.go @@ -111,3 +111,9 @@ func (sm *snapshotsManager) WaitForStorageEpochChange(args storageEpochChangeWai func NewNilSnapshotsManager() *snapshotsManager { return nil } + +// AccountHandlerWithDataTrieMigrationStatus - +type AccountHandlerWithDataTrieMigrationStatus interface { + vmcommon.AccountHandler + IsDataTrieMigrated() (bool, error) +} diff --git a/state/trackableDataTrie/trackableDataTrie.go b/state/trackableDataTrie/trackableDataTrie.go index 4f7607a1980..8a2fe8812ef 100644 --- a/state/trackableDataTrie/trackableDataTrie.go +++ b/state/trackableDataTrie/trackableDataTrie.go @@ -127,8 +127,13 @@ func (tdt *trackableDataTrie) MigrateDataTrieLeaves(args vmcommon.ArgsMigrateDat dataToBeMigrated := args.TrieMigrator.GetLeavesToBeMigrated() log.Debug("num leaves to be migrated", "num", len(dataToBeMigrated), "account", tdt.identifier) for _, leafData := range dataToBeMigrated { + val, err := tdt.getValueWithoutMetadata(leafData.Key, leafData) + if err != nil { + return err + } + dataEntry := dirtyData{ - value: leafData.Value, + value: val, newVersion: args.NewVersion, } diff --git a/state/trackableDataTrie/trackableDataTrie_test.go b/state/trackableDataTrie/trackableDataTrie_test.go index 23fc69d7404..42f6ebc4189 100644 --- a/state/trackableDataTrie/trackableDataTrie_test.go +++ b/state/trackableDataTrie/trackableDataTrie_test.go @@ -827,20 +827,22 @@ func TestTrackableDataTrie_MigrateDataTrieLeaves(t *testing.T) { t.Run("leaves that need to be migrated are added to dirty data", func(t *testing.T) { t.Parallel() + expectedValues := [][]byte{[]byte("value1"), []byte("value2"), []byte("value3")} + address := []byte("identifier") leavesToBeMigrated := []core.TrieData{ { Key: []byte("key1"), - Value: []byte("value1"), + Value: append([]byte("value1key1"), address...), Version: core.NotSpecified, }, { Key: []byte("key2"), - Value: []byte("value2"), + Value: append([]byte("value2key2"), address...), Version: core.NotSpecified, }, { Key: []byte("key3"), - Value: []byte("value3"), + Value: append([]byte("value3key3"), address...), Version: core.NotSpecified, }, } @@ -858,7 +860,7 @@ func TestTrackableDataTrie_MigrateDataTrieLeaves(t *testing.T) { IsAutoBalanceDataTriesEnabledField: true, } - tdt, _ := trackableDataTrie.NewTrackableDataTrie([]byte("identifier"), &hashingMocks.HasherMock{}, &marshallerMock.MarshalizerMock{}, enableEpchs) + tdt, _ := trackableDataTrie.NewTrackableDataTrie(address, &hashingMocks.HasherMock{}, &marshallerMock.MarshalizerMock{}, enableEpchs) tdt.SetDataTrie(tr) args := vmcommon.ArgsMigrateDataTrieLeaves{ OldVersion: core.NotSpecified, @@ -872,7 +874,7 @@ func TestTrackableDataTrie_MigrateDataTrieLeaves(t *testing.T) { assert.Equal(t, len(leavesToBeMigrated), len(dirtyData)) for i := range leavesToBeMigrated { d := dirtyData[string(leavesToBeMigrated[i].Key)] - assert.Equal(t, leavesToBeMigrated[i].Value, d.Value) + assert.Equal(t, expectedValues[i], d.Value) assert.Equal(t, core.TrieNodeVersion(100), d.NewVersion) } })