environment.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. GetHash(n uint64) []byte
  14. Coinbase() []byte
  15. Time() int64
  16. Difficulty() *big.Int
  17. GasLimit() *big.Int
  18. Transfer(from, to Account, amount *big.Int) error
  19. AddLog(state.Log)
  20. VmType() Type
  21. Depth() int
  22. SetDepth(i int)
  23. Call(me ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error)
  24. CallCode(me ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error)
  25. Create(me ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
  26. }
  27. type Account interface {
  28. SubBalance(amount *big.Int)
  29. AddBalance(amount *big.Int)
  30. Balance() *big.Int
  31. Address() []byte
  32. }
  33. // generic transfer method
  34. func Transfer(from, to Account, amount *big.Int) error {
  35. //fmt.Printf(":::%x::: %v < %v\n", from.Address(), from.Balance(), amount)
  36. if from.Balance().Cmp(amount) < 0 {
  37. return errors.New("Insufficient balance in account")
  38. }
  39. from.SubBalance(amount)
  40. to.AddBalance(amount)
  41. return nil
  42. }
  43. type Log struct {
  44. address []byte
  45. topics [][]byte
  46. data []byte
  47. log uint64
  48. }
  49. func (self *Log) Address() []byte {
  50. return self.address
  51. }
  52. func (self *Log) Topics() [][]byte {
  53. return self.topics
  54. }
  55. func (self *Log) Data() []byte {
  56. return self.data
  57. }
  58. func (self *Log) Number() uint64 {
  59. return self.log
  60. }
  61. func (self *Log) RlpData() interface{} {
  62. return []interface{}{self.address, ethutil.ByteSliceToInterface(self.topics), self.data}
  63. }
  64. func (self *Log) String() string {
  65. return fmt.Sprintf("[A=%x T=%x D=%x]", self.address, self.topics, self.data)
  66. }