environment.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package vm
  17. import (
  18. "errors"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/state"
  22. )
  23. // Environment is is required by the virtual machine to get information from
  24. // it's own isolated environment. For an example see `core.VMEnv`
  25. type Environment interface {
  26. State() *state.StateDB
  27. Origin() common.Address
  28. BlockNumber() *big.Int
  29. GetHash(n uint64) common.Hash
  30. Coinbase() common.Address
  31. Time() uint64
  32. Difficulty() *big.Int
  33. GasLimit() *big.Int
  34. Transfer(from, to Account, amount *big.Int) error
  35. AddLog(*state.Log)
  36. AddStructLog(StructLog)
  37. StructLogs() []StructLog
  38. VmType() Type
  39. Depth() int
  40. SetDepth(i int)
  41. Call(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  42. CallCode(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
  43. Create(me ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
  44. }
  45. // StructLog is emited to the Environment each cycle and lists information about the curent internal state
  46. // prior to the execution of the statement.
  47. type StructLog struct {
  48. Pc uint64
  49. Op OpCode
  50. Gas *big.Int
  51. GasCost *big.Int
  52. Memory []byte
  53. Stack []*big.Int
  54. Storage map[common.Hash][]byte
  55. Err error
  56. }
  57. type Account interface {
  58. SubBalance(amount *big.Int)
  59. AddBalance(amount *big.Int)
  60. Balance() *big.Int
  61. Address() common.Address
  62. }
  63. // generic transfer method
  64. func Transfer(from, to Account, amount *big.Int) error {
  65. if from.Balance().Cmp(amount) < 0 {
  66. return errors.New("Insufficient balance in account")
  67. }
  68. from.SubBalance(amount)
  69. to.AddBalance(amount)
  70. return nil
  71. }