execution.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright 2020 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. package t8ntool
  17. import (
  18. "fmt"
  19. "math/big"
  20. "os"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/math"
  23. "github.com/ethereum/go-ethereum/consensus/misc"
  24. "github.com/ethereum/go-ethereum/core"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. "github.com/ethereum/go-ethereum/core/state"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/core/vm"
  29. "github.com/ethereum/go-ethereum/crypto"
  30. "github.com/ethereum/go-ethereum/ethdb"
  31. "github.com/ethereum/go-ethereum/log"
  32. "github.com/ethereum/go-ethereum/params"
  33. "github.com/ethereum/go-ethereum/rlp"
  34. "github.com/ethereum/go-ethereum/trie"
  35. "golang.org/x/crypto/sha3"
  36. )
  37. type Prestate struct {
  38. Env stEnv `json:"env"`
  39. Pre core.GenesisAlloc `json:"pre"`
  40. }
  41. // ExecutionResult contains the execution status after running a state test, any
  42. // error that might have occurred and a dump of the final state if requested.
  43. type ExecutionResult struct {
  44. StateRoot common.Hash `json:"stateRoot"`
  45. TxRoot common.Hash `json:"txRoot"`
  46. ReceiptRoot common.Hash `json:"receiptRoot"`
  47. LogsHash common.Hash `json:"logsHash"`
  48. Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
  49. Receipts types.Receipts `json:"receipts"`
  50. Rejected []int `json:"rejected,omitempty"`
  51. }
  52. type ommer struct {
  53. Delta uint64 `json:"delta"`
  54. Address common.Address `json:"address"`
  55. }
  56. //go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
  57. type stEnv struct {
  58. Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
  59. Difficulty *big.Int `json:"currentDifficulty" gencodec:"required"`
  60. GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
  61. Number uint64 `json:"currentNumber" gencodec:"required"`
  62. Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
  63. BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
  64. Ommers []ommer `json:"ommers,omitempty"`
  65. }
  66. type stEnvMarshaling struct {
  67. Coinbase common.UnprefixedAddress
  68. Difficulty *math.HexOrDecimal256
  69. GasLimit math.HexOrDecimal64
  70. Number math.HexOrDecimal64
  71. Timestamp math.HexOrDecimal64
  72. }
  73. // Apply applies a set of transactions to a pre-state
  74. func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
  75. txs types.Transactions, miningReward int64,
  76. getTracerFn func(txIndex int, txHash common.Hash) (tracer vm.EVMLogger, err error)) (*state.StateDB, *ExecutionResult, error) {
  77. // Capture errors for BLOCKHASH operation, if we haven't been supplied the
  78. // required blockhashes
  79. var hashError error
  80. getHash := func(num uint64) common.Hash {
  81. if pre.Env.BlockHashes == nil {
  82. hashError = fmt.Errorf("getHash(%d) invoked, no blockhashes provided", num)
  83. return common.Hash{}
  84. }
  85. h, ok := pre.Env.BlockHashes[math.HexOrDecimal64(num)]
  86. if !ok {
  87. hashError = fmt.Errorf("getHash(%d) invoked, blockhash for that block not provided", num)
  88. }
  89. return h
  90. }
  91. var (
  92. statedb = MakePreState(rawdb.NewMemoryDatabase(), pre.Pre)
  93. signer = types.MakeSigner(chainConfig, new(big.Int).SetUint64(pre.Env.Number))
  94. gaspool = new(core.GasPool)
  95. blockHash = common.Hash{0x13, 0x37}
  96. rejectedTxs []int
  97. includedTxs types.Transactions
  98. gasUsed = uint64(0)
  99. receipts = make(types.Receipts, 0)
  100. txIndex = 0
  101. )
  102. gaspool.AddGas(pre.Env.GasLimit)
  103. vmContext := vm.BlockContext{
  104. CanTransfer: core.CanTransfer,
  105. Transfer: core.Transfer,
  106. Coinbase: pre.Env.Coinbase,
  107. BlockNumber: new(big.Int).SetUint64(pre.Env.Number),
  108. Time: new(big.Int).SetUint64(pre.Env.Timestamp),
  109. Difficulty: pre.Env.Difficulty,
  110. GasLimit: pre.Env.GasLimit,
  111. GetHash: getHash,
  112. }
  113. // If DAO is supported/enabled, we need to handle it here. In geth 'proper', it's
  114. // done in StateProcessor.Process(block, ...), right before transactions are applied.
  115. if chainConfig.DAOForkSupport &&
  116. chainConfig.DAOForkBlock != nil &&
  117. chainConfig.DAOForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 {
  118. misc.ApplyDAOHardFork(statedb)
  119. }
  120. for i, tx := range txs {
  121. msg, err := tx.AsMessage(signer)
  122. if err != nil {
  123. log.Info("rejected tx", "index", i, "hash", tx.Hash(), "error", err)
  124. rejectedTxs = append(rejectedTxs, i)
  125. continue
  126. }
  127. tracer, err := getTracerFn(txIndex, tx.Hash())
  128. if err != nil {
  129. return nil, nil, err
  130. }
  131. vmConfig.Tracer = tracer
  132. vmConfig.Debug = (tracer != nil)
  133. statedb.Prepare(tx.Hash(), blockHash, txIndex)
  134. txContext := core.NewEVMTxContext(msg)
  135. snapshot := statedb.Snapshot()
  136. evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig)
  137. // (ret []byte, usedGas uint64, failed bool, err error)
  138. msgResult, err := core.ApplyMessage(evm, msg, gaspool)
  139. if err != nil {
  140. statedb.RevertToSnapshot(snapshot)
  141. log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From(), "error", err)
  142. rejectedTxs = append(rejectedTxs, i)
  143. continue
  144. }
  145. includedTxs = append(includedTxs, tx)
  146. if hashError != nil {
  147. return nil, nil, NewError(ErrorMissingBlockhash, hashError)
  148. }
  149. gasUsed += msgResult.UsedGas
  150. // Receipt:
  151. {
  152. var root []byte
  153. if chainConfig.IsByzantium(vmContext.BlockNumber) {
  154. statedb.Finalise(true)
  155. } else {
  156. root = statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)).Bytes()
  157. }
  158. // Create a new receipt for the transaction, storing the intermediate root and
  159. // gas used by the tx.
  160. receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: gasUsed}
  161. if msgResult.Failed() {
  162. receipt.Status = types.ReceiptStatusFailed
  163. } else {
  164. receipt.Status = types.ReceiptStatusSuccessful
  165. }
  166. receipt.TxHash = tx.Hash()
  167. receipt.GasUsed = msgResult.UsedGas
  168. // If the transaction created a contract, store the creation address in the receipt.
  169. if msg.To() == nil {
  170. receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
  171. }
  172. // Set the receipt logs and create the bloom filter.
  173. receipt.Logs = statedb.GetLogs(tx.Hash())
  174. receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
  175. // These three are non-consensus fields:
  176. //receipt.BlockHash
  177. //receipt.BlockNumber
  178. receipt.TransactionIndex = uint(txIndex)
  179. receipts = append(receipts, receipt)
  180. }
  181. txIndex++
  182. }
  183. statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
  184. // Add mining reward?
  185. if miningReward > 0 {
  186. // Add mining reward. The mining reward may be `0`, which only makes a difference in the cases
  187. // where
  188. // - the coinbase suicided, or
  189. // - there are only 'bad' transactions, which aren't executed. In those cases,
  190. // the coinbase gets no txfee, so isn't created, and thus needs to be touched
  191. var (
  192. blockReward = big.NewInt(miningReward)
  193. minerReward = new(big.Int).Set(blockReward)
  194. perOmmer = new(big.Int).Div(blockReward, big.NewInt(32))
  195. )
  196. for _, ommer := range pre.Env.Ommers {
  197. // Add 1/32th for each ommer included
  198. minerReward.Add(minerReward, perOmmer)
  199. // Add (8-delta)/8
  200. reward := big.NewInt(8)
  201. reward.Sub(reward, big.NewInt(0).SetUint64(ommer.Delta))
  202. reward.Mul(reward, blockReward)
  203. reward.Div(reward, big.NewInt(8))
  204. statedb.AddBalance(ommer.Address, reward)
  205. }
  206. statedb.AddBalance(pre.Env.Coinbase, minerReward)
  207. }
  208. // Commit block
  209. root, _, err := statedb.Commit(chainConfig.IsEIP158(vmContext.BlockNumber))
  210. if err != nil {
  211. fmt.Fprintf(os.Stderr, "Could not commit state: %v", err)
  212. return nil, nil, NewError(ErrorEVM, fmt.Errorf("could not commit state: %v", err))
  213. }
  214. execRs := &ExecutionResult{
  215. StateRoot: root,
  216. TxRoot: types.DeriveSha(includedTxs, trie.NewStackTrie(nil)),
  217. ReceiptRoot: types.DeriveSha(receipts, trie.NewStackTrie(nil)),
  218. Bloom: types.CreateBloom(receipts),
  219. LogsHash: rlpHash(statedb.Logs()),
  220. Receipts: receipts,
  221. Rejected: rejectedTxs,
  222. }
  223. return statedb, execRs, nil
  224. }
  225. func MakePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB {
  226. sdb := state.NewDatabase(db)
  227. statedb, _ := state.New(common.Hash{}, sdb, nil)
  228. for addr, a := range accounts {
  229. statedb.SetCode(addr, a.Code)
  230. statedb.SetNonce(addr, a.Nonce)
  231. statedb.SetBalance(addr, a.Balance)
  232. for k, v := range a.Storage {
  233. statedb.SetState(addr, k, v)
  234. }
  235. }
  236. // Commit and re-open to start with a clean state.
  237. root, _, _ := statedb.Commit(false)
  238. statedb, _ = state.New(root, sdb, nil)
  239. return statedb
  240. }
  241. func rlpHash(x interface{}) (h common.Hash) {
  242. hw := sha3.NewLegacyKeccak256()
  243. rlp.Encode(hw, x)
  244. hw.Sum(h[:0])
  245. return h
  246. }