Skip to content

Commit

Permalink
Differentiate chain-id and supernet-id (#1521)
Browse files Browse the repository at this point in the history
* Differentiate chain id and supernet id

* Small change
  • Loading branch information
goran-ethernal authored May 18, 2023
1 parent 47ca1a8 commit ed61412
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 54 deletions.
5 changes: 0 additions & 5 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ var (
errInvalidTokenParams = errors.New("native token params were not submitted in proper format " +
"(<name:symbol:decimals count:mintable flag>)")
errRewardWalletAmountZero = errors.New("reward wallet amount can not be zero or negative")
errChainIDPolyBFT = errors.New("chain id can not be set for polybft consensus")
)

type genesisParams struct {
Expand Down Expand Up @@ -151,10 +150,6 @@ func (p *genesisParams) validateFlags() error {
if err := p.validateRewardWallet(); err != nil {
return err
}

if p.chainID != command.DefaultChainID {
return errChainIDPolyBFT
}
}

// Check if the genesis file already exists
Expand Down
3 changes: 2 additions & 1 deletion command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func (p *genesisParams) generatePolyBftChainConfig(o command.OutputFormatter) er
chainConfig := &chain.Chain{
Name: p.name,
Params: &chain.Params{
Forks: enabledForks,
ChainID: int64(p.chainID),
Forks: enabledForks,
Engine: map[string]interface{}{
string(server.PolyBFTConsensus): polyBftConfig,
},
Expand Down
17 changes: 9 additions & 8 deletions command/rootchain/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ func runCommand(cmd *cobra.Command, _ []string) {
}
}

rootchainCfg, chainID, err := deployContracts(outputter, client, consensusConfig.InitialValidatorSet, cmd.Context())
rootchainCfg, supernetID, err := deployContracts(outputter, client,
chainConfig.Params.ChainID, consensusConfig.InitialValidatorSet, cmd.Context())
if err != nil {
outputter.SetError(fmt.Errorf("failed to deploy rootchain contracts: %w", err))

Expand All @@ -284,9 +285,9 @@ func runCommand(cmd *cobra.Command, _ []string) {
consensusConfig.Bridge.EventTrackerStartBlocks = map[types.Address]uint64{
rootchainCfg.StateSenderAddress: blockNum,
}
consensusConfig.SupernetID = supernetID

// write updated chain configuration
chainConfig.Params.ChainID = chainID
chainConfig.Params.Engine[polybft.ConsensusName] = consensusConfig

if err := cmdHelper.WriteGenesisConfigToDisk(chainConfig, params.genesisPath); err != nil {
Expand All @@ -302,7 +303,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

// deployContracts deploys and initializes rootchain smart contracts
func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client, chainID int64,
initialValidators []*validator.GenesisValidator, cmdCtx context.Context) (*polybft.RootchainConfig, int64, error) {
txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithClient(client))
if err != nil {
Expand Down Expand Up @@ -512,7 +513,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
}

// register supernets manager on stake manager
chainID, err := registerChainOnStakeManager(txRelayer, rootchainConfig, deployerKey)
supernetID, err := registerChainOnStakeManager(txRelayer, rootchainConfig, deployerKey)
if err != nil {
return nil, 0, err
}
Expand All @@ -523,7 +524,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
return nil, 0, err
}

return rootchainConfig, chainID, nil
return rootchainConfig, supernetID, nil
}

// populateExistingTokenAddr checks whether given token is deployed on the provided address.
Expand Down Expand Up @@ -570,7 +571,7 @@ func registerChainOnStakeManager(txRelayer txrelayer.TxRelayer,
var (
childChainRegisteredEvent contractsapi.ChildManagerRegisteredEvent
found bool
chainID int64
supernetID int64
)

for _, log := range receipt.Logs {
Expand All @@ -583,7 +584,7 @@ func registerChainOnStakeManager(txRelayer txrelayer.TxRelayer,
continue
}

chainID = childChainRegisteredEvent.ID.Int64()
supernetID = childChainRegisteredEvent.ID.Int64()
found = true

break
Expand All @@ -593,7 +594,7 @@ func registerChainOnStakeManager(txRelayer txrelayer.TxRelayer,
return 0, errors.New("could not find a log that child chain was registered on stake manager")
}

return chainID, nil
return supernetID, nil
}

// initializeCheckpointManager invokes initialize function on "CheckpointManager" smart contract
Expand Down
2 changes: 1 addition & 1 deletion command/rootchain/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestDeployContracts_NoPanics(t *testing.T) {
outputter := command.InitializeOutputter(GetCommand())

require.NotPanics(t, func() {
_, _, err = deployContracts(outputter, client, []*validator.GenesisValidator{}, context.Background())
_, _, err = deployContracts(outputter, client, 1, []*validator.GenesisValidator{}, context.Background())
})
require.NoError(t, err)
}
4 changes: 3 additions & 1 deletion command/rootchain/staking/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
sidechainHelper "github.com/0xPolygon/polygon-edge/command/sidechain"
)

var supernetIDFlag = "supernet-id"

type stakeParams struct {
accountDir string
accountConfig string
stakeManagerAddr string
nativeRootTokenAddr string
jsonRPC string
chainID uint64
supernetID int64
amount string

amountValue *big.Int
Expand Down
10 changes: 5 additions & 5 deletions command/rootchain/staking/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func setFlags(cmd *cobra.Command) {
"amount to stake",
)

cmd.Flags().Uint64Var(
&params.chainID,
polybftsecrets.ChainIDFlag,
cmd.Flags().Int64Var(
&params.supernetID,
supernetIDFlag,
0,
polybftsecrets.ChainIDFlagDesc,
"ID of supernet provided by stake manager on supernet registration",
)

cmd.Flags().StringVar(
Expand Down Expand Up @@ -123,7 +123,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
}

stakeFn := contractsapi.StakeForStakeManagerFn{
ID: new(big.Int).SetUint64(params.chainID),
ID: new(big.Int).SetInt64(params.supernetID),
Amount: params.amountValue,
}

Expand Down
2 changes: 1 addition & 1 deletion command/rootchain/supernet/supernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {

for _, v := range consensusConfig.InitialValidatorSet {
finalizedStake, err := getFinalizedStake(callerAddr, v.Address, stakeManager,
chainConfig.Params.ChainID, txRelayer)
consensusConfig.SupernetID, txRelayer)
if err != nil {
return fmt.Errorf("could not get finalized stake for validator: %v. Error: %w", v.Address, err)
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/polybft/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ It has a native support for running bridge, which enables running cross-chain tr
9. Initial staking on rootchain - each validator needs to do initial staking on rootchain (StakeManager) contract. **This command is for testing purposes only.**

```bash
$ polygon-edge polybft stake --data-dir ./test-chain-1 --chain-id <id_of_child_chain_from_genesis> \
$ polygon-edge polybft stake --data-dir ./test-chain-1 --supernet-id <supernet_id_from_genesis> \
--amount <amount_of_tokens_to_stake> \
--stake-manager <address_of_StakeManager_contract> --native-root-token <address_of_native_root_token>
```
Expand Down
11 changes: 7 additions & 4 deletions consensus/polybft/polybft_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type PolyBFTConfig struct {

InitialTrieRoot types.Hash `json:"initialTrieRoot"`

// SupernetID indicates ID of given supernet generated by stake manager contract
SupernetID int64 `json:"supernetID"`

// MaxValidatorSetSize indicates the maximum size of validator set
MaxValidatorSetSize uint64 `json:"maxValidatorSetSize"`

Expand All @@ -48,18 +51,18 @@ type PolyBFTConfig struct {
}

// LoadPolyBFTConfig loads chain config from provided path and unmarshals PolyBFTConfig
func LoadPolyBFTConfig(chainConfigFile string) (PolyBFTConfig, int64, error) {
func LoadPolyBFTConfig(chainConfigFile string) (PolyBFTConfig, error) {
chainCfg, err := chain.ImportFromFile(chainConfigFile)
if err != nil {
return PolyBFTConfig{}, 0, err
return PolyBFTConfig{}, err
}

polybftConfig, err := GetPolyBFTConfig(chainCfg)
if err != nil {
return PolyBFTConfig{}, 0, err
return PolyBFTConfig{}, err
}

return polybftConfig, chainCfg.Params.ChainID, err
return polybftConfig, err
}

// GetPolyBFTConfig deserializes provided chain config and returns PolyBFTConfig
Expand Down
16 changes: 8 additions & 8 deletions e2e-polybft/e2e/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestE2E_Bridge_Transfers(t *testing.T) {

cluster.WaitForReady(t)

polybftCfg, _, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
polybftCfg, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
require.NoError(t, err)

validatorSrv := cluster.Servers[0]
Expand Down Expand Up @@ -308,7 +308,7 @@ func TestE2E_Bridge_DepositAndWithdrawERC721(t *testing.T) {

cluster.WaitForReady(t)

polybftCfg, _, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
polybftCfg, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
require.NoError(t, err)

// DEPOSIT ERC721 TOKENS
Expand Down Expand Up @@ -478,7 +478,7 @@ func TestE2E_Bridge_DepositAndWithdrawERC1155(t *testing.T) {

cluster.WaitForReady(t)

polybftCfg, _, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
polybftCfg, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
require.NoError(t, err)

// DEPOSIT ERC1155 TOKENS
Expand Down Expand Up @@ -634,7 +634,7 @@ func TestE2E_CheckpointSubmission(t *testing.T) {
rootChainRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithIPAddress(cluster.Bridge.JSONRPCAddr()))
require.NoError(t, err)

polybftCfg, _, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
polybftCfg, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
require.NoError(t, err)

checkpointManagerAddr := ethgo.Address(polybftCfg.Bridge.CheckpointManagerAddr)
Expand Down Expand Up @@ -687,7 +687,7 @@ func TestE2E_Bridge_ChangeVotingPower(t *testing.T) {
defer cluster.Stop()

// load polybft config
polybftCfg, chainID, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
polybftCfg, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
require.NoError(t, err)

validatorSecretFiles, err := genesis.GetValidatorKeyFiles(cluster.Config.TmpDir, cluster.Config.ValidatorPrefix)
Expand Down Expand Up @@ -720,7 +720,7 @@ func TestE2E_Bridge_ChangeVotingPower(t *testing.T) {
validatorAddr,
polybftCfg.Bridge.CustomSupernetManagerAddr,
polybftCfg.Bridge.StakeManagerAddr,
chainID,
polybftCfg.SupernetID,
rootRelayer,
childRelayer)
require.NoError(t, err)
Expand All @@ -743,7 +743,7 @@ func TestE2E_Bridge_ChangeVotingPower(t *testing.T) {
require.NoError(t, validatorSrv.RootchainFund(polybftCfg.Bridge.RootNativeERC20Addr, validator.WithdrawableRewards))

// stake previously funded amount
require.NoError(t, validatorSrv.Stake(polybftCfg, chainID, validator.WithdrawableRewards))
require.NoError(t, validatorSrv.Stake(polybftCfg, validator.WithdrawableRewards))
})

queryValidators(func(idx int, validator *polybft.ValidatorInfo) {
Expand Down Expand Up @@ -826,7 +826,7 @@ func TestE2E_Bridge_Transfers_AccessLists(t *testing.T) {

cluster.WaitForReady(t)

polybftCfg, _, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
polybftCfg, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
require.NoError(t, err)

validatorSrv := cluster.Servers[0]
Expand Down
24 changes: 12 additions & 12 deletions e2e-polybft/e2e/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestE2E_Consensus_RegisterValidator(t *testing.T) {
rootChainRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithIPAddress(cluster.Bridge.JSONRPCAddr()))
require.NoError(t, err)

polybftConfig, chainID, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
polybftConfig, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
require.NoError(t, err)

// create the first account and extract the address
Expand Down Expand Up @@ -217,21 +217,21 @@ func TestE2E_Consensus_RegisterValidator(t *testing.T) {
require.NoError(t, secondValidator.RegisterValidator(polybftConfig.Bridge.CustomSupernetManagerAddr))

// stake manually for the first validator
require.NoError(t, firstValidator.Stake(polybftConfig, chainID, initialStake))
require.NoError(t, firstValidator.Stake(polybftConfig, initialStake))

// stake manually for the second validator
require.NoError(t, secondValidator.Stake(polybftConfig, chainID, initialStake))
require.NoError(t, secondValidator.Stake(polybftConfig, initialStake))

firstValidatorInfo, err := sidechain.GetValidatorInfo(firstValidatorAddr,
polybftConfig.Bridge.CustomSupernetManagerAddr, polybftConfig.Bridge.StakeManagerAddr,
chainID, rootChainRelayer, childChainRelayer)
polybftConfig.SupernetID, rootChainRelayer, childChainRelayer)
require.NoError(t, err)
require.True(t, firstValidatorInfo.IsActive)
require.True(t, firstValidatorInfo.Stake.Cmp(initialStake) == 0)

secondValidatorInfo, err := sidechain.GetValidatorInfo(secondValidatorAddr,
polybftConfig.Bridge.CustomSupernetManagerAddr, polybftConfig.Bridge.StakeManagerAddr,
chainID, rootChainRelayer, childChainRelayer)
polybftConfig.SupernetID, rootChainRelayer, childChainRelayer)
require.NoError(t, err)
require.True(t, secondValidatorInfo.IsActive)
require.True(t, secondValidatorInfo.Stake.Cmp(initialStake) == 0)
Expand Down Expand Up @@ -282,14 +282,14 @@ func TestE2E_Consensus_RegisterValidator(t *testing.T) {

firstValidatorInfo, err = sidechain.GetValidatorInfo(firstValidatorAddr,
polybftConfig.Bridge.CustomSupernetManagerAddr, polybftConfig.Bridge.StakeManagerAddr,
chainID, rootChainRelayer, childChainRelayer)
polybftConfig.SupernetID, rootChainRelayer, childChainRelayer)
require.NoError(t, err)
require.True(t, firstValidatorInfo.IsActive)
require.True(t, firstValidatorInfo.WithdrawableRewards.Cmp(bigZero) > 0)

secondValidatorInfo, err = sidechain.GetValidatorInfo(secondValidatorAddr,
polybftConfig.Bridge.CustomSupernetManagerAddr, polybftConfig.Bridge.StakeManagerAddr,
chainID, rootChainRelayer, childChainRelayer)
polybftConfig.SupernetID, rootChainRelayer, childChainRelayer)
require.NoError(t, err)
require.True(t, secondValidatorInfo.IsActive)
require.True(t, secondValidatorInfo.WithdrawableRewards.Cmp(bigZero) > 0)
Expand All @@ -309,7 +309,7 @@ func TestE2E_Consensus_Validator_Unstake(t *testing.T) {
}),
)

polybftCfg, chainID, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
polybftCfg, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
require.NoError(t, err)

srv := cluster.Servers[0]
Expand All @@ -336,7 +336,7 @@ func TestE2E_Consensus_Validator_Unstake(t *testing.T) {

validatorInfo, err := sidechain.GetValidatorInfo(validatorAddr,
polybftCfg.Bridge.CustomSupernetManagerAddr, polybftCfg.Bridge.StakeManagerAddr,
chainID, rootChainRelayer, childChainRelayer)
polybftCfg.SupernetID, rootChainRelayer, childChainRelayer)
require.NoError(t, err)
require.True(t, validatorInfo.IsActive)

Expand Down Expand Up @@ -382,7 +382,7 @@ func TestE2E_Consensus_Validator_Unstake(t *testing.T) {
// check that validator is no longer active (out of validator set)
validatorInfo, err = sidechain.GetValidatorInfo(validatorAddr,
polybftCfg.Bridge.CustomSupernetManagerAddr, polybftCfg.Bridge.StakeManagerAddr,
chainID, rootChainRelayer, childChainRelayer)
polybftCfg.SupernetID, rootChainRelayer, childChainRelayer)
require.NoError(t, err)
require.False(t, validatorInfo.IsActive)
require.True(t, validatorInfo.Stake.Cmp(big.NewInt(0)) == 0)
Expand Down Expand Up @@ -575,15 +575,15 @@ func TestE2E_Consensus_CustomRewardToken(t *testing.T) {
rootChainRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithIPAddress(cluster.Bridge.JSONRPCAddr()))
require.NoError(t, err)

polybftConfig, chainID, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
polybftConfig, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName))
require.NoError(t, err)

validatorAcc, err := sidechain.GetAccountFromDir(owner.DataDir())
require.NoError(t, err)

validatorInfo, err := sidechain.GetValidatorInfo(validatorAcc.Ecdsa.Address(),
polybftConfig.Bridge.CustomSupernetManagerAddr, polybftConfig.Bridge.StakeManagerAddr,
chainID, rootChainRelayer, childChainRelayer)
polybftConfig.SupernetID, rootChainRelayer, childChainRelayer)
t.Logf("[Validator#%v] Witdhrawable rewards=%d\n", validatorInfo.Address, validatorInfo.WithdrawableRewards)

require.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions e2e-polybft/framework/test-bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ func (t *TestBridge) registerGenesisValidators(polybftConfig polybft.PolyBFTConf
return g.Wait()
}

func (t *TestBridge) initialStakingOfGenesisValidators(
polybftConfig polybft.PolyBFTConfig, chainID int64) error {
func (t *TestBridge) initialStakingOfGenesisValidators(polybftConfig polybft.PolyBFTConfig) error {
validatorSecrets, err := genesis.GetValidatorKeyFiles(t.clusterConfig.TmpDir, t.clusterConfig.ValidatorPrefix)
if err != nil {
return fmt.Errorf("could not get validator secrets on initial staking of genesis validators: %w", err)
Expand All @@ -392,7 +391,7 @@ func (t *TestBridge) initialStakingOfGenesisValidators(
"--stake-manager", polybftConfig.Bridge.StakeManagerAddr.String(),
"--" + polybftsecrets.AccountDirFlag, path.Join(t.clusterConfig.TmpDir, secret),
"--amount", polybftConfig.InitialValidatorSet[i].Stake.String(),
"--chain-id", strconv.FormatInt(chainID, 10),
"--supernet-id", strconv.FormatInt(polybftConfig.SupernetID, 10),
"--native-root-token", polybftConfig.Bridge.RootNativeERC20Addr.String(),
}

Expand Down
Loading

0 comments on commit ed61412

Please sign in to comment.