diff --git a/cmd/cancun/helper.go b/cmd/cancun/helper.go deleted file mode 100644 index f51bb01..0000000 --- a/cmd/cancun/helper.go +++ /dev/null @@ -1,153 +0,0 @@ -package main - -import ( - "context" - "crypto/ecdsa" - "crypto/rand" - "fmt" - "math/big" - - txfuzz "github.com/MariusVanDerWijden/tx-fuzz" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethclient" - "github.com/ethereum/go-ethereum/rpc" -) - -const ( - maxDataPerBlob = 1 << 17 // 128Kb -) - -func exec(addr common.Address, data []byte, blobs bool) *types.Transaction { - cl, sk := getRealBackend() - backend := ethclient.NewClient(cl) - sender := common.HexToAddress(txfuzz.ADDR) - nonce, err := backend.PendingNonceAt(context.Background(), sender) - if err != nil { - panic(err) - } - chainid, err := backend.ChainID(context.Background()) - if err != nil { - panic(err) - } - fmt.Printf("Nonce: %v\n", nonce) - gp, err := backend.SuggestGasPrice(context.Background()) - if err != nil { - panic(err) - } - tip, err := backend.SuggestGasTipCap(context.Background()) - if err != nil { - tip = big.NewInt(100000000) - //panic(err) - } - var rlpData []byte - var _tx *types.Transaction - if blobs { - blob, err := randomBlobData() - if err != nil { - panic(err) - } - //nonce = nonce - 2 - tx := txfuzz.New4844Tx(nonce, &addr, 500000, chainid, tip.Mul(tip, common.Big1), gp.Mul(gp, common.Big1), common.Big0, data, big.NewInt(1_000_000), blob, make(types.AccessList, 0)) - signedTx, _ := types.SignTx(tx, types.NewCancunSigner(chainid), sk) - rlpData, err = signedTx.MarshalBinary() - if err != nil { - panic(err) - } - _tx = signedTx - } else { - tx := types.NewTx(&types.DynamicFeeTx{ChainID: chainid, Nonce: nonce, GasTipCap: tip, GasFeeCap: gp, Gas: 500000, To: &addr, Data: data}) - signedTx, _ := types.SignTx(tx, types.NewCancunSigner(chainid), sk) - rlpData, err = signedTx.MarshalBinary() - if err != nil { - panic(err) - } - _tx = signedTx - } - - if err := cl.CallContext(context.Background(), nil, "eth_sendRawTransaction", hexutil.Encode(rlpData)); err != nil { - panic(err) - } - return _tx -} - -func getRealBackend() (*rpc.Client, *ecdsa.PrivateKey) { - // eth.sendTransaction({from:personal.listAccounts[0], to:"0xb02A2EdA1b317FBd16760128836B0Ac59B560e9D", value: "100000000000000"}) - - sk := crypto.ToECDSAUnsafe(common.FromHex(txfuzz.SK)) - if crypto.PubkeyToAddress(sk.PublicKey).Hex() != txfuzz.ADDR { - panic(fmt.Sprintf("wrong address want %s got %s", crypto.PubkeyToAddress(sk.PublicKey).Hex(), txfuzz.ADDR)) - } - cl, err := rpc.Dial(address) - if err != nil { - panic(err) - } - return cl, sk -} - -func wait(tx *types.Transaction) { - client, _ := getRealBackend() - backend := ethclient.NewClient(client) - bind.WaitMined(context.Background(), backend, tx) -} - -func deploy(bytecode string) (common.Address, error) { - cl, sk := getRealBackend() - backend := ethclient.NewClient(cl) - sender := common.HexToAddress(txfuzz.ADDR) - nonce, err := backend.PendingNonceAt(context.Background(), sender) - if err != nil { - return common.Address{}, err - } - chainid, err := backend.ChainID(context.Background()) - if err != nil { - return common.Address{}, err - } - fmt.Printf("Nonce: %v\n", nonce) - gp, _ := backend.SuggestGasPrice(context.Background()) - tx := types.NewContractCreation(nonce, common.Big0, 500000, gp.Mul(gp, common.Big2), common.Hex2Bytes(bytecode)) - signedTx, _ := types.SignTx(tx, types.NewLondonSigner(chainid), sk) - if err := backend.SendTransaction(context.Background(), signedTx); err != nil { - return common.Address{}, err - } - return bind.WaitDeployed(context.Background(), backend, signedTx) -} - -func execute(data []byte, gaslimit uint64) { - cl, sk := getRealBackend() - backend := ethclient.NewClient(cl) - sender := common.HexToAddress(txfuzz.ADDR) - nonce, err := backend.PendingNonceAt(context.Background(), sender) - if err != nil { - panic(err) - } - chainid, err := backend.ChainID(context.Background()) - if err != nil { - panic(err) - } - fmt.Printf("Nonce: %v\n", nonce) - gp, _ := backend.SuggestGasPrice(context.Background()) - tx := types.NewContractCreation(nonce, common.Big1, gaslimit, gp.Mul(gp, common.Big2), data) - signedTx, _ := types.SignTx(tx, types.NewLondonSigner(chainid), sk) - backend.SendTransaction(context.Background(), signedTx) -} - -func randomBlobData() ([]byte, error) { - val, err := rand.Int(rand.Reader, big.NewInt(maxDataPerBlob)) - if err != nil { - return nil, err - } - size := int(val.Int64() * 3) - data := make([]byte, size) - n, err := rand.Read(data) - if err != nil { - return nil, err - } - if n != size { - return nil, fmt.Errorf("could not create random blob data with size %d: %v", size, err) - } - return data, nil -} diff --git a/cmd/cancun/main.go b/cmd/cancun/main.go index 2d67664..b445308 100644 --- a/cmd/cancun/main.go +++ b/cmd/cancun/main.go @@ -7,15 +7,12 @@ import ( "fmt" "time" + "github.com/MariusVanDerWijden/tx-fuzz/helper" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto/kzg4844" "github.com/ethereum/go-ethereum/params" ) -var ( - address = "https://rpc.pectra-devnet-0.ethpandaops.io/" -) - func main() { fmt.Println("4788") test4788() @@ -33,15 +30,15 @@ func main() { func test7516() { // JUMPDEST, BLOBBASEFEE, POP, PUSH0, JUMP - execute([]byte{0x5b, 0x4a, 0x50, 0x5f, 0x56}, 30_000_000) + helper.Execute([]byte{0x5b, 0x4a, 0x50, 0x5f, 0x56}, 30_000_000) // BLOBBASEFEE, BLOBBASEFEE, SSTORE - execute([]byte{0x4a, 0x4a, 0x50, 0x55}, 500_000) + helper.Execute([]byte{0x4a, 0x4a, 0x50, 0x55}, 500_000) } func test5656() { // PUSH1, 0x20, PUSH0, PUSH0, MCOPY - execute([]byte{0x60, 0x20, 0x5f, 0x5f, 0x5e}, 30_000_000) + helper.Execute([]byte{0x60, 0x20, 0x5f, 0x5f, 0x5e}, 30_000_000) pushMaxVal := func() []byte { return []byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} @@ -52,24 +49,24 @@ func test5656() { } // PUSH32, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, PUSH0, PUSH0, MCOPY - execute(append(pushMaxVal(), 0x5f, 0x5f, 0x5e), 30_000_000) + helper.Execute(append(pushMaxVal(), 0x5f, 0x5f, 0x5e), 30_000_000) // PUSH0, PUSH32, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, PUSH0, MCOPY code := []byte{0x5f} code = append(code, pushMaxVal()...) code = append(code, []byte{0x5f, 0x5e}...) code = append(code, sstore()...) - execute(code, 30_000_000) + helper.Execute(code, 30_000_000) // PUSH0, PUSH0, PUSH32, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, MCOPY code = []byte{0x5f, 0x5f} code = append(code, pushMaxVal()...) code = append(code, []byte{0x5e}...) code = append(code, sstore()...) - execute(code, 30_000_000) + helper.Execute(code, 30_000_000) // PUSH0, PUSH1, 0xff, MSTORE, JUMPDEST, PUSH1, 0x20, PUSH0, PUSH0, MCOPY, PUSH1, 0x04 JUMP - execute([]byte{0x5f, 0x60, 0xff, 0x52, 0x5b, 0x60, 0x20, 0x5f, 0x5f, 0x5e, 0x60, 0x04, 0x56}, 30_000_000) + helper.Execute([]byte{0x5f, 0x60, 0xff, 0x52, 0x5b, 0x60, 0x20, 0x5f, 0x5f, 0x5e, 0x60, 0x04, 0x56}, 30_000_000) test5656_memBuster() } @@ -109,7 +106,7 @@ func test5656_memBuster() { code = append(code, 0x56) // PUSH0, PUSH1, 0xff, MSTORE, JUMPDEST, PUSH1, 0x20, PUSH0, PUSH0, MCOPY, PUSH1, 0x04 JUMP - execute(code, 30_000_000) + helper.Execute(code, 30_000_000) } func pushSize(size int) []byte { @@ -122,26 +119,26 @@ func pushSize(size int) []byte { func test1153() { // JUMPDEST, GAS, GAS, TSTORE, PUSH0, JUMP - execute([]byte{0x5b, 0x5a, 0x5a, 0x5d, 0x5f, 0x56}, 30_000_000) + helper.Execute([]byte{0x5b, 0x5a, 0x5a, 0x5d, 0x5f, 0x56}, 30_000_000) // JUMPDEST, GAS, DUP1, DUP1, TSTORE, TLOAD, POP, PUSH0, JUMP - execute([]byte{0x5b, 0x5a, 0x80, 0x80, 0x5d, 0x5c, 0x50, 0x5f, 0x56}, 30_000_000) + helper.Execute([]byte{0x5b, 0x5a, 0x80, 0x80, 0x5d, 0x5c, 0x50, 0x5f, 0x56}, 30_000_000) // PUSH0, TLOAD, GAS, TLOAD, SSTORE - execute([]byte{0x5f, 0x5c, 0x5a, 0x55}, 500_000) + helper.Execute([]byte{0x5f, 0x5c, 0x5a, 0x55}, 500_000) // PUSH32, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff TLOAD DUP1, SSTORE - execute([]byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5c, 0x80, 0x55}, 500_000) + helper.Execute([]byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5c, 0x80, 0x55}, 500_000) } func test4788() { // Call addr contractAddr4788 := common.HexToAddress("0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02") - exec(contractAddr4788, Uint64ToHash(0).Bytes(), false) + helper.Exec(contractAddr4788, Uint64ToHash(0).Bytes(), false) t := time.Now().Unix() for i := 0; i < 15; i++ { - exec(contractAddr4788, Uint64ToHash(uint64(t)-uint64(i)).Bytes(), false) + helper.Exec(contractAddr4788, Uint64ToHash(uint64(t)-uint64(i)).Bytes(), false) } // deploy4788Proxy @@ -153,20 +150,20 @@ func test4788() { // Call to 4788 contract t = time.Now().Unix() for i := 0; i < 15; i++ { - exec(addr, Uint64ToHash(uint64(t)-uint64(i)).Bytes(), false) + helper.Exec(addr, Uint64ToHash(uint64(t)-uint64(i)).Bytes(), false) } // short or long calls for i := 0; i < 64; i++ { arr := make([]byte, i) - exec(addr, arr, false) + helper.Exec(addr, arr, false) } // random calls for i := 0; i < 10; i++ { arr := make([]byte, 32) rand.Read(arr) - exec(addr, arr, false) + helper.Exec(addr, arr, false) } } @@ -179,22 +176,22 @@ func test4844() { // 4844 Tests // PUSH0, DATAHASH, PUSH0, DATAHASH, SSTORE - exec(addr, []byte{0x5f, 0x49, 0x5f, 0x49, 0x55}, true) + helper.Exec(addr, []byte{0x5f, 0x49, 0x5f, 0x49, 0x55}, true) var dataHashByteCode []byte for i := 0; i < 10; i++ { // PUSH1 i, DATAHASH, PUSH0, SSTORE dataHashByteCode = append(dataHashByteCode, []byte{0x60, byte(i), 0x49, 0x5f, 0x55}...) } - exec(addr, dataHashByteCode, true) + helper.Exec(addr, dataHashByteCode, true) // PUSH1 0x01, NOT, DATAHASH, PUSH0, NOT, DATAHASH, SSTORE - exec(addr, []byte{0x60, 0x01, 0x19, 0x49, 0x5f, 0x19, 0x49, 0x55}, true) + helper.Exec(addr, []byte{0x60, 0x01, 0x19, 0x49, 0x5f, 0x19, 0x49, 0x55}, true) // PUSH1 0x255, PUSH1 0x01, SHL, DATAHASH, PUSH0, SSTORE - tx := exec(addr, []byte{0x60, 0xff, 0x1b, 0x49, 0x5f, 0x55}, true) + tx := helper.Exec(addr, []byte{0x60, 0xff, 0x1b, 0x49, 0x5f, 0x55}, true) - wait(tx) + helper.Wait(tx) } func test4844_precompile() { @@ -205,30 +202,30 @@ func test4844_precompile() { // Test precompile without blobs staticTestInput := common.FromHex("01d18459b334ffe8e2226eef1db874fda6db2bdd9357268b39220af2d59464fb564c0a11a0f704f4fc3e8acfe0f8245f0ad1347b378fbf96e206da11a5d3630624d25032e67a7e6a4910df5834b8fe70e6bcfeeac0352434196bdf4b2485d5a1978a0d595c823c05947b1156175e72634a377808384256e9921ebf72181890be2d6b58d4a73a880541d1656875654806942307f266e636553e94006d11423f2688945ff3bdf515859eba1005c1a7708d620a94d91a1c0c285f9584e75ec2f82a") - exec(addr, staticTestInput, false) + helper.Exec(addr, staticTestInput, false) invalidInput := make([]byte, len(staticTestInput)) - exec(addr, invalidInput, false) + helper.Exec(addr, invalidInput, false) co, cl, pr, po, err := createPrecompileRandParams() if err != nil { panic(err) } validRandomInput := precompileParamsToBytes(co, cl, pr, po) - tx := exec(addr, validRandomInput, false) + tx := helper.Exec(addr, validRandomInput, false) - wait(tx) + helper.Wait(tx) // Test precompile with blobs - exec(addr, staticTestInput, true) - exec(addr, invalidInput, true) - tx = exec(addr, validRandomInput, true) + helper.Exec(addr, staticTestInput, true) + helper.Exec(addr, invalidInput, true) + tx = helper.Exec(addr, validRandomInput, true) - wait(tx) + helper.Wait(tx) // Full block of verification for i := 0; i < 30_000_000/100_000; i++ { - exec(addr, validRandomInput, false) + helper.Exec(addr, validRandomInput, false) } } @@ -304,7 +301,7 @@ contract BlobProxy { func deployBlobProxy() (common.Address, error) { bytecode := "6080604052348015600f57600080fd5b5060ae80601d6000396000f3fe6080604052348015600f57600080fd5b506000366060600083838080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050905060008151602083016000f090505050915050805190602001f3fea2646970667358221220c23b98a79e6709c832ef1c90f5a3a7583ba88f759611d74a4d775dd22a02296364736f6c63430008120033" - return deploy(bytecode) + return helper.Deploy(bytecode) } /* @@ -326,7 +323,7 @@ pragma solidity >=0.7.0 <0.9.0; */ func deployBlobCaller() (common.Address, error) { bytecode := "608060405234801561001057600080fd5b5061047b806100206000396000f3fe608060405234801561001057600080fd5b5060003660606000600a90506000808273ffffffffffffffffffffffffffffffffffffffff1661c350878760405161004992919061010a565b60006040518083038160008787f1925050503d8060008114610087576040519150601f19603f3d011682016040523d82523d6000602084013e61008c565b606091505b5091509150809350816000806101000a81548160ff02191690831515021790555080600190816100bc9190610373565b50505050915050805190602001f35b600081905092915050565b82818337600083830152505050565b60006100f183856100cb565b93506100fe8385846100d6565b82840190509392505050565b60006101178284866100e5565b91508190509392505050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806101a457607f821691505b6020821081036101b7576101b661015d565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b60006008830261021f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826101e2565b61022986836101e2565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b600061027061026b61026684610241565b61024b565b610241565b9050919050565b6000819050919050565b61028a83610255565b61029e61029682610277565b8484546101ef565b825550505050565b600090565b6102b36102a6565b6102be818484610281565b505050565b5b818110156102e2576102d76000826102ab565b6001810190506102c4565b5050565b601f821115610327576102f8816101bd565b610301846101d2565b81016020851015610310578190505b61032461031c856101d2565b8301826102c3565b50505b505050565b600082821c905092915050565b600061034a6000198460080261032c565b1980831691505092915050565b60006103638383610339565b9150826002028217905092915050565b61037c82610123565b67ffffffffffffffff8111156103955761039461012e565b5b61039f825461018c565b6103aa8282856102e6565b600060209050601f8311600181146103dd57600084156103cb578287015190505b6103d58582610357565b86555061043d565b601f1984166103eb866101bd565b60005b82811015610413578489015182556001820191506020850194506020810190506103ee565b86831015610430578489015161042c601f891682610339565b8355505b6001600288020188555050505b50505050505056fea264697066735822122089d7332a134ee7e7d76876ef5f4e74d939f9d9d9f3344e6afb518c96fff0b63164736f6c63430008120033" - return deploy(bytecode) + return helper.Deploy(bytecode) } /* @@ -348,7 +345,7 @@ contract Call4788 { func deploy4788Proxy() (common.Address, error) { bytecode := "608060405234801561001057600080fd5b5061048a806100206000396000f3fe608060405234801561001057600080fd5b5060003660606000720f3df6d732807ef1319fb7b8bb8522d0beac0290506000808273ffffffffffffffffffffffffffffffffffffffff168686604051610058929190610119565b6000604051808303816000865af19150503d8060008114610095576040519150601f19603f3d011682016040523d82523d6000602084013e61009a565b606091505b509150915080600090816100ae9190610382565b5081600160006101000a81548160ff021916908315150217905550809350505050915050805190602001f35b600081905092915050565b82818337600083830152505050565b600061010083856100da565b935061010d8385846100e5565b82840190509392505050565b60006101268284866100f4565b91508190509392505050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806101b357607f821691505b6020821081036101c6576101c561016c565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b60006008830261022e7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826101f1565b61023886836101f1565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b600061027f61027a61027584610250565b61025a565b610250565b9050919050565b6000819050919050565b61029983610264565b6102ad6102a582610286565b8484546101fe565b825550505050565b600090565b6102c26102b5565b6102cd818484610290565b505050565b5b818110156102f1576102e66000826102ba565b6001810190506102d3565b5050565b601f82111561033657610307816101cc565b610310846101e1565b8101602085101561031f578190505b61033361032b856101e1565b8301826102d2565b50505b505050565b600082821c905092915050565b60006103596000198460080261033b565b1980831691505092915050565b60006103728383610348565b9150826002028217905092915050565b61038b82610132565b67ffffffffffffffff8111156103a4576103a361013d565b5b6103ae825461019b565b6103b98282856102f5565b600060209050601f8311600181146103ec57600084156103da578287015190505b6103e48582610366565b86555061044c565b601f1984166103fa866101cc565b60005b82811015610422578489015182556001820191506020850194506020810190506103fd565b8683101561043f578489015161043b601f891682610348565b8355505b6001600288020188555050505b50505050505056fea2646970667358221220d3505d93c72ff246e512c416145e275fe92925a05a9953337b1add26b509ec7764736f6c63430008120033" - return deploy(bytecode) + return helper.Deploy(bytecode) } func Uint64ToHash(u uint64) common.Hash { diff --git a/cmd/eip4399/main.go b/cmd/eip4399/main.go index cea1602..6106839 100644 --- a/cmd/eip4399/main.go +++ b/cmd/eip4399/main.go @@ -2,23 +2,17 @@ package main import ( "context" - "crypto/ecdsa" "fmt" txfuzz "github.com/MariusVanDerWijden/tx-fuzz" + "github.com/MariusVanDerWijden/tx-fuzz/helper" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" - "github.com/ethereum/go-ethereum/rpc" -) - -var ( - address = "http://127.0.0.1:8545" ) func main() { - cl, sk := getRealBackend() + cl, sk := helper.GetRealBackend() backend := ethclient.NewClient(cl) sender := common.HexToAddress(txfuzz.ADDR) nonce, err := backend.PendingNonceAt(context.Background(), sender) @@ -35,17 +29,3 @@ func main() { signedTx, _ := types.SignTx(tx, types.NewLondonSigner(chainid), sk) backend.SendTransaction(context.Background(), signedTx) } - -func getRealBackend() (*rpc.Client, *ecdsa.PrivateKey) { - // eth.sendTransaction({from:personal.listAccounts[0], to:"0xb02A2EdA1b317FBd16760128836B0Ac59B560e9D", value: "100000000000000"}) - - sk := crypto.ToECDSAUnsafe(common.FromHex(txfuzz.SK)) - if crypto.PubkeyToAddress(sk.PublicKey).Hex() != txfuzz.ADDR { - panic(fmt.Sprintf("wrong address want %s got %s", crypto.PubkeyToAddress(sk.PublicKey).Hex(), txfuzz.ADDR)) - } - cl, err := rpc.Dial(address) - if err != nil { - panic(err) - } - return cl, sk -} diff --git a/cmd/prague/main.go b/cmd/prague/main.go index f597504..0b6b5fd 100644 --- a/cmd/prague/main.go +++ b/cmd/prague/main.go @@ -4,14 +4,12 @@ import ( "fmt" "time" + "github.com/MariusVanDerWijden/tx-fuzz/helper" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" ) -var ( - address = "https://rpc.pectra-devnet-0.ethpandaops.io/" -) - func main() { testTouchContracts() test2537() @@ -31,22 +29,22 @@ func testTouchContracts() { } for _, addr := range addresses { - exec(addr, []byte{}, false) // no data - exec(addr, []byte{1}, false) // 1 byte of data - exec(addr, crypto.Keccak256([]byte{1})[:], false) // 32 bytes of data - exec(addr, make([]byte, 20), false) - exec(addr, make([]byte, 2048), false) // 2048 bytes of data + helper.Exec(addr, []byte{}, false) // no data + helper.Exec(addr, []byte{1}, false) // 1 byte of data + helper.Exec(addr, crypto.Keccak256([]byte{1})[:], false) // 32 bytes of data + helper.Exec(addr, make([]byte, 20), false) + helper.Exec(addr, make([]byte, 2048), false) // 2048 bytes of data } } func test3074() { // auth - execute([]byte{0x5f, 0x5f, 0x5f, 0xf6, 0x80, 0x55}, 200000) - execute([]byte{0x64, 0xff, 0xff, 0xff, 0xff, 0x64, 0xff, 0xff, 0xff, 0xff, 0x64, 0xff, 0xff, 0xff, 0xff, 0xf6, 0x80, 0x55}, 200000) + helper.Execute([]byte{0x5f, 0x5f, 0x5f, 0xf6, 0x80, 0x55}, 200000) + helper.Execute([]byte{0x64, 0xff, 0xff, 0xff, 0xff, 0x64, 0xff, 0xff, 0xff, 0xff, 0x64, 0xff, 0xff, 0xff, 0xff, 0xf6, 0x80, 0x55}, 200000) // authcall - execute([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0xf7, 0x80, 0x55}, 200000) - execute([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5a, 0xf7, 0x80, 0x55}, 200000) - execute([]byte{0x64, 0xff, 0xff, 0xff, 0xff, 0x5f, 0x64, 0xff, 0xff, 0xff, 0xff, 0x5f, 0x5f, 0x5f, 0x5a, 0xf7, 0x80, 0x55}, 200000) + helper.Execute([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0xf7, 0x80, 0x55}, 200000) + helper.Execute([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5a, 0xf7, 0x80, 0x55}, 200000) + helper.Execute([]byte{0x64, 0xff, 0xff, 0xff, 0xff, 0x5f, 0x64, 0xff, 0xff, 0xff, 0xff, 0x5f, 0x5f, 0x5f, 0x5a, 0xf7, 0x80, 0x55}, 200000) time.Sleep(time.Minute) fmt.Println("Execution tests") vectors := [][]byte{ @@ -57,7 +55,7 @@ func test3074() { common.FromHex("0x01f2f89f718c81bfdac9f08fc1cd6b91de657519f87ba41bcd6393a95833a55ef2616a0a70c31ec6801a9ab304dda6d14a67d0897f588b15bf73d99371b9db44fa0000000000000000000000000000000000000000000000000000000000000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), } for _, vec := range vectors { - execute(vec, 300000) + helper.Execute(vec, 300000) } } @@ -89,7 +87,6 @@ func test2537() { testBLS(i, vec) } } - } func test2537Long() { @@ -109,7 +106,7 @@ func testLongBLS(prec int, input []byte) { } base := input for i := 0; i < 20; i++ { - exec(addr, base, false) + helper.Exec(addr, base, false) base = append(base, base...) } } @@ -119,7 +116,7 @@ func testBLS(prec int, input []byte) { if err != nil { panic(err) } - exec(addr, input, false) + helper.Exec(addr, input, false) } /* @@ -143,5 +140,5 @@ func deployPrecompileCaller(precompile string) (common.Address, error) { bytecode1 := "6080604052348015600e575f80fd5b506104568061001c5f395ff3fe608060405234801561000f575f80fd5b505f3660605f60" bytecode2 := "90505f808273ffffffffffffffffffffffffffffffffffffffff1661c35087876040516100459291906100fe565b5f604051808303815f8787f1925050503d805f811461007f576040519150601f19603f3d011682016040523d82523d5f602084013e610084565b606091505b5091509150809350815f806101000a81548160ff02191690831515021790555080600190816100b39190610350565b50505050915050805190602001f35b5f81905092915050565b828183375f83830152505050565b5f6100e583856100c2565b93506100f28385846100cc565b82840190509392505050565b5f61010a8284866100da565b91508190509392505050565b5f81519050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f600282049050600182168061019157607f821691505b6020821081036101a4576101a361014d565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f600883026102067fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826101cb565b61021086836101cb565b95508019841693508086168417925050509392505050565b5f819050919050565b5f819050919050565b5f61025461024f61024a84610228565b610231565b610228565b9050919050565b5f819050919050565b61026d8361023a565b6102816102798261025b565b8484546101d7565b825550505050565b5f90565b610295610289565b6102a0818484610264565b505050565b5b818110156102c3576102b85f8261028d565b6001810190506102a6565b5050565b601f821115610308576102d9816101aa565b6102e2846101bc565b810160208510156102f1578190505b6103056102fd856101bc565b8301826102a5565b50505b505050565b5f82821c905092915050565b5f6103285f198460080261030d565b1980831691505092915050565b5f6103408383610319565b9150826002028217905092915050565b61035982610116565b67ffffffffffffffff81111561037257610371610120565b5b61037c825461017a565b6103878282856102c7565b5f60209050601f8311600181146103b8575f84156103a6578287015190505b6103b08582610335565b865550610417565b601f1984166103c6866101aa565b5f5b828110156103ed578489015182556001820191506020850194506020810190506103c8565b8683101561040a5784890151610406601f891682610319565b8355505b6001600288020188555050505b50505050505056fea2646970667358221220bc28435cfa3208db8cae33e216a1ff54a6e5dce073695cad36274cc363055c5564736f6c63430008190033" // The byte in between bytecode1 and bytecode2 denotes the precompile which we want to call - return deploy(fmt.Sprintf("%v%v%v", bytecode1, precompile, bytecode2)) + return helper.Deploy(fmt.Sprintf("%v%v%v", bytecode1, precompile, bytecode2)) } diff --git a/cmd/shanghai/main.go b/cmd/shanghai/main.go index 5b8a17c..92b1d8d 100644 --- a/cmd/shanghai/main.go +++ b/cmd/shanghai/main.go @@ -1,50 +1,42 @@ package main import ( - "context" - "crypto/ecdsa" "encoding/binary" "fmt" - txfuzz "github.com/MariusVanDerWijden/tx-fuzz" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethclient" - "github.com/ethereum/go-ethereum/rpc" + "github.com/MariusVanDerWijden/tx-fuzz/helper" ) var ( - address = "http://127.0.0.1:8545" maxCodeSize = 24576 maxInitCodeSize = 2 * maxCodeSize ) func main() { // Store coinbase - exec([]byte{0x41, 0x41, 0x55}) + helper.Execute([]byte{0x41, 0x41, 0x55}, 50000) // Call coinbase // 5x PUSH0, COINBASE, GAS, CALL - exec([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x41, 0x5A, 0xf1}) + helper.Execute([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x41, 0x5A, 0xf1}, 50000) // 5x PUSH0, COINBASE, GAS, CALLCODE - exec([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x41, 0x5A, 0xf2}) + helper.Execute([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x41, 0x5A, 0xf2}, 50000) // 5x PUSH0, COINBASE, GAS, DELEGATECALL - exec([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x41, 0x5A, 0xf4}) + helper.Execute([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x41, 0x5A, 0xf4}, 50000) // 5x PUSH0, COINBASE, GAS, STATICCALL - exec([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x41, 0x5A, 0xfA}) + helper.Execute([]byte{0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x41, 0x5A, 0xfA}, 50000) // COINBASE, SELFDESTRUCT - exec([]byte{0x41, 0xff}) + helper.Execute([]byte{0x41, 0xff}, 50000) // COINBASE, EXTCODESIZE - exec([]byte{0x41, 0x3b}) + helper.Execute([]byte{0x41, 0x3b}, 50000) // 3x PUSH0, COINBASE, EXTCODECOPY - exec([]byte{0x5f, 0x5f, 0x5f, 0x41, 0x3C}) + helper.Execute([]byte{0x5f, 0x5f, 0x5f, 0x41, 0x3C}, 50000) // COINBASE, EXTCODEHASH - exec([]byte{0x41, 0x3F}) + helper.Execute([]byte{0x41, 0x3F}, 50000) // COINBASE, BALANCE - exec([]byte{0x41, 0x31}) + helper.Execute([]byte{0x41, 0x31}, 50000) // loop push0 // JUMPDEST, PUSH0, JUMP - exec([]byte{0x58, 0x5f, 0x56}) + helper.Execute([]byte{0x58, 0x5f, 0x56}, 50000) fmt.Println("Limit&MeterInitcode") // limit & meter initcode sizes := []int{ @@ -58,43 +50,24 @@ func main() { // size x JUMPDEST STOP for _, size := range sizes { initcode := repeatOpcode(size, 0x58) - exec(append(initcode, 0x00)) + helper.Execute(append(initcode, 0x00), 50000) } // size x STOP STOP for _, size := range sizes { - exec(repeatOpcode(size, 0x00)) + helper.Execute(repeatOpcode(size, 0x00), 50000) } // PUSH4 size, PUSH0, PUSH0, CREATE for _, size := range sizes { initcode := pushSize(size) - exec(append(initcode, []byte{0x57, 0x57, 0xF0}...)) + helper.Execute(append(initcode, []byte{0x57, 0x57, 0xF0}...), 50000) } // PUSH4 size, PUSH0, PUSH0, CREATE2 for _, size := range sizes { initcode := pushSize(size) - exec(append(initcode, []byte{0x57, 0x57, 0xF5}...)) + helper.Execute(append(initcode, []byte{0x57, 0x57, 0xF5}...), 50000) } } -func exec(data []byte) { - cl, sk := getRealBackend() - backend := ethclient.NewClient(cl) - sender := common.HexToAddress(txfuzz.ADDR) - nonce, err := backend.PendingNonceAt(context.Background(), sender) - if err != nil { - panic(err) - } - chainid, err := backend.ChainID(context.Background()) - if err != nil { - panic(err) - } - fmt.Printf("Nonce: %v\n", nonce) - gp, _ := backend.SuggestGasPrice(context.Background()) - tx := types.NewContractCreation(nonce, common.Big1, 500000, gp.Mul(gp, common.Big2), data) - signedTx, _ := types.SignTx(tx, types.NewLondonSigner(chainid), sk) - backend.SendTransaction(context.Background(), signedTx) -} - // PUSH4 size func pushSize(size int) []byte { code := []byte{63} @@ -111,17 +84,3 @@ func repeatOpcode(size int, opcode byte) []byte { } return initcode } - -func getRealBackend() (*rpc.Client, *ecdsa.PrivateKey) { - // eth.sendTransaction({from:personal.listAccounts[0], to:"0xb02A2EdA1b317FBd16760128836B0Ac59B560e9D", value: "100000000000000"}) - - sk := crypto.ToECDSAUnsafe(common.FromHex(txfuzz.SK)) - if crypto.PubkeyToAddress(sk.PublicKey).Hex() != txfuzz.ADDR { - panic(fmt.Sprintf("wrong address want %s got %s", crypto.PubkeyToAddress(sk.PublicKey).Hex(), txfuzz.ADDR)) - } - cl, err := rpc.Dial(address) - if err != nil { - panic(err) - } - return cl, sk -} diff --git a/cmd/prague/helper.go b/helper/helper.go similarity index 89% rename from cmd/prague/helper.go rename to helper/helper.go index 9a307d7..bd89dba 100644 --- a/cmd/prague/helper.go +++ b/helper/helper.go @@ -1,4 +1,4 @@ -package main +package helper import ( "context" @@ -21,8 +21,8 @@ const ( maxDataPerBlob = 1 << 17 // 128Kb ) -func exec(addr common.Address, data []byte, blobs bool) *types.Transaction { - cl, sk := getRealBackend() +func Exec(addr common.Address, data []byte, blobs bool) *types.Transaction { + cl, sk := GetRealBackend() backend := ethclient.NewClient(cl) sender := common.HexToAddress(txfuzz.ADDR) nonce, err := backend.PendingNonceAt(context.Background(), sender) @@ -47,7 +47,7 @@ func exec(addr common.Address, data []byte, blobs bool) *types.Transaction { var _tx *types.Transaction gasLimit := uint64(30_000_000) if blobs { - blob, err := randomBlobData() + blob, err := RandomBlobData() if err != nil { panic(err) } @@ -75,28 +75,28 @@ func exec(addr common.Address, data []byte, blobs bool) *types.Transaction { return _tx } -func getRealBackend() (*rpc.Client, *ecdsa.PrivateKey) { +func GetRealBackend() (*rpc.Client, *ecdsa.PrivateKey) { // eth.sendTransaction({from:personal.listAccounts[0], to:"0xb02A2EdA1b317FBd16760128836B0Ac59B560e9D", value: "100000000000000"}) sk := crypto.ToECDSAUnsafe(common.FromHex(txfuzz.SK)) if crypto.PubkeyToAddress(sk.PublicKey).Hex() != txfuzz.ADDR { panic(fmt.Sprintf("wrong address want %s got %s", crypto.PubkeyToAddress(sk.PublicKey).Hex(), txfuzz.ADDR)) } - cl, err := rpc.Dial(address) + cl, err := rpc.Dial(txfuzz.RPC) if err != nil { panic(err) } return cl, sk } -func wait(tx *types.Transaction) { - client, _ := getRealBackend() +func Wait(tx *types.Transaction) { + client, _ := GetRealBackend() backend := ethclient.NewClient(client) bind.WaitMined(context.Background(), backend, tx) } -func deploy(bytecode string) (common.Address, error) { - cl, sk := getRealBackend() +func Deploy(bytecode string) (common.Address, error) { + cl, sk := GetRealBackend() backend := ethclient.NewClient(cl) sender := common.HexToAddress(txfuzz.ADDR) nonce, err := backend.PendingNonceAt(context.Background(), sender) @@ -117,8 +117,8 @@ func deploy(bytecode string) (common.Address, error) { return bind.WaitDeployed(context.Background(), backend, signedTx) } -func execute(data []byte, gaslimit uint64) { - cl, sk := getRealBackend() +func Execute(data []byte, gaslimit uint64) { + cl, sk := GetRealBackend() backend := ethclient.NewClient(cl) sender := common.HexToAddress(txfuzz.ADDR) nonce, err := backend.PendingNonceAt(context.Background(), sender) @@ -136,7 +136,7 @@ func execute(data []byte, gaslimit uint64) { backend.SendTransaction(context.Background(), signedTx) } -func randomBlobData() ([]byte, error) { +func RandomBlobData() ([]byte, error) { val, err := rand.Int(rand.Reader, big.NewInt(maxDataPerBlob)) if err != nil { return nil, err diff --git a/main.go b/main.go index 42f7e93..5a69c4b 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package txfuzz var ( + RPC = "http://127.0.0.1:8545" SK = "0xcdfbe6f7602f67a97602e3e9fc24cde1cdffa88acd47745c0b84c5ff55891e1b" ADDR = "0xb02A2EdA1b317FBd16760128836B0Ac59B560e9D" SK2 = "0x8c04e41e317a7cf0cf4c2f7431d0a890a950f352df41ff6d053698df61a73bba"