interface.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // Vm is the basic interface for an implementation of the EVM.
  22. type Vm interface {
  23. // Run should execute the given contract with the input given in in
  24. // and return the contract execution return bytes or an error if it
  25. // failed.
  26. Run(c *Contract, in []byte) ([]byte, error)
  27. }
  28. // StateDB is an EVM database for full state querying.
  29. type StateDB interface {
  30. GetAccount(common.Address) Account
  31. CreateAccount(common.Address) Account
  32. SubBalance(common.Address, *big.Int)
  33. AddBalance(common.Address, *big.Int)
  34. GetBalance(common.Address) *big.Int
  35. GetNonce(common.Address) uint64
  36. SetNonce(common.Address, uint64)
  37. GetCodeHash(common.Address) common.Hash
  38. GetCode(common.Address) []byte
  39. SetCode(common.Address, []byte)
  40. GetCodeSize(common.Address) int
  41. AddRefund(*big.Int)
  42. GetRefund() *big.Int
  43. GetState(common.Address, common.Hash) common.Hash
  44. SetState(common.Address, common.Hash, common.Hash)
  45. Suicide(common.Address) bool
  46. HasSuicided(common.Address) bool
  47. // Exist reports whether the given account exists in state.
  48. // Notably this should also return true for suicided accounts.
  49. Exist(common.Address) bool
  50. // Empty returns whether the given account is empty. Empty
  51. // is defined according to EIP161 (balance = nonce = code = 0).
  52. Empty(common.Address) bool
  53. RevertToSnapshot(int)
  54. Snapshot() int
  55. AddLog(*Log)
  56. }
  57. // Account represents a contract or basic ethereum account.
  58. type Account interface {
  59. SubBalance(amount *big.Int)
  60. AddBalance(amount *big.Int)
  61. SetBalance(*big.Int)
  62. SetNonce(uint64)
  63. Balance() *big.Int
  64. Address() common.Address
  65. ReturnGas(*big.Int)
  66. SetCode(common.Hash, []byte)
  67. ForEachStorage(cb func(key, value common.Hash) bool)
  68. Value() *big.Int
  69. }
  70. // CallContext provides a basic interface for the EVM calling conventions. The EVM Environment
  71. // depends on this context being implemented for doing subcalls and initialising new EVM contracts.
  72. type CallContext interface {
  73. // Call another contract
  74. Call(env *Environment, me ContractRef, addr common.Address, data []byte, gas, value *big.Int) ([]byte, error)
  75. // Take another's contract code and execute within our own context
  76. CallCode(env *Environment, me ContractRef, addr common.Address, data []byte, gas, value *big.Int) ([]byte, error)
  77. // Same as CallCode except sender and value is propagated from parent to child scope
  78. DelegateCall(env *Environment, me ContractRef, addr common.Address, data []byte, gas *big.Int) ([]byte, error)
  79. // Create a new contract
  80. Create(env *Environment, me ContractRef, data []byte, gas, value *big.Int) ([]byte, common.Address, error)
  81. }