From 726fbea72df3f9ebcee777b0174cf49a72dfe4b9 Mon Sep 17 00:00:00 2001 From: stana-ethernal Date: Tue, 16 May 2023 12:40:17 +0200 Subject: [PATCH] cr fix - file reorganization --- benchmark/README.md | 17 ------- benchmark/benchmark_test.go | 2 +- benchmark/{common => }/contract_codes.go | 10 ++--- benchmark/{common => }/executors.go | 2 +- benchmark/{common => }/helper.go | 35 ++++++++------- benchmark/root_child_send_tx.go | 57 ++++++++++++++---------- command/benchmark/benchmark.go | 8 +--- 7 files changed, 59 insertions(+), 72 deletions(-) delete mode 100644 benchmark/README.md rename benchmark/{common => }/contract_codes.go (95%) rename benchmark/{common => }/executors.go (98%) rename benchmark/{common => }/helper.go (69%) diff --git a/benchmark/README.md b/benchmark/README.md deleted file mode 100644 index 5f678336e9..0000000000 --- a/benchmark/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# Benchmark tests -The benchmark folder contains benchmark tests for the smart contracts. - -## Common directory -In the common directory, you'll find: -- the contract_code.go file that contains byte codes for the contracts used in the tests, -- helpers.go that has helper functions needed in tests like deploying contracts and similar, -- executors.go that holds executors which execute the test cases. For example, there is an executor that submits multiple transactions in parallel and measures the execution time. - -## Tests -All test scenarios are executed in benchmark_test.go. Decoupling test scenarios from execution enables the usage of different scenarios in the benchmark command, which is written for the purpose of executing the benchmark test on a test environment. The command can be run with in a following way: -polygon-edge benchmark-test --childJSONRPC="http://127.0.0.1:12001" --rootJSONRPC="http://127.0.0.1:8545" --privateKey="aa75e9a7d427efc732f8e4f1a5b7646adcc61fd5bae40f80d13c8419c9f43d6d" - -## Testing tx send on root and child chains -The RootChildSendTx function executes test cases that measure transaction execution on both the root and child chains. To do this, it first calls RootChildSendTxSetUp to set up the testing environment, which may include starting the cluster, deploying contracts, and building the test cases. After building the test cases, RootChildSendTx returns them along with a cleanup function that should be called after the test cases have been executed. - -The test cases are executed by the TxTestCasesExecutor. The RootJSONRPC, ChildJSONRPC, and PrivateKey flags are used to configure the testing environment. If all of these flags are set, then the local cluster will not be started and the provided addresses will be used as the endpoints to the root and child chains. If any of these flags is not set, the local cluster will be started automatically. If the private key is specified, it will be used as the transaction sender. Otherwise, the local cluster will generate a sender key. If the cluster is not run locally, then the sender must have enough funds for sending transactions. \ No newline at end of file diff --git a/benchmark/benchmark_test.go b/benchmark/benchmark_test.go index 8e94b3ee64..725bafafac 100644 --- a/benchmark/benchmark_test.go +++ b/benchmark/benchmark_test.go @@ -6,5 +6,5 @@ import ( func Benchmark_RunTests(b *testing.B) { // benchmark tests - RootChildSendTx(b) + rootChildSendTx(b) } diff --git a/benchmark/common/contract_codes.go b/benchmark/contract_codes.go similarity index 95% rename from benchmark/common/contract_codes.go rename to benchmark/contract_codes.go index 6cac4cfaf3..8a567032a6 100644 --- a/benchmark/common/contract_codes.go +++ b/benchmark/contract_codes.go @@ -1,5 +1,5 @@ //nolint:lll -package common +package benchmark // pragma solidity ^0.5.16; @@ -22,7 +22,7 @@ package common // return result; // } // } -const SingleContByteCode = `608060405234801561001057600080fd5b50610210806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806320965255146100465780635b9af12b146100a55780637a85644b146100d3575b600080fd5b61004e61011f565b6040518080602001828103825283818151815260200191508051906020019060200280838360005b83811015610091578082015181840152602081019050610076565b505050509050019250505060405180910390f35b6100d1600480360360208110156100bb57600080fd5b8101908080359060200190929190505050610177565b005b610109600480360360408110156100e957600080fd5b8101908080359060200190929190803590602001909291905050506101a6565b6040518082815260200191505060405180910390f35b6060600080548060200260200160405190810160405280929190818152602001828054801561016d57602002820191906000526020600020905b815481526020019060010190808311610159575b5050505050905090565b600081908060018154018082558091505090600182039060005260206000200160009091929091909150555050565b600080828401905060008090505b600a8110156101d05760028202915080806001019150506101b4565b50809150509291505056fea265627a7a72315820ec23cf989c20e0d41d7819001da6dfe6cc129988f15cd8a7b79595a2e61a93d264736f6c63430005100032` +const singleContByteCode = `608060405234801561001057600080fd5b50610210806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806320965255146100465780635b9af12b146100a55780637a85644b146100d3575b600080fd5b61004e61011f565b6040518080602001828103825283818151815260200191508051906020019060200280838360005b83811015610091578082015181840152602081019050610076565b505050509050019250505060405180910390f35b6100d1600480360360208110156100bb57600080fd5b8101908080359060200190929190505050610177565b005b610109600480360360408110156100e957600080fd5b8101908080359060200190929190803590602001909291905050506101a6565b6040518082815260200191505060405180910390f35b6060600080548060200260200160405190810160405280929190818152602001828054801561016d57602002820191906000526020600020905b815481526020019060010190808311610159575b5050505050905090565b600081908060018154018082558091505090600182039060005260206000200160009091929091909150555050565b600080828401905060008090505b600a8110156101d05760028202915080806001019150506101b4565b50809150509291505056fea265627a7a72315820ec23cf989c20e0d41d7819001da6dfe6cc129988f15cd8a7b79595a2e61a93d264736f6c63430005100032` //MULTI CONTRACTS CALL: A->B->C @@ -42,7 +42,7 @@ const SingleContByteCode = `608060405234801561001057600080fd5b506102108061002060 // return valB; // } // } -const MultiContAByteCode = `608060405234801561001057600080fd5b506101c5806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063286d2e3a1461003b57806368685ad31461007f575b600080fd5b61007d6004803603602081101561005157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061009d565b005b6100876100e0565b6040518082815260200191505060405180910390f35b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b6000806000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636cde00cd6040518163ffffffff1660e01b8152600401602060405180830381600087803b15801561014c57600080fd5b505af1158015610160573d6000803e3d6000fd5b505050506040513d602081101561017657600080fd5b81019080805190602001909291905050509050809150509056fea265627a7a7231582082d7a079b4ea6bcf371ef0665da89a56bd53bdc82ae90daa9dd21b61fc6c115864736f6c63430005100032` +const multiContAByteCode = `608060405234801561001057600080fd5b506101c5806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063286d2e3a1461003b57806368685ad31461007f575b600080fd5b61007d6004803603602081101561005157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061009d565b005b6100876100e0565b6040518082815260200191505060405180910390f35b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b6000806000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636cde00cd6040518163ffffffff1660e01b8152600401602060405180830381600087803b15801561014c57600080fd5b505af1158015610160573d6000803e3d6000fd5b505050506040513d602081101561017657600080fd5b81019080805190602001909291905050509050809150509056fea265627a7a7231582082d7a079b4ea6bcf371ef0665da89a56bd53bdc82ae90daa9dd21b61fc6c115864736f6c63430005100032` // pragma solidity ^0.5.16; // interface IContractC { @@ -63,7 +63,7 @@ const MultiContAByteCode = `608060405234801561001057600080fd5b506101c58061002060 // } // } -const MultiContBByteCode = `608060405234801561001057600080fd5b50610205806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063286d2e3a146100465780636cde00cd1461008a578063735b7e6f146100a8575b600080fd5b6100886004803603602081101561005c57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506100c6565b005b61009261010a565b6040518082815260200191505060405180910390f35b6100b06101ca565b6040518082815260200191505060405180910390f35b80600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b600080600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166349ec07186040518163ffffffff1660e01b8152600401602060405180830381600087803b15801561017757600080fd5b505af115801561018b573d6000803e3d6000fd5b505050506040513d60208110156101a157600080fd5b810190808051906020019092919050505090508060008082825401925050819055508091505090565b6000548156fea265627a7a7231582082a5dbbf184a5c59907837a73f0ea2083719218b0bd60ef31a3ef2b209aad00764736f6c63430005100032` +const multiContBByteCode = `608060405234801561001057600080fd5b50610205806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063286d2e3a146100465780636cde00cd1461008a578063735b7e6f146100a8575b600080fd5b6100886004803603602081101561005c57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506100c6565b005b61009261010a565b6040518082815260200191505060405180910390f35b6100b06101ca565b6040518082815260200191505060405180910390f35b80600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b600080600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166349ec07186040518163ffffffff1660e01b8152600401602060405180830381600087803b15801561017757600080fd5b505af115801561018b573d6000803e3d6000fd5b505050506040513d60208110156101a157600080fd5b810190808051906020019092919050505090508060008082825401925050819055508091505090565b6000548156fea265627a7a7231582082a5dbbf184a5c59907837a73f0ea2083719218b0bd60ef31a3ef2b209aad00764736f6c63430005100032` // pragma solidity ^0.5.16; // contract ContractC { @@ -78,4 +78,4 @@ const MultiContBByteCode = `608060405234801561001057600080fd5b506102058061002060 // return uint256(keccak256(abi.encode(block.timestamp, block.difficulty))) % 100; // } // } -const MultiContCByteCode = `608060405234801561001057600080fd5b50610143806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80631990ceb9146100465780633b3cf4e31461006457806349ec071814610082575b600080fd5b61004e6100a0565b6040518082815260200191505060405180910390f35b61006c6100e3565b6040518082815260200191505060405180910390f35b61008a6100e9565b6040518082815260200191505060405180910390f35b60006064424460405160200180838152602001828152602001925050506040516020818303038152906040528051906020012060001c816100dd57fe5b06905090565b60005481565b6000806100f46100a0565b90506000808154809291906001019190505550809150509056fea265627a7a72315820834484e13fa60ebe10a9d7102df12bafa8db4d9cdad5a38d2af6d360adc7ff4064736f6c63430005100032` +const multiContCByteCode = `608060405234801561001057600080fd5b50610143806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80631990ceb9146100465780633b3cf4e31461006457806349ec071814610082575b600080fd5b61004e6100a0565b6040518082815260200191505060405180910390f35b61006c6100e3565b6040518082815260200191505060405180910390f35b61008a6100e9565b6040518082815260200191505060405180910390f35b60006064424460405160200180838152602001828152602001925050506040516020818303038152906040528051906020012060001c816100dd57fe5b06905090565b60005481565b6000806100f46100a0565b90506000808154809291906001019190505550809150509056fea265627a7a72315820834484e13fa60ebe10a9d7102df12bafa8db4d9cdad5a38d2af6d360adc7ff4064736f6c63430005100032` diff --git a/benchmark/common/executors.go b/benchmark/executors.go similarity index 98% rename from benchmark/common/executors.go rename to benchmark/executors.go index c3dd1ce4c8..0fd55888c0 100644 --- a/benchmark/common/executors.go +++ b/benchmark/executors.go @@ -1,4 +1,4 @@ -package common +package benchmark import ( "sync" diff --git a/benchmark/common/helper.go b/benchmark/helper.go similarity index 69% rename from benchmark/common/helper.go rename to benchmark/helper.go index 1e9c5073ed..8fb8948895 100644 --- a/benchmark/common/helper.go +++ b/benchmark/helper.go @@ -1,4 +1,4 @@ -package common +package benchmark import ( "encoding/hex" @@ -12,8 +12,8 @@ import ( "github.com/umbracle/ethgo/wallet" ) -// DeployContractOnRootAndChild deploys contract code on both root and child chain -func DeployContractOnRootAndChild( +// deployContractOnRootAndChild deploys contract code on both root and child chain +func deployContractOnRootAndChild( b *testing.B, childTxRelayer txrelayer.TxRelayer, rootTxRelayer txrelayer.TxRelayer, @@ -26,16 +26,16 @@ func DeployContractOnRootAndChild( require.NoError(b, err) // deploy contract on the child chain - contractChildAddr := DeployContract(b, childTxRelayer, sender, byteCode) + contractChildAddr := deployContract(b, childTxRelayer, sender, byteCode) // deploy contract on the root chain - contractRootAddr := DeployContract(b, rootTxRelayer, sender, byteCode) + contractRootAddr := deployContract(b, rootTxRelayer, sender, byteCode) return contractChildAddr, contractRootAddr } -// DeployContract deploys contract code for the given relayer -func DeployContract(b *testing.B, txRelayer txrelayer.TxRelayer, sender ethgo.Key, byteCode []byte) ethgo.Address { +// deployContract deploys contract code for the given relayer +func deployContract(b *testing.B, txRelayer txrelayer.TxRelayer, sender ethgo.Key, byteCode []byte) ethgo.Address { b.Helper() txn := ðgo.Transaction{ @@ -51,13 +51,14 @@ func DeployContract(b *testing.B, txRelayer txrelayer.TxRelayer, sender ethgo.Ke return receipt.ContractAddress } -// GetTxInput returns input for sending tx, given the abi encoded method and call parameters -func GetTxInput(b *testing.B, method *abi.Method, args interface{}) []byte { +// getTxInput returns input for sending tx, given the abi encoded method and call parameters +func getTxInput(b *testing.B, method *abi.Method, args interface{}) []byte { b.Helper() - var input []byte - - var err error + var ( + input []byte + err error + ) if args != nil { input, err = method.Encode(args) @@ -70,12 +71,12 @@ func GetTxInput(b *testing.B, method *abi.Method, args interface{}) []byte { return input } -// SetContractDependencyAddress calls setContract function on caller contract, to set address of the callee contract -func SetContractDependencyAddress(b *testing.B, txRelayer txrelayer.TxRelayer, callerContractAddr ethgo.Address, +// setContractDependencyAddress calls setContract function on caller contract, to set address of the callee contract +func setContractDependencyAddress(b *testing.B, txRelayer txrelayer.TxRelayer, callerContractAddr ethgo.Address, calleeContractAddr ethgo.Address, setContractAbiMethod *abi.Method, sender ethgo.Key) { b.Helper() - input := GetTxInput(b, setContractAbiMethod, []interface{}{calleeContractAddr}) + input := getTxInput(b, setContractAbiMethod, []interface{}{calleeContractAddr}) receipt, err := txRelayer.SendTransaction( ðgo.Transaction{ To: &callerContractAddr, @@ -85,8 +86,8 @@ func SetContractDependencyAddress(b *testing.B, txRelayer txrelayer.TxRelayer, c require.Equal(b, uint64(types.ReceiptSuccess), receipt.Status) } -// GetPrivateKey initializes a private key from provided raw private key -func GetPrivateKey(b *testing.B, privateKeyRaw string) ethgo.Key { +// getPrivateKey initializes a private key from provided raw private key +func getPrivateKey(b *testing.B, privateKeyRaw string) ethgo.Key { b.Helper() dec, err := hex.DecodeString(privateKeyRaw) diff --git a/benchmark/root_child_send_tx.go b/benchmark/root_child_send_tx.go index bb829096df..3e8fca7a21 100644 --- a/benchmark/root_child_send_tx.go +++ b/benchmark/root_child_send_tx.go @@ -5,7 +5,6 @@ import ( "math/big" "testing" - "github.com/0xPolygon/polygon-edge/benchmark/common" "github.com/0xPolygon/polygon-edge/e2e-polybft/framework" "github.com/0xPolygon/polygon-edge/txrelayer" "github.com/0xPolygon/polygon-edge/types" @@ -26,9 +25,19 @@ var ( PrivateKey = flag.String("privateKey", "", "private key that will be used to send tx") ) -// RootChildSendTx deploys single and multi contracts to the root and child chain, execute tx from the -// test cases and measure its execution -func RootChildSendTx(b *testing.B) { +// The rootChildSendTx function executes test cases that measure transaction execution on both the root and child chains +// To do this, it first calls RootChildSendTxSetUp to set up the testing environment, +// which may include starting the cluster, deploying contracts, and building the test cases. +// After building the test cases, rootChildSendTx returns them along with a cleanup function that should be called +// after the test cases have been executed. The test cases are executed by the TxTestCasesExecutor. +// The rootJSONRPC, childJSONRPC, and privateKey flags are used to configure the testing environment. +// If all of these flags are set, then the local cluster will not be started and the provided addresses +// will be used as the endpoints to the root and child chains. +// If any of these flags is not set, the local cluster will be started automatically. +// If the private key is specified, it will be used as the transaction sender. +// Otherwise, the local cluster will generate a sender key. +// If the cluster is not run locally, then the sender must have enough funds for sending transactions. +func rootChildSendTx(b *testing.B) { b.Helper() // set up environment, get test cases and clean up fn testCases, cleanUpFn := RootChildSendTxSetUp(b) @@ -36,13 +45,13 @@ func RootChildSendTx(b *testing.B) { // Loop over the test cases and measure the execution time of the transactions for _, testInput := range testCases { - common.TxTestCasesExecutor(b, testInput) + TxTestCasesExecutor(b, testInput) } } // RootChildSendTxSetUp sets environment for execution of sentTx test cases on both root and child chains and // returns test cases and clean up fn -func RootChildSendTxSetUp(b *testing.B) ([]common.TxTestCase, func()) { +func RootChildSendTxSetUp(b *testing.B) ([]TxTestCase, func()) { b.Helper() // check if test is called with the root and child node addresses and private key set. // if that is the case use that json rpc addresses, otherwise run the cluster @@ -54,7 +63,7 @@ func RootChildSendTxSetUp(b *testing.B) ([]common.TxTestCase, func()) { var sender ethgo.Key // if the privateKey flag is set then recover the key, otherwise recover the key if privateKeyRaw != "" { - sender = common.GetPrivateKey(b, privateKeyRaw) + sender = getPrivateKey(b, privateKeyRaw) } else { var err error sender, err = wallet.GenerateKey() @@ -95,37 +104,37 @@ func RootChildSendTxSetUp(b *testing.B) ([]common.TxTestCase, func()) { } // deploy contracts - singleContChildAddr, singleContRootAddr := common.DeployContractOnRootAndChild(b, childTxRelayer, rootTxRelayer, - sender, common.SingleContByteCode) - multiAContChildAddr, multiAContRootAddr := common.DeployContractOnRootAndChild(b, childTxRelayer, rootTxRelayer, - sender, common.MultiContAByteCode) - multiBContChildAddr, multiBContRootAddr := common.DeployContractOnRootAndChild(b, childTxRelayer, rootTxRelayer, - sender, common.MultiContBByteCode) - multiCContChildAddr, multiCContRootAddr := common.DeployContractOnRootAndChild(b, childTxRelayer, rootTxRelayer, - sender, common.MultiContCByteCode) + singleContChildAddr, singleContRootAddr := deployContractOnRootAndChild(b, childTxRelayer, rootTxRelayer, + sender, singleContByteCode) + multiAContChildAddr, multiAContRootAddr := deployContractOnRootAndChild(b, childTxRelayer, rootTxRelayer, + sender, multiContAByteCode) + multiBContChildAddr, multiBContRootAddr := deployContractOnRootAndChild(b, childTxRelayer, rootTxRelayer, + sender, multiContBByteCode) + multiCContChildAddr, multiCContRootAddr := deployContractOnRootAndChild(b, childTxRelayer, rootTxRelayer, + sender, multiContCByteCode) // set callee contract addresses for multi call contracts (A->B->C) // set B contract address in A contract - common.SetContractDependencyAddress(b, childTxRelayer, multiAContChildAddr, multiBContChildAddr, + setContractDependencyAddress(b, childTxRelayer, multiAContChildAddr, multiBContChildAddr, multiContSetAddrFunc, sender) - common.SetContractDependencyAddress(b, rootTxRelayer, multiAContRootAddr, multiBContRootAddr, + setContractDependencyAddress(b, rootTxRelayer, multiAContRootAddr, multiBContRootAddr, multiContSetAddrFunc, sender) // set C contract address in B contract - common.SetContractDependencyAddress(b, childTxRelayer, multiBContChildAddr, multiCContChildAddr, + setContractDependencyAddress(b, childTxRelayer, multiBContChildAddr, multiCContChildAddr, multiContSetAddrFunc, sender) - common.SetContractDependencyAddress(b, rootTxRelayer, multiBContRootAddr, multiCContRootAddr, + setContractDependencyAddress(b, rootTxRelayer, multiBContRootAddr, multiCContRootAddr, multiContSetAddrFunc, sender) // create inputs for contract calls singleContInputs := map[string][]byte{ - "calc": common.GetTxInput(b, singleContCalcFunc, []interface{}{big.NewInt(50), big.NewInt(150)}), - "set": common.GetTxInput(b, singleContSetFunc, []interface{}{big.NewInt(10)}), - "get": common.GetTxInput(b, singleContGetFunc, nil), + "calc": getTxInput(b, singleContCalcFunc, []interface{}{big.NewInt(50), big.NewInt(150)}), + "set": getTxInput(b, singleContSetFunc, []interface{}{big.NewInt(10)}), + "get": getTxInput(b, singleContGetFunc, nil), } - multiContInput := common.GetTxInput(b, multiContFnA, nil) + multiContInput := getTxInput(b, multiContFnA, nil) // test cases - testCases := []common.TxTestCase{ + testCases := []TxTestCase{ { Name: "[Child chain] setter 5tx", Relayer: childTxRelayer, diff --git a/command/benchmark/benchmark.go b/command/benchmark/benchmark.go index e52612c197..6240af2bb1 100644 --- a/command/benchmark/benchmark.go +++ b/command/benchmark/benchmark.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/0xPolygon/polygon-edge/benchmark" - "github.com/0xPolygon/polygon-edge/benchmark/common" "github.com/0xPolygon/polygon-edge/command" "github.com/spf13/cobra" ) @@ -65,7 +64,7 @@ func runCommand(cmd *cobra.Command, _ []string) { for _, testInput := range testCases { sendTxResult := testing.Benchmark(func(b *testing.B) { b.Helper() - common.TxTestCasesExecutor(b, testInput) + benchmark.TxTestCasesExecutor(b, testInput) }) benchmarkResult := &benchmarkResult{ name: testInput.Name, @@ -101,11 +100,6 @@ func setFlags(cmd *cobra.Command) { _ = cmd.MarkFlagRequired(privateKeyFlag) } -func (p *benchmarkParams) validateFlags() error { - // add addresses and key validation - return nil -} - func (br *benchmarkResult) GetOutput() string { var buffer bytes.Buffer