diff --git a/common/holders/rootHashHolder.go b/common/holders/rootHashHolder.go index 68f2a295a1b..47be1787feb 100644 --- a/common/holders/rootHashHolder.go +++ b/common/holders/rootHashHolder.go @@ -1,6 +1,7 @@ package holders import ( + "encoding/hex" "fmt" "github.com/multiversx/mx-chain-core-go/core" @@ -19,6 +20,14 @@ func NewRootHashHolder(rootHash []byte, epoch core.OptionalUint32) *rootHashHold } } +// NewDefaultRootHashesHolder creates a rootHashHolder without an epoch set +func NewDefaultRootHashesHolder(rootHash []byte) *rootHashHolder { + return &rootHashHolder{ + rootHash: rootHash, + epoch: core.OptionalUint32{}, + } +} + // NewRootHashHolderAsEmpty creates an empty rootHashHolder func NewRootHashHolderAsEmpty() *rootHashHolder { return &rootHashHolder{ @@ -39,7 +48,7 @@ func (holder *rootHashHolder) GetEpoch() core.OptionalUint32 { // String returns rootHashesHolder as a string func (holder *rootHashHolder) String() string { - return fmt.Sprintf("root hash %s, epoch %v, has value %v", holder.rootHash, holder.epoch.Value, holder.epoch.HasValue) + return fmt.Sprintf("root hash %s, epoch %v, has value %v", hex.EncodeToString(holder.rootHash), holder.epoch.Value, holder.epoch.HasValue) } // IsInterfaceNil returns true if there is no value under the interface diff --git a/common/holders/rootHashHolder_test.go b/common/holders/rootHashHolder_test.go index 645e73c0551..07e50675d29 100644 --- a/common/holders/rootHashHolder_test.go +++ b/common/holders/rootHashHolder_test.go @@ -1,6 +1,7 @@ package holders import ( + "encoding/hex" "testing" "github.com/multiversx/mx-chain-core-go/core" @@ -32,7 +33,8 @@ func TestNewRootHashHolder_String(t *testing.T) { HasValue: true, }, ) - expectedString := "root hash rootHash, epoch 5, has value true" + hexRootHash := hex.EncodeToString([]byte("rootHash")) + expectedString := "root hash " + hexRootHash + ", epoch 5, has value true" assert.Equal(t, expectedString, holder.String()) } diff --git a/common/interface.go b/common/interface.go index 73238c66e8c..82b2960d0ce 100644 --- a/common/interface.go +++ b/common/interface.go @@ -42,8 +42,7 @@ type Trie interface { Delete(key []byte) error RootHash() ([]byte, error) Commit() error - Recreate(root []byte) (Trie, error) - RecreateFromEpoch(options RootHashHolder) (Trie, error) + Recreate(options RootHashHolder) (Trie, error) String() string GetObsoleteHashes() [][]byte GetDirtyHashes() (ModifiedHashes, error) diff --git a/epochStart/bootstrap/disabled/disabledAccountsAdapter.go b/epochStart/bootstrap/disabled/disabledAccountsAdapter.go index 066c9e32866..c44dae7ae48 100644 --- a/epochStart/bootstrap/disabled/disabledAccountsAdapter.go +++ b/epochStart/bootstrap/disabled/disabledAccountsAdapter.go @@ -86,12 +86,7 @@ func (a *accountsAdapter) RootHash() ([]byte, error) { } // RecreateTrie - -func (a *accountsAdapter) RecreateTrie(_ []byte) error { - return nil -} - -// RecreateTrieFromEpoch - -func (a *accountsAdapter) RecreateTrieFromEpoch(_ common.RootHashHolder) error { +func (a *accountsAdapter) RecreateTrie(_ common.RootHashHolder) error { return nil } diff --git a/genesis/process/metaGenesisBlockCreator.go b/genesis/process/metaGenesisBlockCreator.go index de3500d2e2f..f695c274b42 100644 --- a/genesis/process/metaGenesisBlockCreator.go +++ b/genesis/process/metaGenesisBlockCreator.go @@ -19,6 +19,7 @@ import ( disabledCommon "github.com/multiversx/mx-chain-go/common/disabled" "github.com/multiversx/mx-chain-go/common/enablers" "github.com/multiversx/mx-chain-go/common/forking" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/config" "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/dataRetriever/blockchain" @@ -190,7 +191,8 @@ func createMetaGenesisBlockAfterHardFork( return nil, nil, nil, process.ErrWrongTypeAssertion } - err = arg.Accounts.RecreateTrie(hdrHandler.GetRootHash()) + rootHashHolder := holders.NewDefaultRootHashesHolder(hdrHandler.GetRootHash()) + err = arg.Accounts.RecreateTrie(rootHashHolder) if err != nil { return nil, nil, nil, err } diff --git a/genesis/process/shardGenesisBlockCreator.go b/genesis/process/shardGenesisBlockCreator.go index 3c7e47070c7..730f196cc37 100644 --- a/genesis/process/shardGenesisBlockCreator.go +++ b/genesis/process/shardGenesisBlockCreator.go @@ -15,6 +15,7 @@ import ( disabledCommon "github.com/multiversx/mx-chain-go/common/disabled" "github.com/multiversx/mx-chain-go/common/enablers" "github.com/multiversx/mx-chain-go/common/forking" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/config" "github.com/multiversx/mx-chain-go/dataRetriever/blockchain" "github.com/multiversx/mx-chain-go/genesis" @@ -242,7 +243,8 @@ func createShardGenesisBlockAfterHardFork( return nil, nil, nil, err } - err = arg.Accounts.RecreateTrie(hdrHandler.GetRootHash()) + rootHashHolder := holders.NewDefaultRootHashesHolder(hdrHandler.GetRootHash()) + err = arg.Accounts.RecreateTrie(rootHashHolder) if err != nil { return nil, nil, nil, err } diff --git a/integrationTests/benchmarks/loadFromTrie_test.go b/integrationTests/benchmarks/loadFromTrie_test.go index 576326bbc0d..866368e7cf4 100644 --- a/integrationTests/benchmarks/loadFromTrie_test.go +++ b/integrationTests/benchmarks/loadFromTrie_test.go @@ -10,6 +10,7 @@ import ( "github.com/multiversx/mx-chain-core-go/marshal" "github.com/multiversx/mx-chain-go/common" disabledStatistics "github.com/multiversx/mx-chain-go/common/statistics/disabled" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/integrationTests" "github.com/multiversx/mx-chain-go/storage" "github.com/multiversx/mx-chain-go/storage/database" @@ -67,7 +68,7 @@ func testTrieLoadTime(t *testing.T, numChildrenPerBranch int, numTries int, maxT func timeTrieRecreate(tries []*keyForTrie, depth int) { startTime := time.Now() for j := range tries { - _, _ = tries[j].tr.Recreate(tries[j].key) + _, _ = tries[j].tr.Recreate(holders.NewDefaultRootHashesHolder(tries[j].key)) } duration := time.Since(startTime) fmt.Printf("trie with depth %d, duration %d \n", depth, duration.Nanoseconds()/int64(len(tries))) @@ -104,7 +105,7 @@ func generateTriesWithMaxDepth( key := insertKeysIntoTrie(t, tr, numTrieLevels, numChildrenPerBranch) rootHash, _ := tr.RootHash() - collapsedTrie, _ := tr.Recreate(rootHash) + collapsedTrie, _ := tr.Recreate(holders.NewDefaultRootHashesHolder(rootHash)) if numTrieLevels == 1 { key = rootHash diff --git a/integrationTests/state/stateTrie/stateTrie_test.go b/integrationTests/state/stateTrie/stateTrie_test.go index 688adc61353..12ec5115d28 100644 --- a/integrationTests/state/stateTrie/stateTrie_test.go +++ b/integrationTests/state/stateTrie/stateTrie_test.go @@ -26,6 +26,7 @@ import ( 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/common/holders" "github.com/multiversx/mx-chain-go/common/statistics" "github.com/multiversx/mx-chain-go/config" "github.com/multiversx/mx-chain-go/dataRetriever" @@ -245,7 +246,7 @@ func TestAccountsDB_CommitTwoOkAccountsShouldWork(t *testing.T) { // reloading a new trie to test if data is inside rootHash, err = adb.RootHash() require.Nil(t, err) - err = adb.RecreateTrie(rootHash) + err = adb.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) require.Nil(t, err) // checking state1 @@ -282,7 +283,7 @@ func TestTrieDB_RecreateFromStorageShouldWork(t *testing.T) { err := tr1.Commit() require.Nil(t, err) - tr2, err := tr1.Recreate(h1) + tr2, err := tr1.Recreate(holders.NewDefaultRootHashesHolder(h1)) require.Nil(t, err) valRecov, _, err := tr2.Get(key) @@ -332,7 +333,7 @@ func TestAccountsDB_CommitTwoOkAccountsWithRecreationFromStorageShouldWork(t *te fmt.Printf("data committed! Root: %v\n", base64.StdEncoding.EncodeToString(rootHash)) // reloading a new trie to test if data is inside - err = adb.RecreateTrie(h) + err = adb.RecreateTrie(holders.NewDefaultRootHashesHolder(h)) require.Nil(t, err) // checking state1 @@ -1032,7 +1033,7 @@ func BenchmarkCreateOneMillionAccounts(b *testing.B) { rootHash, err := adb.RootHash() require.Nil(b, err) - _ = adb.RecreateTrie(rootHash) + _ = adb.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) fmt.Println("Completely collapsed trie") createAndExecTxs(b, addr, nrTxs, nrOfAccounts, txVal, adb) } @@ -1171,7 +1172,7 @@ func TestTrieDbPruning_GetAccountAfterPruning(t *testing.T) { rootHash2, _ := adb.Commit() adb.PruneTrie(rootHash1, state.OldRoot, state.NewPruningHandler(state.EnableDataRemoval)) - err := adb.RecreateTrie(rootHash2) + err := adb.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash2)) require.Nil(t, err) acc, err := adb.GetExistingAccount(address1) require.NotNil(t, acc) @@ -1218,7 +1219,7 @@ func TestAccountsDB_RecreateTrieInvalidatesDataTriesCache(t *testing.T) { err = adb.RevertToSnapshot(0) require.Nil(t, err) - err = adb.RecreateTrie(rootHash) + err = adb.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) require.Nil(t, err) acc1, _ = adb.LoadAccount(address1) state1 = acc1.(state.UserAccountHandler) @@ -1263,7 +1264,7 @@ func TestTrieDbPruning_GetDataTrieTrackerAfterPruning(t *testing.T) { newRootHash, _ := adb.Commit() adb.PruneTrie(oldRootHash, state.OldRoot, state.NewPruningHandler(state.EnableDataRemoval)) - err := adb.RecreateTrie(newRootHash) + err := adb.RecreateTrie(holders.NewDefaultRootHashesHolder(newRootHash)) require.Nil(t, err) acc, err := adb.GetExistingAccount(address1) require.NotNil(t, acc) @@ -1284,7 +1285,7 @@ func TestTrieDbPruning_GetDataTrieTrackerAfterPruning(t *testing.T) { func collapseTrie(state state.UserAccountHandler, t *testing.T) { stateRootHash := state.GetRootHash() stateTrie := state.DataTrie().(common.Trie) - stateNewTrie, _ := stateTrie.Recreate(stateRootHash) + stateNewTrie, _ := stateTrie.Recreate(holders.NewDefaultRootHashesHolder(stateRootHash)) require.NotNil(t, stateNewTrie) state.SetDataTrie(stateNewTrie) @@ -1377,7 +1378,7 @@ func TestRollbackBlockAndCheckThatPruningIsCancelledOnAccountsTrie(t *testing.T) if !bytes.Equal(rootHash, rootHashOfRollbackedBlock) { time.Sleep(time.Second * 6) - err = shardNode.AccntState.RecreateTrie(rootHashOfRollbackedBlock) + err = shardNode.AccntState.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHashOfRollbackedBlock)) require.True(t, strings.Contains(err.Error(), trie.ErrKeyNotFound.Error())) } @@ -1395,7 +1396,7 @@ func TestRollbackBlockAndCheckThatPruningIsCancelledOnAccountsTrie(t *testing.T) ) time.Sleep(time.Second * 5) - err = shardNode.AccntState.RecreateTrie(rootHashOfFirstBlock) + err = shardNode.AccntState.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHashOfFirstBlock)) require.Nil(t, err) require.Equal(t, uint64(11), nodes[0].BlockChain.GetCurrentBlockHeader().GetNonce()) require.Equal(t, uint64(12), nodes[1].BlockChain.GetCurrentBlockHeader().GetNonce()) @@ -1458,7 +1459,7 @@ func TestRollbackBlockWithSameRootHashAsPreviousAndCheckThatPruningIsNotDone(t * require.Equal(t, uint64(1), nodes[0].BlockChain.GetCurrentBlockHeader().GetNonce()) require.Equal(t, uint64(2), nodes[1].BlockChain.GetCurrentBlockHeader().GetNonce()) - err := shardNode.AccntState.RecreateTrie(rootHashOfFirstBlock) + err := shardNode.AccntState.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHashOfFirstBlock)) require.Nil(t, err) } @@ -1538,7 +1539,7 @@ func TestTriePruningWhenBlockIsFinal(t *testing.T) { require.Equal(t, uint64(17), nodes[0].BlockChain.GetCurrentBlockHeader().GetNonce()) require.Equal(t, uint64(17), nodes[1].BlockChain.GetCurrentBlockHeader().GetNonce()) - err := shardNode.AccntState.RecreateTrie(rootHashOfFirstBlock) + err := shardNode.AccntState.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHashOfFirstBlock)) require.True(t, strings.Contains(err.Error(), trie.ErrKeyNotFound.Error())) } @@ -1686,12 +1687,12 @@ func checkTrieCanBeRecreated(tb testing.TB, node *integrationTests.TestProcessor stateTrie := node.TrieContainer.Get([]byte(dataRetriever.UserAccountsUnit.String())) roothash := node.BlockChain.GetCurrentBlockRootHash() - tr, err := stateTrie.Recreate(roothash) + tr, err := stateTrie.Recreate(holders.NewDefaultRootHashesHolder(roothash)) require.Nil(tb, err) require.NotNil(tb, tr) _, _, finalRoothash := node.BlockChain.GetFinalBlockInfo() - tr, err = stateTrie.Recreate(finalRoothash) + tr, err = stateTrie.Recreate(holders.NewDefaultRootHashesHolder(finalRoothash)) require.Nil(tb, err) require.NotNil(tb, tr) @@ -1703,7 +1704,7 @@ func checkTrieCanBeRecreated(tb testing.TB, node *integrationTests.TestProcessor err = integrationTests.TestMarshalizer.Unmarshal(hdr, hdrBytes) require.Nil(tb, err) - tr, err = stateTrie.Recreate(hdr.GetRootHash()) + tr, err = stateTrie.Recreate(holders.NewDefaultRootHashesHolder(hdr.GetRootHash())) require.Nil(tb, err) require.NotNil(tb, tr) } @@ -1854,7 +1855,7 @@ func testNodeStateSnapshotAndPruning( stateTrie := node.TrieContainer.Get([]byte(dataRetriever.UserAccountsUnit.String())) assert.Equal(t, 1, len(snapshotsRootHashes)) for i := range snapshotsRootHashes { - tr, err := stateTrie.Recreate(snapshotsRootHashes[i]) + tr, err := stateTrie.Recreate(holders.NewDefaultRootHashesHolder(snapshotsRootHashes[i])) require.Nil(t, err) require.NotNil(t, tr) } @@ -1862,7 +1863,7 @@ func testNodeStateSnapshotAndPruning( assert.Equal(t, 1, len(prunedRootHashes)) // if pruning is called for a root hash in a different epoch than the commit, then recreate trie should work for i := 0; i < len(prunedRootHashes)-1; i++ { - tr, err := stateTrie.Recreate(prunedRootHashes[i]) + tr, err := stateTrie.Recreate(holders.NewDefaultRootHashesHolder(prunedRootHashes[i])) require.Nil(t, tr) require.NotNil(t, err) } @@ -2174,10 +2175,10 @@ func checkDataTrieConsistency( for i, rootHash := range dataTriesRootHashes { _, ok := removedAccounts[i] if ok { - err := adb.RecreateTrie(rootHash) + err := adb.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) assert.NotNil(t, err) } else { - err := adb.RecreateTrie(rootHash) + err := adb.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) require.Nil(t, err) } } diff --git a/integrationTests/state/stateTrieSync/stateTrieSync_test.go b/integrationTests/state/stateTrieSync/stateTrieSync_test.go index 4833c99f4fe..74650d4ce11 100644 --- a/integrationTests/state/stateTrieSync/stateTrieSync_test.go +++ b/integrationTests/state/stateTrieSync/stateTrieSync_test.go @@ -12,6 +12,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core/throttler" "github.com/multiversx/mx-chain-go/common" "github.com/multiversx/mx-chain-go/common/errChan" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/epochStart/notifier" "github.com/multiversx/mx-chain-go/integrationTests" @@ -139,7 +140,7 @@ func testNodeRequestInterceptTrieNodesWithMessenger(t *testing.T, version int) { assert.Nil(t, err) cancel() - requesterTrie, err = requesterTrie.Recreate(rootHash) + requesterTrie, err = requesterTrie.Recreate(holders.NewDefaultRootHashesHolder(rootHash)) require.Nil(t, err) newRootHash, _ := requesterTrie.RootHash() @@ -359,7 +360,7 @@ func testMultipleDataTriesSync(t *testing.T, numAccounts int, numDataTrieLeaves err = userAccSyncer.SyncAccounts(rootHash, storageMarker.NewDisabledStorageMarker()) assert.Nil(t, err) - _ = nRequester.AccntState.RecreateTrie(rootHash) + _ = nRequester.AccntState.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) newRootHash, _ := nRequester.AccntState.RootHash() assert.NotEqual(t, nilRootHash, newRootHash) @@ -509,7 +510,7 @@ func testSyncMissingSnapshotNodes(t *testing.T, version int) { for sw.IsSnapshotInProgress() { time.Sleep(time.Millisecond * 100) } - _ = nRequester.AccntState.RecreateTrie(rootHash) + _ = nRequester.AccntState.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) newRootHash, _ := nRequester.AccntState.RootHash() assert.NotEqual(t, nilRootHash, newRootHash) @@ -545,7 +546,7 @@ func copyPartialState(t *testing.T, sourceNode, destinationNode *integrationTest func getDataTriesHashes(t *testing.T, tr common.Trie, dataTriesRootHashes [][]byte) [][]byte { hashes := make([][]byte, 0) for _, rh := range dataTriesRootHashes { - dt, err := tr.Recreate(rh) + dt, err := tr.Recreate(holders.NewDefaultRootHashesHolder(rh)) assert.Nil(t, err) dtHashes, err := dt.GetAllHashes() diff --git a/integrationTests/vm/wasm/wasmvm/wasmVM_test.go b/integrationTests/vm/wasm/wasmvm/wasmVM_test.go index 53ace932675..0028787d84e 100644 --- a/integrationTests/vm/wasm/wasmvm/wasmVM_test.go +++ b/integrationTests/vm/wasm/wasmvm/wasmVM_test.go @@ -14,6 +14,7 @@ import ( "github.com/multiversx/mx-chain-core-go/hashing/sha256" "github.com/multiversx/mx-chain-core-go/marshal" "github.com/multiversx/mx-chain-go/common" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/config" "github.com/multiversx/mx-chain-go/integrationTests" "github.com/multiversx/mx-chain-go/integrationTests/mock" @@ -824,7 +825,7 @@ func TestAndCatchTrieError(t *testing.T) { log.Info("finished a set - commit and recreate trie", "index", i) if i%10 == 5 { testContext.Accounts.PruneTrie(extraNewRootHash, state.NewRoot, state.NewPruningHandler(state.EnableDataRemoval)) - _ = testContext.Accounts.RecreateTrie(rootHash) + _ = testContext.Accounts.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) continue } diff --git a/node/node_test.go b/node/node_test.go index 2cde11d08a0..cbc5514266a 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -115,7 +115,7 @@ func getAccAdapter(balance *big.Int) *stateMock.AccountsStub { return acc, nil } - accDB.RecreateTrieCalled = func(_ []byte) error { + accDB.RecreateTrieCalled = func(_ common.RootHashHolder) error { return nil } @@ -239,7 +239,7 @@ func TestNode_GetBalanceAccNotFoundShouldReturnEmpty(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -341,7 +341,7 @@ func TestNode_GetCodeHashAccNotFoundShouldReturnEmpty(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -412,7 +412,7 @@ func TestNode_GetKeyValuePairsAccNotFoundShouldReturnEmpty(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -477,7 +477,7 @@ func TestNode_GetKeyValuePairs(t *testing.T) { accDB.GetAccountWithBlockInfoCalled = func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return acc, nil, nil } - accDB.RecreateTrieCalled = func(rootHash []byte) error { + accDB.RecreateTrieCalled = func(rootHash common.RootHashHolder) error { return nil } @@ -537,7 +537,7 @@ func TestNode_GetKeyValuePairs_GetAllLeavesShouldFail(t *testing.T) { accDB.GetAccountWithBlockInfoCalled = func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return acc, nil, nil } - accDB.RecreateTrieCalled = func(rootHash []byte) error { + accDB.RecreateTrieCalled = func(rootHash common.RootHashHolder) error { return nil } @@ -591,7 +591,7 @@ func TestNode_GetKeyValuePairsContextShouldTimeout(t *testing.T) { accDB.GetAccountWithBlockInfoCalled = func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return acc, nil, nil } - accDB.RecreateTrieCalled = func(rootHash []byte) error { + accDB.RecreateTrieCalled = func(rootHash common.RootHashHolder) error { return nil } @@ -630,7 +630,7 @@ func TestNode_GetValueForKeyAccNotFoundShouldReturnEmpty(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -673,7 +673,7 @@ func TestNode_GetValueForKey(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return acc, nil, nil }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -712,7 +712,7 @@ func TestNode_GetESDTDataAccNotFoundShouldReturnEmpty(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -755,7 +755,7 @@ func TestNode_GetESDTData(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return acc, nil, nil }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -810,7 +810,7 @@ func TestNode_GetESDTDataForNFT(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return acc, nil, nil }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -870,7 +870,7 @@ func TestNode_GetAllESDTTokens(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -927,7 +927,7 @@ func TestNode_GetAllESDTTokens_GetAllLeavesShouldFail(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -983,7 +983,7 @@ func TestNode_GetAllESDTTokensContextShouldTimeout(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1026,7 +1026,7 @@ func TestNode_GetAllESDTsAccNotFoundShouldReturnEmpty(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -1117,7 +1117,7 @@ func TestNode_GetAllESDTTokensShouldReturnEsdtAndFormattedNft(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1201,7 +1201,7 @@ func TestNode_GetAllIssuedESDTs(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1287,7 +1287,7 @@ func TestNode_GetESDTsWithRole(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1366,7 +1366,7 @@ func TestNode_GetESDTsRoles(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1432,7 +1432,7 @@ func TestNode_GetNFTTokenIDsRegisteredByAddress(t *testing.T) { ) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1489,7 +1489,7 @@ func TestNode_GetNFTTokenIDsRegisteredByAddressContextShouldTimeout(t *testing.T ) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -3369,7 +3369,7 @@ func TestNode_GetAccountAccNotFoundShouldReturnEmpty(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -3416,7 +3416,7 @@ func TestNode_GetAccountAccountExistsShouldReturn(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return accnt, nil, nil }, - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -4058,7 +4058,7 @@ func TestNode_getProofErrWhenComputingProof(t *testing.T) { }, }, nil }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -4744,7 +4744,7 @@ func TestNode_GetGuardianData(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return testAccount, nil, nil }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -4785,7 +4785,7 @@ func TestNode_GetGuardianData(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return testAccount, nil, nil }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -4813,7 +4813,7 @@ func TestNode_GetGuardianData(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return nil, nil, state.NewErrAccountNotFoundAtBlock(providedBlockInfo) }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -4931,7 +4931,7 @@ func TestNode_GetGuardianData(t *testing.T) { GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { return acc, nil, nil }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } diff --git a/node/trieIterators/delegatedListProcessor_test.go b/node/trieIterators/delegatedListProcessor_test.go index 4718349dcf7..2a92b3a2d9f 100644 --- a/node/trieIterators/delegatedListProcessor_test.go +++ b/node/trieIterators/delegatedListProcessor_test.go @@ -118,7 +118,7 @@ func TestDelegatedListProc_GetDelegatorsListContextShouldTimeout(t *testing.T) { GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { return createScAccount(addressContainer, delegators, addressContainer, time.Second), nil }, - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -165,7 +165,7 @@ func TestDelegatedListProc_GetDelegatorsListShouldWork(t *testing.T) { GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { return createScAccount(addressContainer, delegators, addressContainer, 0), nil }, - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } diff --git a/node/trieIterators/directStakedListProcessor_test.go b/node/trieIterators/directStakedListProcessor_test.go index 552ce65d218..07495736455 100644 --- a/node/trieIterators/directStakedListProcessor_test.go +++ b/node/trieIterators/directStakedListProcessor_test.go @@ -73,7 +73,7 @@ func TestDirectStakedListProc_GetDelegatorsListContextShouldTimeout(t *testing.T GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { return createScAccount(addressContainer, validators, addressContainer, time.Second), nil }, - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -117,7 +117,7 @@ func TestDirectStakedListProc_GetDelegatorsListShouldWork(t *testing.T) { GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { return createScAccount(addressContainer, validators, addressContainer, 0), nil }, - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } diff --git a/node/trieIterators/stakeValuesProcessor_test.go b/node/trieIterators/stakeValuesProcessor_test.go index 43c991ff6d7..fa3bc870cdf 100644 --- a/node/trieIterators/stakeValuesProcessor_test.go +++ b/node/trieIterators/stakeValuesProcessor_test.go @@ -121,7 +121,7 @@ func TestTotalStakedValueProcessor_GetTotalStakedValue_CannotGetAccount(t *testi expectedErr := errors.New("expected error") arg := createMockArgs() arg.Accounts.AccountsAdapter = &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { @@ -162,7 +162,7 @@ func TestTotalStakedValueProcessor_GetTotalStakedValue_CannotCastAccount(t *test GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { return nil, nil }, - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -189,7 +189,7 @@ func TestTotalStakedValueProcessor_GetTotalStakedValue_CannotGetRootHash(t *test GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { return acc, nil }, - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -221,7 +221,7 @@ func TestTotalStakedValueProcessor_GetTotalStakedValue_ContextShouldTimeout(t *t GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { return acc, nil }, - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -256,7 +256,7 @@ func TestTotalStakedValueProcessor_GetTotalStakedValue_CannotGetAllLeaves(t *tes GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { return acc, nil }, - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -327,7 +327,7 @@ func TestTotalStakedValueProcessor_GetTotalStakedValue(t *testing.T) { GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { return acc, nil }, - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } diff --git a/process/block/baseProcess_test.go b/process/block/baseProcess_test.go index f24a580bbc3..f88d7e8d667 100644 --- a/process/block/baseProcess_test.go +++ b/process/block/baseProcess_test.go @@ -1035,7 +1035,7 @@ func TestBaseProcessor_RevertStateRecreateTrieFailsShouldErr(t *testing.T) { expectedErr := errors.New("err") arguments := CreateMockArguments(createComponentHolderMocks()) arguments.AccountsDB[state.UserAccountsState] = &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return expectedErr }, } diff --git a/process/block/metablock.go b/process/block/metablock.go index 390e1cebf25..fb53f1207d7 100644 --- a/process/block/metablock.go +++ b/process/block/metablock.go @@ -14,6 +14,7 @@ import ( "github.com/multiversx/mx-chain-core-go/data/block" "github.com/multiversx/mx-chain-core-go/data/headerVersionData" "github.com/multiversx/mx-chain-go/common" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/dataRetriever" processOutport "github.com/multiversx/mx-chain-go/outport/process" "github.com/multiversx/mx-chain-go/process" @@ -1584,7 +1585,8 @@ func (mp *metaProcessor) commitEpochStart(header *block.MetaBlock, body *block.B // RevertStateToBlock recreates the state tries to the root hashes indicated by the provided root hash and header func (mp *metaProcessor) RevertStateToBlock(header data.HeaderHandler, rootHash []byte) error { - err := mp.accountsDB[state.UserAccountsState].RecreateTrie(rootHash) + rootHashHolder := holders.NewDefaultRootHashesHolder(rootHash) + err := mp.accountsDB[state.UserAccountsState].RecreateTrie(rootHashHolder) if err != nil { log.Debug("recreate trie with error for header", "nonce", header.GetNonce(), diff --git a/process/block/metablock_test.go b/process/block/metablock_test.go index 173e14ffb90..c78f2c5b039 100644 --- a/process/block/metablock_test.go +++ b/process/block/metablock_test.go @@ -1204,7 +1204,7 @@ func TestMetaProcessor_RevertStateRevertPeerStateFailsShouldErr(t *testing.T) { arguments := createMockMetaArguments(coreComponents, dataComponents, bootstrapComponents, statusComponents) arguments.AccountsDB[state.UserAccountsState] = &stateMock.AccountsStub{} arguments.AccountsDB[state.UserAccountsState] = &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1232,7 +1232,7 @@ func TestMetaProcessor_RevertStateShouldWork(t *testing.T) { arguments := createMockMetaArguments(coreComponents, dataComponents, bootstrapComponents, statusComponents) arguments.AccountsDB[state.UserAccountsState] = &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { recreateTrieWasCalled = true return nil }, diff --git a/process/block/shardblock.go b/process/block/shardblock.go index 11e62f63ff9..a73b4e0be6d 100644 --- a/process/block/shardblock.go +++ b/process/block/shardblock.go @@ -12,6 +12,7 @@ import ( "github.com/multiversx/mx-chain-core-go/data/block" "github.com/multiversx/mx-chain-core-go/data/headerVersionData" "github.com/multiversx/mx-chain-go/common" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/dataRetriever" processOutport "github.com/multiversx/mx-chain-go/outport/process" "github.com/multiversx/mx-chain-go/process" @@ -403,8 +404,8 @@ func (sp *shardProcessor) requestEpochStartInfo(header data.ShardHeaderHandler, // RevertStateToBlock recreates the state tries to the root hashes indicated by the provided root hash and header func (sp *shardProcessor) RevertStateToBlock(header data.HeaderHandler, rootHash []byte) error { - - err := sp.accountsDB[state.UserAccountsState].RecreateTrie(rootHash) + rootHashHolder := holders.NewDefaultRootHashesHolder(rootHash) + err := sp.accountsDB[state.UserAccountsState].RecreateTrie(rootHashHolder) if err != nil { log.Debug("recreate trie with error for header", "nonce", header.GetNonce(), diff --git a/process/peer/process.go b/process/peer/process.go index 4c04de6a25d..7835cde3cb6 100644 --- a/process/peer/process.go +++ b/process/peer/process.go @@ -15,6 +15,7 @@ import ( "github.com/multiversx/mx-chain-core-go/marshal" "github.com/multiversx/mx-chain-go/common" "github.com/multiversx/mx-chain-go/common/errChan" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/common/validatorInfo" "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/process" @@ -886,10 +887,10 @@ func (vs *validatorStatistics) decreaseForConsensusValidators( } // RevertPeerState takes the current and previous headers and undos the peer state -// -// for all of the consensus members +// for all of the consensus members func (vs *validatorStatistics) RevertPeerState(header data.MetaHeaderHandler) error { - return vs.peerAdapter.RecreateTrie(header.GetValidatorStatsRootHash()) + rootHashHolder := holders.NewDefaultRootHashesHolder(header.GetValidatorStatsRootHash()) + return vs.peerAdapter.RecreateTrie(rootHashHolder) } func (vs *validatorStatistics) updateShardDataPeerState( diff --git a/process/smartContract/scQueryService.go b/process/smartContract/scQueryService.go index ec6ad67e87c..b23c8c9d13d 100644 --- a/process/smartContract/scQueryService.go +++ b/process/smartContract/scQueryService.go @@ -258,15 +258,13 @@ func (service *SCQueryService) recreateTrie(blockRootHash []byte, blockHeader da accountsAdapter := service.blockChainHook.GetAccountsAdapter() + rootHashHolder := holders.NewDefaultRootHashesHolder(blockRootHash) if service.isInHistoricalBalancesMode { - logQueryService.Trace("calling RecreateTrieFromEpoch", "block", blockHeader.GetNonce(), "rootHash", blockRootHash) - holder := holders.NewRootHashHolder(blockRootHash, core.OptionalUint32{Value: blockHeader.GetEpoch(), HasValue: true}) - - return accountsAdapter.RecreateTrieFromEpoch(holder) + rootHashHolder = holders.NewRootHashHolder(blockRootHash, core.OptionalUint32{Value: blockHeader.GetEpoch(), HasValue: true}) } - logQueryService.Trace("calling RecreateTrie", "block", blockHeader.GetNonce(), "rootHash", blockRootHash) - return accountsAdapter.RecreateTrie(blockRootHash) + logQueryService.Trace("calling RecreateTrie", "block", blockHeader.GetNonce(), "rootHashHolder", rootHashHolder) + return accountsAdapter.RecreateTrie(rootHashHolder) } // TODO: extract duplicated code with nodeBlocks.go diff --git a/process/smartContract/scQueryService_test.go b/process/smartContract/scQueryService_test.go index d71542a8aaa..4889dc87ac5 100644 --- a/process/smartContract/scQueryService_test.go +++ b/process/smartContract/scQueryService_test.go @@ -40,7 +40,7 @@ func createMockArgumentsForSCQuery() ArgsNewSCQueryService { BlockChainHook: &testscommon.BlockChainHookStub{ GetAccountsAdapterCalled: func() state.AccountsAdapter { return &stateMocks.AccountsStub{ - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { return nil }, } @@ -438,15 +438,15 @@ func TestExecuteQuery_ShouldReceiveQueryCorrectly(t *testing.T) { recreateTrieFromEpochWasCalled := false providedAccountsAdapter := &stateMocks.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { + if options.GetEpoch().HasValue { + recreateTrieFromEpochWasCalled = true + assert.Equal(t, providedRootHash, options.GetRootHash()) + return nil + } recreateTrieWasCalled = true return nil }, - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { - recreateTrieFromEpochWasCalled = true - assert.Equal(t, providedRootHash, options.GetRootHash()) - return nil - }, } argsNewSCQuery.BlockChainHook = &testscommon.BlockChainHookStub{ GetAccountsAdapterCalled: func() state.AccountsAdapter { @@ -534,13 +534,13 @@ func TestExecuteQuery_ShouldReceiveQueryCorrectly(t *testing.T) { recreateTrieFromEpochWasCalled := false providedAccountsAdapter := &stateMocks.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { + if options.GetEpoch().HasValue { + recreateTrieFromEpochWasCalled = true + assert.Equal(t, providedRootHash, options.GetRootHash()) + return nil + } recreateTrieWasCalled = true - assert.Equal(t, providedRootHash, rootHash) - return nil - }, - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { - recreateTrieFromEpochWasCalled = true return nil }, } @@ -583,7 +583,7 @@ func TestSCQueryService_RecreateTrie(t *testing.T) { argsNewSCQuery.BlockChainHook = &testscommon.BlockChainHookStub{ GetAccountsAdapterCalled: func() state.AccountsAdapter { return &stateMocks.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { require.Fail(t, "should not be called") return nil }, @@ -611,17 +611,17 @@ func TestSCQueryService_RecreateTrie(t *testing.T) { argsNewSCQuery.BlockChainHook = &testscommon.BlockChainHookStub{ GetAccountsAdapterCalled: func() state.AccountsAdapter { return &stateMocks.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { + if options.GetEpoch().HasValue { + recreateTrieWasCalled = false + recreateTrieFromEpochWasCalled = true + + assert.Equal(t, testRootHash, options.GetRootHash()) + return nil + } recreateTrieWasCalled = true recreateTrieFromEpochWasCalled = false - assert.Equal(t, testRootHash, rootHash) - return nil - }, - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { - recreateTrieWasCalled = false - recreateTrieFromEpochWasCalled = true - assert.Equal(t, testRootHash, options.GetRootHash()) return nil }, @@ -653,17 +653,17 @@ func TestSCQueryService_RecreateTrie(t *testing.T) { argsNewSCQuery.BlockChainHook = &testscommon.BlockChainHookStub{ GetAccountsAdapterCalled: func() state.AccountsAdapter { return &stateMocks.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { + if options.GetEpoch().HasValue { + recreateTrieWasCalled = false + recreateTrieFromEpochWasCalled = true + + assert.Equal(t, testRootHash, options.GetRootHash()) + return nil + } recreateTrieWasCalled = true recreateTrieFromEpochWasCalled = false - assert.Equal(t, testRootHash, rootHash) - return nil - }, - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { - recreateTrieWasCalled = false - recreateTrieFromEpochWasCalled = true - assert.Equal(t, testRootHash, options.GetRootHash()) return nil }, diff --git a/process/sync/metablock_test.go b/process/sync/metablock_test.go index fff94e55389..6d183fbf821 100644 --- a/process/sync/metablock_test.go +++ b/process/sync/metablock_test.go @@ -1379,7 +1379,7 @@ func TestMetaBootstrap_RollBackIsEmptyCallRollBackOneBlockOkValsShouldWork(t *te } args.ForkDetector = createForkDetector(currentHdrNonce, currentHdrHash, remFlags) args.Accounts = &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1520,7 +1520,7 @@ func TestMetaBootstrap_RollBackIsEmptyCallRollBackOneBlockToGenesisShouldWork(t } args.ForkDetector = createForkDetector(currentHdrNonce, currentHdrHash, remFlags) args.Accounts = &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } diff --git a/process/sync/shardblock_test.go b/process/sync/shardblock_test.go index 8abfd29e6bc..070b926df0f 100644 --- a/process/sync/shardblock_test.go +++ b/process/sync/shardblock_test.go @@ -1525,7 +1525,7 @@ func TestBootstrap_RollBackIsEmptyCallRollBackOneBlockOkValsShouldWork(t *testin } args.ForkDetector = createForkDetector(currentHdrNonce, currentHdrHash, remFlags) args.Accounts = &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1668,7 +1668,7 @@ func TestBootstrap_RollbackIsEmptyCallRollBackOneBlockToGenesisShouldWork(t *tes } args.ForkDetector = createForkDetector(currentHdrNonce, currentHdrHash, remFlags) args.Accounts = &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } diff --git a/process/transactionEvaluator/simulationAccountsDB.go b/process/transactionEvaluator/simulationAccountsDB.go index ffac23e7994..b3a5cbecb0d 100644 --- a/process/transactionEvaluator/simulationAccountsDB.go +++ b/process/transactionEvaluator/simulationAccountsDB.go @@ -121,12 +121,7 @@ func (r *simulationAccountsDB) RootHash() ([]byte, error) { } // RecreateTrie won't do anything as write operations are disabled on this component -func (r *simulationAccountsDB) RecreateTrie(_ []byte) error { - return nil -} - -// RecreateTrieFromEpoch won't do anything as write operations are disabled on this component -func (r *simulationAccountsDB) RecreateTrieFromEpoch(_ common.RootHashHolder) error { +func (r *simulationAccountsDB) RecreateTrie(_ common.RootHashHolder) error { return nil } diff --git a/process/transactionEvaluator/simulationAccountsDB_test.go b/process/transactionEvaluator/simulationAccountsDB_test.go index cf651e06444..13655ba315f 100644 --- a/process/transactionEvaluator/simulationAccountsDB_test.go +++ b/process/transactionEvaluator/simulationAccountsDB_test.go @@ -52,7 +52,7 @@ func TestReadOnlyAccountsDB_WriteOperationsShouldNotCalled(t *testing.T) { t.Errorf(failErrMsg) return nil }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { t.Errorf(failErrMsg) return nil }, diff --git a/state/accountsDB.go b/state/accountsDB.go index 249dd64f471..4274e5138d6 100644 --- a/state/accountsDB.go +++ b/state/accountsDB.go @@ -431,7 +431,8 @@ func (adb *AccountsDB) loadDataTrieConcurrentSafe(accountHandler baseAccountHand return nil } - dataTrie, err := mainTrie.Recreate(accountHandler.GetRootHash()) + rootHashHolder := holders.NewDefaultRootHashesHolder(accountHandler.GetRootHash()) + dataTrie, err := mainTrie.Recreate(rootHashHolder) if err != nil { return fmt.Errorf("trie was not found for hash, rootHash = %s, err = %w", hex.EncodeToString(accountHandler.GetRootHash()), err) } @@ -556,7 +557,8 @@ func (adb *AccountsDB) removeDataTrie(baseAcc baseAccountHandler) error { return nil } - dataTrie, err := adb.mainTrie.Recreate(rootHash) + rootHashHolder := holders.NewDefaultRootHashesHolder(rootHash) + dataTrie, err := adb.mainTrie.Recreate(rootHashHolder) if err != nil { return err } @@ -745,7 +747,7 @@ func (adb *AccountsDB) RevertToSnapshot(snapshot int) error { if snapshot == 0 { log.Trace("revert snapshot to adb.lastRootHash", "hash", adb.lastRootHash) - return adb.recreateTrie(holders.NewRootHashHolder(adb.lastRootHash, core.OptionalUint32{})) + return adb.recreateTrie(holders.NewDefaultRootHashesHolder(adb.lastRootHash)) } for i := len(adb.entries) - 1; i >= snapshot; i-- { @@ -911,13 +913,8 @@ func (adb *AccountsDB) RootHash() ([]byte, error) { return rootHash, err } -// RecreateTrie is used to reload the trie based on an existing rootHash -func (adb *AccountsDB) RecreateTrie(rootHash []byte) error { - return adb.RecreateTrieFromEpoch(holders.NewRootHashHolder(rootHash, core.OptionalUint32{})) -} - -// RecreateTrieFromEpoch is used to reload the trie based on the provided options -func (adb *AccountsDB) RecreateTrieFromEpoch(options common.RootHashHolder) error { +// RecreateTrie is used to reload the trie based on the provided options +func (adb *AccountsDB) RecreateTrie(options common.RootHashHolder) error { adb.mutOp.Lock() defer adb.mutOp.Unlock() @@ -939,7 +936,7 @@ func (adb *AccountsDB) recreateTrie(options common.RootHashHolder) error { adb.obsoleteDataTrieHashes = make(map[string][][]byte) adb.dataTries.Reset() adb.entries = make([]JournalEntry, 0) - newTrie, err := adb.mainTrie.RecreateFromEpoch(options) + newTrie, err := adb.mainTrie.Recreate(options) if err != nil { return err } @@ -985,7 +982,8 @@ func (adb *AccountsDB) RecreateAllTries(rootHash []byte) (map[string]common.Trie userAccountRootHash := userAccount.GetRootHash() if len(userAccountRootHash) > 0 { - dataTrie, errRecreate := mainTrie.Recreate(userAccountRootHash) + rootHashHolder := holders.NewDefaultRootHashesHolder(userAccountRootHash) + dataTrie, errRecreate := mainTrie.Recreate(rootHashHolder) if errRecreate != nil { return nil, errRecreate } @@ -1023,7 +1021,8 @@ func getUserAccountFromBytes(accountFactory AccountFactory, marshaller marshal.M } func (adb *AccountsDB) recreateMainTrie(rootHash []byte) (map[string]common.Trie, error) { - recreatedTrie, err := adb.getMainTrie().Recreate(rootHash) + rootHashHolder := holders.NewDefaultRootHashesHolder(rootHash) + recreatedTrie, err := adb.getMainTrie().Recreate(rootHashHolder) if err != nil { return nil, err } @@ -1036,7 +1035,8 @@ func (adb *AccountsDB) recreateMainTrie(rootHash []byte) (map[string]common.Trie // GetTrie returns the trie that has the given rootHash func (adb *AccountsDB) GetTrie(rootHash []byte) (common.Trie, error) { - return adb.getMainTrie().Recreate(rootHash) + rootHashHolder := holders.NewDefaultRootHashesHolder(rootHash) + return adb.getMainTrie().Recreate(rootHashHolder) } // Journalize adds a new object to entries list. diff --git a/state/accountsDBApi.go b/state/accountsDBApi.go index 86daccf660c..9d0a67ef9fa 100644 --- a/state/accountsDBApi.go +++ b/state/accountsDBApi.go @@ -63,7 +63,8 @@ func (accountsDB *accountsDBApi) doRecreateTrieWithBlockInfo(newBlockInfo common return currentBlockInfo, nil } - err := accountsDB.innerAccountsAdapter.RecreateTrie(newBlockInfo.GetRootHash()) + rootHashHolder := holders.NewDefaultRootHashesHolder(newBlockInfo.GetRootHash()) + err := accountsDB.innerAccountsAdapter.RecreateTrie(rootHashHolder) if err != nil { accountsDB.blockInfo = nil return nil, err @@ -164,14 +165,8 @@ func (accountsDB *accountsDBApi) RootHash() ([]byte, error) { return blockInfo.GetRootHash(), nil } -// RecreateTrie is used to reload the trie based on an existing rootHash -func (accountsDB *accountsDBApi) RecreateTrie(rootHash []byte) error { - _, err := accountsDB.doRecreateTrieWithBlockInfo(holders.NewBlockInfo([]byte{}, 0, rootHash)) - return err -} - -// RecreateTrieFromEpoch is a not permitted operation in this implementation and thus, will return an error -func (accountsDB *accountsDBApi) RecreateTrieFromEpoch(options common.RootHashHolder) error { +// RecreateTrie is a not permitted operation in this implementation and thus, will return an error +func (accountsDB *accountsDBApi) RecreateTrie(options common.RootHashHolder) error { accountsDB.mutRecreatedTrieBlockInfo.Lock() defer accountsDB.mutRecreatedTrieBlockInfo.Unlock() @@ -184,7 +179,7 @@ func (accountsDB *accountsDBApi) RecreateTrieFromEpoch(options common.RootHashHo return nil } - err := accountsDB.innerAccountsAdapter.RecreateTrieFromEpoch(options) + err := accountsDB.innerAccountsAdapter.RecreateTrie(options) if err != nil { accountsDB.blockInfo = nil return err diff --git a/state/accountsDBApiWithHistory.go b/state/accountsDBApiWithHistory.go index 76994768f6c..b0f22bb19b1 100644 --- a/state/accountsDBApiWithHistory.go +++ b/state/accountsDBApiWithHistory.go @@ -94,12 +94,7 @@ func (accountsDB *accountsDBApiWithHistory) RootHash() ([]byte, error) { } // RecreateTrie is a not permitted operation in this implementation and thus, will return an error -func (accountsDB *accountsDBApiWithHistory) RecreateTrie(_ []byte) error { - return ErrOperationNotPermitted -} - -// RecreateTrieFromEpoch is a not permitted operation in this implementation and thus, will return an error -func (accountsDB *accountsDBApiWithHistory) RecreateTrieFromEpoch(_ common.RootHashHolder) error { +func (accountsDB *accountsDBApiWithHistory) RecreateTrie(_ common.RootHashHolder) error { return ErrOperationNotPermitted } @@ -228,7 +223,7 @@ func (accountsDB *accountsDBApiWithHistory) shouldRecreateTrieUnprotected(rootHa } func (accountsDB *accountsDBApiWithHistory) recreateTrieUnprotected(options common.RootHashHolder) error { - err := accountsDB.innerAccountsAdapter.RecreateTrieFromEpoch(options) + err := accountsDB.innerAccountsAdapter.RecreateTrie(options) if err != nil { return err } diff --git a/state/accountsDBApiWithHistory_test.go b/state/accountsDBApiWithHistory_test.go index 4d9e1a28341..199acb27a48 100644 --- a/state/accountsDBApiWithHistory_test.go +++ b/state/accountsDBApiWithHistory_test.go @@ -8,7 +8,6 @@ import ( "sync" "testing" - "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/atomic" "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-go/common" @@ -99,14 +98,14 @@ func TestAccountsDBApiWithHistory_NotPermittedOrNotImplementedOperationsDoNotPan func TestAccountsDBApiWithHistory_GetAccountWithBlockInfo(t *testing.T) { rootHash := []byte("rootHash") - options := holders.NewRootHashHolder(rootHash, core.OptionalUint32{}) + options := holders.NewDefaultRootHashesHolder(rootHash) arbitraryError := errors.New("arbitrary error") t.Run("recreate trie fails", func(t *testing.T) { expectedErr := errors.New("expected error") accountsAdapter := &mockState.AccountsStub{ - RecreateTrieFromEpochCalled: func(_ common.RootHashHolder) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return expectedErr }, } @@ -122,7 +121,7 @@ func TestAccountsDBApiWithHistory_GetAccountWithBlockInfo(t *testing.T) { var recreatedRootHash []byte accountsAdapter := &mockState.AccountsStub{ - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { recreatedRootHash = options.GetRootHash() return nil }, @@ -147,7 +146,7 @@ func TestAccountsDBApiWithHistory_GetAccountWithBlockInfo(t *testing.T) { var recreatedRootHash []byte accountsAdapter := &mockState.AccountsStub{ - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { recreatedRootHash = options.GetRootHash() return nil }, @@ -168,7 +167,7 @@ func TestAccountsDBApiWithHistory_GetAccountWithBlockInfo(t *testing.T) { var recreatedRootHash []byte accountsAdapter := &mockState.AccountsStub{ - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { recreatedRootHash = options.GetRootHash() return nil }, @@ -189,13 +188,13 @@ func TestAccountsDBApiWithHistory_GetAccountWithBlockInfo(t *testing.T) { func TestAccountsDBApiWithHistory_GetCodeWithBlockInfo(t *testing.T) { contractCodeHash := []byte("codeHash") rootHash := []byte("rootHash") - options := holders.NewRootHashHolder(rootHash, core.OptionalUint32{}) + options := holders.NewDefaultRootHashesHolder(rootHash) t.Run("recreate trie fails", func(t *testing.T) { expectedErr := errors.New("expected error") accountsAdapter := &mockState.AccountsStub{ - RecreateTrieFromEpochCalled: func(_ common.RootHashHolder) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return expectedErr }, } @@ -211,7 +210,7 @@ func TestAccountsDBApiWithHistory_GetCodeWithBlockInfo(t *testing.T) { var recreatedRootHash []byte accountsAdapter := &mockState.AccountsStub{ - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { recreatedRootHash = options.GetRootHash() return nil }, @@ -249,7 +248,7 @@ func TestAccountsDBApiWithHistory_GetAccountWithBlockInfoWhenHighConcurrency(t * var dummyAccountMutex sync.RWMutex accountsAdapter := &mockState.AccountsStub{ - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { rootHash := options.GetRootHash() dummyAccountMutex.Lock() @@ -283,7 +282,7 @@ func TestAccountsDBApiWithHistory_GetAccountWithBlockInfoWhenHighConcurrency(t * go func(rootHashAsInt int) { rootHashAsString := fmt.Sprintf("%d", rootHashAsInt) rootHash := []byte(rootHashAsString) - options := holders.NewRootHashHolder(rootHash, core.OptionalUint32{}) + options := holders.NewDefaultRootHashesHolder(rootHash) account, blockInfo, _ := accountsApiWithHistory.GetAccountWithBlockInfo([]byte("address"), options) userAccount := account.(state.UserAccountHandler) diff --git a/state/accountsDBApi_test.go b/state/accountsDBApi_test.go index 0d9aea1c098..35f80992d5a 100644 --- a/state/accountsDBApi_test.go +++ b/state/accountsDBApi_test.go @@ -71,7 +71,7 @@ func TestAccountsDBAPi_recreateTrieIfNecessary(t *testing.T) { t.Parallel() accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { require.Fail(t, "should have not called RecreateAllTriesCalled") return nil @@ -109,7 +109,7 @@ func TestAccountsDBAPi_recreateTrieIfNecessary(t *testing.T) { t.Parallel() accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { require.Fail(t, "should have not called RecreateAllTriesCalled") return nil @@ -126,7 +126,7 @@ func TestAccountsDBAPi_recreateTrieIfNecessary(t *testing.T) { oldRootHash := []byte("old root hash") accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { assert.Equal(t, rootHash, rootHash) return nil @@ -146,7 +146,7 @@ func TestAccountsDBAPi_recreateTrieIfNecessary(t *testing.T) { oldRootHash := []byte("old root hash") accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { assert.Equal(t, rootHash, rootHash) return expectedErr @@ -169,7 +169,7 @@ func TestAccountsDBAPi_doRecreateTrieWhenReEntranceHappened(t *testing.T) { targetRootHash := []byte("root hash") numCalled := 0 accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { numCalled++ return nil }, @@ -215,13 +215,13 @@ func TestAccountsDBApi_RecreateTrie(t *testing.T) { wasCalled := false accountsApi, _ := state.NewAccountsDBApi(&mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { wasCalled = true return nil }, }, createBlockInfoProviderStub(dummyRootHash)) - err := accountsApi.RecreateTrie(nil) + err := accountsApi.RecreateTrie(holders.NewDefaultRootHashesHolder([]byte{})) assert.NoError(t, err) assert.True(t, wasCalled) } @@ -231,14 +231,14 @@ func TestAccountsDBApi_RecreateTrieFromEpoch(t *testing.T) { t.Run("should error if the roothash holder is nil", func(t *testing.T) { accountsApi, _ := state.NewAccountsDBApi(&mockState.AccountsStub{ - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { assert.Fail(t, "should have not called accountsApi.RecreateTrieFromEpochCalled") return nil }, }, createBlockInfoProviderStub(dummyRootHash)) - err := accountsApi.RecreateTrieFromEpoch(nil) + err := accountsApi.RecreateTrie(nil) assert.Equal(t, trie.ErrNilRootHashHolder, err) }) t.Run("should work", func(t *testing.T) { @@ -246,7 +246,7 @@ func TestAccountsDBApi_RecreateTrieFromEpoch(t *testing.T) { rootHash := []byte("root hash") epoch := core.OptionalUint32{Value: 37, HasValue: true} accountsApi, _ := state.NewAccountsDBApi(&mockState.AccountsStub{ - RecreateTrieFromEpochCalled: func(options common.RootHashHolder) error { + RecreateTrieCalled: func(options common.RootHashHolder) error { wasCalled = true assert.Equal(t, rootHash, options.GetRootHash()) assert.Equal(t, epoch, options.GetEpoch()) @@ -255,7 +255,7 @@ func TestAccountsDBApi_RecreateTrieFromEpoch(t *testing.T) { }, createBlockInfoProviderStub(dummyRootHash)) holder := holders.NewRootHashHolder(rootHash, epoch) - err := accountsApi.RecreateTrieFromEpoch(holder) + err := accountsApi.RecreateTrie(holder) assert.NoError(t, err) assert.True(t, wasCalled) }) @@ -288,7 +288,7 @@ func TestAccountsDBApi_SimpleProxyMethodsShouldWork(t *testing.T) { closeCalled := false getTrieCalled := false accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { require.Fail(t, "should have not called RecreateTrieCalled") return nil @@ -335,7 +335,7 @@ func TestAccountsDBApi_GetExistingAccount(t *testing.T) { t.Parallel() accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return expectedErr }, GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { @@ -354,7 +354,7 @@ func TestAccountsDBApi_GetExistingAccount(t *testing.T) { recreateTrieCalled := false accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { recreateTrieCalled = true return nil }, @@ -379,7 +379,7 @@ func TestAccountsDBApi_GetAccountFromBytes(t *testing.T) { t.Parallel() accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return expectedErr }, GetAccountFromBytesCalled: func(address []byte, accountBytes []byte) (vmcommon.AccountHandler, error) { @@ -398,7 +398,7 @@ func TestAccountsDBApi_GetAccountFromBytes(t *testing.T) { recreateTrieCalled := false accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { recreateTrieCalled = true return nil }, @@ -423,7 +423,7 @@ func TestAccountsDBApi_LoadAccount(t *testing.T) { t.Parallel() accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return expectedErr }, LoadAccountCalled: func(address []byte) (vmcommon.AccountHandler, error) { @@ -442,7 +442,7 @@ func TestAccountsDBApi_LoadAccount(t *testing.T) { recreateTrieCalled := false accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { recreateTrieCalled = true return nil }, @@ -467,7 +467,7 @@ func TestAccountsDBApi_GetCode(t *testing.T) { t.Parallel() accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return expectedErr }, GetCodeCalled: func(codeHash []byte) []byte { @@ -486,7 +486,7 @@ func TestAccountsDBApi_GetCode(t *testing.T) { providedCode := []byte("code") recreateTrieCalled := false accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { recreateTrieCalled = true return nil }, @@ -510,7 +510,7 @@ func TestAccountsDBApi_GetAllLeaves(t *testing.T) { t.Parallel() accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return expectedErr }, GetAllLeavesCalled: func(_ *common.TrieIteratorChannels, _ context.Context, _ []byte, _ common.TrieLeafParser) error { @@ -529,7 +529,7 @@ func TestAccountsDBApi_GetAllLeaves(t *testing.T) { providedChan := &common.TrieIteratorChannels{LeavesChan: make(chan core.KeyValueHolder)} recreateTrieCalled := false accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { recreateTrieCalled = true return nil }, @@ -554,13 +554,13 @@ func TestAccountsDBApi_GetAccountWithBlockInfoWhenHighConcurrency(t *testing.T) var currentBlockInfoMutex sync.RWMutex accountsAdapter := &mockState.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { dummyAccountMutex.Lock() defer dummyAccountMutex.Unlock() // When a trie is recreated, we "add" to it a single account, // having the balance correlated with the trie rootHash (for the sake of the test, for easier assertions). - dummyAccount = createDummyAccountWithBalanceBytes(rootHash) + dummyAccount = createDummyAccountWithBalanceBytes(rootHash.GetRootHash()) return nil }, GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { diff --git a/state/accountsDB_test.go b/state/accountsDB_test.go index b10ea8d5167..00826b500a6 100644 --- a/state/accountsDB_test.go +++ b/state/accountsDB_test.go @@ -529,7 +529,7 @@ func TestAccountsDB_LoadAccountExistingShouldLoadDataTrie(t *testing.T) { } return nil, 0, nil }, - RecreateCalled: func(root []byte) (d common.Trie, err error) { + RecreateCalled: func(holder common.RootHashHolder) (d common.Trie, err error) { return dataTrie, nil }, GetStorageManagerCalled: func() common.StorageManager { @@ -607,7 +607,7 @@ func TestAccountsDB_GetExistingAccountFoundShouldRetAccount(t *testing.T) { } return nil, 0, nil }, - RecreateCalled: func(root []byte) (d common.Trie, err error) { + RecreateCalled: func(root common.RootHashHolder) (d common.Trie, err error) { return dataTrie, nil }, GetStorageManagerCalled: func() common.StorageManager { @@ -830,8 +830,8 @@ func TestAccountsDB_LoadDataWithSomeValuesShouldWork(t *testing.T) { account := generateAccount() mockTrie := &trieMock.TrieStub{ - RecreateCalled: func(root []byte) (trie common.Trie, e error) { - if !bytes.Equal(root, rootHash) { + RecreateCalled: func(root common.RootHashHolder) (trie common.Trie, e error) { + if !bytes.Equal(root.GetRootHash(), rootHash) { return nil, errors.New("bad root hash") } @@ -875,7 +875,7 @@ func TestAccountsDB_CommitShouldCallCommitFromTrie(t *testing.T) { GetCalled: func(_ []byte) ([]byte, uint32, error) { return serializedAccount, 0, nil }, - RecreateCalled: func(root []byte) (trie common.Trie, err error) { + RecreateCalled: func(root common.RootHashHolder) (trie common.Trie, err error) { return &trieMock.TrieStub{ GetCalled: func(_ []byte) ([]byte, uint32, error) { return []byte("doge"), 0, nil @@ -923,14 +923,14 @@ func TestAccountsDB_RecreateTrieMalfunctionTrieShouldErr(t *testing.T) { return &storageManager.StorageManagerStub{} }, } - trieStub.RecreateFromEpochCalled = func(_ common.RootHashHolder) (tree common.Trie, e error) { + trieStub.RecreateCalled = func(_ common.RootHashHolder) (tree common.Trie, e error) { wasCalled = true return nil, errExpected } adb := generateAccountDBFromTrie(trieStub) - err := adb.RecreateTrie(nil) + err := adb.RecreateTrie(holders.NewDefaultRootHashesHolder([]byte{})) assert.Equal(t, errExpected, err) assert.True(t, wasCalled) } @@ -945,13 +945,13 @@ func TestAccountsDB_RecreateTrieOutputsNilTrieShouldErr(t *testing.T) { return &storageManager.StorageManagerStub{} }, } - trieStub.RecreateFromEpochCalled = func(_ common.RootHashHolder) (tree common.Trie, e error) { + trieStub.RecreateCalled = func(_ common.RootHashHolder) (tree common.Trie, e error) { wasCalled = true return nil, nil } adb := generateAccountDBFromTrie(&trieStub) - err := adb.RecreateTrie(nil) + err := adb.RecreateTrie(holders.NewDefaultRootHashesHolder([]byte{})) assert.Equal(t, state.ErrNilTrie, err) assert.True(t, wasCalled) @@ -967,14 +967,14 @@ func TestAccountsDB_RecreateTrieOkValsShouldWork(t *testing.T) { GetStorageManagerCalled: func() common.StorageManager { return &storageManager.StorageManagerStub{} }, - RecreateFromEpochCalled: func(_ common.RootHashHolder) (common.Trie, error) { + RecreateCalled: func(_ common.RootHashHolder) (common.Trie, error) { wasCalled = true return &trieMock.TrieStub{}, nil }, } adb := generateAccountDBFromTrie(&trieStub) - err := adb.RecreateTrie(nil) + err := adb.RecreateTrie(holders.NewDefaultRootHashesHolder([]byte{})) assert.Nil(t, err) assert.True(t, wasCalled) @@ -1436,7 +1436,7 @@ func TestAccountsDB_RecreateTrieInvalidatesJournalEntries(t *testing.T) { _ = adb.SaveAccount(acc) assert.Equal(t, 5, adb.JournalLen()) - err := adb.RecreateTrie(rootHash) + err := adb.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) assert.Nil(t, err) assert.Equal(t, 0, adb.JournalLen()) } @@ -1955,7 +1955,7 @@ func TestAccountsDB_PruningAndPruningCancellingOnTrieRollback(t *testing.T) { } for i := 0; i < len(rootHashes); i++ { - _, err := tr.Recreate(rootHashes[i]) + _, err := tr.Recreate(holders.NewDefaultRootHashesHolder(rootHashes[i])) assert.Nil(t, err) } @@ -1964,7 +1964,7 @@ func TestAccountsDB_PruningAndPruningCancellingOnTrieRollback(t *testing.T) { finalizeTrieState(t, 2, tr, adb, rootHashes) rollbackTrieState(t, 3, tr, adb, rootHashes) - _, err := tr.Recreate(rootHashes[2]) + _, err := tr.Recreate(holders.NewDefaultRootHashesHolder(rootHashes[2])) assert.Nil(t, err) } @@ -1973,7 +1973,7 @@ func finalizeTrieState(t *testing.T, index int, tr common.Trie, adb state.Accoun adb.CancelPrune(rootHashes[index], state.NewRoot) time.Sleep(trieDbOperationDelay) - _, err := tr.Recreate(rootHashes[index-1]) + _, err := tr.Recreate(holders.NewDefaultRootHashesHolder(rootHashes[index-1])) assert.NotNil(t, err) } @@ -1982,7 +1982,7 @@ func rollbackTrieState(t *testing.T, index int, tr common.Trie, adb state.Accoun adb.CancelPrune(rootHashes[index-1], state.OldRoot) time.Sleep(trieDbOperationDelay) - _, err := tr.Recreate(rootHashes[index]) + _, err := tr.Recreate(holders.NewDefaultRootHashesHolder(rootHashes[index])) assert.NotNil(t, err) } @@ -2143,7 +2143,7 @@ func TestAccountsDB_RecreateAllTries(t *testing.T) { return nil }, - RecreateCalled: func(root []byte) (common.Trie, error) { + RecreateCalled: func(root common.RootHashHolder) (common.Trie, error) { return &trieMock.TrieStub{}, nil }, GetStorageManagerCalled: func() common.StorageManager { @@ -2174,7 +2174,7 @@ func TestAccountsDB_RecreateAllTries(t *testing.T) { return nil }, - RecreateCalled: func(root []byte) (common.Trie, error) { + RecreateCalled: func(root common.RootHashHolder) (common.Trie, error) { return &trieMock.TrieStub{}, nil }, GetStorageManagerCalled: func() common.StorageManager { @@ -2346,8 +2346,8 @@ func TestAccountsDB_GetAccountFromBytes(t *testing.T) { }, } args.Trie = &trieMock.TrieStub{ - RecreateCalled: func(root []byte) (common.Trie, error) { - assert.Equal(t, rootHash, root) + RecreateCalled: func(root common.RootHashHolder) (common.Trie, error) { + assert.Equal(t, rootHash, root.GetRootHash()) return &trieMock.TrieStub{}, nil }, GetStorageManagerCalled: func() common.StorageManager { @@ -2379,7 +2379,7 @@ func TestAccountsDB_GetAccountFromBytesShouldLoadDataTrie(t *testing.T) { } return nil, 0, nil }, - RecreateCalled: func(root []byte) (d common.Trie, err error) { + RecreateCalled: func(root common.RootHashHolder) (d common.Trie, err error) { return dataTrie, nil }, GetStorageManagerCalled: func() common.StorageManager { @@ -2930,7 +2930,7 @@ func testAccountMethodsConcurrency( assert.Nil(t, err) for i := 0; i < numOperations; i++ { go func(idx int) { - switch idx % 22 { + switch idx % 21 { case 0: _, _ = adb.GetExistingAccount(addresses[idx]) case 1: @@ -2954,26 +2954,24 @@ func testAccountMethodsConcurrency( case 10: _, _ = adb.RootHash() case 11: - _ = adb.RecreateTrie(rootHash) + _ = adb.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) case 12: - _ = adb.RecreateTrieFromEpoch(holders.NewRootHashHolder(rootHash, core.OptionalUint32{})) - case 13: adb.PruneTrie(rootHash, state.OldRoot, state.NewPruningHandler(state.DisableDataRemoval)) - case 14: + case 13: adb.CancelPrune(rootHash, state.NewRoot) - case 15: + case 14: adb.SnapshotState(rootHash, 0) - case 16: + case 15: _ = adb.IsPruningEnabled() - case 17: + case 16: _ = adb.GetAllLeaves(&common.TrieIteratorChannels{}, context.Background(), rootHash, parsers.NewMainTrieLeafParser()) - case 18: + case 17: _, _ = adb.RecreateAllTries(rootHash) - case 19: + case 18: _, _ = adb.GetTrie(rootHash) - case 20: + case 19: _ = adb.GetStackDebugFirstEntry() - case 21: + case 20: _ = adb.SetSyncer(&mock.AccountsDBSyncerStub{}) } wg.Done() @@ -3063,7 +3061,7 @@ func testAccountLoadInParallel( case 1: _, _ = adb.GetExistingAccount(addresses[idx]) case 2: - _ = adb.RecreateTrie(rootHash) + _ = adb.RecreateTrie(holders.NewDefaultRootHashesHolder(rootHash)) } }(i) } diff --git a/state/interface.go b/state/interface.go index bf515803346..ce1578c8c36 100644 --- a/state/interface.go +++ b/state/interface.go @@ -78,8 +78,7 @@ type AccountsAdapter interface { RevertToSnapshot(snapshot int) error GetCode(codeHash []byte) []byte RootHash() ([]byte, error) - RecreateTrie(rootHash []byte) error - RecreateTrieFromEpoch(options common.RootHashHolder) error + RecreateTrie(options common.RootHashHolder) error PruneTrie(rootHash []byte, identifier TriePruningIdentifier, handler PruningHandler) CancelPrune(rootHash []byte, identifier TriePruningIdentifier) SnapshotState(rootHash []byte, epoch uint32) diff --git a/state/peerAccountsDB_test.go b/state/peerAccountsDB_test.go index 2165357c7ec..3e1f2322c24 100644 --- a/state/peerAccountsDB_test.go +++ b/state/peerAccountsDB_test.go @@ -163,7 +163,7 @@ func TestNewPeerAccountsDB_RecreateAllTries(t *testing.T) { GetStorageManagerCalled: func() common.StorageManager { return &storageManager.StorageManagerStub{} }, - RecreateCalled: func(_ []byte) (common.Trie, error) { + RecreateCalled: func(_ common.RootHashHolder) (common.Trie, error) { recreateCalled = true return nil, nil }, diff --git a/state/storagePruningManager/storagePruningManager_test.go b/state/storagePruningManager/storagePruningManager_test.go index d195d4ef5c9..25bb2860b84 100644 --- a/state/storagePruningManager/storagePruningManager_test.go +++ b/state/storagePruningManager/storagePruningManager_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/multiversx/mx-chain-go/common" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/common/statistics" "github.com/multiversx/mx-chain-go/config" "github.com/multiversx/mx-chain-go/state" @@ -223,7 +224,7 @@ func TestAccountsDB_PruneAfterCancelPruneShouldFail(t *testing.T) { spm.CancelPrune(rootHash, state.OldRoot, trieStorage) spm.PruneTrie(rootHash, state.OldRoot, trieStorage, state.NewPruningHandler(state.EnableDataRemoval)) - newTr, err := tr.Recreate(rootHash) + newTr, err := tr.Recreate(holders.NewDefaultRootHashesHolder(rootHash)) assert.Nil(t, err) assert.NotNil(t, newTr) } diff --git a/state/syncer/baseAccountsSyncer.go b/state/syncer/baseAccountsSyncer.go index e6bf39f45b2..3cee93d7325 100644 --- a/state/syncer/baseAccountsSyncer.go +++ b/state/syncer/baseAccountsSyncer.go @@ -12,6 +12,7 @@ import ( "github.com/multiversx/mx-chain-core-go/hashing" "github.com/multiversx/mx-chain-core-go/marshal" "github.com/multiversx/mx-chain-go/common" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/state" "github.com/multiversx/mx-chain-go/storage" "github.com/multiversx/mx-chain-go/trie" @@ -225,7 +226,8 @@ func (b *baseAccountsSyncer) GetSyncedTries() map[string]common.Trie { var recreatedTrie common.Trie clonedMap := make(map[string]common.Trie, len(b.dataTries)) for key := range b.dataTries { - recreatedTrie, err = dataTrie.Recreate([]byte(key)) + rootHashHolder := holders.NewDefaultRootHashesHolder([]byte(key)) + recreatedTrie, err = dataTrie.Recreate(rootHashHolder) if err != nil { log.Warn("error recreating trie in baseAccountsSyncer.GetSyncedTries", "roothash", []byte(key), "error", err) diff --git a/state/trackableDataTrie/trackableDataTrie.go b/state/trackableDataTrie/trackableDataTrie.go index 3d2fc53d8e5..5808e3833e2 100644 --- a/state/trackableDataTrie/trackableDataTrie.go +++ b/state/trackableDataTrie/trackableDataTrie.go @@ -9,6 +9,7 @@ import ( "github.com/multiversx/mx-chain-core-go/hashing" "github.com/multiversx/mx-chain-core-go/marshal" "github.com/multiversx/mx-chain-go/common" + "github.com/multiversx/mx-chain-go/common/holders" errorsCommon "github.com/multiversx/mx-chain-go/errors" "github.com/multiversx/mx-chain-go/state" "github.com/multiversx/mx-chain-go/state/dataTrieValue" @@ -214,7 +215,8 @@ func (tdt *trackableDataTrie) SaveDirtyData(mainTrie common.Trie) ([]core.TrieDa } if check.IfNil(tdt.tr) { - newDataTrie, err := mainTrie.Recreate(make([]byte, 0)) + emptyRootHash := holders.NewDefaultRootHashesHolder(make([]byte, 0)) + newDataTrie, err := mainTrie.Recreate(emptyRootHash) if err != nil { return nil, err } diff --git a/state/trackableDataTrie/trackableDataTrie_test.go b/state/trackableDataTrie/trackableDataTrie_test.go index eec11bb0847..fb7b47182df 100644 --- a/state/trackableDataTrie/trackableDataTrie_test.go +++ b/state/trackableDataTrie/trackableDataTrie_test.go @@ -367,7 +367,7 @@ func TestTrackableDataTrie_SaveDirtyData(t *testing.T) { recreateCalled := false trie := &trieMock.TrieStub{ - RecreateCalled: func(root []byte) (common.Trie, error) { + RecreateCalled: func(root common.RootHashHolder) (common.Trie, error) { recreateCalled = true return &trieMock.TrieStub{ GetCalled: func(_ []byte) ([]byte, uint32, error) { diff --git a/testscommon/state/accountsAdapterStub.go b/testscommon/state/accountsAdapterStub.go index abb1788a076..034d1f6ae26 100644 --- a/testscommon/state/accountsAdapterStub.go +++ b/testscommon/state/accountsAdapterStub.go @@ -23,8 +23,7 @@ type AccountsStub struct { JournalLenCalled func() int RevertToSnapshotCalled func(snapshot int) error RootHashCalled func() ([]byte, error) - RecreateTrieCalled func(rootHash []byte) error - RecreateTrieFromEpochCalled func(options common.RootHashHolder) error + RecreateTrieCalled func(options common.RootHashHolder) error PruneTrieCalled func(rootHash []byte, identifier state.TriePruningIdentifier, handler state.PruningHandler) CancelPruneCalled func(rootHash []byte, identifier state.TriePruningIdentifier) SnapshotStateCalled func(rootHash []byte, epoch uint32) @@ -176,18 +175,9 @@ func (as *AccountsStub) RootHash() ([]byte, error) { } // RecreateTrie - -func (as *AccountsStub) RecreateTrie(rootHash []byte) error { +func (as *AccountsStub) RecreateTrie(options common.RootHashHolder) error { if as.RecreateTrieCalled != nil { - return as.RecreateTrieCalled(rootHash) - } - - return errNotImplemented -} - -// RecreateTrieFromEpoch - -func (as *AccountsStub) RecreateTrieFromEpoch(options common.RootHashHolder) error { - if as.RecreateTrieFromEpochCalled != nil { - return as.RecreateTrieFromEpochCalled(options) + return as.RecreateTrieCalled(options) } return errNotImplemented diff --git a/testscommon/trie/trieStub.go b/testscommon/trie/trieStub.go index 81c90867e92..30e0ba6066e 100644 --- a/testscommon/trie/trieStub.go +++ b/testscommon/trie/trieStub.go @@ -19,8 +19,7 @@ type TrieStub struct { DeleteCalled func(key []byte) error RootCalled func() ([]byte, error) CommitCalled func() error - RecreateCalled func(root []byte) (common.Trie, error) - RecreateFromEpochCalled func(options common.RootHashHolder) (common.Trie, error) + RecreateCalled func(options common.RootHashHolder) (common.Trie, error) GetObsoleteHashesCalled func() [][]byte AppendToOldHashesCalled func([][]byte) GetSerializedNodesCalled func([]byte, uint64) ([][]byte, uint64, error) @@ -136,18 +135,9 @@ func (ts *TrieStub) Commit() error { } // Recreate - -func (ts *TrieStub) Recreate(root []byte) (common.Trie, error) { +func (ts *TrieStub) Recreate(options common.RootHashHolder) (common.Trie, error) { if ts.RecreateCalled != nil { - return ts.RecreateCalled(root) - } - - return nil, errNotImplemented -} - -// RecreateFromEpoch - -func (ts *TrieStub) RecreateFromEpoch(options common.RootHashHolder) (common.Trie, error) { - if ts.RecreateFromEpochCalled != nil { - return ts.RecreateFromEpochCalled(options) + return ts.RecreateCalled(options) } return nil, errNotImplemented diff --git a/trie/depthFirstSync_test.go b/trie/depthFirstSync_test.go index 456c1b1f3e8..409c618a2c6 100644 --- a/trie/depthFirstSync_test.go +++ b/trie/depthFirstSync_test.go @@ -11,6 +11,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-go/common" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/storage" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -121,7 +122,7 @@ func TestDepthFirstTrieSyncer_StartSyncingNewTrieShouldWork(t *testing.T) { tsm, _ := arg.DB.(*trieStorageManager) db, _ := tsm.mainStorer.(storage.Persister) trie, _ := createInMemoryTrieFromDB(db) - trie, _ = trie.Recreate(roothash) + trie, _ = trie.Recreate(holders.NewDefaultRootHashesHolder(roothash)) require.False(t, check.IfNil(trie)) var val []byte @@ -198,7 +199,7 @@ func TestDepthFirstTrieSyncer_StartSyncingPartiallyFilledTrieShouldWork(t *testi tsm, _ := arg.DB.(*trieStorageManager) db, _ := tsm.mainStorer.(storage.Persister) trie, _ := createInMemoryTrieFromDB(db) - trie, _ = trie.Recreate(roothash) + trie, _ = trie.Recreate(holders.NewDefaultRootHashesHolder(roothash)) require.False(t, check.IfNil(trie)) var val []byte diff --git a/trie/doubleListSync_test.go b/trie/doubleListSync_test.go index e4d737cf8f0..8e631237cc6 100644 --- a/trie/doubleListSync_test.go +++ b/trie/doubleListSync_test.go @@ -11,6 +11,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-go/common" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/storage" "github.com/multiversx/mx-chain-go/storage/storageunit" "github.com/multiversx/mx-chain-go/testscommon" @@ -201,7 +202,7 @@ func TestDoubleListTrieSyncer_StartSyncingNewTrieShouldWork(t *testing.T) { tsm, _ := arg.DB.(*trieStorageManager) db, _ := tsm.mainStorer.(storage.Persister) trie, _ := createInMemoryTrieFromDB(db) - trie, _ = trie.Recreate(roothash) + trie, _ = trie.Recreate(holders.NewDefaultRootHashesHolder(roothash)) require.False(t, check.IfNil(trie)) var val []byte @@ -278,7 +279,7 @@ func TestDoubleListTrieSyncer_StartSyncingPartiallyFilledTrieShouldWork(t *testi tsm, _ := arg.DB.(*trieStorageManager) db, _ := tsm.mainStorer.(storage.Persister) trie, _ := createInMemoryTrieFromDB(db) - trie, _ = trie.Recreate(roothash) + trie, _ = trie.Recreate(holders.NewDefaultRootHashesHolder(roothash)) require.False(t, check.IfNil(trie)) var val []byte diff --git a/trie/extensionNode_test.go b/trie/extensionNode_test.go index ffc46b7d6b0..5ae78db766a 100644 --- a/trie/extensionNode_test.go +++ b/trie/extensionNode_test.go @@ -9,6 +9,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-go/common" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/storage/cache" "github.com/multiversx/mx-chain-go/testscommon" "github.com/multiversx/mx-chain-go/testscommon/hashingMocks" @@ -732,7 +733,7 @@ func TestExtensionNode_reduceNodeCollapsedNode(t *testing.T) { tr := initTrie() _ = tr.Commit() rootHash, _ := tr.RootHash() - collapsedTrie, _ := tr.Recreate(rootHash) + collapsedTrie, _ := tr.Recreate(holders.NewDefaultRootHashesHolder(rootHash)) err := collapsedTrie.Delete([]byte("doe")) assert.Nil(t, err) diff --git a/trie/patriciaMerkleTrie.go b/trie/patriciaMerkleTrie.go index 0f875999bd1..70df549011f 100644 --- a/trie/patriciaMerkleTrie.go +++ b/trie/patriciaMerkleTrie.go @@ -269,13 +269,8 @@ func (tr *patriciaMerkleTrie) Commit() error { return nil } -// Recreate returns a new trie that has the given root hash and database -func (tr *patriciaMerkleTrie) Recreate(root []byte) (common.Trie, error) { - return tr.recreate(root, tr.trieStorage) -} - -// RecreateFromEpoch returns a new trie, given the options -func (tr *patriciaMerkleTrie) RecreateFromEpoch(options common.RootHashHolder) (common.Trie, error) { +// Recreate returns a new trie, given the options +func (tr *patriciaMerkleTrie) Recreate(options common.RootHashHolder) (common.Trie, error) { if check.IfNil(options) { return nil, ErrNilRootHashHolder } diff --git a/trie/patriciaMerkleTrie_test.go b/trie/patriciaMerkleTrie_test.go index 63278d43a1f..4612b8ee331 100644 --- a/trie/patriciaMerkleTrie_test.go +++ b/trie/patriciaMerkleTrie_test.go @@ -389,27 +389,12 @@ func TestPatriciaMerkleTree_DeleteNotPresent(t *testing.T) { func TestPatriciaMerkleTrie_Recreate(t *testing.T) { t.Parallel() - tr := initTrie() - rootHash, _ := tr.RootHash() - _ = tr.Commit() - - newTr, err := tr.Recreate(rootHash) - assert.Nil(t, err) - assert.NotNil(t, newTr) - - root, _ := newTr.RootHash() - assert.Equal(t, rootHash, root) -} - -func TestPatriciaMerkleTrie_RecreateFromEpoch(t *testing.T) { - t.Parallel() - t.Run("nil options", func(t *testing.T) { t.Parallel() tr := initTrie() - newTr, err := tr.RecreateFromEpoch(nil) + newTr, err := tr.Recreate(nil) assert.Nil(t, newTr) assert.Equal(t, trie.ErrNilRootHashHolder, err) }) @@ -421,8 +406,8 @@ func TestPatriciaMerkleTrie_RecreateFromEpoch(t *testing.T) { rootHash, _ := tr.RootHash() _ = tr.Commit() - rootHashHolder := holders.NewRootHashHolder(rootHash, core.OptionalUint32{}) - newTr, err := tr.RecreateFromEpoch(rootHashHolder) + rootHashHolder := holders.NewDefaultRootHashesHolder(rootHash) + newTr, err := tr.Recreate(rootHashHolder) assert.Nil(t, err) assert.True(t, trie.IsBaseTrieStorageManager(newTr.GetStorageManager())) @@ -440,7 +425,7 @@ func TestPatriciaMerkleTrie_RecreateFromEpoch(t *testing.T) { HasValue: true, } rootHashHolder := holders.NewRootHashHolder(rootHash, optionalUint32) - newTr, err := tr.RecreateFromEpoch(rootHashHolder) + newTr, err := tr.Recreate(rootHashHolder) assert.Nil(t, err) assert.True(t, trie.IsTrieStorageManagerInEpoch(newTr.GetStorageManager())) @@ -452,7 +437,7 @@ func TestPatriciaMerkleTrie_RecreateWithInvalidRootHash(t *testing.T) { tr := initTrie() - newTr, err := tr.Recreate(nil) + newTr, err := tr.Recreate(holders.NewDefaultRootHashesHolder([]byte{})) assert.Nil(t, err) root, _ := newTr.RootHash() assert.Equal(t, emptyTrieHash, root) @@ -967,7 +952,7 @@ func TestPatriciaMerkleTrie_ConcurrentOperations(t *testing.T) { numOperations := 1000 wg := sync.WaitGroup{} wg.Add(numOperations) - numFunctions := 19 + numFunctions := 18 initialRootHash, _ := tr.RootHash() @@ -993,31 +978,28 @@ func TestPatriciaMerkleTrie_ConcurrentOperations(t *testing.T) { err := tr.Commit() assert.Nil(t, err) case 5: - _, err := tr.Recreate(initialRootHash) - assert.Nil(t, err) - case 6: epoch := core.OptionalUint32{ Value: 3, HasValue: true, } rootHashHolder := holders.NewRootHashHolder(initialRootHash, epoch) - _, err := tr.RecreateFromEpoch(rootHashHolder) + _, err := tr.Recreate(rootHashHolder) assert.Nil(t, err) - case 7: + case 6: _ = tr.String() - case 8: + case 7: _ = tr.GetObsoleteHashes() - case 9: + case 8: _, err := tr.GetDirtyHashes() assert.Nil(t, err) - case 10: + case 9: _, err := tr.GetSerializedNode(initialRootHash) assert.Nil(t, err) - case 11: + case 10: size1KB := uint64(1024 * 1024) _, _, err := tr.GetSerializedNodes(initialRootHash, size1KB) assert.Nil(t, err) - case 12: + case 11: trieIteratorChannels := &common.TrieIteratorChannels{ LeavesChan: make(chan core.KeyValueHolder, 1000), ErrChan: errChan.NewErrChanWrapper(), @@ -1031,20 +1013,20 @@ func TestPatriciaMerkleTrie_ConcurrentOperations(t *testing.T) { parsers.NewMainTrieLeafParser(), ) assert.Nil(t, err) - case 13: + case 12: _, err := tr.GetAllHashes() assert.Nil(t, err) - case 14: + case 13: _, _, _ = tr.GetProof(initialRootHash) // this might error due to concurrent operations that change the roothash - case 15: + case 14: // extremely hard to compute an existing hash due to concurrent changes. _, _ = tr.VerifyProof([]byte("dog"), []byte("puppy"), [][]byte{[]byte("proof1")}) // this might error due to concurrent operations that change the roothash - case 16: + case 15: sm := tr.GetStorageManager() assert.NotNil(t, sm) - case 17: + case 16: _ = tr.GetOldRoot() - case 18: + case 17: trieStatsHandler := tr.(common.TrieStats) _, err := trieStatsHandler.GetTrieStats("address", initialRootHash) assert.Nil(t, err) @@ -1384,7 +1366,7 @@ func TestPatriciaMerkleTrie_CollectLeavesForMigration(t *testing.T) { addDefaultDataToTrie(tr) _ = tr.Commit() rootHash, _ := tr.RootHash() - collapsedTrie, _ := tr.Recreate(rootHash) + collapsedTrie, _ := tr.Recreate(holders.NewDefaultRootHashesHolder(rootHash)) dtr := collapsedTrie.(dataTrie) dtm := &trieMock.DataTrieMigratorStub{ ConsumeStorageLoadGasCalled: func() bool {