evm.go 4.6 KB

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