evm.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 hash corresponding to their hash.
  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. // If we don't have an explicit author (i.e. not mining), extract from the header
  35. var beneficiary common.Address
  36. if author == nil {
  37. beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation
  38. } else {
  39. beneficiary = *author
  40. }
  41. return vm.BlockContext{
  42. CanTransfer: CanTransfer,
  43. Transfer: Transfer,
  44. GetHash: GetHashFn(header, chain),
  45. Coinbase: beneficiary,
  46. BlockNumber: new(big.Int).Set(header.Number),
  47. Time: new(big.Int).SetUint64(header.Time),
  48. Difficulty: new(big.Int).Set(header.Difficulty),
  49. GasLimit: header.GasLimit,
  50. }
  51. }
  52. // NewEVMTxContext creates a new transaction context for a single transaction.
  53. func NewEVMTxContext(msg Message) vm.TxContext {
  54. return vm.TxContext{
  55. Origin: msg.From(),
  56. GasPrice: new(big.Int).Set(msg.GasPrice()),
  57. }
  58. }
  59. // GetHashFn returns a GetHashFunc which retrieves header hashes by number
  60. func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash {
  61. // Cache will initially contain [refHash.parent],
  62. // Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...]
  63. var cache []common.Hash
  64. return func(n uint64) common.Hash {
  65. // If there's no hash cache yet, make one
  66. if len(cache) == 0 {
  67. cache = append(cache, ref.ParentHash)
  68. }
  69. if idx := ref.Number.Uint64() - n - 1; idx < uint64(len(cache)) {
  70. return cache[idx]
  71. }
  72. // No luck in the cache, but we can start iterating from the last element we already know
  73. lastKnownHash := cache[len(cache)-1]
  74. lastKnownNumber := ref.Number.Uint64() - uint64(len(cache))
  75. for {
  76. header := chain.GetHeader(lastKnownHash, lastKnownNumber)
  77. if header == nil {
  78. break
  79. }
  80. cache = append(cache, header.ParentHash)
  81. lastKnownHash = header.ParentHash
  82. lastKnownNumber = header.Number.Uint64() - 1
  83. if n == lastKnownNumber {
  84. return lastKnownHash
  85. }
  86. }
  87. return common.Hash{}
  88. }
  89. }
  90. // CanTransfer checks whether there are enough funds in the address' account to make a transfer.
  91. // This does not take the necessary gas in to account to make the transfer valid.
  92. func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool {
  93. return db.GetBalance(addr).Cmp(amount) >= 0
  94. }
  95. // Transfer subtracts amount from sender and adds amount to recipient using the given Db
  96. func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {
  97. db.SubBalance(sender, amount)
  98. db.AddBalance(recipient, amount)
  99. }