1717package tracers
1818
1919import (
20+ "crypto/ecdsa"
21+ "crypto/rand"
2022 "encoding/json"
2123 "io/ioutil"
2224 "math/big"
@@ -31,7 +33,9 @@ import (
3133 "github.com/ethereum/go-ethereum/core"
3234 "github.com/ethereum/go-ethereum/core/types"
3335 "github.com/ethereum/go-ethereum/core/vm"
36+ "github.com/ethereum/go-ethereum/crypto"
3437 "github.com/ethereum/go-ethereum/ethdb"
38+ "github.com/ethereum/go-ethereum/params"
3539 "github.com/ethereum/go-ethereum/rlp"
3640 "github.com/ethereum/go-ethereum/tests"
3741)
@@ -116,6 +120,83 @@ type callTracerTest struct {
116120 Result * callTrace `json:"result"`
117121}
118122
123+ func TestPrestateTracerCreate2 (t * testing.T ) {
124+ unsigned_tx := types .NewTransaction (1 , common .HexToAddress ("0x00000000000000000000000000000000deadbeef" ),
125+ new (big.Int ), 5000000 , big .NewInt (1 ), []byte {})
126+
127+ privateKeyECDSA , err := ecdsa .GenerateKey (crypto .S256 (), rand .Reader )
128+ if err != nil {
129+ t .Fatalf ("err %v" , err )
130+ }
131+ signer := types .NewEIP155Signer (big .NewInt (1 ))
132+ tx , err := types .SignTx (unsigned_tx , signer , privateKeyECDSA )
133+ if err != nil {
134+ t .Fatalf ("err %v" , err )
135+ }
136+ /**
137+ This comes from one of the test-vectors on the Skinny Create2 - EIP
138+
139+ address 0x00000000000000000000000000000000deadbeef
140+ salt 0x00000000000000000000000000000000000000000000000000000000cafebabe
141+ init_code 0xdeadbeef
142+ gas (assuming no mem expansion): 32006
143+ result: 0x60f3f640a8508fC6a86d45DF051962668E1e8AC7
144+ */
145+ origin , _ := signer .Sender (tx )
146+ context := vm.Context {
147+ CanTransfer : core .CanTransfer ,
148+ Transfer : core .Transfer ,
149+ Origin : origin ,
150+ Coinbase : common.Address {},
151+ BlockNumber : new (big.Int ).SetUint64 (8000000 ),
152+ Time : new (big.Int ).SetUint64 (5 ),
153+ Difficulty : big .NewInt (0x30000 ),
154+ GasLimit : uint64 (6000000 ),
155+ GasPrice : big .NewInt (1 ),
156+ }
157+ alloc := core.GenesisAlloc {}
158+ // The code pushes 'deadbeef' into memory, then the other params, and calls CREATE2, then returns
159+ // the address
160+ alloc [common .HexToAddress ("0x00000000000000000000000000000000deadbeef" )] = core.GenesisAccount {
161+ Nonce : 1 ,
162+ Code : hexutil .MustDecode ("0x63deadbeef60005263cafebabe6004601c6000F560005260206000F3" ),
163+ Balance : big .NewInt (1 ),
164+ }
165+ alloc [origin ] = core.GenesisAccount {
166+ Nonce : 1 ,
167+ Code : []byte {},
168+ Balance : big .NewInt (500000000000000 ),
169+ }
170+ statedb := tests .MakePreState (ethdb .NewMemDatabase (), alloc )
171+ // Create the tracer, the EVM environment and run it
172+ tracer , err := New ("prestateTracer" )
173+ if err != nil {
174+ t .Fatalf ("failed to create call tracer: %v" , err )
175+ }
176+ evm := vm .NewEVM (context , statedb , params .MainnetChainConfig , vm.Config {Debug : true , Tracer : tracer })
177+
178+ msg , err := tx .AsMessage (signer )
179+ if err != nil {
180+ t .Fatalf ("failed to prepare transaction for tracing: %v" , err )
181+ }
182+ st := core .NewStateTransition (evm , msg , new (core.GasPool ).AddGas (tx .Gas ()))
183+ if _ , _ , _ , err = st .TransitionDb (); err != nil {
184+ t .Fatalf ("failed to execute transaction: %v" , err )
185+ }
186+ // Retrieve the trace result and compare against the etalon
187+ res , err := tracer .GetResult ()
188+ if err != nil {
189+ t .Fatalf ("failed to retrieve trace result: %v" , err )
190+ }
191+ ret := make (map [string ]interface {})
192+ if err := json .Unmarshal (res , & ret ); err != nil {
193+ t .Fatalf ("failed to unmarshal trace result: %v" , err )
194+ }
195+ if _ , has := ret ["0x60f3f640a8508fc6a86d45df051962668e1e8ac7" ]; ! has {
196+ t .Fatalf ("Expected 0x60f3f640a8508fc6a86d45df051962668e1e8ac7 in result" )
197+ }
198+ }
199+
119200// Iterates over all the input-output datasets in the tracer test harness and
120201// runs the JavaScript tracers against them.
121202func TestCallTracer (t * testing.T ) {
@@ -185,8 +266,9 @@ func TestCallTracer(t *testing.T) {
185266 if err := json .Unmarshal (res , ret ); err != nil {
186267 t .Fatalf ("failed to unmarshal trace result: %v" , err )
187268 }
269+
188270 if ! reflect .DeepEqual (ret , test .Result ) {
189- t .Fatalf ("trace mismatch: have %+v, want %+v" , ret , test .Result )
271+ t .Fatalf ("trace mismatch: \n have %+v\n want %+v" , ret , test .Result )
190272 }
191273 })
192274 }
0 commit comments