execution.go 3.98 KB
Newer Older
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
5 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library 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 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16

obscuren's avatar
obscuren committed
17
package core
18 19 20 21

import (
	"math/big"

obscuren's avatar
obscuren committed
22
	"github.com/ethereum/go-ethereum/common"
obscuren's avatar
obscuren committed
23 24
	"github.com/ethereum/go-ethereum/core/state"
	"github.com/ethereum/go-ethereum/core/vm"
obscuren's avatar
obscuren committed
25
	"github.com/ethereum/go-ethereum/crypto"
26
	"github.com/ethereum/go-ethereum/params"
27 28
)

29
// Execution is the execution environment for the given call or create action.
30
type Execution struct {
obscuren's avatar
obscuren committed
31 32 33 34 35
	env     vm.Environment
	address *common.Address
	input   []byte
	evm     vm.VirtualMachine

36 37 38
	Gas, price, value *big.Int
}

39 40
// NewExecution returns a new execution environment that handles all calling
// and creation logic defined by the YP.
obscuren's avatar
obscuren committed
41
func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution {
obscuren's avatar
obscuren committed
42 43 44
	exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
	exe.evm = vm.NewVm(env)
	return exe
45 46
}

47
// Call executes within the given context
obscuren's avatar
obscuren committed
48
func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) {
49
	// Retrieve the executing code
50
	code := self.env.State().GetCode(codeAddr)
51

obscuren's avatar
obscuren committed
52
	return self.exec(&codeAddr, code, caller)
53 54
}

55 56 57
// Create creates a new contract and runs the initialisation procedure of the
// contract. This returns the returned code for the contract and is stored
// elsewhere.
obscuren's avatar
obscuren committed
58
func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
obscuren's avatar
obscuren committed
59 60 61 62
	// Input must be nil for create
	code := self.input
	self.input = nil
	ret, err = self.exec(nil, code, caller)
63 64 65 66 67 68
	// Here we get an error if we run into maximum stack depth,
	// See: https://github.com/ethereum/yellowpaper/pull/131
	// and YP definitions for CREATE instruction
	if err != nil {
		return nil, err, nil
	}
obscuren's avatar
obscuren committed
69 70 71 72
	account = self.env.State().GetStateObject(*self.address)
	return
}

73
// exec executes the given code and executes within the contextAddr context.
obscuren's avatar
obscuren committed
74
func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) {
obscuren's avatar
obscuren committed
75
	env := self.env
obscuren's avatar
obscuren committed
76
	evm := self.evm
77 78
	// Depth check execution. Fail if we're trying to execute above the
	// limit.
79
	if env.Depth() > int(params.CallCreateDepth.Int64()) {
80 81
		caller.ReturnGas(self.Gas, self.price)

82
		return nil, vm.DepthError
83 84
	}

85 86 87 88 89 90
	if !env.CanTransfer(env.State().GetStateObject(caller.Address()), self.value) {
		caller.ReturnGas(self.Gas, self.price)

		return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, env.State().GetBalance(caller.Address()))
	}

91
	var createAccount bool
obscuren's avatar
obscuren committed
92
	if self.address == nil {
obscuren's avatar
obscuren committed
93 94 95
		// Generate a new address
		nonce := env.State().GetNonce(caller.Address())
		env.State().SetNonce(caller.Address(), nonce+1)
96 97 98

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

99
		self.address = &addr
100
		createAccount = true
obscuren's avatar
obscuren committed
101
	}
102
	snapshot := env.State().Copy()
obscuren's avatar
obscuren committed
103

104 105 106 107 108 109 110 111 112
	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)
	}
113
	vm.Transfer(from, to, self.value)
obscuren's avatar
obscuren committed
114

115 116 117
	context := vm.NewContext(caller, to, self.value, self.Gas, self.price)
	context.SetCallCode(contextAddr, code)

obscuren's avatar
obscuren committed
118
	ret, err = evm.Run(context, self.input)
119 120 121
	if err != nil {
		env.State().Set(snapshot)
	}
122 123 124

	return
}