environment.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. Err error
  41. }
  42. type Account interface {
  43. SubBalance(amount *big.Int)
  44. AddBalance(amount *big.Int)
  45. Balance() *big.Int
  46. Address() common.Address
  47. }
  48. // generic transfer method
  49. func Transfer(from, to Account, amount *big.Int) error {
  50. if from.Balance().Cmp(amount) < 0 {
  51. return errors.New("Insufficient balance in account")
  52. }
  53. from.SubBalance(amount)
  54. to.AddBalance(amount)
  55. return nil
  56. }