environment.go 4.4 KB

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