Skip to content

Commit

Permalink
Merge pull request #4724 from multiversx/sync-missing-keys-only
Browse files Browse the repository at this point in the history
add custom error for trie get node
  • Loading branch information
BeniaminDrasovean authored Apr 21, 2023
2 parents a729637 + 2a46bcc commit da1e15e
Show file tree
Hide file tree
Showing 74 changed files with 502 additions and 321 deletions.
3 changes: 0 additions & 3 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,6 @@ const HardforkResolversIdentifier = "hardfork resolver"
// EpochStartInterceptorsIdentifier represents the identifier that is used in the start-in-epoch process
const EpochStartInterceptorsIdentifier = "epoch start interceptor"

// GetNodeFromDBErrorString represents the string which is returned when a getting node from DB returns an error
const GetNodeFromDBErrorString = "getNodeFromDB error"

// TimeoutGettingTrieNodes defines the timeout in trie sync operation if no node is received
const TimeoutGettingTrieNodes = 2 * time.Minute // to consider syncing a very large trie node of 64MB at ~1MB/s

Expand Down
1 change: 1 addition & 0 deletions common/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type StorageManager interface {
SetEpochForPutOperation(uint32)
ShouldTakeSnapshot() bool
GetBaseTrieStorageManager() StorageManager
GetIdentifier() string
IsClosed() bool
Close() error
IsInterfaceNil() bool
Expand Down
4 changes: 2 additions & 2 deletions common/logging/errorLogging.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package logging

import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
chainErrors "github.com/multiversx/mx-chain-go/errors"
logger "github.com/multiversx/mx-chain-logger-go"
)

Expand All @@ -24,7 +24,7 @@ func logErrAsLevelExceptAsDebugIfClosingError(logInstance logger.Logger, logLeve
return
}

if chainErrors.IsClosingError(err) {
if core.IsClosingError(err) {
logLevel = logger.LogDebug
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/dataRetriever/factory/containers"
"github.com/multiversx/mx-chain-go/dataRetriever/resolvers"
triesFactory "github.com/multiversx/mx-chain-go/trie/factory"

"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/process/factory"
Expand Down Expand Up @@ -142,7 +141,7 @@ func (mrcf *metaResolversContainerFactory) AddShardTrieNodeResolvers(container d
identifierTrieNodes := factory.AccountTrieNodesTopic + shardC.CommunicationIdentifier(idx)
resolver, err := mrcf.createTrieNodesResolver(
identifierTrieNodes,
triesFactory.UserAccountTrie,
dataRetriever.UserAccountsUnit.String(),
idx,
)
if err != nil {
Expand Down Expand Up @@ -295,7 +294,7 @@ func (mrcf *metaResolversContainerFactory) generateTrieNodesResolvers() error {
identifierTrieNodes := factory.AccountTrieNodesTopic + core.CommunicationIdentifierBetweenShards(core.MetachainShardId, core.MetachainShardId)
resolver, err := mrcf.createTrieNodesResolver(
identifierTrieNodes,
triesFactory.UserAccountTrie,
dataRetriever.UserAccountsUnit.String(),
core.MetachainShardId,
)
if err != nil {
Expand All @@ -308,7 +307,7 @@ func (mrcf *metaResolversContainerFactory) generateTrieNodesResolvers() error {
identifierTrieNodes = factory.ValidatorTrieNodesTopic + core.CommunicationIdentifierBetweenShards(core.MetachainShardId, core.MetachainShardId)
resolver, err = mrcf.createTrieNodesResolver(
identifierTrieNodes,
triesFactory.PeerAccountTrie,
dataRetriever.PeerAccountsUnit.String(),
core.MetachainShardId,
)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/multiversx/mx-chain-go/testscommon/p2pmocks"
storageStubs "github.com/multiversx/mx-chain-go/testscommon/storage"
trieMock "github.com/multiversx/mx-chain-go/testscommon/trie"
triesFactory "github.com/multiversx/mx-chain-go/trie/factory"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -82,8 +81,8 @@ func createStoreForMeta() dataRetriever.StorageService {

func createTriesHolderForMeta() common.TriesHolder {
triesHolder := state.NewDataTriesHolder()
triesHolder.Put([]byte(triesFactory.UserAccountTrie), &trieMock.TrieStub{})
triesHolder.Put([]byte(triesFactory.PeerAccountTrie), &trieMock.TrieStub{})
triesHolder.Put([]byte(dataRetriever.UserAccountsUnit.String()), &trieMock.TrieStub{})
triesHolder.Put([]byte(dataRetriever.PeerAccountsUnit.String()), &trieMock.TrieStub{})
return triesHolder
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/multiversx/mx-chain-go/dataRetriever/factory/containers"
"github.com/multiversx/mx-chain-go/dataRetriever/resolvers"
"github.com/multiversx/mx-chain-go/process/factory"
triesFactory "github.com/multiversx/mx-chain-go/trie/factory"
)

var _ dataRetriever.ResolversContainerFactory = (*shardResolversContainerFactory)(nil)
Expand Down Expand Up @@ -232,7 +231,7 @@ func (srcf *shardResolversContainerFactory) generateTrieNodesResolvers() error {
identifierTrieNodes := factory.AccountTrieNodesTopic + shardC.CommunicationIdentifier(core.MetachainShardId)
resolver, err := srcf.createTrieNodesResolver(
identifierTrieNodes,
triesFactory.UserAccountTrie,
dataRetriever.UserAccountsUnit.String(),
core.MetachainShardId,
)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/multiversx/mx-chain-go/testscommon/p2pmocks"
storageStubs "github.com/multiversx/mx-chain-go/testscommon/storage"
trieMock "github.com/multiversx/mx-chain-go/testscommon/trie"
triesFactory "github.com/multiversx/mx-chain-go/trie/factory"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -88,8 +87,8 @@ func createStoreForShard() dataRetriever.StorageService {

func createTriesHolderForShard() common.TriesHolder {
triesHolder := state.NewDataTriesHolder()
triesHolder.Put([]byte(triesFactory.UserAccountTrie), &trieMock.TrieStub{})
triesHolder.Put([]byte(triesFactory.PeerAccountTrie), &trieMock.TrieStub{})
triesHolder.Put([]byte(dataRetriever.UserAccountsUnit.String()), &trieMock.TrieStub{})
triesHolder.Put([]byte(dataRetriever.PeerAccountsUnit.String()), &trieMock.TrieStub{})
return triesHolder
}

Expand Down
4 changes: 2 additions & 2 deletions epochStart/bootstrap/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ func (e *epochStartBootstrap) syncUserAccountsState(rootHash []byte) error {
}

e.mutTrieStorageManagers.RLock()
trieStorageManager := e.trieStorageManagers[factory.UserAccountTrie]
trieStorageManager := e.trieStorageManagers[dataRetriever.UserAccountsUnit.String()]
e.mutTrieStorageManagers.RUnlock()

argsUserAccountsSyncer := syncer.ArgsNewUserAccountsSyncer{
Expand Down Expand Up @@ -1132,7 +1132,7 @@ func (e *epochStartBootstrap) createStorageService(

func (e *epochStartBootstrap) syncValidatorAccountsState(rootHash []byte) error {
e.mutTrieStorageManagers.RLock()
peerTrieStorageManager := e.trieStorageManagers[factory.PeerAccountTrie]
peerTrieStorageManager := e.trieStorageManagers[dataRetriever.PeerAccountsUnit.String()]
e.mutTrieStorageManagers.RUnlock()

argsValidatorAccountsSyncer := syncer.ArgsNewValidatorAccountsSyncer{
Expand Down
3 changes: 1 addition & 2 deletions epochStart/shardchain/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/errors"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-logger-go"
Expand Down Expand Up @@ -997,7 +996,7 @@ func (t *trigger) SetProcessed(header data.HeaderHandler, _ data.BodyHandler) {
errNotCritical = t.shardHdrStorage.Put([]byte(epochStartIdentifier), shardHdrBuff)
if errNotCritical != nil {
logLevel := logger.LogWarning
if errors.IsClosingError(errNotCritical) {
if core.IsClosingError(errNotCritical) {
logLevel = logger.LogDebug
}
log.Log(logLevel, "SetProcessed put to shard header storage error", "error", errNotCritical)
Expand Down
17 changes: 0 additions & 17 deletions errors/closingError.go

This file was deleted.

45 changes: 0 additions & 45 deletions errors/closingError_test.go

This file was deleted.

7 changes: 3 additions & 4 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package errors

import "errors"
import (
"errors"
)

// ErrAccountsAdapterCreation signals that the accounts adapter cannot be created based on provided data
var ErrAccountsAdapterCreation = errors.New("error creating accounts adapter")
Expand Down Expand Up @@ -470,9 +472,6 @@ var ErrNilScheduledTxsExecutionHandler = errors.New("nil scheduled transactions
// ErrNilScheduledProcessor signals that a nil scheduled processor was provided
var ErrNilScheduledProcessor = errors.New("nil scheduled processor")

// ErrContextClosing signals that the parent context requested the closing of its children
var ErrContextClosing = errors.New("context closing")

// ErrNilTxsSender signals that a nil transactions sender has been provided
var ErrNilTxsSender = errors.New("nil transactions sender has been provided")

Expand Down
46 changes: 33 additions & 13 deletions errors/missingTrieNodeError.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
package errors

import (
"strings"
"encoding/hex"
"fmt"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-core-go/core"
)

// IsGetNodeFromDBError returns true if the provided error is of type getNodeFromDB
func IsGetNodeFromDBError(err error) bool {
if err == nil {
return false
}
// GetNodeFromDBErrWithKey defines a custom error for trie get node
type GetNodeFromDBErrWithKey struct {
getErr error
key []byte
dbIdentifier string
}

if IsClosingError(err) {
return false
// NewGetNodeFromDBErrWithKey will create a new instance of GetNodeFromDBErrWithKey
func NewGetNodeFromDBErrWithKey(key []byte, err error, id string) *GetNodeFromDBErrWithKey {
return &GetNodeFromDBErrWithKey{
getErr: err,
key: key,
dbIdentifier: id,
}
}

if strings.Contains(err.Error(), common.GetNodeFromDBErrorString) {
return true
}
// Error returns the error as string
func (e *GetNodeFromDBErrWithKey) Error() string {
return fmt.Sprintf(
"%s: %s for key %v",
core.GetNodeFromDBErrorString,
e.getErr.Error(),
hex.EncodeToString(e.key),
)
}

// GetKey will return the key that generated the error
func (e *GetNodeFromDBErrWithKey) GetKey() []byte {
return e.key
}

return false
// GetIdentifier will return the db identifier corresponding to the db
func (e *GetNodeFromDBErrWithKey) GetIdentifier() string {
return e.dbIdentifier
}
6 changes: 3 additions & 3 deletions factory/consensus/consensusComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/multiversx/mx-chain-go/consensus/chronology"
"github.com/multiversx/mx-chain-go/consensus/spos"
"github.com/multiversx/mx-chain-go/consensus/spos/sposFactory"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/errors"
"github.com/multiversx/mx-chain-go/factory"
p2pFactory "github.com/multiversx/mx-chain-go/p2p/factory"
Expand All @@ -24,7 +25,6 @@ import (
"github.com/multiversx/mx-chain-go/process/sync/storageBootstrap"
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/state/syncer"
trieFactory "github.com/multiversx/mx-chain-go/trie/factory"
"github.com/multiversx/mx-chain-go/trie/statistics"
"github.com/multiversx/mx-chain-go/trie/storageMarker"
"github.com/multiversx/mx-chain-go/update"
Expand Down Expand Up @@ -536,7 +536,7 @@ func (ccf *consensusComponentsFactory) createArgsBaseAccountsSyncer(trieStorageM
}

func (ccf *consensusComponentsFactory) createValidatorAccountsSyncer() (process.AccountsDBSyncer, error) {
trieStorageManager, ok := ccf.stateComponents.TrieStorageManagers()[trieFactory.PeerAccountTrie]
trieStorageManager, ok := ccf.stateComponents.TrieStorageManagers()[dataRetriever.PeerAccountsUnit.String()]
if !ok {
return nil, errors.ErrNilTrieStorageManager
}
Expand All @@ -548,7 +548,7 @@ func (ccf *consensusComponentsFactory) createValidatorAccountsSyncer() (process.
}

func (ccf *consensusComponentsFactory) createUserAccountsSyncer() (process.AccountsDBSyncer, error) {
trieStorageManager, ok := ccf.stateComponents.TrieStorageManagers()[trieFactory.UserAccountTrie]
trieStorageManager, ok := ccf.stateComponents.TrieStorageManagers()[dataRetriever.UserAccountsUnit.String()]
if !ok {
return nil, errors.ErrNilTrieStorageManager
}
Expand Down
8 changes: 4 additions & 4 deletions factory/processing/blockProcessorCreator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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/dataRetriever"
dataComp "github.com/multiversx/mx-chain-go/factory/data"
"github.com/multiversx/mx-chain-go/factory/mock"
processComp "github.com/multiversx/mx-chain-go/factory/processing"
Expand All @@ -24,7 +25,6 @@ import (
storageManager "github.com/multiversx/mx-chain-go/testscommon/storage"
trieMock "github.com/multiversx/mx-chain-go/testscommon/trie"
"github.com/multiversx/mx-chain-go/trie"
trieFactory "github.com/multiversx/mx-chain-go/trie/factory"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -104,14 +104,14 @@ func Test_newBlockProcessorCreatorForMeta(t *testing.T) {
storageManagerPeer, _ := trie.CreateTrieStorageManager(storageManagerArgs, options)

trieStorageManagers := make(map[string]common.StorageManager)
trieStorageManagers[trieFactory.UserAccountTrie] = storageManagerUser
trieStorageManagers[trieFactory.PeerAccountTrie] = storageManagerPeer
trieStorageManagers[dataRetriever.UserAccountsUnit.String()] = storageManagerUser
trieStorageManagers[dataRetriever.PeerAccountsUnit.String()] = storageManagerPeer

accounts, err := createAccountAdapter(
&mock.MarshalizerMock{},
&hashingMocks.HasherMock{},
factoryState.NewAccountCreator(),
trieStorageManagers[trieFactory.UserAccountTrie],
trieStorageManagers[dataRetriever.UserAccountsUnit.String()],
)
require.Nil(t, err)

Expand Down
4 changes: 2 additions & 2 deletions factory/state/stateComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (scf *stateComponentsFactory) Create() (*stateComponents, error) {

func (scf *stateComponentsFactory) createAccountsAdapters(triesContainer common.TriesHolder) (state.AccountsAdapter, state.AccountsAdapter, state.AccountsRepository, error) {
accountFactory := factoryState.NewAccountCreator()
merkleTrie := triesContainer.Get([]byte(trieFactory.UserAccountTrie))
merkleTrie := triesContainer.Get([]byte(dataRetriever.UserAccountsUnit.String()))
storagePruning, err := scf.newStoragePruningManager()
if err != nil {
return nil, nil, nil, err
Expand Down Expand Up @@ -198,7 +198,7 @@ func (scf *stateComponentsFactory) createAccountsAdapters(triesContainer common.

func (scf *stateComponentsFactory) createPeerAdapter(triesContainer common.TriesHolder) (state.AccountsAdapter, error) {
accountFactory := factoryState.NewPeerAccountCreator()
merkleTrie := triesContainer.Get([]byte(trieFactory.PeerAccountTrie))
merkleTrie := triesContainer.Get([]byte(dataRetriever.PeerAccountsUnit.String()))
storagePruning, err := scf.newStoragePruningManager()
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit da1e15e

Please sign in to comment.