execution.go 9.1 KB

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