environment.go 4.0 KB

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