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

process blocks #5681

Merged
merged 9 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion node/chainSimulator/configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
"github.com/stretchr/testify/require"
)

const (
// ChainID contains the chain id
ChainID = "chain"
)

// ArgsChainSimulatorConfigs holds all the components needed to create the chain simulator configs
type ArgsChainSimulatorConfigs struct {
NumOfShards uint32
Expand All @@ -48,7 +53,7 @@ func CreateChainSimulatorConfigs(args ArgsChainSimulatorConfigs) (*ArgsConfigsSi
return nil, err
}

configs.GeneralConfig.GeneralSettings.ChainID = "chain"
configs.GeneralConfig.GeneralSettings.ChainID = ChainID

// empty genesis smart contracts file
err = os.WriteFile(configs.ConfigurationPathsHolder.SmartContracts, []byte("[]"), os.ModePerm)
Expand Down Expand Up @@ -102,6 +107,9 @@ func CreateChainSimulatorConfigs(args ArgsChainSimulatorConfigs) (*ArgsConfigsSi
configs.GeneralConfig.SmartContractsStorageForSCQuery.DB.Type = string(storageunit.MemoryDB)
configs.GeneralConfig.SmartContractsStorageSimulate.DB.Type = string(storageunit.MemoryDB)

// enable db lookup extension
configs.GeneralConfig.DbLookupExtensions.Enabled = true

publicKeysBytes := make(map[uint32][]byte)
publicKeysBytes[core.MetachainShardId], err = publicKeys[0].ToByteArray()
if err != nil {
Expand Down
31 changes: 19 additions & 12 deletions node/chainSimulator/process/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package process

import (
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-go/node/chainSimulator/configs"
)

type blocksCreator struct {
nodeHandler NodeHandler
blsKeyBytes []byte
}

// NewBlocksCreator will create a new instance of blocksCreator
func NewBlocksCreator(nodeHandler NodeHandler, blsKeyBytes []byte) (*blocksCreator, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

return &blocksCreator{
nodeHandler: nodeHandler,
Expand All @@ -19,18 +21,8 @@ func NewBlocksCreator(nodeHandler NodeHandler, blsKeyBytes []byte) (*blocksCreat
// CreateNewBlock create and process a new block
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// CreateNewBlock create and process a new block
// CreateNewBlock creates and process a new block

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixing in the next PR

func (creator *blocksCreator) CreateNewBlock() error {
bp := creator.nodeHandler.GetProcessComponents().BlockProcessor()
currentHeader := creator.nodeHandler.GetChainHandler().GetCurrentBlockHeader()
var nonce, round uint64
var prevHash, prevRandSeed []byte
if currentHeader != nil {
nonce, round = currentHeader.GetNonce(), currentHeader.GetRound()
prevHash = creator.nodeHandler.GetChainHandler().GetCurrentBlockHeaderHash()
prevRandSeed = currentHeader.GetRandSeed()
} else {
prevHash = creator.nodeHandler.GetChainHandler().GetGenesisHeaderHash()
prevRandSeed = creator.nodeHandler.GetChainHandler().GetGenesisHeader().GetRandSeed()
}

nonce, round, prevHash, prevRandSeed := creator.getPreviousHeaderData()
newHeader, err := bp.CreateNewHeader(round+1, nonce+1)
if err != nil {
return err
Expand All @@ -55,7 +47,7 @@ func (creator *blocksCreator) CreateNewBlock() error {
return err
}

err = newHeader.SetChainID([]byte("chain"))
err = newHeader.SetChainID([]byte(configs.ChainID))
if err != nil {
return err
}
Expand Down Expand Up @@ -100,6 +92,21 @@ func (creator *blocksCreator) CreateNewBlock() error {
return creator.nodeHandler.GetBroadcastMessenger().BroadcastBlockDataLeader(header, miniBlocks, transactions, creator.blsKeyBytes)
}

func (creator *blocksCreator) getPreviousHeaderData() (nonce, round uint64, prevHash, prevRandSeed []byte) {
currentHeader := creator.nodeHandler.GetChainHandler().GetCurrentBlockHeader()

if currentHeader != nil {
nonce, round = currentHeader.GetNonce(), currentHeader.GetRound()
prevHash = creator.nodeHandler.GetChainHandler().GetCurrentBlockHeaderHash()
prevRandSeed = currentHeader.GetRandSeed()
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could have omitted the "else" branch and return on L101

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

prevHash = creator.nodeHandler.GetChainHandler().GetGenesisHeaderHash()
prevRandSeed = creator.nodeHandler.GetChainHandler().GetGenesisHeader().GetRandSeed()
}

return
}

func (creator *blocksCreator) setHeaderSignatures(header data.HeaderHandler) error {
signingHandler := creator.nodeHandler.GetCryptoComponents().ConsensusSigningHandler()
headerClone := header.ShallowClone()
Expand Down