From 3fa86a9b2067ebd800ba09f81d4466a9d21c0df8 Mon Sep 17 00:00:00 2001 From: 0g-wh Date: Wed, 13 Nov 2024 16:36:28 +0800 Subject: [PATCH 1/2] estimate gas for erc20/uniswap (#24) * Update tps-single-0g.yml * suspend * fix 1559 check * update 0g version * revert * estimateGas * fix gas --- lib/cmd/run/ethlistener.go | 6 +++--- lib/generator/generator.go | 13 ++++++++++--- lib/generator/generator_erc20.go | 24 +++++++++++++++++++++++- lib/generator/generator_uniswap.go | 27 ++++++++++++++++++++++++++- lib/generator/tx_helper.go | 12 ++++++++++++ 5 files changed, 74 insertions(+), 8 deletions(-) diff --git a/lib/cmd/run/ethlistener.go b/lib/cmd/run/ethlistener.go index edae987..b998b5b 100644 --- a/lib/cmd/run/ethlistener.go +++ b/lib/cmd/run/ethlistener.go @@ -143,10 +143,10 @@ func (el *EthereumListener) handleBlockResponse(response map[string]interface{}) el.bestTPS = tps el.gasUsedAtBestTPS = gasUsedPercent } - fmt.Println("TPS:", tps, "GasUsed%:", gasUsedPercent*100) + fmt.Printf("TPS: %d GasUsed%%: %.2f%%\n", tps, gasUsedPercent*100) if totalTxCount < 100 { // exit if total tx count is less than 100 - fmt.Println("Best TPS:", el.bestTPS, "GasUsed%:", el.gasUsedAtBestTPS*100) + fmt.Printf("Best TPS: %d GasUsed%%: %.2f%%\n", el.bestTPS, el.gasUsedAtBestTPS*100) el.Close() return } @@ -158,7 +158,7 @@ func (el *EthereumListener) handleBlockResponse(response map[string]interface{}) return } } - fmt.Println("Best TPS:", el.bestTPS, "GasUsed%:", el.gasUsedAtBestTPS) + fmt.Printf("Best TPS: %d GasUsed%%: %.2f%%\n", el.bestTPS, el.gasUsedAtBestTPS*100) el.Close() } diff --git a/lib/generator/generator.go b/lib/generator/generator.go index 3d7cde8..e059240 100644 --- a/lib/generator/generator.go +++ b/lib/generator/generator.go @@ -137,6 +137,16 @@ func (g *Generator) prepareSenders() error { return nil } +func (g *Generator) estimateGas(msg ethereum.CallMsg) (uint64, error) { + client, err := ethclient.Dial(g.RpcUrl) + if err != nil { + return 0, err + } + defer client.Close() + + return client.EstimateGas(context.Background(), msg) +} + func (g *Generator) deployContract(gasLimit uint64, contractBin, contractABI string, args ...interface{}) (common.Address, error) { client, err := ethclient.Dial(g.RpcUrl) if err != nil { @@ -170,9 +180,6 @@ func (g *Generator) deployContract(gasLimit uint64, contractBin, contractABI str if g.ShouldPersist { g.Store.AddPrepareTx(tx) - if err != nil { - return common.Address{}, err - } } return ercContractAddress, nil diff --git a/lib/generator/generator_erc20.go b/lib/generator/generator_erc20.go index e032cff..452b4d8 100644 --- a/lib/generator/generator_erc20.go +++ b/lib/generator/generator_erc20.go @@ -1,6 +1,7 @@ package generator import ( + "fmt" "math/big" "sync" @@ -34,6 +35,27 @@ func (g *Generator) GenerateERC20() (map[int]types.Transactions, error) { var mutex sync.Mutex ch := make(chan error) + sender := g.Senders[0] + tx, _ := GenerateContractCallingTx( + sender.PrivateKey, + contractAddressStr, + 0, + g.ChainID, + g.GasPrice, + erc20TransferGasLimit, + erc20.MyTokenABI, + "transfer", + common.HexToAddress(g.Recipients[0]), + big.NewInt(0), + ) + ethCallTx := ConvertLegacyTxToCallMsg(tx, sender.Address) + estimateGas, err := g.estimateGas(ethCallTx) + if err != nil { + return txsMap, err + } + + fmt.Println("Estimated gas:", estimateGas) + for index, sender := range g.Senders { go func(index int, sender *account.Account) { txs := types.Transactions{} @@ -44,7 +66,7 @@ func (g *Generator) GenerateERC20() (map[int]types.Transactions, error) { sender.GetNonce(), g.ChainID, g.GasPrice, - erc20TransferGasLimit, + estimateGas, erc20.MyTokenABI, "transfer", common.HexToAddress(recipient), diff --git a/lib/generator/generator_uniswap.go b/lib/generator/generator_uniswap.go index 22e479f..7e97ff6 100644 --- a/lib/generator/generator_uniswap.go +++ b/lib/generator/generator_uniswap.go @@ -1,6 +1,7 @@ package generator import ( + "fmt" "math/big" "sync" @@ -33,6 +34,30 @@ func (g *Generator) GenerateUniswap() (map[int]types.Transactions, error) { var mutex sync.Mutex ch := make(chan error) + sender := g.Senders[0] + tx, _ := GenerateContractCallingTx( + sender.PrivateKey, + pairContractStr, + 0, + g.ChainID, + g.GasPrice, + uniswapSwapGasLimit, + uniswap.UniswapV2PairABI, + "swap", + big.NewInt(0), + big.NewInt(1000), + sender.Address, + []byte{}, + ) + ethCallTx := ConvertLegacyTxToCallMsg(tx, sender.Address) + estimateGas, err := g.estimateGas(ethCallTx) + estimateGas = (uint64)(1.1 * float64(estimateGas)) + if err != nil { + return txsMap, err + } + + fmt.Println("Estimated gas:", estimateGas) + for index, sender := range g.Senders { go func(index int, sender *account.Account) { txs := types.Transactions{} @@ -52,7 +77,7 @@ func (g *Generator) GenerateUniswap() (map[int]types.Transactions, error) { sender.GetNonce(), g.ChainID, g.GasPrice, - uniswapSwapGasLimit, + estimateGas, uniswap.UniswapV2PairABI, "swap", amount0out, diff --git a/lib/generator/tx_helper.go b/lib/generator/tx_helper.go index 0d620b5..4f82152 100644 --- a/lib/generator/tx_helper.go +++ b/lib/generator/tx_helper.go @@ -6,6 +6,7 @@ import ( "math/big" "strings" + "github.com/ethereum/go-ethereum" abipkg "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -77,6 +78,17 @@ func GenerateContractCreationTx(privateKey *ecdsa.PrivateKey, nonce uint64, chai return signedTx, nil } +func ConvertLegacyTxToCallMsg(tx *types.Transaction, from common.Address) ethereum.CallMsg { + return ethereum.CallMsg{ + From: from, + To: tx.To(), + Gas: tx.Gas(), + GasPrice: tx.GasPrice(), + Value: tx.Value(), + Data: tx.Data(), + } +} + func GenerateContractCallingTx(privateKey *ecdsa.PrivateKey, contractAddress string, nonce uint64, chainID, gasPrice *big.Int, gasLimit uint64, contractABI, method string, args ...interface{}) (*types.Transaction, error) { abi, err := abipkg.JSON(strings.NewReader(contractABI)) if err != nil { From 291da58c1556558aee251a196396ffe0c7d77815 Mon Sep 17 00:00:00 2001 From: 0g-wh Date: Wed, 13 Nov 2024 16:38:57 +0800 Subject: [PATCH 2/2] Update show-tps.py --- show-tps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/show-tps.py b/show-tps.py index 9b2ee35..cdde157 100644 --- a/show-tps.py +++ b/show-tps.py @@ -64,7 +64,7 @@ def getTPS(url): if tps is None or gas_used is None: row.append("") else: - row.append(f"{tps:4}, {gas_used * 100:.2f}%") + row.append(f"{tps:4}, {gas_used:.2f}%") table.add_row(row) markdown_table = "| " + " | ".join(table.field_names) + " |\n"