environment.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package vm
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "math/big"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/rlp"
  9. "github.com/ethereum/go-ethereum/core/state"
  10. )
  11. type Environment interface {
  12. State() *state.StateDB
  13. Origin() common.Address
  14. BlockNumber() *big.Int
  15. GetHash(n uint64) common.Hash
  16. Coinbase() common.Address
  17. Time() int64
  18. Difficulty() *big.Int
  19. GasLimit() *big.Int
  20. Transfer(from, to Account, amount *big.Int) error
  21. AddLog(state.Log)
  22. VmType() Type
  23. Depth() int
  24. SetDepth(i int)
  25. Call(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  26. CallCode(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  27. Create(me ContextRef, addr *common.Address, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
  28. }
  29. type Account interface {
  30. SubBalance(amount *big.Int)
  31. AddBalance(amount *big.Int)
  32. Balance() *big.Int
  33. Address() common.Address
  34. }
  35. // generic transfer method
  36. func Transfer(from, to Account, amount *big.Int) error {
  37. //fmt.Printf(":::%x::: %v < %v\n", from.Address(), from.Balance(), amount)
  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 common.Address
  47. topics []common.Hash
  48. data []byte
  49. log uint64
  50. }
  51. func (self *Log) Address() common.Address {
  52. return self.address
  53. }
  54. func (self *Log) Topics() []common.Hash {
  55. return self.topics
  56. }
  57. func (self *Log) Data() []byte {
  58. return self.data
  59. }
  60. func (self *Log) Number() uint64 {
  61. return self.log
  62. }
  63. func (self *Log) EncodeRLP(w io.Writer) error {
  64. return rlp.Encode(w, []interface{}{self.address, self.topics, self.data})
  65. }
  66. /*
  67. func (self *Log) RlpData() interface{} {
  68. return []interface{}{self.address, common.ByteSliceToInterface(self.topics), self.data}
  69. }
  70. */
  71. func (self *Log) String() string {
  72. return fmt.Sprintf("{%x %x %x}", self.address, self.data, self.topics)
  73. }