github.com/ava-labs/subnet-evm@v0.6.4/tests/state_test_util.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2015 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package tests 28 29 import ( 30 "encoding/hex" 31 "encoding/json" 32 "errors" 33 "fmt" 34 "math/big" 35 "strconv" 36 "strings" 37 38 "github.com/ava-labs/subnet-evm/core" 39 "github.com/ava-labs/subnet-evm/core/rawdb" 40 "github.com/ava-labs/subnet-evm/core/state" 41 "github.com/ava-labs/subnet-evm/core/state/snapshot" 42 "github.com/ava-labs/subnet-evm/core/types" 43 "github.com/ava-labs/subnet-evm/core/vm" 44 "github.com/ava-labs/subnet-evm/params" 45 "github.com/ava-labs/subnet-evm/trie" 46 "github.com/ava-labs/subnet-evm/trie/triedb/hashdb" 47 "github.com/ava-labs/subnet-evm/trie/triedb/pathdb" 48 "github.com/ethereum/go-ethereum/common" 49 "github.com/ethereum/go-ethereum/common/hexutil" 50 "github.com/ethereum/go-ethereum/common/math" 51 "github.com/ethereum/go-ethereum/crypto" 52 "github.com/ethereum/go-ethereum/ethdb" 53 "github.com/ethereum/go-ethereum/rlp" 54 "golang.org/x/crypto/sha3" 55 ) 56 57 // StateTest checks transaction processing without block context. 58 // See https://github.com/ethereum/EIPs/issues/176 for the test format specification. 59 type StateTest struct { 60 json stJSON 61 } 62 63 // StateSubtest selects a specific configuration of a General State Test. 64 type StateSubtest struct { 65 Fork string 66 Index int 67 } 68 69 func (t *StateTest) UnmarshalJSON(in []byte) error { 70 return json.Unmarshal(in, &t.json) 71 } 72 73 type stJSON struct { 74 Env stEnv `json:"env"` 75 Pre core.GenesisAlloc `json:"pre"` 76 Tx stTransaction `json:"transaction"` 77 Out hexutil.Bytes `json:"out"` 78 Post map[string][]stPostState `json:"post"` 79 } 80 81 type stPostState struct { 82 Root common.UnprefixedHash `json:"hash"` 83 Logs common.UnprefixedHash `json:"logs"` 84 TxBytes hexutil.Bytes `json:"txbytes"` 85 ExpectException string `json:"expectException"` 86 Indexes struct { 87 Data int `json:"data"` 88 Gas int `json:"gas"` 89 Value int `json:"value"` 90 } 91 } 92 93 //go:generate go run github.com/fjl/gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go 94 type stEnv struct { 95 Coinbase common.Address `json:"currentCoinbase" gencodec:"required"` 96 Difficulty *big.Int `json:"currentDifficulty" gencodec:"required"` 97 Random *big.Int `json:"currentRandom" gencodec:"optional"` 98 GasLimit uint64 `json:"currentGasLimit" gencodec:"required"` 99 Number uint64 `json:"currentNumber" gencodec:"required"` 100 Timestamp uint64 `json:"currentTimestamp" gencodec:"required"` 101 BaseFee *big.Int `json:"currentBaseFee" gencodec:"optional"` 102 } 103 104 //go:generate go run github.com/fjl/gencodec -type stTransaction -field-override stTransactionMarshaling -out gen_sttransaction.go 105 type stTransaction struct { 106 GasPrice *big.Int `json:"gasPrice"` 107 MaxFeePerGas *big.Int `json:"maxFeePerGas"` 108 MaxPriorityFeePerGas *big.Int `json:"maxPriorityFeePerGas"` 109 Nonce uint64 `json:"nonce"` 110 To string `json:"to"` 111 Data []string `json:"data"` 112 AccessLists []*types.AccessList `json:"accessLists,omitempty"` 113 GasLimit []uint64 `json:"gasLimit"` 114 Value []string `json:"value"` 115 PrivateKey []byte `json:"secretKey"` 116 Sender *common.Address `json:"sender"` 117 BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"` 118 BlobGasFeeCap *big.Int `json:"maxFeePerBlobGas,omitempty"` 119 } 120 121 // nolint: unused 122 type stTransactionMarshaling struct { 123 GasPrice *math.HexOrDecimal256 124 MaxFeePerGas *math.HexOrDecimal256 125 MaxPriorityFeePerGas *math.HexOrDecimal256 126 Nonce math.HexOrDecimal64 127 GasLimit []math.HexOrDecimal64 128 PrivateKey hexutil.Bytes 129 BlobGasFeeCap *math.HexOrDecimal256 130 } 131 132 // GetChainConfig takes a fork definition and returns a chain config. 133 // The fork definition can be 134 // - a plain forkname, e.g. `Byzantium`, 135 // - a fork basename, and a list of EIPs to enable; e.g. `Byzantium+1884+1283`. 136 func GetChainConfig(forkString string) (baseConfig *params.ChainConfig, eips []int, err error) { 137 var ( 138 splitForks = strings.Split(forkString, "+") 139 ok bool 140 baseName, eipsStrings = splitForks[0], splitForks[1:] 141 ) 142 143 // NOTE: this is added to support mapping geth fork names to 144 // subnet-evm fork names. 145 forkAliases := map[string]string{ 146 "Berlin": "Pre-SubnetEVM", 147 "London": "SubnetEVM", 148 "ArrowGlacier": "SubnetEVM", 149 "GrayGlacier": "SubnetEVM", 150 "Merge": "SubnetEVM", 151 } 152 if alias, ok := forkAliases[baseName]; ok { 153 baseName = alias 154 } 155 156 if baseConfig, ok = Forks[baseName]; !ok { 157 return nil, nil, UnsupportedForkError{baseName} 158 } 159 for _, eip := range eipsStrings { 160 if eipNum, err := strconv.Atoi(eip); err != nil { 161 return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum) 162 } else { 163 if !vm.ValidEip(eipNum) { 164 return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum) 165 } 166 eips = append(eips, eipNum) 167 } 168 } 169 return baseConfig, eips, nil 170 } 171 172 // Subtests returns all valid subtests of the test. 173 func (t *StateTest) Subtests() []StateSubtest { 174 var sub []StateSubtest 175 for fork, pss := range t.json.Post { 176 for i := range pss { 177 sub = append(sub, StateSubtest{fork, i}) 178 } 179 } 180 return sub 181 } 182 183 // checkError checks if the error returned by the state transition matches any expected error. 184 // A failing expectation returns a wrapped version of the original error, if any, 185 // or a new error detailing the failing expectation. 186 // This function does not return or modify the original error, it only evaluates and returns expectations for the error. 187 func (t *StateTest) checkError(subtest StateSubtest, err error) error { 188 expectedError := t.json.Post[subtest.Fork][subtest.Index].ExpectException 189 if err == nil && expectedError == "" { 190 return nil 191 } 192 if err == nil && expectedError != "" { 193 return fmt.Errorf("expected error %q, got no error", expectedError) 194 } 195 if err != nil && expectedError == "" { 196 return fmt.Errorf("unexpected error: %w", err) 197 } 198 if err != nil && expectedError != "" { 199 // Ignore expected errors (TODO MariusVanDerWijden check error string) 200 return nil 201 } 202 return nil 203 } 204 205 // Run executes a specific subtest and verifies the post-state and logs 206 func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bool, scheme string, postCheck func(err error, snaps *snapshot.Tree, state *state.StateDB)) (result error) { 207 triedb, snaps, statedb, root, err := t.RunNoVerify(subtest, vmconfig, snapshotter, scheme) 208 209 // Invoke the callback at the end of function for further analysis. 210 defer func() { 211 postCheck(result, snaps, statedb) 212 213 if triedb != nil { 214 triedb.Close() 215 } 216 }() 217 checkedErr := t.checkError(subtest, err) 218 if checkedErr != nil { 219 return checkedErr 220 } 221 // The error has been checked; if it was unexpected, it's already returned. 222 if err != nil { 223 // Here, an error exists but it was expected. 224 // We do not check the post state or logs. 225 return nil 226 } 227 post := t.json.Post[subtest.Fork][subtest.Index] 228 // N.B: We need to do this in a two-step process, because the first Commit takes care 229 // of self-destructs, and we need to touch the coinbase _after_ it has potentially self-destructed. 230 if root != common.Hash(post.Root) { 231 return fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root) 232 } 233 if logs := rlpHash(statedb.Logs()); logs != common.Hash(post.Logs) { 234 return fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs) 235 } 236 statedb, _ = state.New(root, statedb.Database(), snaps) 237 return nil 238 } 239 240 // RunNoVerify runs a specific subtest and returns the statedb and post-state root 241 func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapshotter bool, scheme string) (*trie.Database, *snapshot.Tree, *state.StateDB, common.Hash, error) { 242 config, eips, err := GetChainConfig(subtest.Fork) 243 if err != nil { 244 return nil, nil, nil, common.Hash{}, UnsupportedForkError{subtest.Fork} 245 } 246 vmconfig.ExtraEips = eips 247 248 block := t.genesis(config).ToBlock() 249 triedb, snaps, statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, snapshotter, scheme) 250 251 var baseFee *big.Int 252 if config.IsSubnetEVM(0) { 253 baseFee = t.json.Env.BaseFee 254 if baseFee == nil { 255 // Retesteth uses `0x10` for genesis baseFee. Therefore, it defaults to 256 // parent - 2 : 0xa as the basefee for 'this' context. 257 baseFee = big.NewInt(0x0a) 258 } 259 } 260 post := t.json.Post[subtest.Fork][subtest.Index] 261 msg, err := t.json.Tx.toMessage(post, baseFee) 262 if err != nil { 263 triedb.Close() 264 return nil, nil, nil, common.Hash{}, err 265 } 266 267 // Try to recover tx with current signer 268 if len(post.TxBytes) != 0 { 269 var ttx types.Transaction 270 err := ttx.UnmarshalBinary(post.TxBytes) 271 if err != nil { 272 triedb.Close() 273 return nil, nil, nil, common.Hash{}, err 274 } 275 276 if _, err := types.Sender(types.LatestSigner(config), &ttx); err != nil { 277 triedb.Close() 278 return nil, nil, nil, common.Hash{}, err 279 } 280 } 281 282 // Prepare the EVM. 283 txContext := core.NewEVMTxContext(msg) 284 context := core.NewEVMBlockContext(block.Header(), nil, &t.json.Env.Coinbase) 285 context.GetHash = vmTestBlockHash 286 context.BaseFee = baseFee 287 if config.IsSubnetEVM(0) && t.json.Env.Random != nil { 288 context.Difficulty = big.NewInt(0) 289 } 290 evm := vm.NewEVM(context, txContext, statedb, config, vmconfig) 291 292 // Execute the message. 293 snapshot := statedb.Snapshot() 294 gaspool := new(core.GasPool) 295 gaspool.AddGas(block.GasLimit()) 296 _, err = core.ApplyMessage(evm, msg, gaspool) 297 if err != nil { 298 statedb.RevertToSnapshot(snapshot) 299 } 300 // Add 0-value mining reward. This only makes a difference in the cases 301 // where 302 // - the coinbase self-destructed, or 303 // - there are only 'bad' transactions, which aren't executed. In those cases, 304 // the coinbase gets no txfee, so isn't created, and thus needs to be touched 305 statedb.AddBalance(block.Coinbase(), new(big.Int)) 306 // Commit block 307 root, _ := statedb.Commit(block.NumberU64(), config.IsEIP158(block.Number()), false) 308 return triedb, snaps, statedb, root, err 309 } 310 311 func MakePreState(db ethdb.Database, accounts core.GenesisAlloc, snapshotter bool, scheme string) (*trie.Database, *snapshot.Tree, *state.StateDB) { 312 tconf := &trie.Config{Preimages: true} 313 if scheme == rawdb.HashScheme { 314 tconf.HashDB = hashdb.Defaults 315 } else { 316 tconf.PathDB = pathdb.Defaults 317 } 318 triedb := trie.NewDatabase(db, tconf) 319 sdb := state.NewDatabaseWithNodeDB(db, triedb) 320 statedb, _ := state.New(types.EmptyRootHash, sdb, nil) 321 for addr, a := range accounts { 322 statedb.SetCode(addr, a.Code) 323 statedb.SetNonce(addr, a.Nonce) 324 statedb.SetBalance(addr, a.Balance) 325 for k, v := range a.Storage { 326 statedb.SetState(addr, k, v) 327 } 328 } 329 // Commit and re-open to start with a clean state. 330 root, _ := statedb.Commit(0, false, false) 331 332 var snaps *snapshot.Tree 333 if snapshotter { 334 snapconfig := snapshot.Config{ 335 CacheSize: 1, 336 NoBuild: false, 337 AsyncBuild: false, 338 SkipVerify: true, 339 } 340 snaps, _ = snapshot.New(snapconfig, db, triedb, common.Hash{}, root) 341 } 342 statedb, _ = state.New(root, sdb, snaps) 343 return triedb, snaps, statedb 344 } 345 346 func (t *StateTest) genesis(config *params.ChainConfig) *core.Genesis { 347 genesis := &core.Genesis{ 348 Config: config, 349 Coinbase: t.json.Env.Coinbase, 350 Difficulty: t.json.Env.Difficulty, 351 GasLimit: t.json.Env.GasLimit, 352 Number: t.json.Env.Number, 353 Timestamp: t.json.Env.Timestamp, 354 Alloc: t.json.Pre, 355 } 356 if t.json.Env.Random != nil { 357 // Post-Merge 358 genesis.Mixhash = common.BigToHash(t.json.Env.Random) 359 genesis.Difficulty = big.NewInt(0) 360 } 361 return genesis 362 } 363 364 func (tx *stTransaction) toMessage(ps stPostState, baseFee *big.Int) (*core.Message, error) { 365 var from common.Address 366 // If 'sender' field is present, use that 367 if tx.Sender != nil { 368 from = *tx.Sender 369 } else if len(tx.PrivateKey) > 0 { 370 // Derive sender from private key if needed. 371 key, err := crypto.ToECDSA(tx.PrivateKey) 372 if err != nil { 373 return nil, fmt.Errorf("invalid private key: %v", err) 374 } 375 from = crypto.PubkeyToAddress(key.PublicKey) 376 } 377 // Parse recipient if present. 378 var to *common.Address 379 if tx.To != "" { 380 to = new(common.Address) 381 if err := to.UnmarshalText([]byte(tx.To)); err != nil { 382 return nil, fmt.Errorf("invalid to address: %v", err) 383 } 384 } 385 386 // Get values specific to this post state. 387 if ps.Indexes.Data > len(tx.Data) { 388 return nil, fmt.Errorf("tx data index %d out of bounds", ps.Indexes.Data) 389 } 390 if ps.Indexes.Value > len(tx.Value) { 391 return nil, fmt.Errorf("tx value index %d out of bounds", ps.Indexes.Value) 392 } 393 if ps.Indexes.Gas > len(tx.GasLimit) { 394 return nil, fmt.Errorf("tx gas limit index %d out of bounds", ps.Indexes.Gas) 395 } 396 dataHex := tx.Data[ps.Indexes.Data] 397 valueHex := tx.Value[ps.Indexes.Value] 398 gasLimit := tx.GasLimit[ps.Indexes.Gas] 399 // Value, Data hex encoding is messy: https://github.com/ethereum/tests/issues/203 400 value := new(big.Int) 401 if valueHex != "0x" { 402 v, ok := math.ParseBig256(valueHex) 403 if !ok { 404 return nil, fmt.Errorf("invalid tx value %q", valueHex) 405 } 406 value = v 407 } 408 data, err := hex.DecodeString(strings.TrimPrefix(dataHex, "0x")) 409 if err != nil { 410 return nil, fmt.Errorf("invalid tx data %q", dataHex) 411 } 412 var accessList types.AccessList 413 if tx.AccessLists != nil && tx.AccessLists[ps.Indexes.Data] != nil { 414 accessList = *tx.AccessLists[ps.Indexes.Data] 415 } 416 // If baseFee provided, set gasPrice to effectiveGasPrice. 417 gasPrice := tx.GasPrice 418 if baseFee != nil { 419 if tx.MaxFeePerGas == nil { 420 tx.MaxFeePerGas = gasPrice 421 } 422 if tx.MaxFeePerGas == nil { 423 tx.MaxFeePerGas = new(big.Int) 424 } 425 if tx.MaxPriorityFeePerGas == nil { 426 tx.MaxPriorityFeePerGas = tx.MaxFeePerGas 427 } 428 gasPrice = math.BigMin(new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee), 429 tx.MaxFeePerGas) 430 } 431 if gasPrice == nil { 432 return nil, errors.New("no gas price provided") 433 } 434 435 msg := &core.Message{ 436 From: from, 437 To: to, 438 Nonce: tx.Nonce, 439 Value: value, 440 GasLimit: gasLimit, 441 GasPrice: gasPrice, 442 GasFeeCap: tx.MaxFeePerGas, 443 GasTipCap: tx.MaxPriorityFeePerGas, 444 Data: data, 445 AccessList: accessList, 446 BlobHashes: tx.BlobVersionedHashes, 447 BlobGasFeeCap: tx.BlobGasFeeCap, 448 } 449 return msg, nil 450 } 451 452 func rlpHash(x interface{}) (h common.Hash) { 453 hw := sha3.NewLegacyKeccak256() 454 rlp.Encode(hw, x) 455 hw.Sum(h[:0]) 456 return h 457 } 458 459 func vmTestBlockHash(n uint64) common.Hash { 460 return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String()))) 461 }