execution.go 3.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2014 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// 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.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with go-ethereum.  If not, see <http://www.gnu.org/licenses/>.

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
)

type Execution struct {
obscuren's avatar
obscuren committed
30 31 32 33 34
	env     vm.Environment
	address *common.Address
	input   []byte
	evm     vm.VirtualMachine

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

obscuren's avatar
obscuren committed
38
func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution {
obscuren's avatar
obscuren committed
39 40 41
	exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
	exe.evm = vm.NewVm(env)
	return exe
42 43
}

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

obscuren's avatar
obscuren committed
48
	return self.exec(&codeAddr, code, caller)
49 50
}

obscuren's avatar
obscuren committed
51
func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
obscuren's avatar
obscuren committed
52 53 54 55
	// Input must be nil for create
	code := self.input
	self.input = nil
	ret, err = self.exec(nil, code, caller)
56 57 58 59 60 61
	// 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
62 63 64 65
	account = self.env.State().GetStateObject(*self.address)
	return
}

obscuren's avatar
obscuren committed
66
func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) {
obscuren's avatar
obscuren committed
67
	env := self.env
obscuren's avatar
obscuren committed
68
	evm := self.evm
69
	if env.Depth() > int(params.CallCreateDepth.Int64()) {
70 71
		caller.ReturnGas(self.Gas, self.price)

72
		return nil, vm.DepthError
73 74
	}

75
	vsnapshot := env.State().Copy()
76
	var createAccount bool
obscuren's avatar
obscuren committed
77
	if self.address == nil {
obscuren's avatar
obscuren committed
78 79 80
		// Generate a new address
		nonce := env.State().GetNonce(caller.Address())
		env.State().SetNonce(caller.Address(), nonce+1)
81 82 83

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

84
		self.address = &addr
85
		createAccount = true
obscuren's avatar
obscuren committed
86
	}
87
	snapshot := env.State().Copy()
obscuren's avatar
obscuren committed
88

89 90 91 92 93 94 95 96 97 98
	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
99 100
	err = env.Transfer(from, to, self.value)
	if err != nil {
101 102 103
		env.State().Set(vsnapshot)

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

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

108 109 110
	context := vm.NewContext(caller, to, self.value, self.Gas, self.price)
	context.SetCallCode(contextAddr, code)

obscuren's avatar
obscuren committed
111
	ret, err = evm.Run(context, self.input)
112 113 114
	if err != nil {
		env.State().Set(snapshot)
	}
115 116 117

	return
}