Skip to content

Commit

Permalink
Fix IBFT E2E tests (#1960)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Oct 6, 2023
1 parent 7959cf6 commit 6a724be
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 42 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
---
name: E2E tests
name: IBFT E2E tests
on: # yamllint disable-line rule:truthy
push:
branches:
- main
- develop
workflow_dispatch:
workflow_call:
outputs:
Expand Down
3 changes: 3 additions & 0 deletions contracts/staking/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
type TxQueryHandler interface {
Apply(*types.Transaction) (*runtime.ExecutionResult, error)
GetNonce(types.Address) uint64
SetNonPayable(nonPayable bool)
}

// decodeWeb3ArrayOfBytes is a helper function to parse the data
Expand Down Expand Up @@ -103,6 +104,7 @@ func QueryValidators(t TxQueryHandler, from types.Address) ([]types.Address, err
return nil, ErrMethodNotFoundInABI
}

t.SetNonPayable(true)
res, err := t.Apply(createCallViewTx(
from,
AddrStakingContract,
Expand Down Expand Up @@ -146,6 +148,7 @@ func QueryBLSPublicKeys(t TxQueryHandler, from types.Address) ([][]byte, error)
return nil, ErrMethodNotFoundInABI
}

t.SetNonPayable(true)
res, err := t.Apply(createCallViewTx(
from,
AddrStakingContract,
Expand Down
9 changes: 7 additions & 2 deletions contracts/staking/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func appendAll(bytesArrays ...[]byte) []byte {
}

type TxMock struct {
hashToRes map[types.Hash]*runtime.ExecutionResult
nonce map[types.Address]uint64
hashToRes map[types.Hash]*runtime.ExecutionResult
nonce map[types.Address]uint64
nonPayable bool
}

func (m *TxMock) Apply(tx *types.Transaction) (*runtime.ExecutionResult, error) {
Expand All @@ -66,6 +67,10 @@ func (m *TxMock) GetNonce(addr types.Address) uint64 {
return 0
}

func (m *TxMock) SetNonPayable(nonPayable bool) {
m.nonPayable = nonPayable
}

func Test_decodeValidators(t *testing.T) {
tests := []struct {
name string
Expand Down
5 changes: 3 additions & 2 deletions crypto/txsigner_london_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/umbracle/ethgo"

"github.com/0xPolygon/polygon-edge/types"
)
Expand Down Expand Up @@ -118,8 +119,8 @@ func Test_LondonSigner_Sender(t *testing.T) {
tx: &types.Transaction{
Type: types.DynamicFeeTx,
GasPrice: big.NewInt(1000000402),
GasTipCap: big.NewInt(1000000000),
GasFeeCap: big.NewInt(10000000000),
GasTipCap: ethgo.Gwei(1),
GasFeeCap: ethgo.Gwei(10),
Gas: 21000,
To: &to,
Value: big.NewInt(100000000000000),
Expand Down
4 changes: 2 additions & 2 deletions e2e/framework/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func StakeAmount(
txn := &PreparedTransaction{
From: from,
To: &staking.AddrStakingContract,
GasPrice: big.NewInt(1000000000),
GasPrice: ethgo.Gwei(1),
Gas: 1000000,
Value: amount,
Input: MethodSig("stake"),
Expand Down Expand Up @@ -153,7 +153,7 @@ func UnstakeAmount(
txn := &PreparedTransaction{
From: from,
To: &staking.AddrStakingContract,
GasPrice: big.NewInt(1000000000),
GasPrice: ethgo.Gwei(1),
Gas: DefaultGasLimit,
Value: big.NewInt(0),
Input: MethodSig("unstake"),
Expand Down
6 changes: 3 additions & 3 deletions e2e/framework/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (t *TestServer) GenerateGenesis() error {

// add base fee
if t.Config.BaseFee != 0 {
args = append(args, "--base-fee", *common.EncodeUint64(t.Config.BaseFee))
args = append(args, "--base-fee-config", *common.EncodeUint64(t.Config.BaseFee))
}

// add burn contracts
Expand Down Expand Up @@ -571,8 +571,8 @@ func (t *TestServer) DeployContract(
}

const (
DefaultGasPrice = 1879048192 // 0x70000000
DefaultGasLimit = 5242880 // 0x500000
DefaultGasPrice = 10e9 // 0x2540BE400
DefaultGasLimit = 5242880 // 0x500000
)

type PreparedTransaction struct {
Expand Down
6 changes: 3 additions & 3 deletions e2e/ibft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestIbft_Transfer(t *testing.T) {
txn := &framework.PreparedTransaction{
From: senderAddr,
To: &receiverAddr,
GasPrice: big.NewInt(2000000000),
GasPrice: ethgo.Gwei(2),
Gas: 1000000,
Value: framework.EthToWei(1),
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestIbft_TransactionFeeRecipient(t *testing.T) {
txn := &framework.PreparedTransaction{
From: senderAddr,
To: &receiverAddr,
GasPrice: big.NewInt(1000000000),
GasPrice: ethgo.Gwei(1),
Gas: 1000000,
Value: tc.txAmount,
}
Expand All @@ -150,7 +150,7 @@ func TestIbft_TransactionFeeRecipient(t *testing.T) {
// Deploy contract
deployTx := &framework.PreparedTransaction{
From: senderAddr,
GasPrice: big.NewInt(1000000000), // fees should be greater than base fee
GasPrice: ethgo.Gwei(1), // fees should be greater than base fee
Gas: 1000000,
Value: big.NewInt(0),
Input: framework.MethodSig("setA1"),
Expand Down
2 changes: 1 addition & 1 deletion e2e/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestNewFilter_Block(t *testing.T) {
if _, sendErr := srv.SendRawTx(ctx, &framework.PreparedTransaction{
From: from,
To: &to,
GasPrice: big.NewInt(1000000000),
GasPrice: ethgo.Gwei(1),
Gas: 1000000,
Value: big.NewInt(10000),
}, fromKey); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions e2e/pos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func TestPoS_Unstake(t *testing.T) {
// Check the address balance
fee := new(big.Int).Mul(
big.NewInt(int64(receipt.GasUsed)),
big.NewInt(framework.DefaultGasPrice),
ethgo.Gwei(1),
)

accountBalance := framework.GetAccountBalance(t, unstakerAddr, client)
Expand Down Expand Up @@ -625,7 +625,7 @@ func TestPoS_StakeUnstakeWithinSameBlock(t *testing.T) {

stakingContractAddr := staking.AddrStakingContract
defaultBalance := framework.EthToWei(100)
bigGasPrice := big.NewInt(10000000000)
bigGasPrice := big.NewInt(framework.DefaultGasPrice)

senderKey, senderAddr := tests.GenerateKeyAndAddr(t)
numDummyStakers := 10
Expand Down Expand Up @@ -818,7 +818,7 @@ func TestSnapshotUpdating(t *testing.T) {
&framework.PreparedTransaction{
From: faucetAddr,
To: &firstNonValidatorAddr,
GasPrice: big.NewInt(1000000000),
GasPrice: ethgo.Gwei(1),
Gas: 1000000,
Value: framework.EthToWei(300),
}, faucetKey)
Expand Down
4 changes: 2 additions & 2 deletions e2e/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestEthTransfer(t *testing.T) {
txn := &framework.PreparedTransaction{
From: testCase.sender,
To: &testCase.recipient,
GasPrice: big.NewInt(1000000000),
GasPrice: ethgo.Gwei(1),
Gas: 1000000,
Value: testCase.amount,
}
Expand Down Expand Up @@ -396,7 +396,7 @@ func Test_TransactionIBFTLoop(t *testing.T) {

deployTx := &framework.PreparedTransaction{
From: sender,
GasPrice: big.NewInt(framework.DefaultGasPrice),
GasPrice: ethgo.Gwei(1),
Gas: framework.DefaultGasLimit,
Value: big.NewInt(0),
Input: buf,
Expand Down
28 changes: 14 additions & 14 deletions e2e/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func TestTxPool_RecoverableError(t *testing.T) {
transactions := []*types.Transaction{
{
Nonce: 0,
GasPrice: big.NewInt(1000000000),
GasPrice: big.NewInt(framework.DefaultGasPrice),
Gas: 22000,
To: &receiverAddress,
Value: oneEth,
Expand All @@ -404,7 +404,7 @@ func TestTxPool_RecoverableError(t *testing.T) {
},
{
Nonce: 1,
GasPrice: big.NewInt(1000000000),
GasPrice: big.NewInt(framework.DefaultGasPrice),
Gas: 22000,
To: &receiverAddress,
Value: oneEth,
Expand All @@ -414,7 +414,7 @@ func TestTxPool_RecoverableError(t *testing.T) {
{
Type: types.DynamicFeeTx,
Nonce: 2,
GasFeeCap: big.NewInt(10000000000),
GasFeeCap: big.NewInt(framework.DefaultGasPrice),
GasTipCap: big.NewInt(1000000000),
Gas: 22000,
To: &receiverAddress,
Expand Down Expand Up @@ -445,7 +445,7 @@ func TestTxPool_RecoverableError(t *testing.T) {
},
From: types.ZeroAddress.String(),
})
assert.NoError(t, err, "Unable to send transaction, %v", err)
require.NoError(t, err, "Unable to send transaction, %v", err)

txHash := ethgo.Hash(types.StringToHash(response.TxHash))

Expand All @@ -458,28 +458,28 @@ func TestTxPool_RecoverableError(t *testing.T) {

// wait for the last tx to be included in a block
receipt, err := tests.WaitForReceipt(ctx, client.Eth(), hashes[2])
assert.NoError(t, err)
assert.NotNil(t, receipt)
require.NoError(t, err)
require.NotNil(t, receipt)

// assert balance moved
balance, err := client.Eth().GetBalance(ethgo.Address(receiverAddress), ethgo.Latest)
assert.NoError(t, err, "failed to retrieve receiver account balance")
assert.Equal(t, framework.EthToWei(3).String(), balance.String())
require.NoError(t, err, "failed to retrieve receiver account balance")
require.Equal(t, framework.EthToWei(3).String(), balance.String())

// Query 1st and 2nd txs
firstTx, err := client.Eth().GetTransactionByHash(hashes[0])
assert.NoError(t, err)
assert.NotNil(t, firstTx)
require.NoError(t, err)
require.NotNil(t, firstTx)

secondTx, err := client.Eth().GetTransactionByHash(hashes[1])
assert.NoError(t, err)
assert.NotNil(t, secondTx)
require.NoError(t, err)
require.NotNil(t, secondTx)

// first two are in one block
assert.Equal(t, firstTx.BlockNumber, secondTx.BlockNumber)
require.Equal(t, firstTx.BlockNumber, secondTx.BlockNumber)

// last tx is included in next block
assert.NotEqual(t, secondTx.BlockNumber, receipt.BlockNumber)
require.NotEqual(t, secondTx.BlockNumber, receipt.BlockNumber)
}

func TestTxPool_GetPendingTx(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion e2e/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/0xPolygon/polygon-edge/jsonrpc"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/umbracle/ethgo"
)

type testWSRequest struct {
Expand Down Expand Up @@ -114,7 +115,7 @@ func TestWS_Response(t *testing.T) {
_, err = srv.SendRawTx(ctx, &framework.PreparedTransaction{
From: preminedAccounts[0].address,
To: &preminedAccounts[1].address,
GasPrice: big.NewInt(1000000000),
GasPrice: ethgo.Gwei(1),
Gas: 1000000,
Value: big.NewInt(10000),
}, preminedAccounts[0].key)
Expand Down
16 changes: 8 additions & 8 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1116,15 +1116,15 @@ func checkAndProcessTx(msg *types.Transaction, t *Transition) error {
return NewTransitionApplicationError(err, true)
}

// 2. check dynamic fees of the transaction
if err := t.checkDynamicFees(msg); err != nil {
return NewTransitionApplicationError(err, true)
}

// 3. caller has enough balance to cover transaction
// Skip this check if the given flag is provided.
// It happens for eth_call and for other operations that do not change the state.
if !t.ctx.NonPayable {
// 2. check dynamic fees of the transaction
if err := t.checkDynamicFees(msg); err != nil {
return NewTransitionApplicationError(err, true)
}

// 3. caller has enough balance to cover transaction
// Skip this check if the given flag is provided.
// It happens for eth_call and for other operations that do not change the state.
if err := t.subGasLimitPrice(msg); err != nil {
return NewTransitionApplicationError(err, true)
}
Expand Down

0 comments on commit 6a724be

Please sign in to comment.