From 0a10cab9d60b66c2ea4980dedd2403acb95e645d Mon Sep 17 00:00:00 2001 From: BeniaminDrasovean Date: Wed, 6 Mar 2024 11:52:36 +0200 Subject: [PATCH 1/2] merge RecreateTrie and RecreateTrieFromEpoch --- common/holders/rootHashHolder.go | 11 ++- common/holders/rootHashHolder_test.go | 4 +- common/interface.go | 3 +- .../disabled/disabledAccountsAdapter.go | 7 +- genesis/process/metaGenesisBlockCreator.go | 4 +- genesis/process/shardGenesisBlockCreator.go | 4 +- .../benchmarks/loadFromTrie_test.go | 5 +- .../state/stateTrie/stateTrie_test.go | 41 +++++------ .../state/stateTrieSync/stateTrieSync_test.go | 9 +-- .../vm/wasm/wasmvm/wasmVM_test.go | 3 +- node/node_test.go | 58 ++++++++-------- .../delegatedListProcessor_test.go | 4 +- .../directStakedListProcessor_test.go | 4 +- .../stakeValuesProcessor_test.go | 12 ++-- process/block/baseProcess_test.go | 2 +- process/block/metablock.go | 4 +- process/block/metablock_test.go | 5 +- process/block/shardblock.go | 5 +- process/peer/process.go | 9 ++- process/smartContract/scQueryService.go | 10 ++- process/smartContract/scQueryService_test.go | 60 ++++++++-------- process/sync/metablock_test.go | 4 +- process/sync/shardblock_test.go | 4 +- .../simulationAccountsDB.go | 7 +- .../simulationAccountsDB_test.go | 2 +- state/accountsDB.go | 28 ++++---- state/accountsDBApi.go | 15 ++-- state/accountsDBApiWithHistory.go | 9 +-- state/accountsDBApiWithHistory_test.go | 21 +++--- state/accountsDBApi_test.go | 48 ++++++------- state/accountsDB_test.go | 68 +++++++++---------- state/interface.go | 6 +- state/peerAccountsDB_test.go | 2 +- .../storagePruningManager_test.go | 3 +- state/syncer/baseAccountsSyncer.go | 4 +- state/trackableDataTrie/trackableDataTrie.go | 4 +- .../trackableDataTrie_test.go | 2 +- testscommon/state/accountsAdapterStub.go | 16 +---- testscommon/trie/trieStub.go | 16 +---- trie/depthFirstSync_test.go | 5 +- trie/doubleListSync_test.go | 5 +- trie/extensionNode_test.go | 3 +- trie/patriciaMerkleTrie.go | 9 +-- trie/patriciaMerkleTrie_test.go | 58 ++++++---------- 44 files changed, 284 insertions(+), 319 deletions(-) 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 2e14c33730e..3ec5a6fe516 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 61e06df194d..bcd5b566b39 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 40b5f606241..395110f066a 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" @@ -189,7 +190,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 9fef8f05569..c203ae1daba 100644 --- a/genesis/process/shardGenesisBlockCreator.go +++ b/genesis/process/shardGenesisBlockCreator.go @@ -16,6 +16,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" @@ -297,7 +298,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 470f722e899..e31ff52e603 100644 --- a/integrationTests/benchmarks/loadFromTrie_test.go +++ b/integrationTests/benchmarks/loadFromTrie_test.go @@ -9,6 +9,7 @@ import ( "github.com/multiversx/mx-chain-core-go/hashing/blake2b" "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/integrationTests" "github.com/multiversx/mx-chain-go/storage" "github.com/multiversx/mx-chain-go/storage/database" @@ -63,7 +64,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))) @@ -100,7 +101,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 3bc5184767b..048eef52b8c 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/config" "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/epochStart" @@ -241,7 +242,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 @@ -278,7 +279,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) @@ -328,7 +329,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 @@ -1028,7 +1029,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) } @@ -1158,7 +1159,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) @@ -1205,7 +1206,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) @@ -1250,7 +1251,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) @@ -1271,7 +1272,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) @@ -1364,7 +1365,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())) } @@ -1382,7 +1383,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()) @@ -1445,7 +1446,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) } @@ -1525,7 +1526,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())) } @@ -1673,12 +1674,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) @@ -1690,7 +1691,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) } @@ -1852,14 +1853,14 @@ func testNodeStateCheckpointSnapshotAndPruning( stateTrie := node.TrieContainer.Get([]byte(dataRetriever.UserAccountsUnit.String())) assert.Equal(t, 6, len(checkpointsRootHashes)) for i := range checkpointsRootHashes { - tr, err := stateTrie.Recreate(checkpointsRootHashes[i]) + tr, err := stateTrie.Recreate(holders.NewDefaultRootHashesHolder(checkpointsRootHashes[i])) require.Nil(t, err) require.NotNil(t, tr) } 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) } @@ -1867,7 +1868,7 @@ func testNodeStateCheckpointSnapshotAndPruning( 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) } @@ -2179,10 +2180,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 8bfbd584a70..74362efac08 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" @@ -135,7 +136,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() @@ -351,7 +352,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) @@ -501,7 +502,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) @@ -537,7 +538,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 9df0d4e22b5..b9df8f2a40e 100644 --- a/integrationTests/vm/wasm/wasmvm/wasmVM_test.go +++ b/integrationTests/vm/wasm/wasmvm/wasmVM_test.go @@ -7,6 +7,7 @@ package wasmvm import ( "encoding/hex" "fmt" + "github.com/multiversx/mx-chain-go/common/holders" "math" "math/big" "testing" @@ -805,7 +806,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 d341df93636..822722edc09 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -112,7 +112,7 @@ func getAccAdapter(balance *big.Int) *stateMock.AccountsStub { return acc, nil } - accDB.RecreateTrieCalled = func(_ []byte) error { + accDB.RecreateTrieCalled = func(_ common.RootHashHolder) error { return nil } @@ -236,7 +236,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 }, } @@ -338,7 +338,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 }, } @@ -409,7 +409,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 }, } @@ -474,7 +474,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 } @@ -534,7 +534,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 } @@ -588,7 +588,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 } @@ -627,7 +627,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 }, } @@ -670,7 +670,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 }, } @@ -709,7 +709,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 }, } @@ -752,7 +752,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 }, } @@ -807,7 +807,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 }, } @@ -867,7 +867,7 @@ func TestNode_GetAllESDTTokens(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -924,7 +924,7 @@ func TestNode_GetAllESDTTokens_GetAllLeavesShouldFail(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -980,7 +980,7 @@ func TestNode_GetAllESDTTokensContextShouldTimeout(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1023,7 +1023,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 }, } @@ -1114,7 +1114,7 @@ func TestNode_GetAllESDTTokensShouldReturnEsdtAndFormattedNft(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1198,7 +1198,7 @@ func TestNode_GetAllIssuedESDTs(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1284,7 +1284,7 @@ func TestNode_GetESDTsWithRole(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1363,7 +1363,7 @@ func TestNode_GetESDTsRoles(t *testing.T) { }) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1429,7 +1429,7 @@ func TestNode_GetNFTTokenIDsRegisteredByAddress(t *testing.T) { ) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -1486,7 +1486,7 @@ func TestNode_GetNFTTokenIDsRegisteredByAddressContextShouldTimeout(t *testing.T ) accDB := &stateMock.AccountsStub{ - RecreateTrieCalled: func(rootHash []byte) error { + RecreateTrieCalled: func(rootHash common.RootHashHolder) error { return nil }, } @@ -3368,7 +3368,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 }, } @@ -3415,7 +3415,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 }, } @@ -4057,7 +4057,7 @@ func TestNode_getProofErrWhenComputingProof(t *testing.T) { }, }, nil }, - RecreateTrieCalled: func(_ []byte) error { + RecreateTrieCalled: func(_ common.RootHashHolder) error { return nil }, } @@ -4743,7 +4743,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 }, } @@ -4784,7 +4784,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 }, } @@ -4812,7 +4812,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 }, } @@ -4930,7 +4930,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 2921d29caaa..9e39fdf53e6 100644 --- a/process/block/baseProcess_test.go +++ b/process/block/baseProcess_test.go @@ -1029,7 +1029,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 86126bc2c29..05c3587ada6 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" @@ -1582,7 +1583,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 30051e3d582..9b4b9dd004d 100644 --- a/process/block/metablock_test.go +++ b/process/block/metablock_test.go @@ -13,6 +13,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core/atomic" "github.com/multiversx/mx-chain-core-go/data" "github.com/multiversx/mx-chain-core-go/data/block" + "github.com/multiversx/mx-chain-go/common" "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/dataRetriever/blockchain" "github.com/multiversx/mx-chain-go/process" @@ -1203,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 }, } @@ -1231,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 8da3e4a07c1..4527caaf5c9 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" @@ -402,8 +403,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 2de1efce03f..4f818798c2e 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" @@ -80,7 +81,8 @@ type validatorStatistics struct { } // NewValidatorStatisticsProcessor instantiates a new validatorStatistics structure responsible for keeping account of -// each validator actions in the consensus process +// +// each validator actions in the consensus process func NewValidatorStatisticsProcessor(arguments ArgValidatorStatisticsProcessor) (*validatorStatistics, error) { if check.IfNil(arguments.PeerAdapter) { return nil, process.ErrNilPeerAccountsAdapter @@ -864,9 +866,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 10a5be173da..232b97fc66f 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) } func (service *SCQueryService) getCurrentEpoch() uint32 { 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 25af794e196..8ba32e95801 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 7bb474269f3..fa709a51637 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 bc41d151da1..598f4e8e341 100644 --- a/state/accountsDB.go +++ b/state/accountsDB.go @@ -461,7 +461,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) } @@ -586,7 +587,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 } @@ -775,7 +777,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-- { @@ -934,13 +936,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() @@ -962,7 +959,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 } @@ -1008,7 +1005,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 } @@ -1046,7 +1044,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 } @@ -1059,7 +1058,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 791bfc658df..b9408d1cd1e 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 97d698e0b68..8870dac094b 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 } @@ -232,7 +227,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 beb7ad371bb..982f822092a 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" @@ -100,14 +99,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 }, } @@ -123,7 +122,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 }, @@ -148,7 +147,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 }, @@ -169,7 +168,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 }, @@ -190,13 +189,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 }, } @@ -212,7 +211,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 }, @@ -250,7 +249,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() @@ -284,7 +283,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 1a22366ab06..fd56c05bedb 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) }) @@ -289,7 +289,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 @@ -336,7 +336,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) { @@ -355,7 +355,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 }, @@ -380,7 +380,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) { @@ -399,7 +399,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 }, @@ -424,7 +424,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) { @@ -443,7 +443,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 }, @@ -468,7 +468,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 { @@ -487,7 +487,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 }, @@ -511,7 +511,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 { @@ -530,7 +530,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 }, @@ -555,13 +555,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 95785e9c231..fda69b8cfcf 100644 --- a/state/accountsDB_test.go +++ b/state/accountsDB_test.go @@ -510,7 +510,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 { @@ -588,7 +588,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 { @@ -811,8 +811,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") } @@ -856,7 +856,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 @@ -904,14 +904,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) } @@ -926,13 +926,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) @@ -948,14 +948,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) @@ -1466,7 +1466,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()) } @@ -1985,7 +1985,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) } @@ -1994,7 +1994,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) } @@ -2003,7 +2003,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) } @@ -2012,7 +2012,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) } @@ -2398,7 +2398,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 }, } @@ -2426,7 +2426,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 }, } @@ -2595,8 +2595,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 }, } @@ -2625,7 +2625,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 { @@ -3168,7 +3168,7 @@ func testAccountMethodsConcurrency( assert.Nil(t, err) for i := 0; i < numOperations; i++ { go func(idx int) { - switch idx % 23 { + switch idx % 22 { case 0: _, _ = adb.GetExistingAccount(addresses[idx]) case 1: @@ -3192,28 +3192,26 @@ 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.SetStateCheckpoint(rootHash) - case 17: + case 16: _ = adb.IsPruningEnabled() - case 18: + case 17: _ = adb.GetAllLeaves(&common.TrieIteratorChannels{}, context.Background(), rootHash, parsers.NewMainTrieLeafParser()) - case 19: + case 18: _, _ = adb.RecreateAllTries(rootHash) - case 20: + case 19: _, _ = adb.GetTrie(rootHash) - case 21: + case 20: _ = adb.GetStackDebugFirstEntry() - case 22: + case 21: _ = adb.SetSyncer(&mock.AccountsDBSyncerStub{}) } wg.Done() @@ -3306,7 +3304,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 56dd0e1b8c4..646b5bed38a 100644 --- a/state/interface.go +++ b/state/interface.go @@ -37,8 +37,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) @@ -183,7 +182,8 @@ type DataTrie interface { } // PeerAccountHandler models a peer state account, which can journalize a normal account's data -// with some extra features like signing statistics or rating information +// +// with some extra features like signing statistics or rating information type PeerAccountHandler interface { SetBLSPublicKey([]byte) error GetRewardAddress() []byte diff --git a/state/peerAccountsDB_test.go b/state/peerAccountsDB_test.go index 65beb8432dd..2e2076f4a0b 100644 --- a/state/peerAccountsDB_test.go +++ b/state/peerAccountsDB_test.go @@ -187,7 +187,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 104a198becd..338535fd0b7 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/config" "github.com/multiversx/mx-chain-go/state" "github.com/multiversx/mx-chain-go/state/factory" @@ -212,7 +213,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 a01f1155fed..452534a1d92 100644 --- a/state/syncer/baseAccountsSyncer.go +++ b/state/syncer/baseAccountsSyncer.go @@ -3,6 +3,7 @@ package syncer import ( "context" "fmt" + "github.com/multiversx/mx-chain-go/common/holders" "sync" "sync/atomic" "time" @@ -224,7 +225,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 8a2fe8812ef..08131e22899 100644 --- a/state/trackableDataTrie/trackableDataTrie.go +++ b/state/trackableDataTrie/trackableDataTrie.go @@ -2,6 +2,7 @@ package trackableDataTrie import ( "fmt" + "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" @@ -208,7 +209,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 42f6ebc4189..5c67328dd38 100644 --- a/state/trackableDataTrie/trackableDataTrie_test.go +++ b/state/trackableDataTrie/trackableDataTrie_test.go @@ -345,7 +345,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 c5cf9f74535..f20d6401d9e 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) @@ -177,18 +176,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 65197f171fc..94113a25fd0 100644 --- a/trie/doubleListSync_test.go +++ b/trie/doubleListSync_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "github.com/multiversx/mx-chain-go/common/holders" "sync" "testing" "time" @@ -213,7 +214,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 @@ -290,7 +291,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 ac243f3aaff..02030e9d772 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 501539a3e54..7969a952ee2 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) @@ -994,7 +979,7 @@ func TestPatriciaMerkleTrie_ConcurrentOperations(t *testing.T) { numOperations := 1000 wg := sync.WaitGroup{} wg.Add(numOperations) - numFunctions := 19 + numFunctions := 18 initialRootHash, _ := tr.RootHash() @@ -1020,31 +1005,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(), @@ -1058,20 +1040,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) @@ -1401,7 +1383,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 { From d055cae508b93b73e106a9d63753be03718359db Mon Sep 17 00:00:00 2001 From: BeniaminDrasovean Date: Wed, 6 Mar 2024 12:35:50 +0200 Subject: [PATCH 2/2] sort imports --- integrationTests/vm/wasm/wasmvm/wasmVM_test.go | 2 +- state/syncer/baseAccountsSyncer.go | 2 +- state/trackableDataTrie/trackableDataTrie.go | 2 +- trie/doubleListSync_test.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/integrationTests/vm/wasm/wasmvm/wasmVM_test.go b/integrationTests/vm/wasm/wasmvm/wasmVM_test.go index b9df8f2a40e..abb475535c8 100644 --- a/integrationTests/vm/wasm/wasmvm/wasmVM_test.go +++ b/integrationTests/vm/wasm/wasmvm/wasmVM_test.go @@ -7,7 +7,6 @@ package wasmvm import ( "encoding/hex" "fmt" - "github.com/multiversx/mx-chain-go/common/holders" "math" "math/big" "testing" @@ -19,6 +18,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" diff --git a/state/syncer/baseAccountsSyncer.go b/state/syncer/baseAccountsSyncer.go index 8ff8e87bef8..3cee93d7325 100644 --- a/state/syncer/baseAccountsSyncer.go +++ b/state/syncer/baseAccountsSyncer.go @@ -3,7 +3,6 @@ package syncer import ( "context" "fmt" - "github.com/multiversx/mx-chain-go/common/holders" "sync" "sync/atomic" "time" @@ -13,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" diff --git a/state/trackableDataTrie/trackableDataTrie.go b/state/trackableDataTrie/trackableDataTrie.go index 3341377975e..5808e3833e2 100644 --- a/state/trackableDataTrie/trackableDataTrie.go +++ b/state/trackableDataTrie/trackableDataTrie.go @@ -2,7 +2,6 @@ package trackableDataTrie import ( "fmt" - "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" @@ -10,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" diff --git a/trie/doubleListSync_test.go b/trie/doubleListSync_test.go index b4d8d3a52ce..8e631237cc6 100644 --- a/trie/doubleListSync_test.go +++ b/trie/doubleListSync_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "github.com/multiversx/mx-chain-go/common/holders" "sync" "testing" "time" @@ -12,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"