Skip to content

Commit

Permalink
Add blade admin flag (#24)
Browse files Browse the repository at this point in the history
* Introduce blade admin

* Move to the newest main branch on contracts

* Remove NativeTokenConfig from e2e tests

* Comments fix
  • Loading branch information
goran-ethernal authored and Stefan-Ethernal committed Nov 28, 2023
1 parent 1d1db42 commit 53324e1
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 122 deletions.
9 changes: 8 additions & 1 deletion command/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func setFlags(cmd *cobra.Command) {
nativeTokenConfigFlag,
"",
"native token configuration, provided in the following format: "+
"<name:symbol:decimals count:mintable token owner address>",
"<name:symbol:decimals count>",
)

cmd.Flags().StringVar(
Expand Down Expand Up @@ -220,6 +220,13 @@ func setFlags(cmd *cobra.Command) {
defaultBlockTrackerPollInterval,
"interval (number of seconds) at which block tracker polls for latest block at rootchain",
)

cmd.Flags().StringVar(
&params.bladeAdmin,
bladeAdminFlag,
"",
"address of owner/admin of NativeERC20 token and StakeManager",
)
}

// Access Control Lists
Expand Down
5 changes: 5 additions & 0 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type genesisParams struct {
blockTrackerPollInterval time.Duration

proxyContractsAdmin string
bladeAdmin string
}

func (p *genesisParams) validateFlags() error {
Expand Down Expand Up @@ -154,6 +155,10 @@ func (p *genesisParams) validateFlags() error {
return err
}

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

if err := p.parseStakeInfo(); err != nil {
return err
}
Expand Down
11 changes: 5 additions & 6 deletions command/genesis/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,31 @@ func Test_extractNativeTokenMetadata(t *testing.T) {
},
{
name: "not enough params provided",
rawConfig: "Test:TST:18",
rawConfig: "Test:TST",
expectErr: true,
},
{
name: "empty name provided",
rawConfig: ":TST:18:0x123456789",
rawConfig: ":TST:18",
expectErr: true,
},
{
name: "empty symbol provided",
rawConfig: "Test::18:0x123456789",
rawConfig: "Test::18",
expectErr: true,
},
{
name: "invalid decimals number provided",
rawConfig: "Test:TST:9999999999999999999999999999999999999999999999999999999999:false:0x123456789",
rawConfig: "Test:TST:9999999999999999999999999999999999999999999999999999999999:false",
expectErr: true,
},
{
name: "valid config",
rawConfig: "MyToken:MTK:9:0x123456789",
rawConfig: "MyToken:MTK:9",
expectedCfg: &polybft.TokenConfig{
Name: "MyToken",
Symbol: "MTK",
Decimals: 9,
Owner: types.StringToAddress("0x123456789"),
},
expectErr: false,
},
Expand Down
65 changes: 37 additions & 28 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
bridgeAllowListEnabledFlag = "bridge-allow-list-enabled"
bridgeBlockListAdminFlag = "bridge-block-list-admin"
bridgeBlockListEnabledFlag = "bridge-block-list-enabled"
bladeAdminFlag = "blade-admin"

bootnodePortStart = 30301

Expand All @@ -57,7 +58,13 @@ const (
)

var (
errNoGenesisValidators = errors.New("genesis validators aren't provided")
errNoGenesisValidators = errors.New("genesis validators aren't provided")
errProxyAdminNotProvided = errors.New("proxy contracts admin address must be set")
errProxyAdminIsZeroAddress = errors.New("proxy contracts admin address must not be zero address")
errProxyAdminIsSystemCaller = errors.New("proxy contracts admin address must not be system caller address")
errBladeAdminNotProvided = errors.New("blade admin address must be set")
errBladeAdminIsZeroAddress = errors.New("blade admin address must not be zero address")
errBladeAdminIsSystemCaller = errors.New("blade admin address must not be system caller address")
)

type contractInfo struct {
Expand Down Expand Up @@ -139,6 +146,7 @@ func (p *genesisParams) generateChainConfig(o command.OutputFormatter) error {
BlockTimeDrift: p.blockTimeDrift,
BlockTrackerPollInterval: common.Duration{Duration: p.blockTrackerPollInterval},
ProxyContractsAdmin: types.StringToAddress(p.proxyContractsAdmin),
BladeAdmin: types.StringToAddress(p.bladeAdmin),
}

enabledForks := chain.AllForksEnabled
Expand Down Expand Up @@ -488,30 +496,6 @@ func (p *genesisParams) getValidatorAccounts() ([]*validator.GenesisValidator, e
return validators, nil
}

// validatePolyBFTParams validates params for polybft consensus
func (p *genesisParams) validatePolyBFTParams() error {
if err := p.extractNativeTokenMetadata(); err != nil {
return err
}

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

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

if p.epochSize < 2 {
// Epoch size must be greater than 1, so new transactions have a chance to be added to a block.
// Otherwise, every block would be an endblock (meaning it will not have any transactions).
// Check is placed here to avoid additional parsing if epochSize < 2
return errInvalidEpochSize
}

return p.validateProxyContractsAdmin()
}

// validateRewardWalletAndToken validates reward wallet flag
func (p *genesisParams) validateRewardWalletAndToken() error {
if p.rewardWallet == "" {
Expand All @@ -537,16 +521,41 @@ func (p *genesisParams) validateRewardWalletAndToken() error {

func (p *genesisParams) validateProxyContractsAdmin() error {
if strings.TrimSpace(p.proxyContractsAdmin) == "" {
return errors.New("proxy contracts admin address must be set")
return errProxyAdminNotProvided
}

if err := types.IsValidAddress(p.proxyContractsAdmin); err != nil {
return fmt.Errorf("proxy contracts admin address is not a valid address: %w", err)
}

proxyContractsAdminAddr := types.StringToAddress(p.proxyContractsAdmin)
if proxyContractsAdminAddr == types.ZeroAddress {
return errors.New("proxy contracts admin address must not be zero address")
return errProxyAdminIsZeroAddress
}

if proxyContractsAdminAddr == contracts.SystemCaller {
return errors.New("proxy contracts admin address must not be system caller address")
return errProxyAdminIsSystemCaller
}

return nil
}

func (p *genesisParams) validateBladeAdminFlag() error {
if strings.TrimSpace(p.bladeAdmin) == "" {
return errBladeAdminNotProvided
}

if err := types.IsValidAddress(p.bladeAdmin); err != nil {
return fmt.Errorf("blade admin address is not a valid address: %w", err)
}

bladeAdminAddr := types.StringToAddress(p.proxyContractsAdmin)
if bladeAdminAddr == types.ZeroAddress {
return errBladeAdminIsZeroAddress
}

if bladeAdminAddr == contracts.SystemCaller {
return errBladeAdminIsSystemCaller
}

return nil
Expand Down
1 change: 1 addition & 0 deletions consensus/polybft/contracts_initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func initStakeManager(polyBFTConfig PolyBFTConfig, transition *state.Transition)
NewBls: contracts.BLSContract,
EpochManager: contracts.EpochManagerContract,
NewDomain: signer.DomainValidatorSetString,
Owner: polyBFTConfig.BladeAdmin,
}

input, err := initFn.EncodeAbi()
Expand Down
1 change: 1 addition & 0 deletions consensus/polybft/contractsapi/contractsapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion consensus/polybft/contractsapi/gen_sc_data.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func GenesisPostHookFactory(config *chain.Chain, engineName string) func(txn *st
// initialize NativeERC20 SC
params := &contractsapi.InitializeNativeERC20Fn{
Predicate_: contracts.ChildERC20PredicateContract,
Owner_: polyBFTConfig.NativeTokenConfig.Owner,
Owner_: polyBFTConfig.BladeAdmin,
RootToken_: types.ZeroAddress, // in case native mintable token is used, it is always root token
Name_: polyBFTConfig.NativeTokenConfig.Name,
Symbol_: polyBFTConfig.NativeTokenConfig.Symbol,
Expand Down
24 changes: 11 additions & 13 deletions consensus/polybft/polybft_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

const (
ConsensusName = "polybft"
minNativeTokenParamsNumber = 4
minNativeTokenParamsNumber = 3

defaultNativeTokenName = "Polygon"
defaultNativeTokenSymbol = "MATIC"
Expand All @@ -28,11 +28,10 @@ var (
Name: defaultNativeTokenName,
Symbol: defaultNativeTokenSymbol,
Decimals: defaultNativeTokenDecimals,
Owner: types.ZeroAddress,
}

errInvalidTokenParams = errors.New("native token params were not submitted in proper format " +
"(<name:symbol:decimals count:token owner address>)")
"(<name:symbol:decimals count>)")
)

// PolyBFTConfig is the configuration file for the Polybft consensus protocol.
Expand Down Expand Up @@ -77,11 +76,15 @@ type PolyBFTConfig struct {

// BlockTrackerPollInterval specifies interval
// at which block tracker polls for blocks on a rootchain
BlockTrackerPollInterval common.Duration `json:"blockTrackerPollInterval,omitempty"`
BlockTrackerPollInterval common.Duration `json:"blockTrackerPollInterval"`

// ProxyContractsAdmin is the address that will have the privilege to change both the proxy
// implementation address and the admin
ProxyContractsAdmin types.Address `json:"proxyContractsAdmin,omitempty"`
ProxyContractsAdmin types.Address `json:"proxyContractsAdmin"`

// BladeAdmin is the address that will be the owner of the NativeERC20 mintable token,
// and StakeManager contract which manages validators
BladeAdmin types.Address `json:"bladeAdmin"`
}

// LoadPolyBFTConfig loads chain config from provided path and unmarshals PolyBFTConfig
Expand Down Expand Up @@ -192,10 +195,9 @@ func (r *RootchainConfig) ToBridgeConfig() *BridgeConfig {

// TokenConfig is the configuration of native token used by edge network
type TokenConfig struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals uint8 `json:"decimals"`
Owner types.Address `json:"owner"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals uint8 `json:"decimals"`
}

func ParseRawTokenConfig(rawConfig string) (*TokenConfig, error) {
Expand Down Expand Up @@ -226,14 +228,10 @@ func ParseRawTokenConfig(rawConfig string) (*TokenConfig, error) {
return nil, errInvalidTokenParams
}

// owner address
owner := types.StringToAddress(strings.TrimSpace(params[3]))

return &TokenConfig{
Name: name,
Symbol: symbol,
Decimals: uint8(decimals),
Owner: owner,
}, nil
}

Expand Down
6 changes: 0 additions & 6 deletions e2e-polybft/e2e/acls_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package e2e

import (
"fmt"
"math/big"
"testing"

Expand Down Expand Up @@ -44,7 +43,6 @@ func TestE2E_AllowList_ContractDeployment(t *testing.T) {
otherAddr := types.Address{0x1}

cluster := framework.NewTestCluster(t, 5,
framework.WithNativeTokenConfig(fmt.Sprintf(framework.NativeTokenMintableTestCfg, adminAddr)),
framework.WithPremine(adminAddr, targetAddr),
framework.WithContractDeployerAllowListAdmin(adminAddr),
framework.WithContractDeployerAllowListEnabled(otherAddr),
Expand Down Expand Up @@ -143,7 +141,6 @@ func TestE2E_BlockList_ContractDeployment(t *testing.T) {
otherAddr := types.Address{0x1}

cluster := framework.NewTestCluster(t, 5,
framework.WithNativeTokenConfig(fmt.Sprintf(framework.NativeTokenMintableTestCfg, adminAddr)),
framework.WithPremine(adminAddr, targetAddr),
framework.WithContractDeployerBlockListAdmin(adminAddr),
framework.WithContractDeployerBlockListEnabled(otherAddr),
Expand Down Expand Up @@ -227,7 +224,6 @@ func TestE2E_AllowList_Transactions(t *testing.T) {
otherAddr := types.Address(other.Address())

cluster := framework.NewTestCluster(t, 5,
framework.WithNativeTokenConfig(fmt.Sprintf(framework.NativeTokenMintableTestCfg, adminAddr)),
framework.WithPremine(adminAddr, targetAddr, otherAddr),
framework.WithTransactionsAllowListAdmin(adminAddr),
framework.WithTransactionsAllowListEnabled(otherAddr),
Expand Down Expand Up @@ -321,7 +317,6 @@ func TestE2E_BlockList_Transactions(t *testing.T) {
otherAddr := types.Address(other.Address())

cluster := framework.NewTestCluster(t, 5,
framework.WithNativeTokenConfig(fmt.Sprintf(framework.NativeTokenMintableTestCfg, adminAddr)),
framework.WithPremine(adminAddr, targetAddr, otherAddr),
framework.WithTransactionsBlockListAdmin(adminAddr),
framework.WithTransactionsBlockListEnabled(otherAddr),
Expand Down Expand Up @@ -392,7 +387,6 @@ func TestE2E_AddressLists_Bridge(t *testing.T) {
otherAddr := types.Address(other.Address())

cluster := framework.NewTestCluster(t, 5,
framework.WithNativeTokenConfig(fmt.Sprintf(framework.NativeTokenMintableTestCfg, adminAddr)),
framework.WithPremine(adminAddr, targetAddr, otherAddr),
framework.WithBridgeAllowListAdmin(adminAddr),
framework.WithBridgeAllowListEnabled(otherAddr),
Expand Down
16 changes: 0 additions & 16 deletions e2e-polybft/e2e/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,6 @@ func TestE2E_Bridge_ERC721Transfer(t *testing.T) {
stateSyncedLogsCount = 2
)

minter, err := wallet.GenerateKey()
require.NoError(t, err)

receiverKeys := make([]string, transfersCount)
receivers := make([]string, transfersCount)
receiversAddrs := make([]types.Address, transfersCount)
Expand All @@ -497,8 +494,6 @@ func TestE2E_Bridge_ERC721Transfer(t *testing.T) {

cluster := framework.NewTestCluster(t, 5,
framework.WithEpochSize(epochSize),
framework.WithNativeTokenConfig(fmt.Sprintf(framework.NativeTokenMintableTestCfg, minter.Address())),
framework.WithPremine(types.Address(minter.Address())),
framework.WithPremine(receiversAddrs...))
defer cluster.Stop()

Expand Down Expand Up @@ -634,9 +629,6 @@ func TestE2E_Bridge_ERC1155Transfer(t *testing.T) {
stateSyncedLogsCount = 2
)

minter, err := wallet.GenerateKey()
require.NoError(t, err)

receiverKeys := make([]string, transfersCount)
receivers := make([]string, transfersCount)
receiversAddrs := make([]types.Address, transfersCount)
Expand All @@ -662,8 +654,6 @@ func TestE2E_Bridge_ERC1155Transfer(t *testing.T) {
cluster := framework.NewTestCluster(t, 5,
framework.WithNumBlockConfirmations(0),
framework.WithEpochSize(epochSize),
framework.WithNativeTokenConfig(fmt.Sprintf(framework.NativeTokenMintableTestCfg, minter.Address())),
framework.WithPremine(types.Address(minter.Address())),
framework.WithPremine(receiversAddrs...))
defer cluster.Stop()

Expand Down Expand Up @@ -862,7 +852,6 @@ func TestE2E_Bridge_ChildChainMintableTokensTransfer(t *testing.T) {
cluster := framework.NewTestCluster(t, 5,
framework.WithNumBlockConfirmations(0),
framework.WithEpochSize(epochSize),
framework.WithNativeTokenConfig(fmt.Sprintf(framework.NativeTokenMintableTestCfg, adminAddr)),
framework.WithBridgeBlockListAdmin(adminAddr),
framework.WithPremine(append(depositors, adminAddr)...)) //nolint:makezero
defer cluster.Stop()
Expand Down Expand Up @@ -1214,14 +1203,9 @@ func TestE2E_Bridge_ChangeVotingPower(t *testing.T) {
epochSize = 5
)

minter, err := wallet.GenerateKey()
require.NoError(t, err)

cluster := framework.NewTestCluster(t, 5,
framework.WithEpochSize(epochSize),
framework.WithEpochReward(1000000),
framework.WithNativeTokenConfig(fmt.Sprintf(framework.NativeTokenMintableTestCfg, minter.Address())),
framework.WithPremine(types.Address(minter.Address())),
)
defer cluster.Stop()

Expand Down
Loading

0 comments on commit 53324e1

Please sign in to comment.