environment.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2014 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 vm
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/params"
  21. )
  22. // Environment is an EVM requirement and helper which allows access to outside
  23. // information such as states.
  24. type Environment interface {
  25. // The current ruleset
  26. ChainConfig() *params.ChainConfig
  27. // The state database
  28. Db() Database
  29. // Creates a restorable snapshot
  30. SnapshotDatabase() int
  31. // Set database to previous snapshot
  32. RevertToSnapshot(int)
  33. // Address of the original invoker (first occurrence of the VM invoker)
  34. Origin() common.Address
  35. // The block number this VM is invoked on
  36. BlockNumber() *big.Int
  37. // The n'th hash ago from this block number
  38. GetHash(uint64) common.Hash
  39. // The handler's address
  40. Coinbase() common.Address
  41. // The current time (block time)
  42. Time() *big.Int
  43. // Difficulty set on the current block
  44. Difficulty() *big.Int
  45. // The gas limit of the block
  46. GasLimit() *big.Int
  47. // Determines whether it's possible to transact
  48. CanTransfer(from common.Address, balance *big.Int) bool
  49. // Transfers amount from one account to the other
  50. Transfer(from, to Account, amount *big.Int)
  51. // Adds a LOG to the state
  52. AddLog(*Log)
  53. // Type of the VM
  54. Vm() Vm
  55. // Get the curret calling depth
  56. Depth() int
  57. // Set the current calling depth
  58. SetDepth(i int)
  59. // Call another contract
  60. Call(me ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  61. // Take another's contract code and execute within our own context
  62. CallCode(me ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  63. // Same as CallCode except sender and value is propagated from parent to child scope
  64. DelegateCall(me ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error)
  65. // Create a new contract
  66. Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error)
  67. }
  68. // Vm is the basic interface for an implementation of the EVM.
  69. type Vm interface {
  70. // Run should execute the given contract with the input given in in
  71. // and return the contract execution return bytes or an error if it
  72. // failed.
  73. Run(c *Contract, in []byte) ([]byte, error)
  74. }
  75. // Database is a EVM database for full state querying.
  76. type Database interface {
  77. GetAccount(common.Address) Account
  78. CreateAccount(common.Address) Account
  79. AddBalance(common.Address, *big.Int)
  80. GetBalance(common.Address) *big.Int
  81. GetNonce(common.Address) uint64
  82. SetNonce(common.Address, uint64)
  83. GetCodeHash(common.Address) common.Hash
  84. GetCodeSize(common.Address) int
  85. GetCode(common.Address) []byte
  86. SetCode(common.Address, []byte)
  87. AddRefund(*big.Int)
  88. GetRefund() *big.Int
  89. GetState(common.Address, common.Hash) common.Hash
  90. SetState(common.Address, common.Hash, common.Hash)
  91. Suicide(common.Address) bool
  92. HasSuicided(common.Address) bool
  93. // Exist reports whether the given account exists in state.
  94. // Notably this should also return true for suicided accounts.
  95. Exist(common.Address) bool
  96. // Empty returns whether the given account is empty. Empty
  97. // is defined according to EIP161 (balance = nonce = code = 0).
  98. Empty(common.Address) bool
  99. }
  100. // Account represents a contract or basic ethereum account.
  101. type Account interface {
  102. SubBalance(amount *big.Int)
  103. AddBalance(amount *big.Int)
  104. SetBalance(*big.Int)
  105. SetNonce(uint64)
  106. Balance() *big.Int
  107. Address() common.Address
  108. ReturnGas(*big.Int, *big.Int)
  109. SetCode(common.Hash, []byte)
  110. ForEachStorage(cb func(key, value common.Hash) bool)
  111. Value() *big.Int
  112. }