evm.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2016 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 core
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/consensus"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/core/vm"
  23. )
  24. // ChainContext supports retrieving headers and consensus parameters from the
  25. // current blockchain to be used during transaction processing.
  26. type ChainContext interface {
  27. // Engine retrieves the chain's consensus engine.
  28. Engine() consensus.Engine
  29. // GetHeader returns the header corresponding to the hash/number argument pair.
  30. GetHeader(common.Hash, uint64) *types.Header
  31. }
  32. // NewEVMBlockContext creates a new context for use in the EVM.
  33. func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common.Address) vm.BlockContext {
  34. var (
  35. beneficiary common.Address
  36. baseFee *big.Int
  37. random *common.Hash
  38. )
  39. // If we don't have an explicit author (i.e. not mining), extract from the header
  40. if author == nil {
  41. beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation
  42. } else {
  43. beneficiary = *author
  44. }
  45. if header.BaseFee != nil {
  46. baseFee = new(big.Int).Set(header.BaseFee)
  47. }
  48. if header.Difficulty.Cmp(common.Big0) == 0 {
  49. random = &header.MixDigest
  50. }
  51. return vm.BlockContext{
  52. CanTransfer: CanTransfer,
  53. Transfer: Transfer,
  54. GetHash: GetHashFn(header, chain),
  55. Coinbase: beneficiary,
  56. BlockNumber: new(big.Int).Set(header.Number),
  57. Time: new(big.Int).SetUint64(header.Time),
  58. Difficulty: new(big.Int).Set(header.Difficulty),
  59. BaseFee: baseFee,
  60. GasLimit: header.GasLimit,
  61. Random: random,
  62. }
  63. }
  64. // NewEVMTxContext creates a new transaction context for a single transaction.
  65. func NewEVMTxContext(msg Message) vm.TxContext {
  66. return vm.TxContext{
  67. Origin: msg.From(),
  68. GasPrice: new(big.Int).Set(msg.GasPrice()),
  69. }
  70. }
  71. // GetHashFn returns a GetHashFunc which retrieves header hashes by number
  72. func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash {
  73. // Cache will initially contain [refHash.parent],
  74. // Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...]
  75. var cache []common.Hash
  76. return func(n uint64) common.Hash {
  77. if ref.Number.Uint64() <= n {
  78. // This situation can happen if we're doing tracing and using
  79. // block overrides.
  80. return common.Hash{}
  81. }
  82. // If there's no hash cache yet, make one
  83. if len(cache) == 0 {
  84. cache = append(cache, ref.ParentHash)
  85. }
  86. if idx := ref.Number.Uint64() - n - 1; idx < uint64(len(cache)) {
  87. return cache[idx]
  88. }
  89. // No luck in the cache, but we can start iterating from the last element we already know
  90. lastKnownHash := cache[len(cache)-1]
  91. lastKnownNumber := ref.Number.Uint64() - uint64(len(cache))
  92. for {
  93. header := chain.GetHeader(lastKnownHash, lastKnownNumber)
  94. if header == nil {
  95. break
  96. }
  97. cache = append(cache, header.ParentHash)
  98. lastKnownHash = header.ParentHash
  99. lastKnownNumber = header.Number.Uint64() - 1
  100. if n == lastKnownNumber {
  101. return lastKnownHash
  102. }
  103. }
  104. return common.Hash{}
  105. }
  106. }
  107. // CanTransfer checks whether there are enough funds in the address' account to make a transfer.
  108. // This does not take the necessary gas in to account to make the transfer valid.
  109. func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool {
  110. return db.GetBalance(addr).Cmp(amount) >= 0
  111. }
  112. // Transfer subtracts amount from sender and adds amount to recipient using the given Db
  113. func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {
  114. db.SubBalance(sender, amount)
  115. db.AddBalance(recipient, amount)
  116. }