execution.go 2.52 KB
Newer Older
obscuren's avatar
obscuren committed
1
package core
2 3 4

import (
	"math/big"
5
	"time"
6

obscuren's avatar
obscuren committed
7
	"github.com/ethereum/go-ethereum/common"
obscuren's avatar
obscuren committed
8 9
	"github.com/ethereum/go-ethereum/core/state"
	"github.com/ethereum/go-ethereum/core/vm"
obscuren's avatar
obscuren committed
10
	"github.com/ethereum/go-ethereum/crypto"
11
	"github.com/ethereum/go-ethereum/params"
12 13 14
)

type Execution struct {
obscuren's avatar
obscuren committed
15 16 17 18 19
	env     vm.Environment
	address *common.Address
	input   []byte
	evm     vm.VirtualMachine

20 21 22
	Gas, price, value *big.Int
}

obscuren's avatar
obscuren committed
23
func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution {
obscuren's avatar
obscuren committed
24 25 26
	exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
	exe.evm = vm.NewVm(env)
	return exe
27 28
}

obscuren's avatar
obscuren committed
29
func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) {
30
	// Retrieve the executing code
31
	code := self.env.State().GetCode(codeAddr)
32

obscuren's avatar
obscuren committed
33
	return self.exec(&codeAddr, code, caller)
34 35
}

obscuren's avatar
obscuren committed
36
func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
obscuren's avatar
obscuren committed
37 38 39 40
	// Input must be nil for create
	code := self.input
	self.input = nil
	ret, err = self.exec(nil, code, caller)
obscuren's avatar
obscuren committed
41 42 43 44
	account = self.env.State().GetStateObject(*self.address)
	return
}

obscuren's avatar
obscuren committed
45
func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) {
obscuren's avatar
obscuren committed
46 47
	start := time.Now()

obscuren's avatar
obscuren committed
48
	env := self.env
obscuren's avatar
obscuren committed
49
	evm := self.evm
50
	if env.Depth() > int(params.CallCreateDepth.Int64()) {
51 52
		caller.ReturnGas(self.Gas, self.price)

53 54 55
		return nil, vm.DepthError{}
	}

56
	vsnapshot := env.State().Copy()
57
	var createAccount bool
obscuren's avatar
obscuren committed
58
	if self.address == nil {
obscuren's avatar
obscuren committed
59 60 61
		// Generate a new address
		nonce := env.State().GetNonce(caller.Address())
		env.State().SetNonce(caller.Address(), nonce+1)
62 63 64

		addr := crypto.CreateAddress(caller.Address(), nonce)

65
		self.address = &addr
66
		createAccount = true
obscuren's avatar
obscuren committed
67
	}
68
	snapshot := env.State().Copy()
obscuren's avatar
obscuren committed
69

70 71 72 73 74 75 76 77 78 79
	var (
		from = env.State().GetStateObject(caller.Address())
		to   *state.StateObject
	)
	if createAccount {
		to = env.State().CreateAccount(*self.address)
	} else {
		to = env.State().GetOrNewStateObject(*self.address)
	}

obscuren's avatar
obscuren committed
80 81
	err = env.Transfer(from, to, self.value)
	if err != nil {
82 83 84
		env.State().Set(vsnapshot)

		caller.ReturnGas(self.Gas, self.price)
obscuren's avatar
obscuren committed
85

obscuren's avatar
obscuren committed
86
		return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance())
obscuren's avatar
obscuren committed
87 88
	}

89 90 91
	context := vm.NewContext(caller, to, self.value, self.Gas, self.price)
	context.SetCallCode(contextAddr, code)

obscuren's avatar
obscuren committed
92 93
	ret, err = evm.Run(context, self.input)
	evm.Printf("message call took %v", time.Since(start)).Endl()
94 95 96
	if err != nil {
		env.State().Set(snapshot)
	}
97 98 99

	return
}