Skip to content

Commit

Permalink
some fixes and e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
dusannosovic-ethernal committed Dec 23, 2023
1 parent 6d6e78d commit 18cf6de
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 68 deletions.
2 changes: 2 additions & 0 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type Genesis struct {
Number uint64 `json:"number"`
GasUsed uint64 `json:"gasUsed"`
ParentHash types.Hash `json:"parentHash"`

StakeTokenAddr types.Address `json:"stakeTokenAddr"`
}

// GenesisHeader converts the initially defined genesis struct to a header
Expand Down
9 changes: 9 additions & 0 deletions command/genesis/predeploy/genesis_predeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ func setFlags(cmd *cobra.Command) {
"the path to the contract artifacts JSON",
)

cmd.Flags().StringVar(
&params.artifactsPath,
artifactsPathFlag,
"",
"the path to the contract artifacts JSON",
)

cmd.Flags().StringArrayVar(
&params.constructorArgs,
constructorArgsPath,
[]string{},
"the constructor arguments, if any",
)

cmd.MarkFlagsMutuallyExclusive(artifactsNameFlag, artifactsPathFlag)
}

func runPreRun(_ *cobra.Command, _ []string) error {
Expand Down
3 changes: 3 additions & 0 deletions command/genesis/predeploy/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
chainFlag = "chain"
predeployAddressFlag = "predeploy-address"
artifactsNameFlag = "artifacts-name"
artifactsPathFlag = "artifacts-path"
constructorArgsPath = "constructor-args"
)

Expand All @@ -39,6 +40,7 @@ type predeployParams struct {

address types.Address
artifactsName string
artifactsPath string
constructorArgs []string

genesisConfig *chain.Chain
Expand Down Expand Up @@ -122,6 +124,7 @@ func (p *predeployParams) updateGenesisConfig() error {
}

predeployAccount, err := predeployment.GenerateGenesisAccountFromFile(
p.artifactsPath,
p.contractArtifact,
p.constructorArgs,
p.address,
Expand Down
108 changes: 55 additions & 53 deletions consensus/polybft/contractsapi/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,58 +72,7 @@ var (
TestSimple *artifact.Artifact
TestRewardToken *artifact.Artifact

contractArtifacts = map[string]*artifact.Artifact{
"CheckpointManager": CheckpointManager,
"ExitHelper": ExitHelper,
"StateSender": StateSender,
"RootERC20Predicate": RootERC20Predicate,
"RootERC721Predicate": RootERC721Predicate,
"RootERC1155Predicate": RootERC1155Predicate,
"ChildMintableERC20Predicate": ChildMintableERC20Predicate,
"ChildMintableERC721Predicate": ChildMintableERC721Predicate,
"ChildMintableERC1155Predicate": ChildMintableERC1155Predicate,
"BLS": BLS,
"BLS256": BLS256,
"System": System,
"Merkle": Merkle,
"ChildValidatorSet": ChildValidatorSet,
"NativeERC20": NativeERC20,
"NativeERC20Mintable": NativeERC20Mintable,
"StateReceiver": StateReceiver,
"ChildERC20": ChildERC20,
"ChildERC20Predicate": ChildERC20Predicate,
"ChildERC20PredicateACL": ChildERC20PredicateACL,
"RootMintableERC20Predicate": RootMintableERC20Predicate,
"RootMintableERC20PredicateACL": RootMintableERC20PredicateACL,
"ChildERC721": ChildERC721,
"ChildERC721Predicate": ChildERC721Predicate,
"ChildERC721PredicateACL": ChildERC721PredicateACL,
"RootMintableERC721Predicate": RootMintableERC721Predicate,
"RootMintableERC721PredicateACL": RootMintableERC721PredicateACL,
"ChildERC1155": ChildERC1155,
"ChildERC1155Predicate": ChildERC1155Predicate,
"ChildERC1155PredicateACL": ChildERC1155PredicateACL,
"RootMintableERC1155Predicate": RootMintableERC1155Predicate,
"RootMintableERC1155PredicateACL": RootMintableERC1155PredicateACL,
"L2StateSender": L2StateSender,
"CustomSupernetManager": CustomSupernetManager,
"StakeManager": StakeManager,
"EpochManager": EpochManager,
"RootERC721": RootERC721,
"RootERC1155": RootERC1155,
"EIP1559Burn": EIP1559Burn,
"BladeManager": BladeManager,
"GenesisProxy": GenesisProxy,
"TransparentUpgradeableProxy": TransparentUpgradeableProxy,
"NetworkParams": NetworkParams,
"ForkParams": ForkParams,
"ChildGovernor": ChildGovernor,
"ChildTimelock": ChildTimelock,
"TestWriteBlockMetadata": TestWriteBlockMetadata,
"RootERC20": RootERC20,
"TestSimple": TestSimple,
"TestRewardToken": TestRewardToken,
}
contractArtifacts map[string]*artifact.Artifact
)

func init() {
Expand Down Expand Up @@ -368,6 +317,59 @@ func init() {
if err != nil {
log.Fatal(err)
}

contractArtifacts = map[string]*artifact.Artifact{
"CheckpointManager": CheckpointManager,
"ExitHelper": ExitHelper,
"StateSender": StateSender,
"RootERC20Predicate": RootERC20Predicate,
"RootERC721Predicate": RootERC721Predicate,
"RootERC1155Predicate": RootERC1155Predicate,
"ChildMintableERC20Predicate": ChildMintableERC20Predicate,
"ChildMintableERC721Predicate": ChildMintableERC721Predicate,
"ChildMintableERC1155Predicate": ChildMintableERC1155Predicate,
"BLS": BLS,
"BLS256": BLS256,
"System": System,
"Merkle": Merkle,
"ChildValidatorSet": ChildValidatorSet,
"NativeERC20": NativeERC20,
"NativeERC20Mintable": NativeERC20Mintable,
"StateReceiver": StateReceiver,
"ChildERC20": ChildERC20,
"ChildERC20Predicate": ChildERC20Predicate,
"ChildERC20PredicateACL": ChildERC20PredicateACL,
"RootMintableERC20Predicate": RootMintableERC20Predicate,
"RootMintableERC20PredicateACL": RootMintableERC20PredicateACL,
"ChildERC721": ChildERC721,
"ChildERC721Predicate": ChildERC721Predicate,
"ChildERC721PredicateACL": ChildERC721PredicateACL,
"RootMintableERC721Predicate": RootMintableERC721Predicate,
"RootMintableERC721PredicateACL": RootMintableERC721PredicateACL,
"ChildERC1155": ChildERC1155,
"ChildERC1155Predicate": ChildERC1155Predicate,
"ChildERC1155PredicateACL": ChildERC1155PredicateACL,
"RootMintableERC1155Predicate": RootMintableERC1155Predicate,
"RootMintableERC1155PredicateACL": RootMintableERC1155PredicateACL,
"L2StateSender": L2StateSender,
"CustomSupernetManager": CustomSupernetManager,
"StakeManager": StakeManager,
"EpochManager": EpochManager,
"RootERC721": RootERC721,
"RootERC1155": RootERC1155,
"EIP1559Burn": EIP1559Burn,
"BladeManager": BladeManager,
"GenesisProxy": GenesisProxy,
"TransparentUpgradeableProxy": TransparentUpgradeableProxy,
"NetworkParams": NetworkParams,
"ForkParams": ForkParams,
"ChildGovernor": ChildGovernor,
"ChildTimelock": ChildTimelock,
"TestWriteBlockMetadata": TestWriteBlockMetadata,
"RootERC20": RootERC20,
"TestSimple": TestSimple,
"TestRewardToken": TestRewardToken,
}
}

func readTestContractContent(contractFileName string) []byte {
Expand All @@ -384,5 +386,5 @@ func GetArtifactFromArtifactName(artifactName string) (*artifact.Artifact, error
return contractArtifact, nil
}

return nil, fmt.Errorf("")
return nil, fmt.Errorf("Can't find artifact")
}
44 changes: 41 additions & 3 deletions e2e-polybft/e2e/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,11 @@ func TestE2E_Bridge_NonNative(t *testing.T) {
stateSyncedLogsCount = 2
)

var (
premineBalance = ethgo.Ether(2e6) // 2M native tokens (so that we have enough balance to fund new validator)
stakeAmount = ethgo.Ether(500)
)

receiverKeys := make([]string, transfersCount)
receivers := make([]string, transfersCount)
receiversAddrs := make([]types.Address, transfersCount)
Expand Down Expand Up @@ -1480,8 +1485,9 @@ func TestE2E_Bridge_NonNative(t *testing.T) {
framework.WithPremine(receiversAddrs...),
framework.WithBridge(),
framework.WithSecretsCallback(func(addrs []types.Address, tcc *framework.TestClusterConfig) {
for i := 0; i < len(addrs); i++ {
tcc.StakeAmounts = append(tcc.StakeAmounts, ethgo.Ether(10))
for _, addr := range addrs {
tcc.Premine = append(tcc.Premine, fmt.Sprintf("%s:%s", addr, premineBalance))
tcc.StakeAmounts = append(tcc.StakeAmounts, stakeAmount)
}
}),
framework.WithPredeploy(),
Expand All @@ -1490,7 +1496,39 @@ func TestE2E_Bridge_NonNative(t *testing.T) {

cluster.WaitForReady(t)

firstValidator := cluster.Servers[0]

validatorAcc, err := validatorHelper.GetAccountFromDir(firstValidator.DataDir())
require.NoError(t, err)

relayer, err := txrelayer.NewTxRelayer(txrelayer.WithIPAddress(firstValidator.JSONRPCAddr()))
require.NoError(t, err)

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


firstValidatorInfo, err := validatorHelper.GetValidatorInfo(validatorAcc.Ecdsa.Address(), relayer)
require.NoError(t, err)
require.True(t, firstValidatorInfo.Stake.Cmp(stakeAmount) == 0)

cluster.WaitForBlock(epochSize*1, 1*time.Minute)

require.NoError(t, firstValidator.Stake(polybftCfg, big.NewInt(1000), types.ZeroAddress))

cluster.WaitForBlock(epochSize*3, 90*time.Second)

updatedStakeAmount := big.NewInt(0)

firstValidatorInfo, err = validatorHelper.GetValidatorInfo(validatorAcc.Ecdsa.Address(), relayer)
require.NoError(t, err)
require.True(t, firstValidatorInfo.Stake.Cmp(updatedStakeAmount.Add(stakeAmount, big.NewInt(1000))) == 0)

require.NoError(t, firstValidator.Unstake(big.NewInt(1000)))

cluster.WaitForBlock(epochSize*4, 30*time.Second)

firstValidatorInfo, err = validatorHelper.GetValidatorInfo(validatorAcc.Ecdsa.Address(), relayer)
require.NoError(t, err)
require.True(t, firstValidatorInfo.Stake.Cmp(stakeAmount) == 0)

}
2 changes: 1 addition & 1 deletion e2e-polybft/e2e/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ func TestE2E_Consensus_ChangeVotingPowerByStakingPendingRewards(t *testing.T) {
require.NoError(t, validatorSrv.WithdrawRewards())

// stake withdrawable rewards (since rewards are in native erc20 token in this test)
require.NoError(t, validatorSrv.Stake(polybftCfg, validator.WithdrawableRewards))
require.NoError(t, validatorSrv.Stake(polybftCfg, validator.WithdrawableRewards, types.ZeroAddress))
})

queryValidators(func(idx int, validator *polybft.ValidatorInfo) {
Expand Down
24 changes: 19 additions & 5 deletions e2e-polybft/framework/test-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import (
"testing"
"time"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/genesis"
cmdHelper "github.com/0xPolygon/polygon-edge/command/helper"
"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"
Expand Down Expand Up @@ -115,7 +118,7 @@ type TestClusterConfig struct {
SecretsCallback func([]types.Address, *TestClusterConfig)
BladeAdmin string
RewardWallet string
Predeploy bool
PredeployNonNative bool

ContractDeployerAllowListAdmin []types.Address
ContractDeployerAllowListEnabled []types.Address
Expand Down Expand Up @@ -459,7 +462,7 @@ func WithRewardWallet(rewardWallet string) ClusterOption {

func WithPredeploy() ClusterOption {
return func(h *TestClusterConfig) {
h.Predeploy = true
h.PredeployNonNative = true
}
}

Expand Down Expand Up @@ -711,15 +714,26 @@ func NewTestCluster(t *testing.T, validatorsCount int, opts ...ClusterOption) *T
require.NoError(t, err)
}

if config.Predeploy {
if config.PredeployNonNative {
// run predeploy genesis population
args := []string{
"predeploy",
"--predeploy-address", "0x1046",
"genesis", "predeploy",
"--predeploy-address", contracts.ERC20Contract.String(),
"--artifacts-name", "RootERC20",
"--chain", genesisPath,
}
err = cluster.cmdRun(args...)
require.NoError(t, err)

chainConfig, err := chain.ImportFromFile(genesisPath)
if err != nil {
require.NoError(t, err)
}
chainConfig.Genesis.StakeTokenAddr = contracts.ERC20Contract

if err := cmdHelper.WriteGenesisConfigToDisk(chainConfig, genesisPath); err != nil {
require.NoError(t, err)
}
}

if cluster.Config.HasBridge {
Expand Down
6 changes: 5 additions & 1 deletion e2e-polybft/framework/test-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (t *TestServer) RootchainFundFor(accounts []types.Address, amounts []*big.I
}

// Stake stakes given amount to validator account encapsulated by given server instance
func (t *TestServer) Stake(polybftConfig polybft.PolyBFTConfig, amount *big.Int) error {
func (t *TestServer) Stake(polybftConfig polybft.PolyBFTConfig, amount *big.Int, stakeTokenAddr types.Address) error {
args := []string{
"validator",
"stake",
Expand All @@ -241,6 +241,10 @@ func (t *TestServer) Stake(polybftConfig polybft.PolyBFTConfig, amount *big.Int)
"--amount", amount.String(),
}

if stakeTokenAddr != types.ZeroAddress {
args = append(args, "--stake-token", types.AddressToString(stakeTokenAddr))
}

return runCommand(t.clusterConfig.Binary, args, t.clusterConfig.GetStdout("stake"))
}

Expand Down
33 changes: 28 additions & 5 deletions helper/predeployment/predeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,36 @@ func getPredeployAccount(address types.Address, input []byte, chainID int64) (*c
// GenerateGenesisAccountFromFile generates an account that is going to be directly
// inserted into state
func GenerateGenesisAccountFromFile(
contractArtifact *artifact.Artifact,
filepath string,
conArtifact *artifact.Artifact,
constructorArgs []string,
predeployAddress types.Address,
chainID int64,
) (*chain.GenesisAccount, error) {
finalBytecode := contractArtifact.Bytecode
constructorInfo := contractArtifact.Abi.Constructor
var finalBytecode []byte
var constructorInfo *abi.Method
var contractABI *abi.ABI

if filepath == "" {
finalBytecode = conArtifact.Bytecode
constructorInfo = conArtifact.Abi.Constructor
contractABI = conArtifact.Abi
} else {
// Create the artifact from JSON
artifact, err := loadContractArtifact(filepath)
if err != nil {
return nil, err
}

// Generate the contract ABI object
contractABI, err = abi.NewABI(string(artifact.ABI))
if err != nil {
return nil, fmt.Errorf("unable to create contract ABI, %w", err)
}

finalBytecode = artifact.Bytecode
constructorInfo = contractABI.Constructor
}

if constructorInfo != nil {
// Constructor arguments are passed in as an array of values.
Expand All @@ -192,13 +215,13 @@ func GenerateGenesisAccountFromFile(
// Encode the constructor params
constructor, err := abi.Encode(
parsedArguments,
contractArtifact.Abi.Constructor.Inputs,
contractABI.Constructor.Inputs,
)
if err != nil {
return nil, fmt.Errorf("unable to encode constructor arguments, %w", err)
}

finalBytecode = append(contractArtifact.Bytecode, constructor...)
finalBytecode = append(conArtifact.Bytecode, constructor...)
}

return getPredeployAccount(predeployAddress, finalBytecode, chainID)
Expand Down

0 comments on commit 18cf6de

Please sign in to comment.