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

Add BlockTrackCreator #5161

Draft
wants to merge 5 commits into
base: feat/chain-go-sdk
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion cmd/sovereignnode/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/google/gops v0.3.18
github.com/multiversx/mx-chain-core-go v1.2.1-0.20230331113546-aacb0c8158ce
github.com/multiversx/mx-chain-go v1.4.14-0.20230404125732-acff3222b0c2
github.com/multiversx/mx-chain-go v1.4.14-0.20230407124003-bc09ec1ec848
github.com/multiversx/mx-chain-logger-go v1.0.11
github.com/urfave/cli v1.22.10
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/sovereignnode/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,8 @@ github.com/multiversx/mx-chain-crypto-go v1.2.5 h1:tuq3BUNMhKud5DQbZi9DiVAAHUXyp
github.com/multiversx/mx-chain-crypto-go v1.2.5/go.mod h1:teqhNyWEqfMPgNn8sgWXlgtJ1a36jGCnhs/tRpXW6r4=
github.com/multiversx/mx-chain-es-indexer-go v1.4.1-0.20230331083741-0fd8a2156e96 h1:okIfLr+NqX04eHNp9k97KuLhpYfLJOjmGZaOia9xcGg=
github.com/multiversx/mx-chain-es-indexer-go v1.4.1-0.20230331083741-0fd8a2156e96/go.mod h1:Y6jgeoMBpDCtm7lurtChhgPyhpQ0GF5OruW/tl/++JI=
github.com/multiversx/mx-chain-go v1.4.14-0.20230404125732-acff3222b0c2 h1:h1dVhP+usVJlUI4gxQcci9gRo+P5Alkxsxeq0yCjobw=
github.com/multiversx/mx-chain-go v1.4.14-0.20230404125732-acff3222b0c2/go.mod h1:BhfUdbEkbJHF3jhRUCaGoj5AstUrG/LKC40o/GOTowY=
github.com/multiversx/mx-chain-go v1.4.14-0.20230407124003-bc09ec1ec848 h1:RBQBp6RkNCw9vlutn7K1ZfqoaMhwBSXnyPqLjVXt4qI=
github.com/multiversx/mx-chain-go v1.4.14-0.20230407124003-bc09ec1ec848/go.mod h1:BhfUdbEkbJHF3jhRUCaGoj5AstUrG/LKC40o/GOTowY=
github.com/multiversx/mx-chain-logger-go v1.0.11 h1:DFsHa+sc5fKwhDR50I8uBM99RTDTEW68ESyr5ALRDwE=
github.com/multiversx/mx-chain-logger-go v1.0.11/go.mod h1:1srDkP0DQucWQ+rYfaq0BX2qLnULsUdRPADpYUTM6dA=
github.com/multiversx/mx-chain-p2p-go v1.0.15 h1:H7273huZG/zAR6MPvWuXwBEVBsJWH1MeSIDshYV0nh0=
Expand Down
2 changes: 2 additions & 0 deletions cmd/sovereignnode/sovereignNodeRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/multiversx/mx-chain-go/p2p"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/interceptors"
"github.com/multiversx/mx-chain-go/process/track"
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
"github.com/multiversx/mx-chain-go/state/syncer"
"github.com/multiversx/mx-chain-go/storage/cache"
Expand Down Expand Up @@ -1239,6 +1240,7 @@ func (snr *sovereignNodeRunner) CreateManagedProcessComponents(
HistoryRepo: historyRepository,
SnapshotsEnabled: configs.FlagsConfig.SnapshotsEnabled,
ChainRunType: snr.getChainRunType(),
BlockTrackCreator: track.NewSovereignBlockTrackCreator(),
}
processComponentsFactory, err := processComp.NewProcessComponentsFactory(processArgs)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions factory/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/multiversx/mx-chain-go/outport"
"github.com/multiversx/mx-chain-go/p2p"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/track"
txSimData "github.com/multiversx/mx-chain-go/process/txsimulator/data"
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
Expand Down Expand Up @@ -541,3 +542,9 @@ type PersistentStatusHandler interface {
core.AppStatusHandler
SetStorage(store storage.Storer) error
}

// BlockTrackerCreator is able to create block trackers
type BlockTrackerCreator interface {
CreateBlockTracker(args track.ArgBaseTracker) (process.BlockTracker, error)
IsInterfaceNil() bool
}
5 changes: 4 additions & 1 deletion factory/processing/blockProcessorCreator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
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"
"github.com/multiversx/mx-chain-go/process/track"
"github.com/multiversx/mx-chain-go/process/txsimulator"
"github.com/multiversx/mx-chain-go/state"
factoryState "github.com/multiversx/mx-chain-go/state/factory"
Expand Down Expand Up @@ -75,7 +76,9 @@ func Test_newBlockProcessorCreatorForShard(t *testing.T) {
t.Parallel()

shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
pcf, err := processComp.NewProcessComponentsFactory(componentsMock.GetProcessComponentsFactoryArgs(shardCoordinator))
args := componentsMock.GetProcessComponentsFactoryArgs(shardCoordinator)
args.BlockTrackCreator = track.NewSovereignBlockTrackCreator()
pcf, err := processComp.NewProcessComponentsFactory(args)
require.NoError(t, err)
require.NotNil(t, pcf)

Expand Down
5 changes: 5 additions & 0 deletions factory/processing/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package processing

import "errors"

var errNilBlockTrackCreator = errors.New("nil block track creator provided")
20 changes: 7 additions & 13 deletions factory/processing/processComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ type ProcessComponentsFactoryArgs struct {
BootstrapComponents factory.BootstrapComponentsHolder
StatusComponents factory.StatusComponentsHolder
StatusCoreComponents factory.StatusCoreComponentsHolder
BlockTrackCreator mainFactory.BlockTrackerCreator
ChainRunType common.ChainRunType
}

Expand Down Expand Up @@ -192,6 +193,7 @@ type processComponentsFactory struct {
bootstrapComponents factory.BootstrapComponentsHolder
statusComponents factory.StatusComponentsHolder
statusCoreComponents factory.StatusCoreComponentsHolder
blockTrackCreator mainFactory.BlockTrackerCreator
chainRunType common.ChainRunType
}

Expand Down Expand Up @@ -231,6 +233,7 @@ func NewProcessComponentsFactory(args ProcessComponentsFactoryArgs) (*processCom
statusCoreComponents: args.StatusCoreComponents,
snapshotsEnabled: args.SnapshotsEnabled,
chainRunType: args.ChainRunType,
blockTrackCreator: args.BlockTrackCreator,
}, nil
}

Expand Down Expand Up @@ -1357,19 +1360,7 @@ func (pcf *processComponentsFactory) newBlockTracker(
FeeHandler: pcf.coreData.EconomicsData(),
}

if pcf.bootstrapComponents.ShardCoordinator().SelfId() < pcf.bootstrapComponents.ShardCoordinator().NumberOfShards() {
return pcf.createShardBlockTracker(argBaseTracker)
}

if pcf.bootstrapComponents.ShardCoordinator().SelfId() == core.MetachainShardId {
arguments := track.ArgMetaTracker{
ArgBaseTracker: argBaseTracker,
}

return track.NewMetaBlockTrack(arguments)
}

return nil, errors.New("could not create block tracker")
return pcf.blockTrackCreator.CreateBlockTracker(argBaseTracker)
}

func (pcf *processComponentsFactory) createShardBlockTracker(argBaseTracker track.ArgBaseTracker) (process.BlockTracker, error) {
Expand Down Expand Up @@ -2033,6 +2024,9 @@ func checkProcessComponentsArgs(args ProcessComponentsFactoryArgs) error {
if check.IfNil(args.Crypto.ManagedPeersHolder()) {
return fmt.Errorf("%s: %w", baseErrMessage, errErd.ErrNilManagedPeersHolder)
}
if check.IfNil(args.BlockTrackCreator) {
return fmt.Errorf("%s: %w", baseErrMessage, errNilBlockTrackCreator)
}

return nil
}
Expand Down
15 changes: 15 additions & 0 deletions factory/processing/processComponentsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ 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/factory"
"github.com/multiversx/mx-chain-go/factory/mock"
processComp "github.com/multiversx/mx-chain-go/factory/processing"
"github.com/multiversx/mx-chain-go/process/track"
componentsMock "github.com/multiversx/mx-chain-go/testscommon/components"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -57,6 +59,18 @@ func testManagedProcessComponentsCreateShouldWork(t *testing.T, shardID uint32,
return 0
}

var blockTrackCreator factory.BlockTrackerCreator
switch chainType {
case common.ChainRunTypeRegular:
if shardID == core.MetachainShardId {
blockTrackCreator = track.NewMetaBlockTrackCreator()
} else {
blockTrackCreator = track.NewShardBlockTrackCreator()
}
case common.ChainRunTypeSovereign:
blockTrackCreator = track.NewSovereignBlockTrackCreator()
}

shardCoordinator.CurrentShard = core.MetachainShardId
dataComponents := componentsMock.GetDataComponents(coreComponents, shardCoordinator)
cryptoComponents := componentsMock.GetCryptoComponents(coreComponents)
Expand All @@ -71,6 +85,7 @@ func testManagedProcessComponentsCreateShouldWork(t *testing.T, shardID uint32,
networkComponents,
)
processArgs.ChainRunType = chainType
processArgs.BlockTrackCreator = blockTrackCreator

componentsMock.SetShardCoordinator(t, processArgs.BootstrapComponents, shardCoordinator)

Expand Down
2 changes: 2 additions & 0 deletions factory/processing/processComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
processComp "github.com/multiversx/mx-chain-go/factory/processing"
"github.com/multiversx/mx-chain-go/genesis"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/track"
componentsMock "github.com/multiversx/mx-chain-go/testscommon/components"
"github.com/multiversx/mx-chain-go/testscommon/mainFactoryMocks"
"github.com/multiversx/mx-chain-go/testscommon/outport"
Expand Down Expand Up @@ -140,6 +141,7 @@ func TestProcessComponentsFactory_CreateShouldWork(t *testing.T) {

shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
processArgs := componentsMock.GetProcessComponentsFactoryArgs(shardCoordinator)
processArgs.BlockTrackCreator = track.NewSovereignBlockTrackCreator()
pcf, _ := processComp.NewProcessComponentsFactory(processArgs)

require.NotNil(t, pcf)
Expand Down
19 changes: 19 additions & 0 deletions node/nodeRunner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package node

import (
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -56,6 +57,7 @@ import (
"github.com/multiversx/mx-chain-go/p2p"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/interceptors"
"github.com/multiversx/mx-chain-go/process/track"
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
"github.com/multiversx/mx-chain-go/state/syncer"
"github.com/multiversx/mx-chain-go/storage/cache"
Expand Down Expand Up @@ -1201,6 +1203,10 @@ func (nr *nodeRunner) CreateManagedProcessComponents(
requestedItemsHandler := cache.NewTimeCache(
time.Duration(uint64(time.Millisecond) * coreComponents.GenesisNodesSetup().GetRoundDuration()))

blockTrackCreator, err := createBlockTrackCreator(bootstrapComponents)
if err != nil {
return nil, err
}
processArgs := processComp.ProcessComponentsFactoryArgs{
Config: *configs.GeneralConfig,
EpochConfig: *configs.EpochConfig,
Expand Down Expand Up @@ -1229,6 +1235,7 @@ func (nr *nodeRunner) CreateManagedProcessComponents(
HistoryRepo: historyRepository,
SnapshotsEnabled: configs.FlagsConfig.SnapshotsEnabled,
ChainRunType: nr.getChainRunType(),
BlockTrackCreator: blockTrackCreator,
}
processComponentsFactory, err := processComp.NewProcessComponentsFactory(processArgs)
if err != nil {
Expand All @@ -1248,6 +1255,18 @@ func (nr *nodeRunner) CreateManagedProcessComponents(
return managedProcessComponents, nil
}

// TODO: We should refactor process components factory to be specific for each node type (shard/meta/sovereign)
func createBlockTrackCreator(bootstrapComponents mainFactory.BootstrapComponentsHolder) (mainFactory.BlockTrackerCreator, error) {
if bootstrapComponents.ShardCoordinator().SelfId() < bootstrapComponents.ShardCoordinator().NumberOfShards() {
return track.NewShardBlockTrackCreator(), nil
}
if bootstrapComponents.ShardCoordinator().SelfId() == core.MetachainShardId {
return track.NewMetaBlockTrackCreator(), nil
}

return nil, errors.New("error creating block track creator because of invalid shard id")
}

// CreateManagedDataComponents is the managed data components factory
func (nr *nodeRunner) CreateManagedDataComponents(
statusCoreComponents mainFactory.StatusCoreComponentsHolder,
Expand Down
22 changes: 22 additions & 0 deletions process/track/metaBlockTrackCreator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package track

import (
"github.com/multiversx/mx-chain-go/process"
)

type metaBlockTrackCreator struct{}

// NewMetaBlockTrackCreator creates a shard block track creator
func NewMetaBlockTrackCreator() *metaBlockTrackCreator {
return &metaBlockTrackCreator{}
}

// CreateBlockTracker creates a shard block track
func (creator *metaBlockTrackCreator) CreateBlockTracker(args ArgBaseTracker) (process.BlockTracker, error) {
return NewMetaBlockTrack(ArgMetaTracker{ArgBaseTracker: args})
}

// IsInterfaceNil checks if the underlying pointer is nil
func (creator *metaBlockTrackCreator) IsInterfaceNil() bool {
return creator == nil
}
22 changes: 22 additions & 0 deletions process/track/shardBlockTrackCreator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package track

import (
"github.com/multiversx/mx-chain-go/process"
)

type shardBlockTrackCreator struct{}

// NewShardBlockTrackCreator creates a shard block track creator
func NewShardBlockTrackCreator() *shardBlockTrackCreator {
return &shardBlockTrackCreator{}
}

// CreateBlockTracker creates a shard block track
func (creator *shardBlockTrackCreator) CreateBlockTracker(args ArgBaseTracker) (process.BlockTracker, error) {
return NewShardBlockTrack(ArgShardTracker{ArgBaseTracker: args})
}

// IsInterfaceNil checks if the underlying pointer is nil
func (creator *shardBlockTrackCreator) IsInterfaceNil() bool {
return creator == nil
}
27 changes: 27 additions & 0 deletions process/track/sovereignChainShardBlockTrackCreator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package track

import (
"github.com/multiversx/mx-chain-go/process"
)

type sovereignBlockTrackCreator struct{}

// NewSovereignBlockTrackCreator creates a sovereign block track creator
func NewSovereignBlockTrackCreator() *sovereignBlockTrackCreator {
return &sovereignBlockTrackCreator{}
}

// CreateBlockTracker creates a new sovereign block track
func (creator *sovereignBlockTrackCreator) CreateBlockTracker(args ArgBaseTracker) (process.BlockTracker, error) {
blockTracker, err := NewShardBlockTrack(ArgShardTracker{ArgBaseTracker: args})
if err != nil {
return nil, err
}

return NewSovereignChainShardBlockTrack(blockTracker)
}

// IsInterfaceNil checks if the underlying pointer is nil
func (creator *sovereignBlockTrackCreator) IsInterfaceNil() bool {
return creator == nil
}
10 changes: 6 additions & 4 deletions testscommon/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/multiversx/mx-chain-go/p2p"
p2pConfig "github.com/multiversx/mx-chain-go/p2p/config"
p2pFactory "github.com/multiversx/mx-chain-go/p2p/factory"
"github.com/multiversx/mx-chain-go/process/track"
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
"github.com/multiversx/mx-chain-go/state"
Expand Down Expand Up @@ -570,10 +571,11 @@ func GetProcessArgs(
MaxServiceFee: 100,
},
},
Version: "v1.0.0",
HistoryRepo: &dblookupext.HistoryRepositoryStub{},
SnapshotsEnabled: false,
ChainRunType: common.ChainRunTypeRegular,
Version: "v1.0.0",
HistoryRepo: &dblookupext.HistoryRepositoryStub{},
SnapshotsEnabled: false,
ChainRunType: common.ChainRunTypeRegular,
BlockTrackCreator: track.NewShardBlockTrackCreator(),
}
}

Expand Down