Skip to content

Commit

Permalink
Merge pull request #6558 from multiversx/feat/sovereign-mainchain-hea…
Browse files Browse the repository at this point in the history
…der-sync

Feat/sovereign mainchain header sync
  • Loading branch information
mariusmihaic authored Nov 25, 2024
2 parents b62056f + 910d464 commit c93b2a5
Show file tree
Hide file tree
Showing 118 changed files with 3,452 additions and 583 deletions.
39 changes: 39 additions & 0 deletions cmd/sovereignnode/chainSimulator/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package common

import (
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/factory"
"github.com/multiversx/mx-chain-go/factory/runType"
"github.com/multiversx/mx-chain-go/node/chainSimulator/process"
sovRunType "github.com/multiversx/mx-chain-go/sovereignnode/runType"
)

// GetCurrentSovereignHeader returns current sovereign chain block handler from blockchain hook
func GetCurrentSovereignHeader(nodeHandler process.NodeHandler) data.SovereignChainHeaderHandler {
return nodeHandler.GetChainHandler().GetCurrentBlockHeader().(data.SovereignChainHeaderHandler)
}

// CreateSovereignRunTypeComponents will create sovereign run type components
func CreateSovereignRunTypeComponents(args runType.ArgsRunTypeComponents, sovereignExtraConfig config.SovereignConfig) (factory.RunTypeComponentsHolder, error) {
argsSovRunType, err := sovRunType.CreateSovereignArgsRunTypeComponents(args, sovereignExtraConfig)
if err != nil {
return nil, err
}

sovereignComponentsFactory, err := runType.NewSovereignRunTypeComponentsFactory(*argsSovRunType)
if err != nil {
return nil, err
}

managedRunTypeComponents, err := runType.NewManagedRunTypeComponents(sovereignComponentsFactory)
if err != nil {
return nil, err
}
err = managedRunTypeComponents.Create()
if err != nil {
return nil, err
}

return managedRunTypeComponents, nil
}
35 changes: 7 additions & 28 deletions cmd/sovereignnode/chainSimulator/sovereignChainSimulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/multiversx/mx-chain-go/node"
"github.com/multiversx/mx-chain-go/node/chainSimulator"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/block/sovereign/incomingHeader"
sovCommon "github.com/multiversx/mx-chain-go/sovereignnode/chainSimulator/common"
sovereignConfig "github.com/multiversx/mx-chain-go/sovereignnode/config"
"github.com/multiversx/mx-chain-go/sovereignnode/incomingHeader"
sovRunType "github.com/multiversx/mx-chain-go/sovereignnode/runType"
)

const (
Expand Down Expand Up @@ -54,11 +54,13 @@ func NewSovereignChainSimulator(args ArgsSovereignChainSimulator) (chainSimulato
args.CreateRunTypeCoreComponents = func() (factory.RunTypeCoreComponentsHolder, error) {
return createSovereignRunTypeCoreComponents()
}
args.CreateIncomingHeaderSubscriber = func(config *config.NotifierConfig, dataPool dataRetriever.PoolsHolder, mainChainNotarizationStartRound uint64, runTypeComponents factory.RunTypeComponentsHolder) (process.IncomingHeaderSubscriber, error) {
args.CreateIncomingHeaderSubscriber = func(config config.WebSocketConfig, dataPool dataRetriever.PoolsHolder, mainChainNotarizationStartRound uint64, runTypeComponents factory.RunTypeComponentsHolder) (process.IncomingHeaderSubscriber, error) {
return incomingHeader.CreateIncomingHeaderProcessor(config, dataPool, mainChainNotarizationStartRound, runTypeComponents)
}
args.CreateRunTypeComponents = func(argsRunType runType.ArgsRunTypeComponents) (factory.RunTypeComponentsHolder, error) {
return createSovereignRunTypeComponents(argsRunType, *configs.SovereignExtraConfig)
if args.CreateRunTypeComponents == nil {
args.CreateRunTypeComponents = func(args runType.ArgsRunTypeComponents) (factory.RunTypeComponentsHolder, error) {
return sovCommon.CreateSovereignRunTypeComponents(args, *configs.SovereignExtraConfig)
}
}
args.NodeFactory = node.NewSovereignNodeFactory(configs.SovereignExtraConfig.GenesisConfig.NativeESDT)
args.ChainProcessorFactory = NewSovereignChainHandlerFactory()
Expand Down Expand Up @@ -105,26 +107,3 @@ func createSovereignRunTypeCoreComponents() (factory.RunTypeCoreComponentsHolder

return managedRunTypeCoreComponents, nil
}

func createSovereignRunTypeComponents(args runType.ArgsRunTypeComponents, sovereignExtraConfig config.SovereignConfig) (factory.RunTypeComponentsHolder, error) {
argsSovRunType, err := sovRunType.CreateSovereignArgsRunTypeComponents(args, sovereignExtraConfig)
if err != nil {
return nil, err
}

sovereignComponentsFactory, err := runType.NewSovereignRunTypeComponentsFactory(*argsSovRunType)
if err != nil {
return nil, err
}

managedRunTypeComponents, err := runType.NewManagedRunTypeComponents(sovereignComponentsFactory)
if err != nil {
return nil, err
}
err = managedRunTypeComponents.Create()
if err != nil {
return nil, err
}

return managedRunTypeComponents, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ import (

"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/dataRetriever/requestHandlers"
"github.com/multiversx/mx-chain-go/factory"
"github.com/multiversx/mx-chain-go/factory/runType"
chainSim "github.com/multiversx/mx-chain-go/integrationTests/chainSimulator"
"github.com/multiversx/mx-chain-go/integrationTests/chainSimulator/staking"
"github.com/multiversx/mx-chain-go/node/chainSimulator"
"github.com/multiversx/mx-chain-go/node/chainSimulator/components/api"
"github.com/multiversx/mx-chain-go/node/chainSimulator/process"
proc "github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/headerCheck"
sovereignChainSimulator "github.com/multiversx/mx-chain-go/sovereignnode/chainSimulator"
"github.com/multiversx/mx-chain-go/sovereignnode/chainSimulator/common"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/multiversx/mx-chain-go/testscommon/components"
testsFactory "github.com/multiversx/mx-chain-go/testscommon/factory"
)

const (
Expand All @@ -41,6 +49,27 @@ func TestSovereignChainSimulator_EpochChange(t *testing.T) {
Value: 50, // do not lower this value so that each validator can participate in consensus as leader to get rewards
}

sovConfig := config.SovereignConfig{}

sovRequestHandler := &testscommon.ExtendedShardHeaderRequestHandlerStub{
RequestHandlerStub: testscommon.RequestHandlerStub{
RequestMiniBlockHandlerCalled: func(destShardID uint32, miniblockHash []byte) {
require.Fail(t, "should not request miniBlock")
},
RequestMiniBlocksHandlerCalled: func(destShardID uint32, miniblocksHashes [][]byte) {
require.Fail(t, "should not request miniBlocks")
},
RequestRewardTxHandlerCalled: func(destShardID uint32, txHashes [][]byte) {
require.Fail(t, "should not request reward txs")
},
},
}
sovRequestHandlerFactory := &testsFactory.RequestHandlerFactoryMock{
CreateRequestHandlerCalled: func(args requestHandlers.RequestHandlerArgs) (proc.RequestHandler, error) {
return sovRequestHandler, nil
},
}

var protocolSustainabilityAddress string
cs, err := sovereignChainSimulator.NewSovereignChainSimulator(sovereignChainSimulator.ArgsSovereignChainSimulator{
SovereignConfigPath: sovereignConfigPath,
Expand Down Expand Up @@ -68,6 +97,16 @@ func TestSovereignChainSimulator_EpochChange(t *testing.T) {

protocolSustainabilityAddress = cfg.EconomicsConfig.RewardsSettings.RewardsConfigByEpoch[0].ProtocolSustainabilityAddress
cfg.EpochConfig.EnableEpochs = newCfg
sovConfig = cfg.GeneralConfig.SovereignConfig
},
CreateRunTypeComponents: func(args runType.ArgsRunTypeComponents) (factory.RunTypeComponentsHolder, error) {
runTypeComps, err := common.CreateSovereignRunTypeComponents(args, sovConfig)
require.Nil(t, err)

runTypeCompsHolder := components.GetRunTypeComponentsStub(runTypeComps)
runTypeCompsHolder.RequestHandlerFactory = sovRequestHandlerFactory

return runTypeCompsHolder, nil
},
},
})
Expand Down Expand Up @@ -235,15 +274,11 @@ func checkProtocolSustainabilityAddressBalanceIncreased(
}

func getAllFeesInEpoch(nodeHandler process.NodeHandler) (*big.Int, *big.Int) {
sovHdr := getCurrSovHdr(nodeHandler)
sovHdr := common.GetCurrentSovereignHeader(nodeHandler)
return sovHdr.GetAccumulatedFeesInEpoch(), sovHdr.GetDevFeesInEpoch()
}

func getAllFees(nodeHandler process.NodeHandler) (*big.Int, *big.Int) {
sovHdr := getCurrSovHdr(nodeHandler)
sovHdr := common.GetCurrentSovereignHeader(nodeHandler)
return sovHdr.GetAccumulatedFees(), sovHdr.GetDeveloperFees()
}

func getCurrSovHdr(nodeHandler process.NodeHandler) data.SovereignChainHeaderHandler {
return nodeHandler.GetChainHandler().GetCurrentBlockHeader().(data.SovereignChainHeaderHandler)
}
13 changes: 8 additions & 5 deletions cmd/sovereignnode/chainSimulator/tests/esdt/issueSc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ func TestSovereignChainSimulator_SmartContract_IssueToken(t *testing.T) {
PathToInitialConfig: defaultPathToInitialConfig,
GenesisTimestamp: time.Now().Unix(),
RoundDurationInMillis: uint64(6000),
RoundsPerEpoch: core.OptionalUint64{},
ApiInterface: api.NewNoApiInterface(),
MinNodesPerShard: 2,
RoundsPerEpoch: core.OptionalUint64{
HasValue: true,
Value: 25,
},
ApiInterface: api.NewNoApiInterface(),
MinNodesPerShard: 2,
AlterConfigsFunction: func(cfg *config.Configs) {
cfg.SystemSCConfig.ESDTSystemSCConfig.BaseIssuingCost = issuePrice
},
Expand All @@ -59,10 +62,10 @@ func TestSovereignChainSimulator_SmartContract_IssueToken(t *testing.T) {

defer cs.Close()

time.Sleep(time.Second) // wait for VM to be ready for processing queries
err = cs.GenerateBlocksUntilEpochIsReached(6)
require.Nil(t, err)

nodeHandler := cs.GetNodeHandler(core.SovereignChainShardId)

systemScAddress := chainSim.GetSysAccBytesAddress(t, nodeHandler)

wallet, err := cs.GenerateAndMintWalletAddress(core.SovereignChainShardId, big.NewInt(0).Mul(chainSim.OneEGLD, big.NewInt(100)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ func checkCrossNotarizedHeader(t *testing.T, round int, crossNotarizedHeader dat

if round == 1 { // first cross header is dummy with nonce 0
require.Equal(t, uint64(0), crossNotarizedHeader.GetNonce())
} else if round == 2 { // first cross notarized header is 10000000
} else { // cross header with nonce is notarized
require.Equal(t, headerNonce, crossNotarizedHeader.GetNonce())
} else { // cross header with nonce-1 is notarized
require.Equal(t, headerNonce-1, crossNotarizedHeader.GetNonce())
}
}
Loading

0 comments on commit c93b2a5

Please sign in to comment.