From 3396f7edac6ad17a295b59219e08e91e9e67e681 Mon Sep 17 00:00:00 2001 From: Dusan Date: Mon, 25 Dec 2023 15:49:08 +0100 Subject: [PATCH] comment fix --- .../genesis/predeploy/genesis_predeploy.go | 4 +- command/genesis/predeploy/params.go | 35 +++++----------- consensus/polybft/contracts_initializer.go | 8 ++-- e2e-polybft/e2e/bridge_test.go | 40 +++++++++++-------- e2e-polybft/e2e/consensus_test.go | 4 +- e2e-polybft/framework/test-cluster.go | 22 +++++----- e2e-polybft/framework/test-server.go | 7 ++-- 7 files changed, 57 insertions(+), 63 deletions(-) diff --git a/command/genesis/predeploy/genesis_predeploy.go b/command/genesis/predeploy/genesis_predeploy.go index 689309058d..3a9ced2bce 100644 --- a/command/genesis/predeploy/genesis_predeploy.go +++ b/command/genesis/predeploy/genesis_predeploy.go @@ -42,7 +42,7 @@ func setFlags(cmd *cobra.Command) { ¶ms.artifactsName, artifactsNameFlag, "", - "the path to the contract artifacts JSON", + "the built-in contract artifact name", ) cmd.Flags().StringVar( @@ -54,7 +54,7 @@ func setFlags(cmd *cobra.Command) { cmd.Flags().StringArrayVar( ¶ms.constructorArgs, - constructorArgsPathFlag, + constructorArgsFlag, []string{}, "the constructor arguments, if any", ) diff --git a/command/genesis/predeploy/params.go b/command/genesis/predeploy/params.go index ffd561bb25..a5dd0492d0 100644 --- a/command/genesis/predeploy/params.go +++ b/command/genesis/predeploy/params.go @@ -16,12 +16,12 @@ import ( ) const ( - chainFlag = "chain" - predeployAddressFlag = "predeploy-address" - artifactsNameFlag = "artifacts-name" - artifactsPathFlag = "artifacts-path" - constructorArgsPathFlag = "constructor-args" - deployerAddrFlag = "deployer-address" + chainFlag = "chain" + predeployAddressFlag = "predeploy-address" + artifactsNameFlag = "artifacts-name" + artifactsPathFlag = "artifacts-path" + constructorArgsFlag = "constructor-args" + deployerAddrFlag = "deployer-address" ) var ( @@ -55,7 +55,6 @@ type predeployParams struct { func (p *predeployParams) getRequiredFlags() []string { return []string{ predeployAddressFlag, - artifactsNameFlag, } } @@ -64,11 +63,13 @@ func (p *predeployParams) initRawParams() (err error) { return errArtifactPathAndNameMissing } - if err := p.initPredeployAddress(); err != nil { + p.address, err = types.IsValidAddress(p.addressRaw, false) + if err != nil { return err } - if err := p.initDeployerAddress(); err != nil { + p.deployerAddr, err = types.IsValidAddress(p.deployerAddrRaw, false) + if err != nil { return err } @@ -87,22 +88,6 @@ func (p *predeployParams) initRawParams() (err error) { return nil } -func (p *predeployParams) initPredeployAddress() error { - if p.addressRaw == "" { - return errInvalidPredeployAddress - } - - p.address = types.StringToAddress(p.addressRaw) - - return nil -} - -func (p *predeployParams) initDeployerAddress() error { - p.deployerAddr = types.StringToAddress(p.deployerAddrRaw) - - return nil -} - func (p *predeployParams) verifyMinAddress() error { address, err := hex.DecodeHexToBig(p.address.String()) if err != nil { diff --git a/consensus/polybft/contracts_initializer.go b/consensus/polybft/contracts_initializer.go index e64690702b..c02dc7d5b4 100644 --- a/consensus/polybft/contracts_initializer.go +++ b/consensus/polybft/contracts_initializer.go @@ -45,10 +45,10 @@ func initStakeManager(polyBFTConfig PolyBFTConfig, transition *state.Transition) input, err := approveFn.EncodeAbi() if err != nil { - return fmt.Errorf("NativeERC20.approve params encoding failed: %w", err) + return fmt.Errorf("Staking ERC20.approve params encoding failed: %w", err) } - err = callContract(validator.Address, polyBFTConfig.StakeTokenAddr, input, "StakeToken.approve", transition) + err = callContract(validator.Address, polyBFTConfig.StakeTokenAddr, input, "Staking ERC20.approve", transition) if err != nil { return fmt.Errorf("Error while calling contract %w", err) } @@ -381,11 +381,11 @@ func mintStakeToken(polyBFTConfig PolyBFTConfig, transition *state.Transition) e input, err := mintFn.EncodeAbi() if err != nil { - return fmt.Errorf("StakeToken.mint params encoding failed: %w", err) + return fmt.Errorf("Staking ERC20.mint params encoding failed: %w", err) } if err := callContract(polyBFTConfig.BladeAdmin, polyBFTConfig.StakeTokenAddr, - input, "StakeToken.mint", transition); err != nil { + input, "Staking ERC20.mint", transition); err != nil { return err } } diff --git a/e2e-polybft/e2e/bridge_test.go b/e2e-polybft/e2e/bridge_test.go index 7340cc8a5f..332044936e 100644 --- a/e2e-polybft/e2e/bridge_test.go +++ b/e2e-polybft/e2e/bridge_test.go @@ -1443,7 +1443,7 @@ func TestE2E_Bridge_NonMintableERC20Token_WithPremine(t *testing.T) { }) } -func TestE2E_Bridge_NonNative(t *testing.T) { +func TestE2E_Bridge_NonNativeStakingToken(t *testing.T) { const ( transfersCount = 5 amount = 100 @@ -1453,7 +1453,9 @@ func TestE2E_Bridge_NonNative(t *testing.T) { ) var ( - stakeAmount = ethgo.Ether(500) + stakeAmount = ethgo.Ether(500) + addedStakeAmount = ethgo.Gwei(1) + mintAmount = ethgo.Gwei(10) ) minter, err := wallet.GenerateKey() @@ -1469,23 +1471,30 @@ func TestE2E_Bridge_NonNative(t *testing.T) { tcc.StakeAmounts = append(tcc.StakeAmounts, stakeAmount) } }), - framework.WithPredeploy(), + framework.WithPredeploy(fmt.Sprintf("%s:%s", contracts.ERC20Contract, "RootERC20")), ) defer cluster.Stop() cluster.WaitForReady(t) + polybftCfg, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName)) + require.NoError(t, err) + + //first validator server(minter) firstValidator := cluster.Servers[0] + //second validator server + secondValidator := cluster.Servers[1] - validatorAcc, err := validatorHelper.GetAccountFromDir(firstValidator.DataDir()) + //validator account from second validator + validatorAccTwo, err := validatorHelper.GetAccountFromDir(secondValidator.DataDir()) require.NoError(t, err) relayer, err := txrelayer.NewTxRelayer(txrelayer.WithIPAddress(firstValidator.JSONRPCAddr())) require.NoError(t, err) mintFn := &contractsapi.MintRootERC20Fn{ - To: validatorAcc.Address(), - Amount: big.NewInt(100000), + To: validatorAccTwo.Address(), + Amount: mintAmount, } mintInput, err := mintFn.EncodeAbi() @@ -1502,30 +1511,27 @@ func TestE2E_Bridge_NonNative(t *testing.T) { require.NoError(t, err) require.Equal(t, uint64(types.ReceiptSuccess), receipt.Status) - polybftCfg, err := polybft.LoadPolyBFTConfig(path.Join(cluster.Config.TmpDir, chainConfigFileName)) - require.NoError(t, err) - - firstValidatorInfo, err := validatorHelper.GetValidatorInfo(validatorAcc.Ecdsa.Address(), relayer) + secondValidatorInfo, err := validatorHelper.GetValidatorInfo(validatorAccTwo.Ecdsa.Address(), relayer) require.NoError(t, err) - require.True(t, firstValidatorInfo.Stake.Cmp(stakeAmount) == 0) + require.True(t, secondValidatorInfo.Stake.Cmp(stakeAmount) == 0) require.NoError(t, cluster.WaitForBlock(epochSize*1, 1*time.Minute)) - require.NoError(t, firstValidator.Stake(polybftCfg.StakeTokenAddr, big.NewInt(1000))) + require.NoError(t, firstValidator.Stake(polybftCfg.StakeTokenAddr, addedStakeAmount)) require.NoError(t, cluster.WaitForBlock(epochSize*3, 90*time.Second)) updatedStakeAmount := big.NewInt(0) - firstValidatorInfo, err = validatorHelper.GetValidatorInfo(validatorAcc.Ecdsa.Address(), relayer) + secondValidatorInfo, err = validatorHelper.GetValidatorInfo(validatorAccTwo.Ecdsa.Address(), relayer) require.NoError(t, err) - require.True(t, firstValidatorInfo.Stake.Cmp(updatedStakeAmount.Add(stakeAmount, big.NewInt(1000))) == 0) + require.True(t, secondValidatorInfo.Stake.Cmp(updatedStakeAmount.Add(stakeAmount, addedStakeAmount)) == 0) - require.NoError(t, firstValidator.Unstake(big.NewInt(1000))) + require.NoError(t, secondValidator.Unstake(addedStakeAmount)) require.NoError(t, cluster.WaitForBlock(epochSize*4, 30*time.Second)) - firstValidatorInfo, err = validatorHelper.GetValidatorInfo(validatorAcc.Ecdsa.Address(), relayer) + secondValidatorInfo, err = validatorHelper.GetValidatorInfo(validatorAccTwo.Ecdsa.Address(), relayer) require.NoError(t, err) - require.True(t, firstValidatorInfo.Stake.Cmp(stakeAmount) == 0) + require.True(t, secondValidatorInfo.Stake.Cmp(stakeAmount) == 0) } diff --git a/e2e-polybft/e2e/consensus_test.go b/e2e-polybft/e2e/consensus_test.go index 2656c7222d..52abfb5a72 100644 --- a/e2e-polybft/e2e/consensus_test.go +++ b/e2e-polybft/e2e/consensus_test.go @@ -207,8 +207,8 @@ func TestE2E_Consensus_RegisterValidator(t *testing.T) { initialBalance := ethgo.Ether(1000) // mint tokens to new validators - require.NoError(t, owner.MintNativeERC20Token([]string{firstValidatorAddr.String(), secondValidatorAddr.String()}, - []*big.Int{initialBalance, initialBalance})) + require.NoError(t, owner.MintERC20Token([]string{firstValidatorAddr.String(), secondValidatorAddr.String()}, + []*big.Int{initialBalance, initialBalance}, polybftConfig.StakeTokenAddr)) // first validator's balance to be received firstBalance, err := relayer.Client().Eth().GetBalance(firstValidatorAddr, ethgo.Latest) diff --git a/e2e-polybft/framework/test-cluster.go b/e2e-polybft/framework/test-cluster.go index 3183f25e73..9fc8524209 100644 --- a/e2e-polybft/framework/test-cluster.go +++ b/e2e-polybft/framework/test-cluster.go @@ -21,7 +21,6 @@ import ( "github.com/0xPolygon/polygon-edge/command/genesis" "github.com/0xPolygon/polygon-edge/consensus/polybft" "github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi" - "github.com/0xPolygon/polygon-edge/contracts" "github.com/0xPolygon/polygon-edge/helper/common" "github.com/0xPolygon/polygon-edge/txrelayer" "github.com/0xPolygon/polygon-edge/types" @@ -116,7 +115,7 @@ type TestClusterConfig struct { SecretsCallback func([]types.Address, *TestClusterConfig) BladeAdmin string RewardWallet string - PredeployNonNative bool + PredeployContract string ContractDeployerAllowListAdmin []types.Address ContractDeployerAllowListEnabled []types.Address @@ -458,9 +457,9 @@ func WithRewardWallet(rewardWallet string) ClusterOption { } } -func WithPredeploy() ClusterOption { +func WithPredeploy(predeployString string) ClusterOption { return func(h *TestClusterConfig) { - h.PredeployNonNative = true + h.PredeployContract = predeployString } } @@ -707,8 +706,10 @@ func NewTestCluster(t *testing.T, validatorsCount int, opts ...ClusterOption) *T } args = append(args, "--proxy-contracts-admin", proxyAdminAddr) - if config.PredeployNonNative { - args = append(args, "--stake-token", contracts.ERC20Contract.String()) + if config.PredeployContract != "" { + parts := strings.Split(config.PredeployContract, ":") + require.Equal(t, 2, len(parts)) + args = append(args, "--stake-token", parts[0]) } // run genesis command with all the arguments @@ -716,12 +717,15 @@ func NewTestCluster(t *testing.T, validatorsCount int, opts ...ClusterOption) *T require.NoError(t, err) } - if config.PredeployNonNative { + if config.PredeployContract != "" { + + parts := strings.Split(config.PredeployContract, ":") + require.Equal(t, 2, len(parts)) // run predeploy genesis population args := []string{ "genesis", "predeploy", - "--predeploy-address", contracts.ERC20Contract.String(), - "--artifacts-name", "RootERC20", + "--predeploy-address", parts[0], + "--artifacts-name", parts[1], "--chain", genesisPath, "--deployer-address", config.BladeAdmin} diff --git a/e2e-polybft/framework/test-server.go b/e2e-polybft/framework/test-server.go index c7e50826d8..866e4d77da 100644 --- a/e2e-polybft/framework/test-server.go +++ b/e2e-polybft/framework/test-server.go @@ -17,7 +17,6 @@ import ( "github.com/0xPolygon/polygon-edge/consensus/polybft" "github.com/0xPolygon/polygon-edge/consensus/polybft/validator" "github.com/0xPolygon/polygon-edge/consensus/polybft/wallet" - "github.com/0xPolygon/polygon-edge/contracts" "github.com/0xPolygon/polygon-edge/server/proto" txpoolProto "github.com/0xPolygon/polygon-edge/txpool/proto" "github.com/0xPolygon/polygon-edge/types" @@ -289,8 +288,8 @@ func (t *TestServer) WhitelistValidators(addresses []string) error { return runCommand(t.clusterConfig.Binary, args, t.clusterConfig.GetStdout("validator")) } -// MintNativeERC20Token mints given amounts of native erc20 token on blade to given addresses -func (t *TestServer) MintNativeERC20Token(addresses []string, amounts []*big.Int) error { +// MintERC20Token mints given amounts of native erc20 token on blade to given addresses +func (t *TestServer) MintERC20Token(addresses []string, amounts []*big.Int, stakeToken types.Address) error { acc, err := validatorHelper.GetAccountFromDir(t.DataDir()) if err != nil { return err @@ -304,7 +303,7 @@ func (t *TestServer) MintNativeERC20Token(addresses []string, amounts []*big.Int args := []string{ "mint-erc20", "--jsonrpc", t.JSONRPCAddr(), - "--erc20-token", contracts.NativeERC20TokenContract.String(), + "--erc20-token", stakeToken.String(), "--private-key", hex.EncodeToString(rawKey), }