environment.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. type Environment interface {
  9. State() *state.StateDB
  10. Origin() common.Address
  11. BlockNumber() *big.Int
  12. GetHash(n uint64) common.Hash
  13. Coinbase() common.Address
  14. Time() int64
  15. Difficulty() *big.Int
  16. GasLimit() *big.Int
  17. Transfer(from, to Account, amount *big.Int) error
  18. AddLog(*state.Log)
  19. AddStructLog(StructLog)
  20. StructLogs() []StructLog
  21. VmType() Type
  22. Depth() int
  23. SetDepth(i int)
  24. Call(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  25. CallCode(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  26. Create(me ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
  27. }
  28. type StructLog struct {
  29. Pc uint64
  30. Op OpCode
  31. Gas *big.Int
  32. Memory []byte
  33. Stack []*big.Int
  34. Storage map[common.Hash][]byte
  35. }
  36. type Account interface {
  37. SubBalance(amount *big.Int)
  38. AddBalance(amount *big.Int)
  39. Balance() *big.Int
  40. Address() common.Address
  41. }
  42. // generic transfer method
  43. func Transfer(from, to Account, amount *big.Int) error {
  44. if from.Balance().Cmp(amount) < 0 {
  45. return errors.New("Insufficient balance in account")
  46. }
  47. from.SubBalance(amount)
  48. to.AddBalance(amount)
  49. return nil
  50. }