evm.go 3.7 KB

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