good morning!!!!

Skip to content
Snippets Groups Projects
environment.go 1.96 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    package vm
    
    	"errors"
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	"io"
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	"github.com/ethereum/go-ethereum/common"
    
    	"github.com/ethereum/go-ethereum/core/state"
    
    	"github.com/ethereum/go-ethereum/rlp"
    
    )
    
    type Environment interface {
    
    	State() *state.StateDB
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	Origin() common.Address
    
    	BlockNumber() *big.Int
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	GetHash(n uint64) common.Hash
    	Coinbase() common.Address
    
    	Time() int64
    	Difficulty() *big.Int
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	GasLimit() *big.Int
    
    	Transfer(from, to Account, amount *big.Int) error
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	AddLog(state.Log)
    
    	Depth() int
    	SetDepth(i int)
    
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	Call(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
    	CallCode(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
    
    	Create(me ContextRef, addr *common.Address, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
    
    type Account interface {
    	SubBalance(amount *big.Int)
    	AddBalance(amount *big.Int)
    	Balance() *big.Int
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	Address() common.Address
    
    }
    
    // generic transfer method
    func Transfer(from, to Account, amount *big.Int) error {
    
    	//fmt.Printf(":::%x::: %v < %v\n", from.Address(), from.Balance(), amount)
    
    	if from.Balance().Cmp(amount) < 0 {
    		return errors.New("Insufficient balance in account")
    	}
    
    	from.SubBalance(amount)
    	to.AddBalance(amount)
    
    	return nil
    }
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    
    type Log struct {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	address common.Address
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	topics  []common.Hash
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	data    []byte
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	log     uint64
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    func (self *Log) Address() common.Address {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	return self.address
    }
    
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    func (self *Log) Topics() []common.Hash {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	return self.topics
    }
    
    func (self *Log) Data() []byte {
    	return self.data
    }
    
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    func (self *Log) Number() uint64 {
    	return self.log
    }
    
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    func (self *Log) EncodeRLP(w io.Writer) error {
    	return rlp.Encode(w, []interface{}{self.address, self.topics, self.data})
    }
    
    /*
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    func (self *Log) RlpData() interface{} {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	return []interface{}{self.address, common.ByteSliceToInterface(self.topics), self.data}
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    }
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    */
    
    
    func (self *Log) String() string {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	return fmt.Sprintf("{%x %x %x}", self.address, self.data, self.topics)