Skip to content

Commit

Permalink
Implement e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Mar 21, 2023
1 parent 0f530a4 commit 4f2b6f7
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 17 deletions.
92 changes: 92 additions & 0 deletions e2e-polybft/e2e/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/0xPolygon/polygon-edge/command/genesis"
"github.com/0xPolygon/polygon-edge/command/sidechain"
"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/e2e-polybft/framework"
"github.com/0xPolygon/polygon-edge/txrelayer"
Expand Down Expand Up @@ -615,3 +616,94 @@ func TestE2E_Consensus_CorrectnessOfExtraValidatorsShouldNotDependOnDelegate(t *
close(endCh)
<-waitCh
}

func TestE2E_Consensus_MintableERC20NativeToken(t *testing.T) {
const (
validatorCount = 5
epochSize = 5
minterPath = "test-chain-1"
)

validatorsAddrs := make([]types.Address, validatorCount)
initialBalance := ethgo.Gwei(1)

cluster := framework.NewTestCluster(t,
validatorCount,
framework.WithMintableNativeToken(true),
framework.WithEpochSize(epochSize),
framework.WithSecretsCallback(func(addrs []types.Address, config *framework.TestClusterConfig) {
for i, addr := range addrs {
// first one is the owner of the NativeERC20Mintable SC
// and it should have premine set to default 1M tokens
// (it is going to send mint transactions to all the other validators)
if i > 0 {
// premine other validators with as minimum amount as possible just to ensure liveness of consensus protocol
config.PremineValidators = append(config.PremineValidators, fmt.Sprintf("%s:%d", addr, initialBalance))
}
validatorsAddrs[i] = addr
}
}))
defer cluster.Stop()

minterSrv := cluster.Servers[0]
targetJSONRPC := minterSrv.JSONRPC()

minterAcc, err := sidechain.GetAccountFromDir(minterSrv.DataDir())
require.NoError(t, err)

cluster.WaitForReady(t)

// send mint transactions
relayer, err := txrelayer.NewTxRelayer(txrelayer.WithClient(targetJSONRPC))
require.NoError(t, err)

mintFn, exists := contractsapi.NativeERC20Mintable.Abi.Methods["mint"]
require.True(t, exists)

nativeTokenAddr := ethgo.Address(contracts.NativeERC20TokenContract)
mintAmount := ethgo.Ether(10)

// make sure minter account can mint tokens
// (first account, which is minter sends mint transactions to all the other validators)
for _, addr := range validatorsAddrs[1:] {
balance, err := targetJSONRPC.Eth().GetBalance(ethgo.Address(addr), ethgo.Latest)
require.NoError(t, err)
t.Logf("Pre-mint balance: %v=%d\n", addr, balance)

mintInput, err := mintFn.Encode([]interface{}{addr, mintAmount})
require.NoError(t, err)

receipt, err := relayer.SendTransaction(
&ethgo.Transaction{
To: &nativeTokenAddr,
Input: mintInput,
}, minterAcc.Ecdsa)
require.NoError(t, err)
require.Equal(t, uint64(types.ReceiptSuccess), receipt.Status)

balance, err = targetJSONRPC.Eth().GetBalance(ethgo.Address(addr), ethgo.Latest)
require.NoError(t, err)

t.Logf("Post-mint balance: %v=%d\n", addr, balance)
require.Equal(t, new(big.Int).Add(mintAmount, initialBalance), balance)
}

minterBalance, err := targetJSONRPC.Eth().GetBalance(minterAcc.Ecdsa.Address(), ethgo.Latest)
require.NoError(t, err)
require.Equal(t, ethgo.Ether(1e6), minterBalance)

// try sending mint transaction from non minter account and make sure it would fail
nonMinterAcc, err := sidechain.GetAccountFromDir(cluster.Servers[1].DataDir())
require.NoError(t, err)

mintInput, err := mintFn.Encode([]interface{}{validatorsAddrs[2], ethgo.Ether(1)})
require.NoError(t, err)

receipt, err := relayer.SendTransaction(
&ethgo.Transaction{
To: &nativeTokenAddr,
Input: mintInput,
}, nonMinterAcc.Ecdsa)
require.NoError(t, err)
require.Equal(t, uint64(types.ReceiptFailed), receipt.Status)
}
45 changes: 28 additions & 17 deletions e2e-polybft/framework/test-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,24 @@ func resolveBinary() string {
type TestClusterConfig struct {
t *testing.T

Name string
Premine []string // address[:amount]
PremineValidators []string // address:[amount]
HasBridge bool
BootnodeCount int
NonValidatorCount int
WithLogs bool
WithStdout bool
LogsDir string
TmpDir string
BlockGasLimit uint64
ValidatorPrefix string
Binary string
ValidatorSetSize uint64
EpochSize int
EpochReward int
SecretsCallback func([]types.Address, *TestClusterConfig)
Name string
Premine []string // address[:amount]
PremineValidators []string // address:[amount]
MintableNativeToken bool
HasBridge bool
BootnodeCount int
NonValidatorCount int
WithLogs bool
WithStdout bool
LogsDir string
TmpDir string
BlockGasLimit uint64
ValidatorPrefix string
Binary string
ValidatorSetSize uint64
EpochSize int
EpochReward int
SecretsCallback func([]types.Address, *TestClusterConfig)

ContractDeployerAllowListAdmin []types.Address
ContractDeployerAllowListEnabled []types.Address
Expand Down Expand Up @@ -175,6 +176,12 @@ func WithPremine(addresses ...types.Address) ClusterOption {
}
}

func WithMintableNativeToken(mintableToken bool) ClusterOption {
return func(h *TestClusterConfig) {
h.MintableNativeToken = mintableToken
}
}

func WithSecretsCallback(fn func([]types.Address, *TestClusterConfig)) ClusterOption {
return func(h *TestClusterConfig) {
h.SecretsCallback = fn
Expand Down Expand Up @@ -361,6 +368,10 @@ func NewTestCluster(t *testing.T, validatorsCount int, opts ...ClusterOption) *T
}
}

if cluster.Config.MintableNativeToken {
args = append(args, "--mintable-native-token")
}

if cluster.Config.HasBridge {
rootchainIP, err := helper.ReadRootchainIP()
require.NoError(t, err)
Expand Down

0 comments on commit 4f2b6f7

Please sign in to comment.