environment.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // Environment is is required by the virtual machine to get information from
  22. // it's own isolated environment.
  23. // Environment is an EVM requirement and helper which allows access to outside
  24. // information such as states.
  25. type Environment interface {
  26. // The state database
  27. Db() Database
  28. // Creates a restorable snapshot
  29. MakeSnapshot() Database
  30. // Set database to previous snapshot
  31. SetSnapshot(Database)
  32. // Address of the original invoker (first occurance of the VM invoker)
  33. Origin() common.Address
  34. // The block number this VM is invoken on
  35. BlockNumber() *big.Int
  36. // The n'th hash ago from this block number
  37. GetHash(uint64) common.Hash
  38. // The handler's address
  39. Coinbase() common.Address
  40. // The current time (block time)
  41. Time() *big.Int
  42. // Difficulty set on the current block
  43. Difficulty() *big.Int
  44. // The gas limit of the block
  45. GasLimit() *big.Int
  46. // Determines whether it's possible to transact
  47. CanTransfer(from common.Address, balance *big.Int) bool
  48. // Transfers amount from one account to the other
  49. Transfer(from, to Account, amount *big.Int)
  50. // Adds a LOG to the state
  51. AddLog(*Log)
  52. // Adds a structured log to the env
  53. AddStructLog(StructLog)
  54. // Returns all coalesced structured logs
  55. StructLogs() []StructLog
  56. // Type of the VM
  57. VmType() Type
  58. // Current calling depth
  59. Depth() int
  60. SetDepth(i int)
  61. // Call another contract
  62. Call(me ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  63. // Take another's contract code and execute within our own context
  64. CallCode(me ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  65. // Same as CallCode except sender and value is propagated from parent to child scope
  66. DelegateCall(me ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error)
  67. // Create a new contract
  68. Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error)
  69. }
  70. // Database is a EVM database for full state querying
  71. type Database interface {
  72. GetAccount(common.Address) Account
  73. CreateAccount(common.Address) Account
  74. AddBalance(common.Address, *big.Int)
  75. GetBalance(common.Address) *big.Int
  76. GetNonce(common.Address) uint64
  77. SetNonce(common.Address, uint64)
  78. GetCode(common.Address) []byte
  79. SetCode(common.Address, []byte)
  80. AddRefund(*big.Int)
  81. GetRefund() *big.Int
  82. GetState(common.Address, common.Hash) common.Hash
  83. SetState(common.Address, common.Hash, common.Hash)
  84. Delete(common.Address) bool
  85. Exist(common.Address) bool
  86. IsDeleted(common.Address) bool
  87. }
  88. // StructLog is emited to the Environment each cycle and lists information about the curent internal state
  89. // prior to the execution of the statement.
  90. type StructLog struct {
  91. Pc uint64
  92. Op OpCode
  93. Gas *big.Int
  94. GasCost *big.Int
  95. Memory []byte
  96. Stack []*big.Int
  97. Storage map[common.Hash][]byte
  98. Err error
  99. }
  100. type Account interface {
  101. SubBalance(amount *big.Int)
  102. AddBalance(amount *big.Int)
  103. SetBalance(*big.Int)
  104. SetNonce(uint64)
  105. Balance() *big.Int
  106. Address() common.Address
  107. ReturnGas(*big.Int, *big.Int)
  108. SetCode([]byte)
  109. EachStorage(cb func(key, value []byte))
  110. Value() *big.Int
  111. }