github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/tests/state_test_util.go (about) 1 // Copyright 2021 The adkgo Authors 2 // This file is part of the adkgo library (adapted for adkgo from go--ethereum v1.10.8). 3 // 4 // the adkgo library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // the adkgo library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the adkgo library. If not, see <http://www.gnu.org/licenses/>. 16 17 package tests 18 19 import ( 20 "encoding/hex" 21 "encoding/json" 22 "fmt" 23 "math/big" 24 "strconv" 25 "strings" 26 27 "github.com/aidoskuneen/adk-node/common" 28 "github.com/aidoskuneen/adk-node/common/hexutil" 29 "github.com/aidoskuneen/adk-node/common/math" 30 "github.com/aidoskuneen/adk-node/core" 31 "github.com/aidoskuneen/adk-node/core/rawdb" 32 "github.com/aidoskuneen/adk-node/core/state" 33 "github.com/aidoskuneen/adk-node/core/state/snapshot" 34 "github.com/aidoskuneen/adk-node/core/types" 35 "github.com/aidoskuneen/adk-node/core/vm" 36 "github.com/aidoskuneen/adk-node/crypto" 37 "github.com/aidoskuneen/adk-node/ethdb" 38 "github.com/aidoskuneen/adk-node/params" 39 "github.com/aidoskuneen/adk-node/rlp" 40 "golang.org/x/crypto/sha3" 41 ) 42 43 // StateTest checks transaction processing without block context. 44 // See https://github.com/ethereum/EIPs/issues/176 for the test format specification. 45 type StateTest struct { 46 json stJSON 47 } 48 49 // StateSubtest selects a specific configuration of a General State Test. 50 type StateSubtest struct { 51 Fork string 52 Index int 53 } 54 55 func (t *StateTest) UnmarshalJSON(in []byte) error { 56 return json.Unmarshal(in, &t.json) 57 } 58 59 type stJSON struct { 60 Env stEnv `json:"env"` 61 Pre core.GenesisAlloc `json:"pre"` 62 Tx stTransaction `json:"transaction"` 63 Out hexutil.Bytes `json:"out"` 64 Post map[string][]stPostState `json:"post"` 65 } 66 67 type stPostState struct { 68 Root common.UnprefixedHash `json:"hash"` 69 Logs common.UnprefixedHash `json:"logs"` 70 TxBytes hexutil.Bytes `json:"txbytes"` 71 ExpectException string `json:"expectException"` 72 Indexes struct { 73 Data int `json:"data"` 74 Gas int `json:"gas"` 75 Value int `json:"value"` 76 } 77 } 78 79 //go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go 80 81 type stEnv struct { 82 Coinbase common.Address `json:"currentCoinbase" gencodec:"required"` 83 Difficulty *big.Int `json:"currentDifficulty" gencodec:"required"` 84 GasLimit uint64 `json:"currentGasLimit" gencodec:"required"` 85 Number uint64 `json:"currentNumber" gencodec:"required"` 86 Timestamp uint64 `json:"currentTimestamp" gencodec:"required"` 87 BaseFee *big.Int `json:"currentBaseFee" gencodec:"optional"` 88 } 89 90 type stEnvMarshaling struct { 91 Coinbase common.UnprefixedAddress 92 Difficulty *math.HexOrDecimal256 93 GasLimit math.HexOrDecimal64 94 Number math.HexOrDecimal64 95 Timestamp math.HexOrDecimal64 96 BaseFee *math.HexOrDecimal256 97 } 98 99 //go:generate gencodec -type stTransaction -field-override stTransactionMarshaling -out gen_sttransaction.go 100 101 type stTransaction struct { 102 GasPrice *big.Int `json:"gasPrice"` 103 MaxFeePerGas *big.Int `json:"maxFeePerGas"` 104 MaxPriorityFeePerGas *big.Int `json:"maxPriorityFeePerGas"` 105 Nonce uint64 `json:"nonce"` 106 To string `json:"to"` 107 Data []string `json:"data"` 108 AccessLists []*types.AccessList `json:"accessLists,omitempty"` 109 GasLimit []uint64 `json:"gasLimit"` 110 Value []string `json:"value"` 111 PrivateKey []byte `json:"secretKey"` 112 } 113 114 type stTransactionMarshaling struct { 115 GasPrice *math.HexOrDecimal256 116 MaxFeePerGas *math.HexOrDecimal256 117 MaxPriorityFeePerGas *math.HexOrDecimal256 118 Nonce math.HexOrDecimal64 119 GasLimit []math.HexOrDecimal64 120 PrivateKey hexutil.Bytes 121 } 122 123 // GetChainConfig takes a fork definition and returns a chain config. 124 // The fork definition can be 125 // - a plain forkname, e.g. `Byzantium`, 126 // - a fork basename, and a list of EIPs to enable; e.g. `Byzantium+1884+1283`. 127 func GetChainConfig(forkString string) (baseConfig *params.ChainConfig, eips []int, err error) { 128 var ( 129 splitForks = strings.Split(forkString, "+") 130 ok bool 131 baseName, eipsStrings = splitForks[0], splitForks[1:] 132 ) 133 if baseConfig, ok = Forks[baseName]; !ok { 134 return nil, nil, UnsupportedForkError{baseName} 135 } 136 for _, eip := range eipsStrings { 137 if eipNum, err := strconv.Atoi(eip); err != nil { 138 return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum) 139 } else { 140 if !vm.ValidEip(eipNum) { 141 return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum) 142 } 143 eips = append(eips, eipNum) 144 } 145 } 146 return baseConfig, eips, nil 147 } 148 149 // Subtests returns all valid subtests of the test. 150 func (t *StateTest) Subtests() []StateSubtest { 151 var sub []StateSubtest 152 for fork, pss := range t.json.Post { 153 for i := range pss { 154 sub = append(sub, StateSubtest{fork, i}) 155 } 156 } 157 return sub 158 } 159 160 // Run executes a specific subtest and verifies the post-state and logs 161 func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bool) (*snapshot.Tree, *state.StateDB, error) { 162 snaps, statedb, root, err := t.RunNoVerify(subtest, vmconfig, snapshotter) 163 if err != nil { 164 return snaps, statedb, err 165 } 166 post := t.json.Post[subtest.Fork][subtest.Index] 167 // N.B: We need to do this in a two-step process, because the first Commit takes care 168 // of suicides, and we need to touch the coinbase _after_ it has potentially suicided. 169 if root != common.Hash(post.Root) { 170 return snaps, statedb, fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root) 171 } 172 if logs := rlpHash(statedb.Logs()); logs != common.Hash(post.Logs) { 173 return snaps, statedb, fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs) 174 } 175 return snaps, statedb, nil 176 } 177 178 // RunNoVerify runs a specific subtest and returns the statedb and post-state root 179 func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapshotter bool) (*snapshot.Tree, *state.StateDB, common.Hash, error) { 180 config, eips, err := GetChainConfig(subtest.Fork) 181 if err != nil { 182 return nil, nil, common.Hash{}, UnsupportedForkError{subtest.Fork} 183 } 184 vmconfig.ExtraEips = eips 185 block := t.genesis(config).ToBlock(nil) 186 snaps, statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, snapshotter) 187 188 var baseFee *big.Int 189 if config.IsLondon(new(big.Int)) { 190 baseFee = t.json.Env.BaseFee 191 if baseFee == nil { 192 // Retesteth uses `0x10` for genesis baseFee. Therefore, it defaults to 193 // parent - 2 : 0xa as the basefee for 'this' context. 194 baseFee = big.NewInt(0x0a) 195 } 196 } 197 post := t.json.Post[subtest.Fork][subtest.Index] 198 msg, err := t.json.Tx.toMessage(post, baseFee) 199 if err != nil { 200 return nil, nil, common.Hash{}, err 201 } 202 203 // Try to recover tx with current signer 204 if len(post.TxBytes) != 0 { 205 var ttx types.Transaction 206 err := ttx.UnmarshalBinary(post.TxBytes) 207 if err != nil { 208 return nil, nil, common.Hash{}, err 209 } 210 211 if _, err := types.Sender(types.LatestSigner(config), &ttx); err != nil { 212 return nil, nil, common.Hash{}, err 213 } 214 } 215 216 // Prepare the EVM. 217 txContext := core.NewEVMTxContext(msg) 218 context := core.NewEVMBlockContext(block.Header(), nil, &t.json.Env.Coinbase) 219 context.GetHash = vmTestBlockHash 220 context.BaseFee = baseFee 221 evm := vm.NewEVM(context, txContext, statedb, config, vmconfig) 222 223 // Execute the message. 224 snapshot := statedb.Snapshot() 225 gaspool := new(core.GasPool) 226 gaspool.AddGas(block.GasLimit()) 227 if _, err := core.ApplyMessage(evm, msg, gaspool); err != nil { 228 statedb.RevertToSnapshot(snapshot) 229 } 230 231 // Commit block 232 statedb.Commit(config.IsEIP158(block.Number())) 233 // Add 0-value mining reward. This only makes a difference in the cases 234 // where 235 // - the coinbase suicided, or 236 // - there are only 'bad' transactions, which aren't executed. In those cases, 237 // the coinbase gets no txfee, so isn't created, and thus needs to be touched 238 statedb.AddBalance(block.Coinbase(), new(big.Int)) 239 // And _now_ get the state root 240 root := statedb.IntermediateRoot(config.IsEIP158(block.Number())) 241 return snaps, statedb, root, nil 242 } 243 244 func (t *StateTest) gasLimit(subtest StateSubtest) uint64 { 245 return t.json.Tx.GasLimit[t.json.Post[subtest.Fork][subtest.Index].Indexes.Gas] 246 } 247 248 func MakePreState(db ethdb.Database, accounts core.GenesisAlloc, snapshotter bool) (*snapshot.Tree, *state.StateDB) { 249 sdb := state.NewDatabase(db) 250 statedb, _ := state.New(common.Hash{}, sdb, nil) 251 for addr, a := range accounts { 252 statedb.SetCode(addr, a.Code) 253 statedb.SetNonce(addr, a.Nonce) 254 statedb.SetBalance(addr, a.Balance) 255 for k, v := range a.Storage { 256 statedb.SetState(addr, k, v) 257 } 258 } 259 // Commit and re-open to start with a clean state. 260 root, _ := statedb.Commit(false) 261 262 var snaps *snapshot.Tree 263 if snapshotter { 264 snaps, _ = snapshot.New(db, sdb.TrieDB(), 1, root, false, true, false) 265 } 266 statedb, _ = state.New(root, sdb, snaps) 267 return snaps, statedb 268 } 269 270 func (t *StateTest) genesis(config *params.ChainConfig) *core.Genesis { 271 return &core.Genesis{ 272 Config: config, 273 Coinbase: t.json.Env.Coinbase, 274 Difficulty: t.json.Env.Difficulty, 275 GasLimit: t.json.Env.GasLimit, 276 Number: t.json.Env.Number, 277 Timestamp: t.json.Env.Timestamp, 278 Alloc: t.json.Pre, 279 } 280 } 281 282 func (tx *stTransaction) toMessage(ps stPostState, baseFee *big.Int) (core.Message, error) { 283 // Derive sender from private key if present. 284 var from common.Address 285 if len(tx.PrivateKey) > 0 { 286 key, err := crypto.ToECDSA(tx.PrivateKey) 287 if err != nil { 288 return nil, fmt.Errorf("invalid private key: %v", err) 289 } 290 from = crypto.PubkeyToAddress(key.PublicKey) 291 } 292 // Parse recipient if present. 293 var to *common.Address 294 if tx.To != "" { 295 to = new(common.Address) 296 if err := to.UnmarshalText([]byte(tx.To)); err != nil { 297 return nil, fmt.Errorf("invalid to address: %v", err) 298 } 299 } 300 301 // Get values specific to this post state. 302 if ps.Indexes.Data > len(tx.Data) { 303 return nil, fmt.Errorf("tx data index %d out of bounds", ps.Indexes.Data) 304 } 305 if ps.Indexes.Value > len(tx.Value) { 306 return nil, fmt.Errorf("tx value index %d out of bounds", ps.Indexes.Value) 307 } 308 if ps.Indexes.Gas > len(tx.GasLimit) { 309 return nil, fmt.Errorf("tx gas limit index %d out of bounds", ps.Indexes.Gas) 310 } 311 dataHex := tx.Data[ps.Indexes.Data] 312 valueHex := tx.Value[ps.Indexes.Value] 313 gasLimit := tx.GasLimit[ps.Indexes.Gas] 314 // Value, Data hex encoding is messy: https://github.com/ethereum/tests/issues/203 315 value := new(big.Int) 316 if valueHex != "0x" { 317 v, ok := math.ParseBig256(valueHex) 318 if !ok { 319 return nil, fmt.Errorf("invalid tx value %q", valueHex) 320 } 321 value = v 322 } 323 data, err := hex.DecodeString(strings.TrimPrefix(dataHex, "0x")) 324 if err != nil { 325 return nil, fmt.Errorf("invalid tx data %q", dataHex) 326 } 327 var accessList types.AccessList 328 if tx.AccessLists != nil && tx.AccessLists[ps.Indexes.Data] != nil { 329 accessList = *tx.AccessLists[ps.Indexes.Data] 330 } 331 // If baseFee provided, set gasPrice to effectiveGasPrice. 332 gasPrice := tx.GasPrice 333 if baseFee != nil { 334 if tx.MaxFeePerGas == nil { 335 tx.MaxFeePerGas = gasPrice 336 } 337 if tx.MaxFeePerGas == nil { 338 tx.MaxFeePerGas = new(big.Int) 339 } 340 if tx.MaxPriorityFeePerGas == nil { 341 tx.MaxPriorityFeePerGas = tx.MaxFeePerGas 342 } 343 gasPrice = math.BigMin(new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee), 344 tx.MaxFeePerGas) 345 } 346 if gasPrice == nil { 347 return nil, fmt.Errorf("no gas price provided") 348 } 349 350 msg := types.NewMessage(from, to, tx.Nonce, value, gasLimit, gasPrice, 351 tx.MaxFeePerGas, tx.MaxPriorityFeePerGas, data, accessList, false) 352 return msg, nil 353 } 354 355 func rlpHash(x interface{}) (h common.Hash) { 356 hw := sha3.NewLegacyKeccak256() 357 rlp.Encode(hw, x) 358 hw.Sum(h[:0]) 359 return h 360 } 361 362 func vmTestBlockHash(n uint64) common.Hash { 363 return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String()))) 364 }