From b9709bddf9021526b6355889bdb3551d7e8a784e Mon Sep 17 00:00:00 2001 From: sylvan Date: Thu, 29 Dec 2022 11:20:50 +0800 Subject: [PATCH] Feature: add internal contract for call tracer (#3) --- .gitignore | 3 +- Makefile | 3 +- accounts/abi/bind/bind_test.go | 162 +++++++++++++++--- .../internaltransaction/contracts/.gitkeep | 0 .../internaltransaction/contracts/Ether.sol | 23 +++ .../internaltransaction/migrations/.gitkeep | 0 contracts/internaltransaction/test/.gitkeep | 0 .../internaltransaction/truffle-config.js | 141 +++++++++++++++ 8 files changed, 307 insertions(+), 25 deletions(-) create mode 100644 contracts/internaltransaction/contracts/.gitkeep create mode 100644 contracts/internaltransaction/contracts/Ether.sol create mode 100644 contracts/internaltransaction/migrations/.gitkeep create mode 100644 contracts/internaltransaction/test/.gitkeep create mode 100644 contracts/internaltransaction/truffle-config.js diff --git a/.gitignore b/.gitignore index 1d21def9f897..7add02f1cf8a 100644 --- a/.gitignore +++ b/.gitignore @@ -54,5 +54,6 @@ geth-tutorial/ trace.out geth-trace.out audit.log -__debug_bin/ local +soljson-v0.8.17+commit.8df45f5f.js + diff --git a/Makefile b/Makefile index ada72962c4f8..76bdb82d79ff 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ GOBIN = ./build/bin GO ?= latest GORUN = env GO111MODULE=on go run +TEST_ARGS ?= "" geth: $(GORUN) build/ci.go install ./cmd/geth @@ -17,7 +18,7 @@ all: $(GORUN) build/ci.go install test: all - $(GORUN) build/ci.go test + $(GORUN) build/ci.go test $(TEST_ARGS) format: $(GORUN) build/ci.go format diff --git a/accounts/abi/bind/bind_test.go b/accounts/abi/bind/bind_test.go index cbbce7b30889..f01bf38884fa 100644 --- a/accounts/abi/bind/bind_test.go +++ b/accounts/abi/bind/bind_test.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "os/exec" + "path" "path/filepath" "runtime" "strings" @@ -539,7 +540,7 @@ var bindTests = []struct { struct A { bytes32 B; } - + function F() public view returns (A[] memory a, uint256[] memory c, bool[] memory d) { A[] memory a = new A[](2); a[0].B = bytes32(uint256(1234) << 96); @@ -547,7 +548,7 @@ var bindTests = []struct { bool[] memory d; return (a, c, d); } - + function G() public view returns (A[] memory a) { A[] memory a = new A[](2); a[0].B = bytes32(uint256(1234) << 96); @@ -569,10 +570,10 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337)) - + sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000000000)}}, 10000000) defer sim.Close() - + // Deploy a structs method invoker contract and execute its default method _, _, structs, err := DeployStructs(auth, sim) if err != nil { @@ -737,7 +738,10 @@ var bindTests = []struct { } } `, []string{`6060604052346000575b6086806100176000396000f300606060405263ffffffff60e060020a60003504166349f8e98281146022575b6000565b34600057602c6055565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b335b905600a165627a7a72305820aef6b7685c0fa24ba6027e4870404a57df701473fe4107741805c19f5138417c0029`}, - []string{`[{"constant":true,"inputs":[],"name":"callFrom","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"}]`}, + []string{` + [{"constant":true,"inputs":[],"name":"callFrom","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"}] + `, + }, ` "math/big" @@ -1699,13 +1703,13 @@ var bindTests = []struct { `NewFallbacks`, ` pragma solidity >=0.6.0 <0.7.0; - + contract NewFallbacks { event Fallback(bytes data); fallback() external { emit Fallback(msg.data); } - + event Received(address addr, uint value); receive() external payable { emit Received(msg.sender, msg.value); @@ -1717,7 +1721,7 @@ var bindTests = []struct { ` "bytes" "math/big" - + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" "github.com/ethereum/go-ethereum/core" @@ -1726,22 +1730,22 @@ var bindTests = []struct { ` key, _ := crypto.GenerateKey() addr := crypto.PubkeyToAddress(key.PublicKey) - + sim := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(10000000000000000)}}, 1000000) defer sim.Close() - + opts, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337)) _, _, c, err := DeployNewFallbacks(opts, sim) if err != nil { t.Fatalf("Failed to deploy contract: %v", err) } sim.Commit() - + // Test receive function opts.Value = big.NewInt(100) c.Receive(opts) sim.Commit() - + var gotEvent bool iter, _ := c.FilterReceived(nil) defer iter.Close() @@ -1758,14 +1762,14 @@ var bindTests = []struct { if !gotEvent { t.Fatal("Expect to receive event emitted by receive") } - + // Test fallback function gotEvent = false opts.Value = nil calldata := []byte{0x01, 0x02, 0x03} c.Fallback(opts, calldata) sim.Commit() - + iter2, _ := c.FilterFallback(nil) defer iter2.Close() for iter2.Next() { @@ -1860,7 +1864,7 @@ var bindTests = []struct { `NewErrors`, ` pragma solidity >0.8.4; - + contract NewErrors { error MyError(uint256); error MyError1(uint256); @@ -1875,7 +1879,7 @@ var bindTests = []struct { []string{`[{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"MyError","type":"error"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"MyError1","type":"error"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"MyError2","type":"error"},{"inputs":[{"internalType":"uint256","name":"a","type":"uint256"},{"internalType":"uint256","name":"b","type":"uint256"},{"internalType":"uint256","name":"c","type":"uint256"}],"name":"MyError3","type":"error"},{"inputs":[],"name":"Error","outputs":[],"stateMutability":"pure","type":"function"}]`}, ` "math/big" - + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" "github.com/ethereum/go-ethereum/core" @@ -1889,7 +1893,7 @@ var bindTests = []struct { sim = backends.NewSimulatedBackend(core.GenesisAlloc{user.From: {Balance: big.NewInt(1000000000000000000)}}, ethconfig.Defaults.Miner.GasCeil) ) defer sim.Close() - + _, tx, contract, err := DeployNewErrors(user, sim) if err != nil { t.Fatal(err) @@ -1914,12 +1918,12 @@ var bindTests = []struct { name: `ConstructorWithStructParam`, contract: ` pragma solidity >=0.8.0 <0.9.0; - + contract ConstructorWithStructParam { struct StructType { uint256 field; } - + constructor(StructType memory st) {} } `, @@ -1947,7 +1951,7 @@ var bindTests = []struct { t.Fatalf("DeployConstructorWithStructParam() got err %v; want nil err", err) } sim.Commit() - + if _, err = bind.WaitDeployed(nil, sim, tx); err != nil { t.Logf("Deployment tx: %+v", tx) t.Errorf("bind.WaitDeployed(nil, %T, ) got err %v; want nil err", sim, err) @@ -1995,7 +1999,7 @@ var bindTests = []struct { t.Fatalf("DeployNameConflict() got err %v; want nil err", err) } sim.Commit() - + if _, err = bind.WaitDeployed(nil, sim, tx); err != nil { t.Logf("Deployment tx: %+v", tx) t.Errorf("bind.WaitDeployed(nil, %T, ) got err %v; want nil err", sim, err) @@ -2039,6 +2043,118 @@ var bindTests = []struct { } `, }, + // Test bind with internal transaction + { + "InternalTransaction", + ` + // SPDX-License-Identifier: MIT + pragma solidity ^0.8.17; + + contract SendEther { + address payable private _payableReceiver; + bool private _payble; + + event PayableReceiver(address payable indexed payableReceiver); + + function sendViaSend(address payable _to) public payable { + // Send returns a boolean value indicating success or failure. + // This function is not recommended for sending Ether. + bool sent = _to.send(msg.value / 2); + require(sent, "Failed to send Ether"); + if (_payble) { + bool sendBack = _payableReceiver.send(msg.value / 2); + require(sendBack, "Failed to send Ether to last caller"); + } + _payableReceiver = _to; + _payble = true; + emit PayableReceiver(_payableReceiver); + } + } + `, + []string{`608060405234801561001057600080fd5b50610482806100206000396000f3fe60806040526004361061001e5760003560e01c806374be480614610023575b600080fd5b61003d600480360381019061003891906102a6565b61003f565b005b60008173ffffffffffffffffffffffffffffffffffffffff166108fc600234610068919061030c565b9081150290604051600060405180830381858888f193505050509050806100c4576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016100bb9061039a565b60405180910390fd5b600060149054906101000a900460ff16156101815760008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc600234610123919061030c565b9081150290604051600060405180830381858888f1935050505090508061017f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101769061042c565b60405180910390fd5b505b816000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001600060146101000a81548160ff02191690831515021790555060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167fe38bfb4c48fbc55ba234e5160b339e3df541af75d181a209e1fcb1a2e87a41da60405160405180910390a25050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061027382610248565b9050919050565b61028381610268565b811461028e57600080fd5b50565b6000813590506102a08161027a565b92915050565b6000602082840312156102bc576102bb610243565b5b60006102ca84828501610291565b91505092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b6000610317826102d3565b9150610322836102d3565b925082610332576103316102dd565b5b828204905092915050565b600082825260208201905092915050565b7f4661696c656420746f2073656e64204574686572000000000000000000000000600082015250565b600061038460148361033d565b915061038f8261034e565b602082019050919050565b600060208201905081810360008301526103b381610377565b9050919050565b7f4661696c656420746f2073656e6420457468657220746f206c6173742063616c60008201527f6c65720000000000000000000000000000000000000000000000000000000000602082015250565b600061041660238361033d565b9150610421826103ba565b604082019050919050565b6000602082019050818103600083015261044581610409565b905091905056fea264697066735822122084dd753928b50f1ed62e69a35acc1e28c20767e3139b6cf23f572d60a6c114a864736f6c63430008110033`}, + []string{` + [ + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address payable", + "name": "payableReceiver", + "type": "address" + } + ], + "name": "PayableReceiver", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "address payable", + "name": "_to", + "type": "address" + } + ], + "name": "sendViaSend", + "outputs": [], + "stateMutability": "payable", + "type": "function" + } + ] + `, + }, + ` + "math/big" + + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/crypto" + `, + ` + // Generate a new random account and a funded simulator + key, _ := crypto.GenerateKey() + auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337)) + + receivers := []*bind.TransactOpts{} + + for i := 0; i < 2; i++ { + // Generate a new random account and a funded simulator + k, _ := crypto.GenerateKey() + receiver, _ := bind.NewKeyedTransactorWithChainID(k, big.NewInt(1337)) + receivers = append(receivers, receiver) + } + + sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000000000)}}, 10000000) + defer sim.Close() + + // Deploy a structs method invoker contract and execute its default method + _, _, structs, err := DeployInternalTransaction(auth, sim) + if err != nil { + t.Fatalf("Failed to deploy defaulter contract: %v", err) + } + sim.Commit() + for _, receiver := range receivers { + opts := &bind.TransactOpts{From: auth.From, Value: big.NewInt(100000000000000), Signer: auth.Signer} + if _, err := structs.SendViaSend(opts, receiver.From); err != nil { + t.Fatalf("Failed to invoke SendViaSend method: %v", err) + } + } + `, + nil, + nil, + nil, + nil, + }, +} + +func getRuntimePackagePath(rootStep int) string { + _, filename, _, _ := runtime.Caller(1) + rootPath := filename + for i := 0; i < rootStep; i++ { + rootPath = path.Dir(rootPath) + } + return rootPath } // Tests that packages generated by the binder can be successfully compiled and @@ -2050,9 +2166,9 @@ func TestGolangBindings(t *testing.T) { t.Skip("go sdk not found for testing") } // Create a temporary workspace for the test suite - ws := t.TempDir() - + ws := filepath.Join(getRuntimePackagePath(4), "local") // for local debug pkg := filepath.Join(ws, "bindtest") + os.RemoveAll(pkg) if err := os.MkdirAll(pkg, 0700); err != nil { t.Fatalf("failed to create package: %v", err) } diff --git a/contracts/internaltransaction/contracts/.gitkeep b/contracts/internaltransaction/contracts/.gitkeep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/contracts/internaltransaction/contracts/Ether.sol b/contracts/internaltransaction/contracts/Ether.sol new file mode 100644 index 000000000000..ea80ef0da741 --- /dev/null +++ b/contracts/internaltransaction/contracts/Ether.sol @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.17; + +contract SendEther { + address payable private _payableReceiver; + bool private _payble; + + event PayableReceiver(address payable indexed payableReceiver); + + function sendViaSend(address payable _to) public payable { + // Send returns a boolean value indicating success or failure. + // This function is not recommended for sending Ether. + bool sent = _to.send(msg.value / 2); + require(sent, "Failed to send Ether"); + if (_payble) { + bool sendBack = _payableReceiver.send(msg.value / 2); + require(sendBack, "Failed to send Ether to last caller"); + } + _payableReceiver = _to; + _payble = true; + emit PayableReceiver(_payableReceiver); + } +} diff --git a/contracts/internaltransaction/migrations/.gitkeep b/contracts/internaltransaction/migrations/.gitkeep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/contracts/internaltransaction/test/.gitkeep b/contracts/internaltransaction/test/.gitkeep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/contracts/internaltransaction/truffle-config.js b/contracts/internaltransaction/truffle-config.js new file mode 100644 index 000000000000..fd5a79ce4199 --- /dev/null +++ b/contracts/internaltransaction/truffle-config.js @@ -0,0 +1,141 @@ +/** + * Use this file to configure your truffle project. It's seeded with some + * common settings for different networks and features like migrations, + * compilation, and testing. Uncomment the ones you need or modify + * them to suit your project as necessary. + * + * More information about configuration can be found at: + * + * https://trufflesuite.com/docs/truffle/reference/configuration + * + * Hands-off deployment with Infura + * -------------------------------- + * + * Do you have a complex application that requires lots of transactions to deploy? + * Use this approach to make deployment a breeze ๐Ÿ–๏ธ: + * + * Infura deployment needs a wallet provider (like @truffle/hdwallet-provider) + * to sign transactions before they're sent to a remote public node. + * Infura accounts are available for free at ๐Ÿ”: https://infura.io/register + * + * You'll need a mnemonic - the twelve word phrase the wallet uses to generate + * public/private key pairs. You can store your secrets ๐Ÿค in a .env file. + * In your project root, run `$ npm install dotenv`. + * Create .env (which should be .gitignored) and declare your MNEMONIC + * and Infura PROJECT_ID variables inside. + * For example, your .env file will have the following structure: + * + * MNEMONIC = + * PROJECT_ID = + * + * Deployment with Truffle Dashboard (Recommended for best security practice) + * -------------------------------------------------------------------------- + * + * Are you concerned about security and minimizing rekt status ๐Ÿค”? + * Use this method for best security: + * + * Truffle Dashboard lets you review transactions in detail, and leverages + * MetaMask for signing, so there's no need to copy-paste your mnemonic. + * More details can be found at ๐Ÿ”Ž: + * + * https://trufflesuite.com/docs/truffle/getting-started/using-the-truffle-dashboard/ + */ + +// require('dotenv').config(); +// const { MNEMONIC, PROJECT_ID } = process.env; + +// const HDWalletProvider = require('@truffle/hdwallet-provider'); + +module.exports = { + /** + * Networks define how you connect to your ethereum client and let you set the + * defaults web3 uses to send transactions. If you don't specify one truffle + * will spin up a managed Ganache instance for you on port 9545 when you + * run `develop` or `test`. You can ask a truffle command to use a specific + * network from the command line, e.g + * + * $ truffle test --network + */ + + networks: { + // Useful for testing. The `development` name is special - truffle uses it by default + // if it's defined here and no other network is specified at the command line. + // You should run a client (like ganache, geth, or parity) in a separate terminal + // tab if you use this network and you must also set the `host`, `port` and `network_id` + // options below to some value. + // + // development: { + // host: "127.0.0.1", // Localhost (default: none) + // port: 8545, // Standard Ethereum port (default: none) + // network_id: "*", // Any network (default: none) + // }, + // + // An additional network, but with some advanced optionsโ€ฆ + // advanced: { + // port: 8777, // Custom port + // network_id: 1342, // Custom network + // gas: 8500000, // Gas sent with each transaction (default: ~6700000) + // gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei) + // from:
, // Account to send transactions from (default: accounts[0]) + // websocket: true // Enable EventEmitter interface for web3 (default: false) + // }, + // + // Useful for deploying to a public network. + // Note: It's important to wrap the provider as a function to ensure truffle uses a new provider every time. + // goerli: { + // provider: () => new HDWalletProvider(MNEMONIC, `https://goerli.infura.io/v3/${PROJECT_ID}`), + // network_id: 5, // Goerli's id + // confirmations: 2, // # of confirmations to wait between deployments. (default: 0) + // timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50) + // skipDryRun: true // Skip dry run before migrations? (default: false for public nets ) + // }, + // + // Useful for private networks + // private: { + // provider: () => new HDWalletProvider(MNEMONIC, `https://network.io`), + // network_id: 2111, // This network is yours, in the cloud. + // production: true // Treats this network as if it was a public net. (default: false) + // } + }, + + // Set default mocha options here, use special reporters, etc. + mocha: { + // timeout: 100000 + }, + + // Configure your compilers + compilers: { + solc: { + version: "0.8.17" // Fetch exact version from solc-bin (default: truffle's version) + // docker: true, // Use "0.5.1" you've installed locally with docker (default: false) + // settings: { // See the solidity docs for advice about optimization and evmVersion + // optimizer: { + // enabled: false, + // runs: 200 + // }, + // evmVersion: "byzantium" + // } + } + } + + // Truffle DB is currently disabled by default; to enable it, change enabled: + // false to enabled: true. The default storage location can also be + // overridden by specifying the adapter settings, as shown in the commented code below. + // + // NOTE: It is not possible to migrate your contracts to truffle DB and you should + // make a backup of your artifacts to a safe location before enabling this feature. + // + // After you backed up your artifacts you can utilize db by running migrate as follows: + // $ truffle migrate --reset --compile-all + // + // db: { + // enabled: false, + // host: "127.0.0.1", + // adapter: { + // name: "indexeddb", + // settings: { + // directory: ".db" + // } + // } + // } +};