environment.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package vm
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/big"
  6. "github.com/ethereum/go-ethereum/ethutil"
  7. "github.com/ethereum/go-ethereum/state"
  8. )
  9. type Environment interface {
  10. State() *state.StateDB
  11. Origin() []byte
  12. BlockNumber() *big.Int
  13. PrevHash() []byte
  14. Coinbase() []byte
  15. Time() int64
  16. Difficulty() *big.Int
  17. BlockHash() []byte
  18. GasLimit() *big.Int
  19. Transfer(from, to Account, amount *big.Int) error
  20. AddLog(state.Log)
  21. Depth() int
  22. SetDepth(i int)
  23. Call(me ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error)
  24. CallCode(me ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error)
  25. Create(me ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, ClosureRef)
  26. }
  27. type Object interface {
  28. GetStorage(key *big.Int) *ethutil.Value
  29. SetStorage(key *big.Int, value *ethutil.Value)
  30. }
  31. type Account interface {
  32. SubBalance(amount *big.Int)
  33. AddBalance(amount *big.Int)
  34. Balance() *big.Int
  35. }
  36. // generic transfer method
  37. func Transfer(from, to Account, amount *big.Int) error {
  38. if from.Balance().Cmp(amount) < 0 {
  39. return errors.New("Insufficient balance in account")
  40. }
  41. from.SubBalance(amount)
  42. to.AddBalance(amount)
  43. return nil
  44. }
  45. type Log struct {
  46. address []byte
  47. topics [][]byte
  48. data []byte
  49. }
  50. func (self *Log) Address() []byte {
  51. return self.address
  52. }
  53. func (self *Log) Topics() [][]byte {
  54. return self.topics
  55. }
  56. func (self *Log) Data() []byte {
  57. return self.data
  58. }
  59. func (self *Log) RlpData() interface{} {
  60. return []interface{}{self.address, ethutil.ByteSliceToInterface(self.topics), self.data}
  61. }
  62. func (self *Log) String() string {
  63. return fmt.Sprintf("[A=%x T=%x D=%x]", self.address, self.topics, self.data)
  64. }