Skip to content

Commit

Permalink
Fix consensus e2e tests (#28)
Browse files Browse the repository at this point in the history
* Fix e2e tests

* Fix migration e2e test (#30)

* Remove StakeManagerAddr, StakeTokenAddr and CustomSupernetManagerAddr from bridge config

* Fix integration test (#31)

* Fix fund command UT

* Small fixes

---------

Co-authored-by: Stefan Negovanović <[email protected]>
  • Loading branch information
goran-ethernal and Stefan-Ethernal authored Nov 29, 2023
1 parent c6ae2d7 commit c702e13
Show file tree
Hide file tree
Showing 32 changed files with 240 additions and 500 deletions.
37 changes: 2 additions & 35 deletions command/bridge/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ const (
rootERC1155PredicateName = "RootERC1155Predicate"
childERC1155MintablePredicateName = "ChildERC1155MintablePredicate"
erc1155TemplateName = "ERC1155Template"
customSupernetManagerName = "CustomSupernetManager"
stakeManagerName = "StakeManager"
)

var (
Expand Down Expand Up @@ -100,9 +98,6 @@ var (
erc1155TemplateName: func(rootchainConfig *polybft.RootchainConfig, addr types.Address) {
rootchainConfig.ChildERC1155Address = addr
},
getProxyNameForImpl(customSupernetManagerName): func(rootchainConfig *polybft.RootchainConfig, addr types.Address) {
rootchainConfig.CustomSupernetManagerAddress = addr
},
}

// initializersMap maps rootchain contract names to initializer function callbacks
Expand Down Expand Up @@ -251,20 +246,6 @@ func GetCommand() *cobra.Command {
" (otherwise provided secrets are used to resolve deployer account)",
)

cmd.Flags().StringVar(
&params.stakeTokenAddr,
helper.StakeTokenFlag,
"",
helper.StakeTokenFlagDesc,
)

cmd.Flags().StringVar(
&params.stakeManagerAddr,
helper.StakeManagerFlag,
"",
helper.StakeManagerFlagDesc,
)

cmd.Flags().StringVar(
&params.proxyContractsAdmin,
helper.ProxyContractsAdminFlag,
Expand All @@ -273,8 +254,6 @@ func GetCommand() *cobra.Command {
)

cmd.MarkFlagsMutuallyExclusive(helper.TestModeFlag, deployerKeyFlag)
_ = cmd.MarkFlagRequired(helper.StakeManagerFlag)
_ = cmd.MarkFlagRequired(helper.StakeTokenFlag)

return cmd
}
Expand Down Expand Up @@ -341,15 +320,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

// populate bridge configuration
bridgeConfig := deploymentResultInfo.RootchainCfg.ToBridgeConfig()
if consensusCfg.Bridge != nil {
// only true if stake-manager-deploy command was executed
// users can still deploy stake manager manually
// only used for e2e tests
bridgeConfig.StakeTokenAddr = consensusCfg.Bridge.StakeTokenAddr
}

consensusCfg.Bridge = bridgeConfig
consensusCfg.Bridge = deploymentResultInfo.RootchainCfg.ToBridgeConfig()

consensusCfg.Bridge.EventTrackerStartBlocks = map[types.Address]uint64{
deploymentResultInfo.RootchainCfg.StateSenderAddress: blockNum,
Expand Down Expand Up @@ -402,11 +373,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
byteCodeBuilder func() ([]byte, error)
}

rootchainConfig := &polybft.RootchainConfig{
JSONRPCAddr: params.jsonRPCAddress,
// update stake manager address in genesis in case if stake manager was deployed manually
StakeManagerAddress: types.StringToAddress(params.stakeManagerAddr),
}
rootchainConfig := &polybft.RootchainConfig{JSONRPCAddr: params.jsonRPCAddress}

allContracts := []*contractInfo{
{
Expand Down
21 changes: 0 additions & 21 deletions command/bridge/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import (
"testing"

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

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/bridge/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/types"
)
Expand All @@ -38,26 +35,8 @@ func TestDeployContracts_NoPanics(t *testing.T) {
require.NoError(t, err)
require.Equal(t, uint64(types.ReceiptSuccess), receipt.Status)

txn := &ethgo.Transaction{
To: nil, // contract deployment
Input: contractsapi.StakeManager.Bytecode,
}

receipt, err = server.SendTxn(txn)
require.NoError(t, err)
require.Equal(t, uint64(types.ReceiptSuccess), receipt.Status)

outputter := command.InitializeOutputter(GetCommand())
params.stakeManagerAddr = receipt.ContractAddress.String()
params.stakeTokenAddr = types.StringToAddress("0x123456789").String()
params.proxyContractsAdmin = "0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed"
consensusCfg = polybft.PolyBFTConfig{
NativeTokenConfig: &polybft.TokenConfig{
Name: "Test",
Symbol: "TST",
Decimals: 18,
},
}

require.NotPanics(t, func() {
_, err = deployContracts(outputter, client, 1, []*validator.GenesisValidator{}, context.Background())
Expand Down
11 changes: 0 additions & 11 deletions command/bridge/deploy/params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package deploy

import (
"errors"
"fmt"
"os"

Expand All @@ -18,8 +17,6 @@ type deployParams struct {
genesisPath string
deployerKey string
jsonRPCAddress string
stakeTokenAddr string
stakeManagerAddr string
proxyContractsAdmin string
isTestMode bool
}
Expand All @@ -36,13 +33,5 @@ func (ip *deployParams) validateFlags() error {
return err
}

if consensusCfg.NativeTokenConfig == nil {
return errors.New("native token configuration is undefined")
}

if params.stakeTokenAddr == "" {
return errors.New("stake token address is not provided")
}

return helper.ValidateProxyContractsAdmin(ip.proxyContractsAdmin)
}
39 changes: 0 additions & 39 deletions command/bridge/fund/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ func setFlags(cmd *cobra.Command) {
"the rootchain JSON RPC endpoint",
)

cmd.Flags().StringVar(
&params.stakeTokenAddr,
helper.StakeTokenFlag,
"",
helper.StakeTokenFlagDesc,
)

cmd.Flags().BoolVar(
&params.mintStakeToken,
mintStakeTokenFlag,
false,
"indicates if stake token deployer should mint root tokens to given validators",
)

cmd.Flags().StringVar(
&params.deployerPrivateKey,
polybftsecrets.PrivateKeyFlag,
Expand Down Expand Up @@ -98,12 +84,6 @@ func runCommand(cmd *cobra.Command, _ []string) {
return
}

var stakeTokenAddr types.Address

if params.mintStakeToken {
stakeTokenAddr = types.StringToAddress(params.stakeTokenAddr)
}

results := make([]command.CommandResult, len(params.addresses))
g, ctx := errgroup.WithContext(cmd.Context())

Expand Down Expand Up @@ -139,28 +119,9 @@ func runCommand(cmd *cobra.Command, _ []string) {
return fmt.Errorf("failed to fund validator '%s'", validatorAddr)
}

if params.mintStakeToken {
// mint tokens to validator, so he is able to send them
mintTxn, err := helper.CreateMintTxn(validatorAddr, stakeTokenAddr, params.amountValues[i], true)
if err != nil {
return fmt.Errorf("failed to create mint native tokens transaction for validator '%s'. err: %w",
validatorAddr, err)
}

receipt, err := txRelayer.SendTransaction(mintTxn, deployerKey)
if err != nil {
return fmt.Errorf("failed to send mint native tokens transaction to validator '%s'. err: %w", validatorAddr, err)
}

if receipt.Status == uint64(types.ReceiptFailed) {
return fmt.Errorf("failed to mint native tokens to validator '%s'", validatorAddr)
}
}

results[i] = &result{
ValidatorAddr: validatorAddr,
TxHash: types.Hash(receipt.TransactionHash),
IsMinted: params.mintStakeToken,
}
}

Expand Down
13 changes: 0 additions & 13 deletions command/bridge/fund/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fund

import (
"errors"
"fmt"
"math/big"

bridgeHelper "github.com/0xPolygon/polygon-edge/command/bridge/helper"
Expand All @@ -23,9 +22,7 @@ var (
type fundParams struct {
addresses []string
amounts []string
stakeTokenAddr string
deployerPrivateKey string
mintStakeToken bool
jsonRPCAddress string

amountValues []*big.Int
Expand Down Expand Up @@ -56,15 +53,5 @@ func (fp *fundParams) validateFlags() error {
fp.amountValues[i] = amountValue
}

if fp.mintStakeToken {
if fp.stakeTokenAddr == "" {
return bridgeHelper.ErrMandatoryStakeToken
}

if err := types.IsValidAddress(fp.stakeTokenAddr); err != nil {
return fmt.Errorf("invalid stake token address is provided: %w", err)
}
}

return nil
}
28 changes: 0 additions & 28 deletions command/bridge/fund/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/stretchr/testify/require"

bridgeHelper "github.com/0xPolygon/polygon-edge/command/bridge/helper"
"github.com/0xPolygon/polygon-edge/types"
)

Expand Down Expand Up @@ -44,33 +43,6 @@ func Test_validateFlags(t *testing.T) {
},
err: "address \x10 has invalid length",
},
{
// stake token address omitted
buildParamsFn: func() *fundParams {
return &fundParams{
mintStakeToken: true,
addresses: []string{
types.StringToAddress("0x10").String(),
types.StringToAddress("0x20").String()},
amounts: []string{"10", "20"},
}
},
err: bridgeHelper.ErrMandatoryStakeToken.Error(),
},
{
// stake token address omitted
buildParamsFn: func() *fundParams {
return &fundParams{
mintStakeToken: true,
stakeTokenAddr: "0xA",
addresses: []string{
types.StringToAddress("0x10").String(),
types.StringToAddress("0x20").String()},
amounts: []string{"10", "20"},
}
},
err: "invalid stake token address is provided",
},
{
// valid scenario
buildParamsFn: func() *fundParams {
Expand Down
4 changes: 1 addition & 3 deletions command/bridge/fund/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ import (
type result struct {
ValidatorAddr types.Address `json:"address"`
TxHash types.Hash `json:"tx_hash"`
IsMinted bool `json:"mint"`
}

func (r *result) GetOutput() string {
var buffer bytes.Buffer

vals := make([]string, 0, 3)
vals := make([]string, 0, 2)
vals = append(vals, fmt.Sprintf("Validator (address)|%s", r.ValidatorAddr))
vals = append(vals, fmt.Sprintf("Transaction (hash)|%s", r.TxHash))
vals = append(vals, fmt.Sprintf("Is minted|%v", r.IsMinted))

buffer.WriteString("\n[ROOTCHAIN FUND]\n")
buffer.WriteString(helper.FormatKV(vals))
Expand Down
17 changes: 5 additions & 12 deletions command/bridge/helper/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,22 @@ import (
const (
TestAccountPrivKey = "aa75e9a7d427efc732f8e4f1a5b7646adcc61fd5bae40f80d13c8419c9f43d6d"
TestModeFlag = "test"
SupernetManagerFlag = "supernet-manager"
SupernetManagerFlagDesc = "address of supernet manager contract"
StakeManagerFlag = "stake-manager"
StakeManagerFlagDesc = "address of stake manager contract"
NativeRootTokenFlag = "native-root-token"
NativeRootTokenFlagDesc = "address of native root token"
GenesisPathFlag = "genesis"
GenesisPathFlagDesc = "genesis file path, which contains chain configuration"
DefaultGenesisPath = "./genesis.json"
StakeTokenFlag = "stake-token"
StakeTokenFlagDesc = "address of ERC20 token used for staking on rootchain"
ProxyContractsAdminFlag = "proxy-contracts-admin"
ProxyContractsAdminDesc = "admin for proxy contracts"
StakeTokenFlag = "stake-token"
StakeTokenFlagDesc = "address of ERC20 token used for staking"
AddressesFlag = "addresses"
AmountsFlag = "amounts"
Erc20TokenFlag = "erc20-token" //nolint:gosec
)

var (
ErrRootchainNotFound = errors.New("rootchain not found")
ErrRootchainPortBind = errors.New("port 8545 is not bind with localhost")
ErrMandatoryStakeToken = errors.New("stake token address is mandatory")
errTestModeSecrets = errors.New("rootchain test mode does not imply specifying secrets parameters")
ErrRootchainNotFound = errors.New("rootchain not found")
ErrRootchainPortBind = errors.New("port 8545 is not bind with localhost")
errTestModeSecrets = errors.New("rootchain test mode does not imply specifying secrets parameters")

ErrNoAddressesProvided = errors.New("no addresses provided")
ErrInconsistentLength = errors.New("addresses and amounts must be equal length")
Expand Down
13 changes: 8 additions & 5 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,14 @@ func (p *genesisParams) initGenesisConfig() error {
chainConfig := &chain.Chain{
Name: p.name,
Genesis: &chain.Genesis{
GasLimit: p.blockGasLimit,
Difficulty: 1,
Alloc: map[types.Address]*chain.GenesisAccount{},
ExtraData: p.extraData,
GasUsed: command.DefaultGenesisGasUsed,
GasLimit: p.blockGasLimit,
Difficulty: 1,
Alloc: map[types.Address]*chain.GenesisAccount{},
ExtraData: p.extraData,
GasUsed: command.DefaultGenesisGasUsed,
BaseFee: p.parsedBaseFeeConfig.baseFee,
BaseFeeEM: p.parsedBaseFeeConfig.baseFeeEM,
BaseFeeChangeDenom: p.parsedBaseFeeConfig.baseFeeChangeDenom,
},
Params: &chain.Params{
ChainID: int64(p.chainID),
Expand Down
8 changes: 7 additions & 1 deletion command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,17 @@ func (p *genesisParams) getValidatorAccounts() ([]*validator.GenesisValidator, e
}

addr := types.StringToAddress(trimmedAddress)

stake, exists := p.stakeInfos[addr]
if !exists {
stake = command.DefaultStake
}

validators[i] = &validator.GenesisValidator{
MultiAddr: parts[0],
Address: addr,
BlsKey: trimmedBLSKey,
Stake: p.stakeInfos[addr],
Stake: stake,
}
}

Expand Down
2 changes: 1 addition & 1 deletion command/genesis/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func ReadValidatorsByPrefix(dir, prefix string,

stake, exists := stakeInfos[types.Address(account.Ecdsa.Address())]
if !exists {
stake = big.NewInt(0)
stake = command.DefaultStake
}

validators[i] = &validator.GenesisValidator{
Expand Down
Loading

0 comments on commit c702e13

Please sign in to comment.