Skip to content

Commit

Permalink
Change genesis premine logic (#981)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal authored Dec 1, 2022
1 parent 82fd78d commit f33af57
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 36 deletions.
1 change: 0 additions & 1 deletion command/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ func setFlags(cmd *cobra.Command) {
defaultBridge,
"enabling bridge, default is not enabled",
)
cmd.MarkFlagsMutuallyExclusive(premineFlag, premineValidatorsFlag)
cmd.MarkFlagsMutuallyExclusive(validatorsFlag, premineValidatorsFlag)
}
}
Expand Down
13 changes: 11 additions & 2 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,19 @@ func (p *genesisParams) initGenesisConfig() error {
chainConfig.Genesis.Alloc[staking.AddrStakingContract] = stakingAccount
}

if err := fillPremineMap(chainConfig.Genesis.Alloc, p.premine); err != nil {
return err
premineInfos := make([]*premineInfo, len(p.premine))

for i, premineRaw := range p.premine {
premineInfo, err := parsePremineInfo(premineRaw)
if err != nil {
return err
}

premineInfos[i] = premineInfo
}

fillPremineMap(chainConfig.Genesis.Alloc, premineInfos)

p.genesisConfig = chainConfig

return nil
Expand Down
42 changes: 32 additions & 10 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,44 @@ func (p *genesisParams) generatePolyBFTConfig() (*chain.Chain, error) {
}
}

var premine []string = nil
if len(p.premine) > 0 {
premine = p.premine
} else if p.premineValidators != "" {
premine = make([]string, len(validatorsInfo))
var (
validatorPreminesMap map[types.Address]int
premineInfos []*premineInfo
)

if p.premineValidators != "" {
validatorPreminesMap = make(map[types.Address]int, len(validatorsInfo))

for i, vi := range validatorsInfo {
premine[i] = fmt.Sprintf("%s:%s",
vi.Account.Ecdsa.Address().String(), p.premineValidators)
premineInfo, err := parsePremineInfo(fmt.Sprintf("%s:%s",
vi.Account.Ecdsa.Address().String(), p.premineValidators))
if err != nil {
return nil, err
}

premineInfos = append(premineInfos, premineInfo)
validatorPreminesMap[premineInfo.address] = i
}
}

// premine accounts
if err := fillPremineMap(allocs, premine); err != nil {
return nil, err
if len(p.premine) > 0 {
for _, premine := range p.premine {
premineInfo, err := parsePremineInfo(premine)
if err != nil {
return nil, err
}

if i, ok := validatorPreminesMap[premineInfo.address]; ok {
premineInfos[i] = premineInfo
} else {
premineInfos = append(premineInfos, premineInfo)
}
}
}

// premine accounts
fillPremineMap(allocs, premineInfos)

// set initial validator set
genesisValidators, err := p.getGenesisValidators(validatorsInfo, allocs)
if err != nil {
Expand Down
51 changes: 28 additions & 23 deletions command/genesis/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package genesis
import (
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -40,6 +41,11 @@ func (g *GenesisGenError) GetType() string {
return g.errorType
}

type premineInfo struct {
address types.Address
balance *big.Int
}

// verifyGenesisExistence checks if the genesis file at the specified path is present
func verifyGenesisExistence(genesisPath string) *GenesisGenError {
_, err := os.Stat(genesisPath)
Expand All @@ -61,34 +67,33 @@ func verifyGenesisExistence(genesisPath string) *GenesisGenError {
}

// fillPremineMap fills the premine map for the genesis.json file with passed in balances and accounts
func fillPremineMap(
premineMap map[types.Address]*chain.GenesisAccount,
premine []string,
) error {
for _, prem := range premine {
var addr types.Address

val := command.DefaultPremineBalance

if indx := strings.Index(prem, ":"); indx != -1 {
// <addr>:<balance>
addr, val = types.StringToAddress(prem[:indx]), prem[indx+1:]
} else {
// <addr>
addr = types.StringToAddress(prem)
func fillPremineMap(premineMap map[types.Address]*chain.GenesisAccount, premineInfos []*premineInfo) {
for _, premine := range premineInfos {
premineMap[premine.address] = &chain.GenesisAccount{
Balance: premine.balance,
}
}
}

amount, err := types.ParseUint256orHex(&val)
if err != nil {
return fmt.Errorf("failed to parse amount %s: %w", val, err)
}
// parsePremineInfo parses provided premine information and returns premine address and premine balance
func parsePremineInfo(premineInfoRaw string) (*premineInfo, error) {
address := types.ZeroAddress
val := command.DefaultPremineBalance

if delimiterIdx := strings.Index(premineInfoRaw, ":"); delimiterIdx != -1 {
// <addr>:<balance>
address, val = types.StringToAddress(premineInfoRaw[:delimiterIdx]), premineInfoRaw[delimiterIdx+1:]
} else {
// <addr>
address = types.StringToAddress(premineInfoRaw)
}

premineMap[addr] = &chain.GenesisAccount{
Balance: amount,
}
amount, err := types.ParseUint256orHex(&val)
if err != nil {
return nil, fmt.Errorf("failed to parse amount %s: %w", val, err)
}

return nil
return &premineInfo{address: address, balance: amount}, nil
}

type GenesisTarget struct {
Expand Down

0 comments on commit f33af57

Please sign in to comment.