github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/evmcore/dummy_block.go (about) 1 // Copyright 2015 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 evmcore 18 19 import ( 20 "math" 21 "math/big" 22 23 "github.com/unicornultrafoundation/go-helios/hash" 24 "github.com/unicornultrafoundation/go-helios/native/idx" 25 "github.com/unicornultrafoundation/go-u2u/common" 26 "github.com/unicornultrafoundation/go-u2u/core/types" 27 "github.com/unicornultrafoundation/go-u2u/trie" 28 29 "github.com/unicornultrafoundation/go-u2u/native" 30 "github.com/unicornultrafoundation/go-u2u/u2u" 31 ) 32 33 type ( 34 EvmHeader struct { 35 Number *big.Int 36 Hash common.Hash 37 ParentHash common.Hash 38 Root common.Hash 39 TxHash common.Hash 40 Time native.Timestamp 41 Coinbase common.Address 42 43 GasLimit uint64 44 GasUsed uint64 45 46 BaseFee *big.Int 47 } 48 49 EvmBlock struct { 50 EvmHeader 51 52 Transactions types.Transactions 53 } 54 ) 55 56 // NewEvmBlock constructor. 57 func NewEvmBlock(h *EvmHeader, txs types.Transactions) *EvmBlock { 58 b := &EvmBlock{ 59 EvmHeader: *h, 60 Transactions: txs, 61 } 62 63 if len(txs) == 0 { 64 b.EvmHeader.TxHash = types.EmptyRootHash 65 } else { 66 b.EvmHeader.TxHash = types.DeriveSha(txs, trie.NewStackTrie(nil)) 67 } 68 69 return b 70 } 71 72 // ToEvmHeader converts native.Block to EvmHeader. 73 func ToEvmHeader(block *native.Block, index idx.Block, prevHash hash.Event, rules u2u.Rules) *EvmHeader { 74 baseFee := rules.Economy.MinGasPrice 75 if !rules.Upgrades.London { 76 baseFee = nil 77 } 78 return &EvmHeader{ 79 Hash: common.Hash(block.Atropos), 80 ParentHash: common.Hash(prevHash), 81 Root: common.Hash(block.Root), 82 Number: big.NewInt(int64(index)), 83 Time: block.Time, 84 GasLimit: math.MaxUint64, 85 GasUsed: block.GasUsed, 86 BaseFee: baseFee, 87 } 88 } 89 90 // ConvertFromEthHeader converts ETH-formatted header to Hashgraph EVM header 91 func ConvertFromEthHeader(h *types.Header) *EvmHeader { 92 // NOTE: incomplete conversion 93 return &EvmHeader{ 94 Number: h.Number, 95 Coinbase: h.Coinbase, 96 GasLimit: math.MaxUint64, 97 GasUsed: h.GasUsed, 98 Root: h.Root, 99 TxHash: h.TxHash, 100 ParentHash: h.ParentHash, 101 Time: native.FromUnix(int64(h.Time)), 102 Hash: common.BytesToHash(h.Extra), 103 BaseFee: h.BaseFee, 104 } 105 } 106 107 // EthHeader returns header in ETH format 108 func (h *EvmHeader) EthHeader() *types.Header { 109 if h == nil { 110 return nil 111 } 112 // NOTE: incomplete conversion 113 ethHeader := &types.Header{ 114 Number: h.Number, 115 Coinbase: h.Coinbase, 116 GasLimit: 0xffffffffffff, // don't use h.GasLimit (too much bits) here to avoid parsing issues 117 GasUsed: h.GasUsed, 118 Root: h.Root, 119 TxHash: h.TxHash, 120 ParentHash: h.ParentHash, 121 Time: uint64(h.Time.Unix()), 122 Extra: h.Hash.Bytes(), 123 BaseFee: h.BaseFee, 124 125 Difficulty: new(big.Int), 126 } 127 ethHeader.SetExternalHash(h.Hash) 128 return ethHeader 129 } 130 131 // Header is a copy of EvmBlock.EvmHeader. 132 func (b *EvmBlock) Header() *EvmHeader { 133 if b == nil { 134 return nil 135 } 136 // copy values 137 h := b.EvmHeader 138 // copy refs 139 h.Number = new(big.Int).Set(b.Number) 140 if b.BaseFee != nil { 141 h.BaseFee = new(big.Int).Set(b.BaseFee) 142 } 143 144 return &h 145 } 146 147 func (b *EvmBlock) NumberU64() uint64 { 148 return b.Number.Uint64() 149 } 150 151 func (b *EvmBlock) EthBlock() *types.Block { 152 if b == nil { 153 return nil 154 } 155 return types.NewBlock(b.EvmHeader.EthHeader(), b.Transactions, nil, nil, trie.NewStackTrie(nil)) 156 } 157 158 func (b *EvmBlock) EstimateSize() int { 159 est := 0 160 for _, tx := range b.Transactions { 161 est += len(tx.Data()) 162 } 163 return est + b.Transactions.Len()*256 164 }