github.com/ethereum/go-ethereum@v1.16.1/eth/tracers/internal/tracetest/util.go (about) 1 // Copyright 2022 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package tracetest 18 19 import ( 20 "encoding/json" 21 "math/big" 22 "strings" 23 "unicode" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/common/math" 27 "github.com/ethereum/go-ethereum/consensus/misc/eip4844" 28 "github.com/ethereum/go-ethereum/core" 29 "github.com/ethereum/go-ethereum/core/types" 30 "github.com/ethereum/go-ethereum/core/vm" 31 32 // Force-load native and js packages, to trigger registration 33 _ "github.com/ethereum/go-ethereum/eth/tracers/js" 34 _ "github.com/ethereum/go-ethereum/eth/tracers/native" 35 ) 36 37 // camel converts a snake cased input string into a camel cased output. 38 func camel(str string) string { 39 pieces := strings.Split(str, "_") 40 for i := 1; i < len(pieces); i++ { 41 pieces[i] = string(unicode.ToUpper(rune(pieces[i][0]))) + pieces[i][1:] 42 } 43 return strings.Join(pieces, "") 44 } 45 46 // traceContext defines a context used to construct the block context 47 type traceContext struct { 48 Number math.HexOrDecimal64 `json:"number"` 49 Difficulty *math.HexOrDecimal256 `json:"difficulty"` 50 Time math.HexOrDecimal64 `json:"timestamp"` 51 GasLimit math.HexOrDecimal64 `json:"gasLimit"` 52 Miner common.Address `json:"miner"` 53 BaseFee *math.HexOrDecimal256 `json:"baseFeePerGas"` 54 } 55 56 func (c *traceContext) toBlockContext(genesis *core.Genesis) vm.BlockContext { 57 context := vm.BlockContext{ 58 CanTransfer: core.CanTransfer, 59 Transfer: core.Transfer, 60 Coinbase: c.Miner, 61 BlockNumber: new(big.Int).SetUint64(uint64(c.Number)), 62 Time: uint64(c.Time), 63 Difficulty: (*big.Int)(c.Difficulty), 64 GasLimit: uint64(c.GasLimit), 65 } 66 if genesis.Config.IsLondon(context.BlockNumber) { 67 context.BaseFee = (*big.Int)(c.BaseFee) 68 } 69 70 if genesis.Config.TerminalTotalDifficulty != nil && genesis.Config.TerminalTotalDifficulty.Sign() == 0 { 71 context.Random = &genesis.Mixhash 72 } 73 74 if genesis.ExcessBlobGas != nil && genesis.BlobGasUsed != nil { 75 header := &types.Header{Number: genesis.Config.LondonBlock, Time: *genesis.Config.CancunTime} 76 excess := eip4844.CalcExcessBlobGas(genesis.Config, header, genesis.Timestamp) 77 header.ExcessBlobGas = &excess 78 context.BlobBaseFee = eip4844.CalcBlobFee(genesis.Config, header) 79 } 80 return context 81 } 82 83 // tracerTestEnv defines a tracer test required fields 84 type tracerTestEnv struct { 85 Genesis *core.Genesis `json:"genesis"` 86 Context *traceContext `json:"context"` 87 Input string `json:"input"` 88 TracerConfig json.RawMessage `json:"tracerConfig"` 89 }