environment.go 3.7 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. // 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(n 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. // Create a new contract
  66. Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error)
  67. }
  68. // Database is a EVM database for full state querying
  69. type Database interface {
  70. GetAccount(common.Address) Account
  71. CreateAccount(common.Address) Account
  72. AddBalance(common.Address, *big.Int)
  73. GetBalance(common.Address) *big.Int
  74. GetNonce(common.Address) uint64
  75. SetNonce(common.Address, uint64)
  76. GetCode(common.Address) []byte
  77. SetCode(common.Address, []byte)
  78. AddRefund(*big.Int)
  79. GetRefund() *big.Int
  80. GetState(common.Address, common.Hash) common.Hash
  81. SetState(common.Address, common.Hash, common.Hash)
  82. Delete(common.Address) bool
  83. Exist(common.Address) bool
  84. IsDeleted(common.Address) bool
  85. }
  86. // StructLog is emited to the Environment each cycle and lists information about the curent internal state
  87. // prior to the execution of the statement.
  88. type StructLog struct {
  89. Pc uint64
  90. Op OpCode
  91. Gas *big.Int
  92. GasCost *big.Int
  93. Memory []byte
  94. Stack []*big.Int
  95. Storage map[common.Hash][]byte
  96. Err error
  97. }
  98. type Account interface {
  99. SubBalance(amount *big.Int)
  100. AddBalance(amount *big.Int)
  101. SetBalance(*big.Int)
  102. SetNonce(uint64)
  103. Balance() *big.Int
  104. Address() common.Address
  105. ReturnGas(*big.Int, *big.Int)
  106. SetCode([]byte)
  107. }