github.com/cgcardona/r-subnet-evm@v0.1.5/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/json" 31 "fmt" 32 "math/big" 33 "strconv" 34 "strings" 35 36 "github.com/cgcardona/r-subnet-evm/core" 37 "github.com/cgcardona/r-subnet-evm/core/state" 38 "github.com/cgcardona/r-subnet-evm/core/state/snapshot" 39 "github.com/cgcardona/r-subnet-evm/core/types" 40 "github.com/cgcardona/r-subnet-evm/core/vm" 41 "github.com/cgcardona/r-subnet-evm/ethdb" 42 "github.com/cgcardona/r-subnet-evm/params" 43 "github.com/ethereum/go-ethereum/common" 44 "github.com/ethereum/go-ethereum/common/hexutil" 45 ) 46 47 // StateTest checks transaction processing without block context. 48 // See https://github.com/ethereum/EIPs/issues/176 for the test format specification. 49 type StateTest struct { 50 json stJSON 51 } 52 53 // StateSubtest selects a specific configuration of a General State Test. 54 type StateSubtest struct { 55 Fork string 56 Index int 57 } 58 59 func (t *StateTest) UnmarshalJSON(in []byte) error { 60 return json.Unmarshal(in, &t.json) 61 } 62 63 type stJSON struct { 64 Env stEnv `json:"env"` 65 Pre core.GenesisAlloc `json:"pre"` 66 Tx stTransaction `json:"transaction"` 67 Out hexutil.Bytes `json:"out"` 68 Post map[string][]stPostState `json:"post"` 69 } 70 71 type stPostState struct { 72 Root common.UnprefixedHash `json:"hash"` 73 Logs common.UnprefixedHash `json:"logs"` 74 TxBytes hexutil.Bytes `json:"txbytes"` 75 ExpectException string `json:"expectException"` 76 Indexes struct { 77 Data int `json:"data"` 78 Gas int `json:"gas"` 79 Value int `json:"value"` 80 } 81 } 82 83 //go:generate go run github.com/fjl/gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go 84 type stEnv struct { 85 Coinbase common.Address `json:"currentCoinbase" gencodec:"required"` 86 Difficulty *big.Int `json:"currentDifficulty" gencodec:"required"` 87 GasLimit uint64 `json:"currentGasLimit" gencodec:"required"` 88 Number uint64 `json:"currentNumber" gencodec:"required"` 89 Timestamp uint64 `json:"currentTimestamp" gencodec:"required"` 90 BaseFee *big.Int `json:"currentBaseFee" gencodec:"optional"` 91 } 92 93 //go:generate go run github.com/fjl/gencodec -type stTransaction -field-override stTransactionMarshaling -out gen_sttransaction.go 94 type stTransaction struct { 95 GasPrice *big.Int `json:"gasPrice"` 96 MaxFeePerGas *big.Int `json:"maxFeePerGas"` 97 MaxPriorityFeePerGas *big.Int `json:"maxPriorityFeePerGas"` 98 Nonce uint64 `json:"nonce"` 99 To string `json:"to"` 100 Data []string `json:"data"` 101 AccessLists []*types.AccessList `json:"accessLists,omitempty"` 102 GasLimit []uint64 `json:"gasLimit"` 103 Value []string `json:"value"` 104 PrivateKey []byte `json:"secretKey"` 105 } 106 107 // GetChainConfig takes a fork definition and returns a chain config. 108 // The fork definition can be 109 // - a plain forkname, e.g. `Byzantium`, 110 // - a fork basename, and a list of EIPs to enable; e.g. `Byzantium+1884+1283`. 111 func GetChainConfig(forkString string) (baseConfig *params.ChainConfig, eips []int, err error) { 112 var ( 113 splitForks = strings.Split(forkString, "+") 114 ok bool 115 baseName, eipsStrings = splitForks[0], splitForks[1:] 116 ) 117 if baseConfig, ok = Forks[baseName]; !ok { 118 return nil, nil, UnsupportedForkError{baseName} 119 } 120 for _, eip := range eipsStrings { 121 if eipNum, err := strconv.Atoi(eip); err != nil { 122 return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum) 123 } else { 124 if !vm.ValidEip(eipNum) { 125 return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum) 126 } 127 eips = append(eips, eipNum) 128 } 129 } 130 return baseConfig, eips, nil 131 } 132 133 // Subtests returns all valid subtests of the test. 134 func (t *StateTest) Subtests() []StateSubtest { 135 var sub []StateSubtest 136 for fork, pss := range t.json.Post { 137 for i := range pss { 138 sub = append(sub, StateSubtest{fork, i}) 139 } 140 } 141 return sub 142 } 143 144 func MakePreState(db ethdb.Database, accounts core.GenesisAlloc, snapshotter bool) (*snapshot.Tree, *state.StateDB) { 145 sdb := state.NewDatabase(db) 146 statedb, _ := state.New(common.Hash{}, sdb, nil) 147 for addr, a := range accounts { 148 statedb.SetCode(addr, a.Code) 149 statedb.SetNonce(addr, a.Nonce) 150 statedb.SetBalance(addr, a.Balance) 151 for k, v := range a.Storage { 152 statedb.SetState(addr, k, v) 153 } 154 } 155 // Commit and re-open to start with a clean state. 156 root, _ := statedb.Commit(false, false) 157 158 var snaps *snapshot.Tree 159 if snapshotter { 160 snaps, _ = snapshot.New(db, sdb.TrieDB(), 1, common.Hash{}, root, false, true, false) 161 } 162 statedb, _ = state.New(root, sdb, snaps) 163 return snaps, statedb 164 }