Skip to content

Commit

Permalink
test: fixing things and adding make entry for the e2e test.
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Nedkov <[email protected]>
  • Loading branch information
Psykepro committed Aug 14, 2023
1 parent 2072038 commit c9c8d4d
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 46 deletions.
20 changes: 20 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,26 @@ benchmark-sequencer-erc20-transfers: stop
touch ./results/out.dat ; \
go test -bench=. -timeout=600m | tee ./results/out.dat ;


.PHONY: benchmark-sequencer-uniswap-transfers
benchmark-sequencer-uniswap-transfers: stop
$(RUNL1NETWORK)
$(RUNSTATEDB)
$(RUNPOOLDB)
$(RUNEVENTDB)
sleep 5
$(RUNZKPROVER)
$(RUNSYNC)
sleep 2
$(RUNL2GASPRICER)
$(RUNJSONRPC)
docker ps -a
docker logs $(DOCKERCOMPOSEZKPROVER)
@ cd benchmarks/sequencer/e2e/uniswap-transfers ; \
mkdir -p results ; \
touch ./results/out.dat ; \
go test -bench=. -timeout=600m | tee ./results/out.dat ;

.PHONY: run-db
run-db: ## Runs the node database
$(RUNSTATEDB)
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks/sequencer/common/params/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ const (
// PrometheusPort is the port where prometheus is running
PrometheusPort = 9092
// NumberOfOperations is the number of transactions to send
NumberOfOperations = 10
NumberOfOperations = 300
)
23 changes: 17 additions & 6 deletions test/benchmarks/sequencer/common/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ import (
"github.com/0xPolygonHermez/zkevm-node/test/operations"
"github.com/0xPolygonHermez/zkevm-node/test/scripts/uniswap/pkg"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)

// SendAndWait sends a number of transactions and waits for them to be marked as pending in the pool
func SendAndWait(auth *bind.TransactOpts, client *ethclient.Client, getTxsByStatus func(ctx context.Context, status pool.TxStatus, limit uint64) ([]pool.Transaction, error), nTxs int, erc20SC *ERC20.ERC20, uniswapDeployments *pkg.Deployments, txSenderFunc func(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *pkg.Deployments) error) error {
func SendAndWait(
auth *bind.TransactOpts,
client *ethclient.Client,
getTxsByStatus func(ctx context.Context, status pool.TxStatus, limit uint64) ([]pool.Transaction, error),
nTxs int,
erc20SC *ERC20.ERC20,
uniswapDeployments *pkg.Deployments,
txSenderFunc func(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *pkg.Deployments) ([]*types.Transaction, error),
) ([]*types.Transaction, error) {
auth.GasLimit = 2100000
log.Debugf("Sending %d txs ...", nTxs)
startingNonce := uint64(0)
Expand All @@ -29,18 +38,20 @@ func SendAndWait(auth *bind.TransactOpts, client *ethclient.Client, getTxsByStat
maxNonce := uint64(nTxs) + startingNonce
IP := getPublicIP()

allTxs := make([]*types.Transaction, 0, nTxs)
for nonce := startingNonce; nonce < maxNonce; nonce++ {
err := txSenderFunc(client, auth.GasPrice, nonce, auth, erc20SC, uniswapDeployments)
txs, err := txSenderFunc(client, auth.GasPrice, nonce, auth, erc20SC, uniswapDeployments)
if err != nil {
for err != nil && err.Error() == "nonce intrinsic error" {
log.Warnf("nonce intrinsic error, retrying with nonce %d", nonce)
err = txSenderFunc(client, auth.GasPrice, nonce, auth, erc20SC, uniswapDeployments)
txs, err = txSenderFunc(client, auth.GasPrice, nonce, auth, erc20SC, uniswapDeployments)
}
if err == nil {
continue
}
return err
return nil, err
}
allTxs = append(allTxs, txs...)
}
log.Debug("All txs were sent!")
log.Debug("Waiting pending transactions To be added in the pool ...")
Expand All @@ -62,12 +73,12 @@ func SendAndWait(auth *bind.TransactOpts, client *ethclient.Client, getTxsByStat
return done, nil
})
if err != nil {
return err
return nil, err
}

log.Debug("All pending txs are added in the pool!")

return nil
return allTxs, nil
}

func getPublicIP() string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func BenchmarkSequencerERC20TransfersPoolProcess(b *testing.B) {
}
initialCount, err := pl.CountTransactionsByStatus(params.Ctx, pool.TxStatusSelected)
require.NoError(b, err)
err = transactions.SendAndWait(auth, client, pl.GetTxsByStatus, params.NumberOfOperations, erc20SC, nil, TxSender)
_, err = transactions.SendAndWait(auth, client, pl.GetTxsByStatus, params.NumberOfOperations, erc20SC, nil, TxSender)
require.NoError(b, err)

var (
Expand Down
8 changes: 5 additions & 3 deletions test/benchmarks/sequencer/e2e/erc20-transfers/tx_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package erc20_transfers
import (
"math/big"

"github.com/ethereum/go-ethereum/core/types"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/params"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/ERC20"
Expand All @@ -23,7 +25,7 @@ var (
)

// TxSender sends ERC20 transfer to the sequencer
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *uniswap.Deployments) error {
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *uniswap.Deployments) ([]*types.Transaction, error) {
log.Debugf("sending tx num: %d nonce: %d", countTxs, nonce)
auth.Nonce = new(big.Int).SetUint64(nonce)
var actualTransferAmount *big.Int
Expand All @@ -32,10 +34,10 @@ func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth
} else {
actualTransferAmount = big.NewInt(0).Add(transferAmountBig, auth.Nonce)
}
_, err := erc20SC.Transfer(auth, params.To, actualTransferAmount)
tx, err := erc20SC.Transfer(auth, params.To, actualTransferAmount)
if err == nil {
countTxs += 1
}

return err
return []*types.Transaction{tx}, err
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func BenchmarkSequencerEthTransfersPoolProcess(b *testing.B) {
require.NoError(b, err)
timeForSetup := time.Since(start)
setup.BootstrapSequencer(b, opsman)
err = transactions.SendAndWait(auth, client, pl.GetTxsByStatus, params.NumberOfOperations, nil, nil, TxSender)
_, err = transactions.SendAndWait(auth, client, pl.GetTxsByStatus, params.NumberOfOperations, nil, nil, TxSender)
require.NoError(b, err)

var (
Expand Down
17 changes: 12 additions & 5 deletions test/benchmarks/sequencer/e2e/eth-transfers/tx_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ var (
)

// TxSender sends eth transfer to the sequencer
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *uniswap.Deployments) error {
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *uniswap.Deployments) ([]*types.Transaction, error) {
log.Debugf("sending tx num: %d nonce: %d", countTxs, nonce)
auth.Nonce = big.NewInt(int64(nonce))
tx := types.NewTransaction(nonce, params.To, ethAmount, uint64(gasLimit), gasPrice, nil)
auth.Nonce = new(big.Int).SetUint64(nonce)
tx := types.NewTx(&types.LegacyTx{
GasPrice: gasPrice,
Nonce: nonce,
Gas: uint64(gasLimit),
To: &params.To,
Value: ethAmount,
Data: nil,
})
signedTx, err := auth.Signer(auth.From, tx)
if err != nil {
return err
return nil, err
}

err = l2Client.SendTransaction(params.Ctx, signedTx)
Expand All @@ -44,5 +51,5 @@ func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth
countTxs += 1
}

return err
return []*types.Transaction{signedTx}, err
}
16 changes: 11 additions & 5 deletions test/benchmarks/sequencer/e2e/uniswap-transfers/tx_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package uniswap_transfers

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

"github.com/ethereum/go-ethereum/core/types"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/ERC20"
Expand All @@ -21,21 +25,23 @@ var (
)

// TxSender sends eth transfer to the sequencer
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *uniswap.Deployments) error {
log.Debugf("swap number: %d", countTxs, nonce)
func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *uniswap.Deployments) ([]*types.Transaction, error) {
msg := fmt.Sprintf("# swap cycle number: %d #", countTxs)
delimiter := strings.Repeat("#", len(msg))
log.Infof("%s\n%s\n%s", delimiter, msg, delimiter)
var err error

uniswap.SwapTokens(l2Client, auth, *uniswapDeployments)
transactions := uniswap.SwapTokens(l2Client, auth, *uniswapDeployments)
if errors.Is(err, state.ErrStateNotSynchronized) || errors.Is(err, state.ErrInsufficientFunds) {
for errors.Is(err, state.ErrStateNotSynchronized) || errors.Is(err, state.ErrInsufficientFunds) {
time.Sleep(sleepTime)
uniswap.SwapTokens(l2Client, auth, *uniswapDeployments)
transactions = uniswap.SwapTokens(l2Client, auth, *uniswapDeployments)
}
}

if err == nil {
countTxs += 1
}

return err
return transactions, err
}
54 changes: 41 additions & 13 deletions test/benchmarks/sequencer/scripts/common/results/print.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
package results

import (
"fmt"
"time"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/params"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)

// Print prints the results of the benchmark
func Print(elapsed time.Duration) {
func Print(client *ethclient.Client, elapsed time.Duration, txs []*types.Transaction) {
// calculate the total gas used
var totalGas uint64
for _, tx := range txs {
// Fetch the transaction receipt
receipt, err := client.TransactionReceipt(params.Ctx, tx.Hash())
if err != nil {
log.Error("Unable to fetch transaction receipt", "error", err)
continue
}

totalGas += receipt.GasUsed
}

// calculate the average gas used per transaction
var avgGas uint64
if len(txs) > 0 {
avgGas = totalGas / uint64(len(txs))
}

// calculate the gas per second
gasPerSecond := float64(len(txs)*int(avgGas)) / elapsed.Seconds()

// Print results
log.Info("##########")
log.Info("# Result #")
log.Info("##########")
log.Infof("Total time took for the sequencer to select all txs from the pool: %v", elapsed)
log.Infof("Number of txs sent: %d", params.NumberOfOperations)
log.Infof("Txs per second: %f", float64(params.NumberOfOperations)/elapsed.Seconds())
fmt.Println("##########")
fmt.Println("# Result #")
fmt.Println("##########")
fmt.Printf("Total time took for the sequencer to select all txs from the pool: %v\n", elapsed)
fmt.Printf("Number of operations sent: %d\n", params.NumberOfOperations)
fmt.Printf("Txs per second: %f\n", float64(params.NumberOfOperations)/elapsed.Seconds())
fmt.Printf("Average gas used per transaction: %d\n", avgGas)
fmt.Printf("Total Gas: %d\n", totalGas)
fmt.Printf("Gas per second: %f\n", gasPerSecond)
}

func PrintUniswapDeployments(deployments time.Duration, count uint64) {
log.Info("#######################")
log.Info("# Uniswap Deployments #")
log.Info("#######################")
log.Infof("Total time took for the sequencer to deploy all contracts: %v", deployments)
log.Infof("Number of txs sent: %d", count)
log.Infof("Txs per second: %f", float64(count)/deployments.Seconds())
fmt.Println("#######################")
fmt.Println("# Uniswap Deployments #")
fmt.Println("#######################")
fmt.Printf("Total time took for the sequencer to deploy all contracts: %v\n", deployments)
fmt.Printf("Number of txs sent: %d\n", count)
fmt.Printf("Txs per second: %f\n", float64(count)/deployments.Seconds())
}
4 changes: 2 additions & 2 deletions test/benchmarks/sequencer/scripts/erc20-transfers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
panic(err)
}
// Send Txs
err = transactions.SendAndWait(
allTxs, err := transactions.SendAndWait(
auth,
l2Client,
pl.GetTxsByStatus,
Expand All @@ -53,5 +53,5 @@ func main() {
panic(err)
}
elapsed := lastL2BlockTimestamp.Sub(start)
results.Print(elapsed)
results.Print(l2Client, elapsed, allTxs)
}
6 changes: 4 additions & 2 deletions test/benchmarks/sequencer/scripts/eth-transfers/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"time"

"github.com/0xPolygonHermez/zkevm-node/pool"
Expand All @@ -23,7 +24,7 @@ func main() {

start := time.Now()
// Send Txs
err = transactions.SendAndWait(
allTxs, err := transactions.SendAndWait(
auth,
l2Client,
pl.GetTxsByStatus,
Expand All @@ -33,6 +34,7 @@ func main() {
ethtransfers.TxSender,
)
if err != nil {
fmt.Println(auth.Nonce)
panic(err)
}

Expand All @@ -47,5 +49,5 @@ func main() {
panic(err)
}
elapsed := lastL2BlockTimestamp.Sub(start)
results.Print(elapsed)
results.Print(l2Client, elapsed, allTxs)
}
4 changes: 2 additions & 2 deletions test/benchmarks/sequencer/scripts/uniswap-transfers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
elapsedTimeForDeployments := time.Since(start)

// Send Txs
err = transactions.SendAndWait(
allTxs, err := transactions.SendAndWait(
auth,
l2Client,
pl.GetTxsByStatus,
Expand All @@ -55,7 +55,7 @@ func main() {
}
elapsed := lastL2BlockTimestamp.Sub(start)
results.PrintUniswapDeployments(elapsedTimeForDeployments, deploymentTxsCount)
results.Print(elapsed)
results.Print(l2Client, elapsed, allTxs)

totalTxsCount := uniswap.GetExecutedTransactionsCount()
log.Info("##############################")
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/uniswap/pkg/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func DeployContractsAndAddLiquidity(client *ethclient.Client, auth *bind.Transac
log.Debugf("Mint C Coin tx: %v", tx.Hash().Hex())
fmt.Println()
// wrapping eth
wethDepositoAmount := "10000000000000000"
wethDepositoAmount := "0000000000000000"
log.Debugf("Depositing %v ETH for account %v on token wEth", wethDepositoAmount, auth.From)
auth.Value, _ = big.NewInt(0).SetString(wethDepositoAmount, encoding.Base10)
tx, err = wethSC.Deposit(auth)
Expand Down
Loading

0 comments on commit c9c8d4d

Please sign in to comment.