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

Genesis accounts with esdt balances #5871

Merged
4 changes: 4 additions & 0 deletions cmd/sovereignnode/config/sovereignConfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@
AcknowledgeTimeout = 60
# Payload version to process
Version = 1

[GenesisConfig]
# NativeESDT specifies the sovereign shard's native esdt currency
NativeESDT = "WEGLD-bd4d79"
6 changes: 6 additions & 0 deletions config/sovereignConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type SovereignConfig struct {
OutgoingSubscribedEvents OutgoingSubscribedEvents `toml:"OutgoingSubscribedEvents"`
OutGoingBridge OutGoingBridge `toml:"OutGoingBridge"`
NotifierConfig NotifierConfig `toml:"NotifierConfig"`
GenesisConfig GenesisConfig `toml:"GenesisConfig"`
OutGoingBridgeCertificate OutGoingBridgeCertificate
}

Expand Down Expand Up @@ -58,3 +59,8 @@ type WebSocketConfig struct {
AcknowledgeTimeout int `toml:"AcknowledgeTimeout"`
Version uint32 `toml:"Version"`
}

// GenesisConfig should hold all sovereign genesis related configs
type GenesisConfig struct {
NativeESDT string `toml:"NativeESDT"`
}
4 changes: 2 additions & 2 deletions factory/processing/blockProcessorCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/multiversx/mx-chain-go/process/smartContract/hooks"
"github.com/multiversx/mx-chain-go/process/smartContract/hooks/counters"
"github.com/multiversx/mx-chain-go/process/smartContract/processProxy"
"github.com/multiversx/mx-chain-go/process/smartContract/processorV2"
"github.com/multiversx/mx-chain-go/process/smartContract/processorV2/scrProcFactory"
"github.com/multiversx/mx-chain-go/process/smartContract/scrCommon"
"github.com/multiversx/mx-chain-go/process/throttle"
"github.com/multiversx/mx-chain-go/process/transaction"
Expand Down Expand Up @@ -266,7 +266,7 @@ func (pcf *processComponentsFactory) newShardBlockProcessor(
WasmVMChangeLocker: wasmVMChangeLocker,
}

scProcessorProxy, err := processorV2.CreateSCRProcessor(pcf.chainRunType, argsNewScProcessor)
scProcessorProxy, err := scrProcFactory.CreateSCRProcessor(pcf.chainRunType, argsNewScProcessor, pcf.epochNotifier)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions factory/processing/processComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,8 @@ func (pcf *processComponentsFactory) generateGenesisHeadersAndApplyInitialBalanc
ShardCoordinatorFactory: pcf.shardCoordinatorFactory,
TxPreprocessorCreator: pcf.txPreprocessorCreator,
DNSV2Addresses: pcf.config.BuiltInFunctions.DNSV2Addresses,
// TODO: We should only pass the whole config instead of passing sub-configs as above
Config: pcf.config,
}

gbc, err := pcf.genesisBlockCreatorFactory.CreateGenesisBlockCreator(arg)
Expand Down
4 changes: 2 additions & 2 deletions factory/processing/txSimulatorProcessComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/multiversx/mx-chain-go/process/coordinator"
"github.com/multiversx/mx-chain-go/process/factory/shard"
"github.com/multiversx/mx-chain-go/process/smartContract"
"github.com/multiversx/mx-chain-go/process/smartContract/processorV2"
"github.com/multiversx/mx-chain-go/process/smartContract/processorV2/scrProcFactory"
"github.com/multiversx/mx-chain-go/process/smartContract/scrCommon"
"github.com/multiversx/mx-chain-go/process/transaction"
"github.com/multiversx/mx-chain-go/process/transactionEvaluator"
Expand Down Expand Up @@ -367,7 +367,7 @@ func (pcf *processComponentsFactory) createArgsTxSimulatorProcessorShard(
IsGenesisProcessing: false,
}

scProcessor, err := processorV2.CreateSCRProcessor(pcf.chainRunType, scProcArgs)
scProcessor, err := scrProcFactory.CreateSCRProcessor(pcf.chainRunType, scProcArgs, pcf.epochNotifier)
if err != nil {
return args, nil, nil, err
}
Expand Down
1 change: 1 addition & 0 deletions genesis/process/argGenesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type ArgsGenesisBlockCreator struct {
TxExecutionOrderHandler common.TxExecutionOrderHandler
TxPreprocessorCreator preprocess.TxPreProcessorCreator
ChainRunType common.ChainRunType
Config config.Config

GenesisNodePrice *big.Int
GenesisString string
Expand Down
2 changes: 2 additions & 0 deletions genesis/process/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ package process
import "errors"

var errNilGenesisBlockCreator = errors.New("nil genesis block creator provided")

var errCouldNotGenerateInitialESDTTransfers = errors.New("could not generate initial esdt transfers")
23 changes: 16 additions & 7 deletions genesis/process/shardGenesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"github.com/multiversx/mx-chain-go/process/smartContract/builtInFunctions"
"github.com/multiversx/mx-chain-go/process/smartContract/hooks"
"github.com/multiversx/mx-chain-go/process/smartContract/hooks/counters"
"github.com/multiversx/mx-chain-go/process/smartContract/processProxy"
"github.com/multiversx/mx-chain-go/process/smartContract/processorV2/scrProcFactory"
"github.com/multiversx/mx-chain-go/process/smartContract/scrCommon"
syncDisabled "github.com/multiversx/mx-chain-go/process/sync/disabled"
"github.com/multiversx/mx-chain-go/process/transaction"
Expand Down Expand Up @@ -174,6 +174,20 @@
return createShardGenesisBlockAfterHardFork(arg, body, hardForkBlockProcessor)
}

processors, err := createProcessorsForShardGenesisBlock(arg, createGenesisConfig(), createGenesisRoundConfig())
if err != nil {
return nil, nil, nil, err

Check warning on line 179 in genesis/process/shardGenesisBlockCreator.go

View check run for this annotation

Codecov / codecov/patch

genesis/process/shardGenesisBlockCreator.go#L179

Added line #L179 was not covered by tests
}

return baseCreateShardGenesisBlock(arg, nodesListSplitter, processors)
}

func baseCreateShardGenesisBlock(
arg ArgsGenesisBlockCreator,
nodesListSplitter genesis.NodesListSplitter,
processors *genesisProcessors,
) (data.HeaderHandler, [][]byte, *genesis.IndexingData, error) {

indexingData := &genesis.IndexingData{
DelegationTxs: make([]data.TransactionHandler, 0),
ScrsTxs: make(map[string]data.TransactionHandler),
Expand All @@ -182,11 +196,6 @@
DeployInitialScTxs: make([]data.TransactionHandler, 0),
}

processors, err := createProcessorsForShardGenesisBlock(arg, createGenesisConfig(), createGenesisRoundConfig())
if err != nil {
return nil, nil, nil, err
}

deployMetrics := &deployedScMetrics{}

scAddresses, scTxs, err := deployInitialSmartContracts(processors, arg, deployMetrics)
Expand Down Expand Up @@ -582,7 +591,7 @@
WasmVMChangeLocker: genesisWasmVMLocker,
}

scProcessorProxy, err := processProxy.NewSmartContractProcessorProxy(argsNewScProcessor, epochNotifier)
scProcessorProxy, err := scrProcFactory.CreateSCRProcessor(arg.ChainRunType, argsNewScProcessor, epochNotifier)
if err != nil {
return nil, err
}
Expand Down
114 changes: 102 additions & 12 deletions genesis/process/sovereignGenesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/factory"
"github.com/multiversx/mx-chain-go/genesis"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/update"
"github.com/multiversx/mx-chain-go/vm"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
)
Expand All @@ -29,18 +29,25 @@
return nil, errNilGenesisBlockCreator
}

log.Debug("NewSovereignGenesisBlockCreator", "native esdt token", gbc.arg.Config.SovereignConfig.GenesisConfig.NativeESDT)

return &sovereignGenesisBlockCreator{
genesisBlockCreator: gbc,
}, nil
}

// CreateGenesisBlocks will create sovereign genesis blocks
func (gbc *sovereignGenesisBlockCreator) CreateGenesisBlocks() (map[uint32]data.HeaderHandler, error) {
err := gbc.initGenesisAccounts()
if err != nil {
return nil, err

Check warning on line 43 in genesis/process/sovereignGenesisBlockCreator.go

View check run for this annotation

Codecov / codecov/patch

genesis/process/sovereignGenesisBlockCreator.go#L43

Added line #L43 was not covered by tests
}

if !mustDoGenesisProcess(gbc.arg) {
return gbc.createSovereignEmptyGenesisBlocks()
}

err := gbc.computeSovereignDNSAddresses(gbc.arg.EpochConfig.EnableEpochs)
err = gbc.computeSovereignDNSAddresses(gbc.arg.EpochConfig.EnableEpochs)
if err != nil {
return nil, err
}
Expand All @@ -55,8 +62,27 @@
return gbc.createSovereignHeaders(argsCreateBlock)
}

func (gbc *sovereignGenesisBlockCreator) initGenesisAccounts() error {
acc, err := gbc.arg.Accounts.LoadAccount(core.SystemAccountAddress)
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you loading and saving the same accounts without any changes ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is mandatory at genesis to have accounts initialized.

if err != nil {
return err
}

err = gbc.arg.Accounts.SaveAccount(acc)
if err != nil {
return err
}

acc, err = gbc.arg.Accounts.LoadAccount(core.ESDTSCAddress)
if err != nil {
return err
}

return gbc.arg.Accounts.SaveAccount(acc)
}

func (gbc *sovereignGenesisBlockCreator) createSovereignEmptyGenesisBlocks() (map[uint32]data.HeaderHandler, error) {
err := gbc.computeSovereignDNSAddresses(createGenesisConfig())
err := gbc.computeSovereignDNSAddresses(createSovereignGenesisConfig())
if err != nil {
return nil, err
}
Expand All @@ -77,6 +103,12 @@
return mapEmptyGenesisBlocks, nil
}

func createSovereignGenesisConfig() config.EnableEpochs {
cfg := createGenesisConfig()
cfg.ESDTMultiTransferEnableEpoch = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could return error if the config is wrong. instead of setting it to 0 here.

Copy link
Contributor Author

@mariusmihaic mariusmihaic Feb 19, 2024

Choose a reason for hiding this comment

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

This is not the loaded config from config.toml.
This is a "mocked" created config for sovereign bootStrapping at genesis.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When we'll make the chain start correctly with every flag on epoch 0, we should also change this mocked config to return everything with enable epoch 0 as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add a task for this

return cfg
}

func (gbc *sovereignGenesisBlockCreator) computeSovereignDNSAddresses(enableEpochsConfig config.EnableEpochs) error {
initialAddresses, err := factory.DecodeAddresses(gbc.arg.Core.AddressPubKeyConverter(), gbc.arg.DNSV2Addresses)
if err != nil {
Expand All @@ -96,9 +128,7 @@

genesisBlock, scResults, gbc.initialIndexingData[shardID], err = createSovereignShardGenesisBlock(
args.mapArgsGenesisBlockCreator[shardID],
args.mapBodies[shardID],
args.nodesListSplitter,
args.mapHardForkBlockProcessor[shardID],
)

if err != nil {
Expand Down Expand Up @@ -132,16 +162,20 @@

func createSovereignShardGenesisBlock(
arg ArgsGenesisBlockCreator,
body *block.Body,
nodesListSplitter genesis.NodesListSplitter,
hardForkBlockProcessor update.HardForkBlockProcessor,
) (data.HeaderHandler, [][]byte, *genesis.IndexingData, error) {
genesisBlock, scAddresses, indexingData, err := CreateShardGenesisBlock(arg, body, nodesListSplitter, hardForkBlockProcessor)
sovereignGenesisConfig := createSovereignGenesisConfig()
shardProcessors, err := createProcessorsForShardGenesisBlock(arg, sovereignGenesisConfig, createGenesisRoundConfig())
if err != nil {
return nil, nil, nil, err
}

metaProcessor, err := createProcessorsForMetaGenesisBlock(arg, createGenesisConfig(), createGenesisRoundConfig())
genesisBlock, scAddresses, indexingData, err := baseCreateShardGenesisBlock(arg, nodesListSplitter, shardProcessors)
if err != nil {
return nil, nil, nil, err

Check warning on line 175 in genesis/process/sovereignGenesisBlockCreator.go

View check run for this annotation

Codecov / codecov/patch

genesis/process/sovereignGenesisBlockCreator.go#L175

Added line #L175 was not covered by tests
}

metaProcessor, err := createProcessorsForMetaGenesisBlock(arg, sovereignGenesisConfig, createGenesisRoundConfig())
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -158,14 +192,20 @@
}
indexingData.StakingTxs = stakingTxs

rootHash, err := arg.Accounts.Commit()
metaScrsTxs := metaProcessor.txCoordinator.GetAllCurrentUsedTxs(block.SmartContractResultBlock)
genesisESDTTransfers, err := createSovereignGenesisESDTTransfers(arg, shardProcessors.scrProcessor)
if err != nil {
return nil, nil, nil, fmt.Errorf("%w encountered when creating sovereign genesis block while commiting", err)
return nil, nil, nil, err

Check warning on line 198 in genesis/process/sovereignGenesisBlockCreator.go

View check run for this annotation

Codecov / codecov/patch

genesis/process/sovereignGenesisBlockCreator.go#L198

Added line #L198 was not covered by tests
}

metaScrsTxs := metaProcessor.txCoordinator.GetAllCurrentUsedTxs(block.SmartContractResultBlock)
indexingData.ScrsTxs = mergeScrs(indexingData.ScrsTxs, genesisESDTTransfers)
indexingData.ScrsTxs = mergeScrs(indexingData.ScrsTxs, metaScrsTxs)

rootHash, err := arg.Accounts.Commit()
if err != nil {
return nil, nil, nil, fmt.Errorf("%w encountered when creating sovereign genesis block while commiting", err)

Check warning on line 206 in genesis/process/sovereignGenesisBlockCreator.go

View check run for this annotation

Codecov / codecov/patch

genesis/process/sovereignGenesisBlockCreator.go#L206

Added line #L206 was not covered by tests
}

err = setRootHash(genesisBlock, rootHash)
if err != nil {
return nil, nil, nil, err
Expand Down Expand Up @@ -277,3 +317,53 @@

return stakingTxs, nil
}

func createSovereignGenesisESDTTransfers(args ArgsGenesisBlockCreator, scrProcessor process.SmartContractResultProcessor) (map[string]data.TransactionHandler, error) {
initialESDTTxs := make(map[string]data.TransactionHandler, 0)
initialAccounts := args.AccountsParser.InitialAccounts()

tokenID := args.Config.SovereignConfig.GenesisConfig.NativeESDT
for nonce, initialAcc := range initialAccounts {
accInitialBalance := initialAcc.GetBalanceValue()

log.Debug("creating genesis initial esdt balance",
"address", initialAcc.GetAddress(), "balance", accInitialBalance.String(), "tokenID", tokenID)

scr := &smartContractResult.SmartContractResult{
Nonce: uint64(nonce),
RcvAddr: initialAcc.AddressBytes(),
SndAddr: core.ESDTSCAddress,
Data: createGenesisSCRData(tokenID, accInitialBalance),
axenteoctavian marked this conversation as resolved.
Show resolved Hide resolved
Value: big.NewInt(0),
}

retCode, err := scrProcessor.ProcessSmartContractResult(scr)
if err != nil {
return nil, err
}

if retCode != vmcommon.Ok {
log.Error("could not generate initial esdt balances",
"func", "createSovereignGenesisESDTTransfers", "ret code", retCode, "address", initialAcc.GetAddress())
return nil, errCouldNotGenerateInitialESDTTransfers
}

hash, err := core.CalculateHash(args.Core.InternalMarshalizer(), args.Core.Hasher(), scr)
if err != nil {
return nil, err
}

initialESDTTxs[string(hash)] = scr
}

return initialESDTTxs, nil
}

func createGenesisSCRData(tokenID string, value *big.Int) []byte {
numTokensToTransferBytes := big.NewInt(1).Bytes()
return []byte(core.BuiltInFunctionMultiESDTNFTTransfer +
axenteoctavian marked this conversation as resolved.
Show resolved Hide resolved
"@" + hex.EncodeToString(numTokensToTransferBytes) +
"@" + hex.EncodeToString([]byte(tokenID)) + // tokenID
"@" + hex.EncodeToString(big.NewInt(0).Bytes()) + //nonce
"@" + hex.EncodeToString(value.Bytes())) // value
}
Loading
Loading