-
Notifications
You must be signed in to change notification settings - Fork 201
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
Changes from 9 commits
7054104
6562a21
6b2d86c
b667a31
507f129
f32f105
1bb7da1
adac471
21f7362
9d86b31
46897e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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 | ||
} | ||
|
||
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 | ||
} | ||
|
@@ -55,8 +62,27 @@ | |
return gbc.createSovereignHeaders(argsCreateBlock) | ||
} | ||
|
||
func (gbc *sovereignGenesisBlockCreator) initGenesisAccounts() error { | ||
acc, err := gbc.arg.Accounts.LoadAccount(core.SystemAccountAddress) | ||
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 | ||
} | ||
|
@@ -77,6 +103,12 @@ | |
return mapEmptyGenesisBlocks, nil | ||
} | ||
|
||
func createSovereignGenesisConfig() config.EnableEpochs { | ||
cfg := createGenesisConfig() | ||
cfg.ESDTMultiTransferEnableEpoch = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the loaded config from config.toml. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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 { | ||
|
@@ -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 | ||
} | ||
|
||
metaProcessor, err := createProcessorsForMetaGenesisBlock(arg, sovereignGenesisConfig, createGenesisRoundConfig()) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
|
@@ -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 | ||
} | ||
|
||
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) | ||
} | ||
|
||
err = setRootHash(genesisBlock, rootHash) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
|
@@ -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 | ||
} |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.