Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sovereign] Fix shard id computations #6722

Open
wants to merge 5 commits into
base: feat/chain-go-sdk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions cmd/sovereignnode/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"os"
"runtime"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-core-go/core"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/urfave/cli"

"github.com/multiversx/mx-chain-go/common/operationmodes"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/facade"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/urfave/cli"
)

var (
Expand Down Expand Up @@ -619,10 +620,7 @@ func applyCompatibleConfigs(log logger.Logger, configs *config.Configs) error {
// would bring confusion
isInImportDBMode := configs.ImportDbConfig.IsImportDBMode
if isInImportDBMode {
err := processConfigImportDBMode(log, configs)
if err != nil {
return err
}
processConfigImportDBMode(log, configs)
}
if !isInImportDBMode && configs.ImportDbConfig.ImportDbNoSigCheckFlag {
return fmt.Errorf("import-db-no-sig-check can only be used with the import-db flag")
Expand Down Expand Up @@ -708,20 +706,12 @@ func processSnapshotLessObserverMode(log logger.Logger, configs *config.Configs)
)
}

func processConfigImportDBMode(log logger.Logger, configs *config.Configs) error {
func processConfigImportDBMode(log logger.Logger, configs *config.Configs) {
importDbFlags := configs.ImportDbConfig
generalConfigs := configs.GeneralConfig
p2pConfigs := configs.MainP2pConfig
fullArchiveP2PConfigs := configs.FullArchiveP2pConfig
prefsConfig := configs.PreferencesConfig

var err error

importDbFlags.ImportDBTargetShardID, err = common.ProcessDestinationShardAsObserver(prefsConfig.Preferences.DestinationShardAsObserver)
if err != nil {
return err
}

importDbFlags.ImportDBTargetShardID = core.SovereignChainShardId
generalConfigs.GeneralSettings.StartInEpochEnabled = false

// We need to increment "NumActivePersisters" in order to make the storage resolvers work (since they open 2 epochs in advance)
Expand All @@ -744,7 +734,6 @@ func processConfigImportDBMode(log logger.Logger, configs *config.Configs) error
"import DB shard ID", importDbFlags.ImportDBTargetShardID,
"kad dht discoverer", "off",
)
return nil
}

func processConfigFullArchiveMode(log logger.Logger, configs *config.Configs) {
Expand Down
2 changes: 1 addition & 1 deletion sharding/nodesCoordinator/indexHashedNodesCoordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ func (ihnc *indexHashedNodesCoordinator) ShardIdForEpoch(epoch uint32) (uint32,
return 0, fmt.Errorf("%w epoch=%v", ErrEpochNodesConfigDoesNotExist, epoch)
}

return nodesConfig.shardID, nil
return ihnc.numberOfShardsComputer.ShardIdFromNodesConfig(nodesConfig), nil
}

// ShuffleOutForEpoch verifies if the shards changed in the new epoch and calls the shuffleOutHandler
Expand Down
1 change: 1 addition & 0 deletions sharding/nodesCoordinator/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type NodesCoordinatorHelper interface {
// NumberOfShardsComputer provides the number of shards computation capabilities
type NumberOfShardsComputer interface {
ComputeNumberOfShards(config *epochNodesConfig) (uint32, error)
ShardIdFromNodesConfig(nodesConfig *epochNodesConfig) uint32
IsInterfaceNil() bool
}

Expand Down
5 changes: 5 additions & 0 deletions sharding/nodesCoordinator/numberOfShardsComputer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func (nsc *numberOfShardsWithMetaComputer) ComputeNumberOfShards(config *epochNo
return nbShards - 1, nil
}

// ShardIdFromNodesConfig returns the shard id from nodes config
func (snsc *numberOfShardsWithMetaComputer) ShardIdFromNodesConfig(nodesConfig *epochNodesConfig) uint32 {
return nodesConfig.shardID
}

// IsInterfaceNil returns true if there is no value under the interface
func (nsc *numberOfShardsWithMetaComputer) IsInterfaceNil() bool {
return nsc == nil
Expand Down
8 changes: 8 additions & 0 deletions sharding/nodesCoordinator/numberOfShardsComputer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,11 @@ func TestNumberOfShardsComputer_ComputeNumberOfShards(t *testing.T) {
require.Equal(t, expectedNbShardsWithoutMeta, nbShards)
})
}

func TestNumberOfShardsComputer_ShardIdFromNodesConfig(t *testing.T) {
t.Parallel()

ssc := newNumberOfShardsWithMetaComputer()
require.Equal(t, uint32(3), ssc.ShardIdFromNodesConfig(&epochNodesConfig{shardID: 3}))
require.Equal(t, core.MetachainShardId, ssc.ShardIdFromNodesConfig(&epochNodesConfig{shardID: core.MetachainShardId}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func NewSovereignIndexHashedNodesCoordinator(arguments ArgNodesCoordinator) (*so
nodesConfig := make(map[uint32]*epochNodesConfig, nodesCoordinatorStoredEpochs)

nodesConfig[arguments.Epoch] = &epochNodesConfig{
nbShards: arguments.NbShards,
shardID: arguments.ShardIDAsObserver,
nbShards: 1,
shardID: core.SovereignChainShardId,
eligibleMap: make(map[uint32][]Validator),
waitingMap: make(map[uint32][]Validator),
selectors: make(map[uint32]RandomSelector),
Expand Down
5 changes: 5 additions & 0 deletions sharding/nodesCoordinator/sovereignNumberOfShardsComputer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func (snsc *sovereignNumberOfShardsComputer) ComputeNumberOfShards(config *epoch
return nbShards, nil
}

// ShardIdFromNodesConfig always returns sovereign shard id
func (snsc *sovereignNumberOfShardsComputer) ShardIdFromNodesConfig(_ *epochNodesConfig) uint32 {
return core.SovereignChainShardId
}

// IsInterfaceNil returns true if there is no value under the interface
func (snsc *sovereignNumberOfShardsComputer) IsInterfaceNil() bool {
return snsc == nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ func TestSovereignNumberOfShardsComputer_ComputeNumberOfShards(t *testing.T) {
require.Equal(t, uint32(0), nbShards)
})
}

func TestSovereignNumberOfShardsComputer_ShardIdFromNodesConfig(t *testing.T) {
t.Parallel()

ssc := newSovereignNumberOfShardsComputer()
require.Equal(t, core.SovereignChainShardId, ssc.ShardIdFromNodesConfig(nil))
require.Equal(t, core.SovereignChainShardId, ssc.ShardIdFromNodesConfig(&epochNodesConfig{shardID: core.MetachainShardId}))
}