good morning!!!!

Skip to content
Snippets Groups Projects
execution.go 2.41 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    package vm
    
    	"github.com/ethereum/go-ethereum/ethstate"
    	"github.com/ethereum/go-ethereum/ethutil"
    
    )
    
    type Execution struct {
    	vm                VirtualMachine
    	address, input    []byte
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	Gas, price, value *big.Int
    
    	object            *ethstate.StateObject
    
    	SkipTransfer      bool
    
    }
    
    func NewExecution(vm VirtualMachine, address, input []byte, gas, gasPrice, value *big.Int) *Execution {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    	return &Execution{vm: vm, address: address, input: input, Gas: gas, price: gasPrice, value: value}
    
    }
    
    func (self *Execution) Addr() []byte {
    	return self.address
    }
    
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    func (self *Execution) Exec(codeAddr []byte, caller ClosureRef) ([]byte, error) {
    	// Retrieve the executing code
    	code := self.vm.Env().State().GetCode(codeAddr)
    
    	return self.exec(code, codeAddr, caller)
    }
    
    func (self *Execution) exec(code, caddr []byte, caller ClosureRef) (ret []byte, err error) {
    
    	env := self.vm.Env()
    
    	snapshot := env.State().Copy()
    
    	defer func() {
    
    		if IsDepthErr(err) || IsOOGErr(err) {
    
    			env.State().Set(snapshot)
    		}
    	}()
    
    
    	msg := env.State().Manifest().AddMessage(&ethstate.Message{
    		To: self.address, From: caller.Address(),
    		Input:  self.input,
    		Origin: env.Origin(),
    		Block:  env.BlockHash(), Timestamp: env.Time(), Coinbase: env.Coinbase(), Number: env.BlockNumber(),
    		Value: self.value,
    	})
    
    
    	from, to := caller.Object(), env.State().GetOrNewStateObject(self.address)
    
    	// Skipping transfer is used on testing for the initial call
    	if !self.SkipTransfer {
    		err = env.Transfer(from, to, self.value)
    	}
    
    
    	if err != nil {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    		caller.ReturnGas(self.Gas, self.price)
    
    		err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance)
    
    		self.object = to
    
    		// Pre-compiled contracts (address.go) 1, 2 & 3.
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    		naddr := ethutil.BigD(caddr).Uint64()
    
    		if p := Precompiled[naddr]; p != nil {
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    			if self.Gas.Cmp(p.Gas) >= 0 {
    
    				ret = p.Call(self.input)
    
    				self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret)
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    			// Create a new callable closure
    
    			c := NewClosure(msg, caller, to, code, self.Gas, self.price)
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    			c.exe = self
    
    
    			if self.vm.Depth() == MaxCallDepth {
    
    				c.UseGas(self.Gas)
    
    				return c.Return(nil), DepthError{}
    
    
    			// Executer the closure and get the return value (if any)
    			ret, _, err = c.Call(self.vm, self.input)
    			msg.Output = ret
    		}
    	}
    
    	return
    }
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    
    func (self *Execution) Create(caller ClosureRef) (ret []byte, err error) {
    	return self.exec(self.input, nil, caller)
    }