environment.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package vm
  2. import (
  3. "errors"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/core/state"
  7. )
  8. // Environment is is required by the virtual machine to get information from
  9. // it's own isolated environment. For an example see `core.VMEnv`
  10. type Environment interface {
  11. State() *state.StateDB
  12. Origin() common.Address
  13. BlockNumber() *big.Int
  14. GetHash(n uint64) common.Hash
  15. Coinbase() common.Address
  16. Time() int64
  17. Difficulty() *big.Int
  18. GasLimit() *big.Int
  19. Transfer(from, to Account, amount *big.Int) error
  20. AddLog(*state.Log)
  21. AddStructLog(StructLog)
  22. StructLogs() []StructLog
  23. VmType() Type
  24. Depth() int
  25. SetDepth(i int)
  26. Call(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  27. CallCode(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  28. Create(me ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
  29. }
  30. // StructLog is emited to the Environment each cycle and lists information about the curent internal state
  31. // prior to the execution of the statement.
  32. type StructLog struct {
  33. Pc uint64
  34. Op OpCode
  35. Gas *big.Int
  36. GasCost *big.Int
  37. Memory []byte
  38. Stack []*big.Int
  39. Storage map[common.Hash][]byte
  40. }
  41. type Account interface {
  42. SubBalance(amount *big.Int)
  43. AddBalance(amount *big.Int)
  44. Balance() *big.Int
  45. Address() common.Address
  46. }
  47. // generic transfer method
  48. func Transfer(from, to Account, amount *big.Int) error {
  49. if from.Balance().Cmp(amount) < 0 {
  50. return errors.New("Insufficient balance in account")
  51. }
  52. from.SubBalance(amount)
  53. to.AddBalance(amount)
  54. return nil
  55. }