evm.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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).Set(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. var cache map[uint64]common.Hash
  57. return func(n uint64) common.Hash {
  58. // If there's no hash cache yet, make one
  59. if cache == nil {
  60. cache = map[uint64]common.Hash{
  61. ref.Number.Uint64() - 1: ref.ParentHash,
  62. }
  63. }
  64. // Try to fulfill the request from the cache
  65. if hash, ok := cache[n]; ok {
  66. return hash
  67. }
  68. // Not cached, iterate the blocks and cache the hashes
  69. for header := chain.GetHeader(ref.ParentHash, ref.Number.Uint64()-1); header != nil; header = chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) {
  70. cache[header.Number.Uint64()-1] = header.ParentHash
  71. if n == header.Number.Uint64()-1 {
  72. return header.ParentHash
  73. }
  74. }
  75. return common.Hash{}
  76. }
  77. }
  78. // CanTransfer checks wether there are enough funds in the address' account to make a transfer.
  79. // This does not take the necessary gas in to account to make the transfer valid.
  80. func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool {
  81. return db.GetBalance(addr).Cmp(amount) >= 0
  82. }
  83. // Transfer subtracts amount from sender and adds amount to recipient using the given Db
  84. func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {
  85. db.SubBalance(sender, amount)
  86. db.AddBalance(recipient, amount)
  87. }