Skip to content

Commit

Permalink
Ensure premining reserve account (0x0 address) (#1685)
Browse files Browse the repository at this point in the history
* Ensure that escrow account is premined in the genesis

* Rename escrow to reserve

---------

Co-authored-by: Victor Castell <[email protected]>
  • Loading branch information
Stefan-Ethernal and vcastellm authored Jul 4, 2023
1 parent 1084d99 commit 4cf4b79
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 14 deletions.
41 changes: 34 additions & 7 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ var (
errInvalidEpochSize = errors.New("epoch size must be greater than 1")
errInvalidTokenParams = errors.New("native token params were not submitted in proper format " +
"(<name:symbol:decimals count:mintable flag:[mintable token owner address]>)")
errRewardWalletAmountZero = errors.New("reward wallet amount can not be zero or negative")
errRewardWalletAmountZero = errors.New("reward wallet amount can not be zero or negative")
errReserveAccMustBePremined = errors.New("it is mandatory to premine reserve account (0x0 address)")
)

type genesisParams struct {
Expand Down Expand Up @@ -124,6 +125,8 @@ type genesisParams struct {
nativeTokenConfigRaw string
nativeTokenConfig *polybft.TokenConfig

premineInfos []*premineInfo

// rewards
rewardTokenCode string
rewardWallet string
Expand Down Expand Up @@ -154,6 +157,10 @@ func (p *genesisParams) validateFlags() error {
if err := p.validateRewardWallet(); err != nil {
return err
}

if err := p.validatePremineInfo(); err != nil {
return err
}
}

// Check if the genesis file already exists
Expand Down Expand Up @@ -418,12 +425,7 @@ func (p *genesisParams) initGenesisConfig() error {
chainConfig.Genesis.Alloc[staking.AddrStakingContract] = stakingAccount
}

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

for _, premineInfo := range p.premineInfos {
chainConfig.Genesis.Alloc[premineInfo.address] = &chain.GenesisAccount{
Balance: premineInfo.amount,
}
Expand Down Expand Up @@ -476,6 +478,31 @@ func (p *genesisParams) validateRewardWallet() error {
return nil
}

// validatePremineInfo validates whether reserve account (0x0 address) is premined
func (p *genesisParams) validatePremineInfo() error {
p.premineInfos = make([]*premineInfo, 0, len(p.premine))
isReserveAccPremined := false

for _, premine := range p.premine {
premineInfo, err := parsePremineInfo(premine)
if err != nil {
return fmt.Errorf("invalid premine balance amount provided: %w", err)
}

p.premineInfos = append(p.premineInfos, premineInfo)

if premineInfo.address == types.ZeroAddress {
isReserveAccPremined = true
}
}

if !isReserveAccPremined {
return errReserveAccMustBePremined
}

return nil
}

// validateBurnContract validates burn contract. If native token is mintable,
// burn contract flag must not be set. If native token is non mintable only one burn contract
// can be set and the specified address will be used to predeploy default EIP1559 burn contract.
Expand Down
59 changes: 59 additions & 0 deletions command/genesis/params_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package genesis

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
"github.com/umbracle/ethgo"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/types"
)
Expand Down Expand Up @@ -116,3 +119,59 @@ func Test_extractNativeTokenMetadata(t *testing.T) {
})
}
}

func Test_validatePremineInfo(t *testing.T) {
t.Parallel()

cases := []struct {
name string
premineRaw []string
expectedPremines []*premineInfo
expectErrMsg string
}{
{
name: "invalid premine balance",
premineRaw: []string{"0x12345:loremIpsum"},
expectedPremines: []*premineInfo{},
expectErrMsg: "invalid premine balance amount provided",
},
{
name: "missing zero address premine",
premineRaw: []string{types.StringToAddress("12").String()},
expectedPremines: []*premineInfo{
{address: types.StringToAddress("12"), amount: command.DefaultPremineBalance},
},
expectErrMsg: errReserveAccMustBePremined.Error(),
},
{
name: "valid premine information",
premineRaw: []string{
fmt.Sprintf("%s:%d", types.StringToAddress("1"), ethgo.Ether(10)),
fmt.Sprintf("%s:%d", types.ZeroAddress, ethgo.Ether(10000)),
},
expectedPremines: []*premineInfo{
{address: types.StringToAddress("1"), amount: ethgo.Ether(10)},
{address: types.ZeroAddress, amount: ethgo.Ether(10000)},
},
expectErrMsg: "",
},
}

for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()

p := &genesisParams{premine: c.premineRaw}
err := p.validatePremineInfo()

if c.expectErrMsg != "" {
require.ErrorContains(t, err, c.expectErrMsg)
} else {
require.NoError(t, err)
}

require.Equal(t, c.expectedPremines, p.premineInfos)
})
}
}
9 changes: 2 additions & 7 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,8 @@ func (p *genesisParams) generatePolyBftChainConfig(o command.OutputFormatter) er
// populate premine balance map
premineBalances := make(map[types.Address]*premineInfo, len(p.premine))

for _, premine := range p.premine {
premineInfo, err := parsePremineInfo(premine)
if err != nil {
return fmt.Errorf("invalid balance amount provided '%s' : %w", premine, err)
}

premineBalances[premineInfo.address] = premineInfo
for _, premine := range p.premineInfos {
premineBalances[premine.address] = premine
}

walletPremineInfo, err := parsePremineInfo(p.rewardWallet)
Expand Down

0 comments on commit 4cf4b79

Please sign in to comment.