Skip to content

Commit

Permalink
Introduce blade admin
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Nov 28, 2023
1 parent 797e037 commit dddd843
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 50 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"`

// BladeAdminAddress is the address that will be the owner of the NativeERC20 mintable token,
// and StakeManager contract which handles validator actions like whitelist
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
1 change: 1 addition & 0 deletions e2e-polybft/framework/test-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ func NewTestCluster(t *testing.T, validatorsCount int, opts ...ClusterOption) *T
"--premine", "0x0000000000000000000000000000000000000000",
"--reward-wallet", testRewardWalletAddr.String(),
"--trieroot", cluster.Config.InitialStateRoot.String(),
"--blade-admin", addresses[0].String(), // we put first validator as owner by default
}

if cluster.Config.BlockTime != 0 {
Expand Down

0 comments on commit dddd843

Please sign in to comment.