|
17 | 17 | package core |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "errors" |
20 | 21 | "math/big" |
21 | 22 |
|
22 | 23 | "github.com/ethereum/go-ethereum/common" |
23 | | - "github.com/ethereum/go-ethereum/core/state" |
24 | 24 | "github.com/ethereum/go-ethereum/core/vm" |
25 | 25 | "github.com/ethereum/go-ethereum/crypto" |
26 | 26 | "github.com/ethereum/go-ethereum/params" |
27 | 27 | ) |
28 | 28 |
|
29 | | -// Execution is the execution environment for the given call or create action. |
30 | | -type Execution struct { |
31 | | - env vm.Environment |
32 | | - address *common.Address |
33 | | - input []byte |
34 | | - evm vm.VirtualMachine |
35 | | - |
36 | | - Gas, price, value *big.Int |
| 29 | +// Call executes within the given contract |
| 30 | +func Call(env vm.Environment, caller vm.ContractRef, addr common.Address, input []byte, gas, gasPrice, value *big.Int) (ret []byte, err error) { |
| 31 | + ret, _, err = exec(env, caller, &addr, &addr, input, env.Db().GetCode(addr), gas, gasPrice, value) |
| 32 | + return ret, err |
37 | 33 | } |
38 | 34 |
|
39 | | -// NewExecution returns a new execution environment that handles all calling |
40 | | -// and creation logic defined by the YP. |
41 | | -func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution { |
42 | | - exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value} |
43 | | - exe.evm = vm.NewVm(env) |
44 | | - return exe |
| 35 | +// CallCode executes the given address' code as the given contract address |
| 36 | +func CallCode(env vm.Environment, caller vm.ContractRef, addr common.Address, input []byte, gas, gasPrice, value *big.Int) (ret []byte, err error) { |
| 37 | + prev := caller.Address() |
| 38 | + ret, _, err = exec(env, caller, &prev, &addr, input, env.Db().GetCode(addr), gas, gasPrice, value) |
| 39 | + return ret, err |
45 | 40 | } |
46 | 41 |
|
47 | | -// Call executes within the given context |
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 | | - |
52 | | - return self.exec(&codeAddr, code, caller) |
53 | | -} |
54 | | - |
55 | | -// Create creates a new contract and runs the initialisation procedure of the |
56 | | -// contract. This returns the returned code for the contract and is stored |
57 | | -// elsewhere. |
58 | | -func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) { |
59 | | - // Input must be nil for create |
60 | | - code := self.input |
61 | | - self.input = nil |
62 | | - ret, err = self.exec(nil, code, caller) |
| 42 | +// Create creates a new contract with the given code |
| 43 | +func Create(env vm.Environment, caller vm.ContractRef, code []byte, gas, gasPrice, value *big.Int) (ret []byte, address common.Address, err error) { |
| 44 | + ret, address, err = exec(env, caller, nil, nil, nil, code, gas, gasPrice, value) |
63 | 45 | // Here we get an error if we run into maximum stack depth, |
64 | 46 | // See: https://github.com/ethereum/yellowpaper/pull/131 |
65 | 47 | // and YP definitions for CREATE instruction |
66 | 48 | if err != nil { |
67 | | - return nil, err, nil |
| 49 | + return nil, address, err |
68 | 50 | } |
69 | | - account = self.env.State().GetStateObject(*self.address) |
70 | | - return |
| 51 | + return ret, address, err |
71 | 52 | } |
72 | 53 |
|
73 | | -// exec executes the given code and executes within the contextAddr context. |
74 | | -func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) { |
75 | | - env := self.env |
76 | | - evm := self.evm |
| 54 | +func exec(env vm.Environment, caller vm.ContractRef, address, codeAddr *common.Address, input, code []byte, gas, gasPrice, value *big.Int) (ret []byte, addr common.Address, err error) { |
| 55 | + evm := vm.NewVm(env) |
| 56 | + |
77 | 57 | // Depth check execution. Fail if we're trying to execute above the |
78 | 58 | // limit. |
79 | 59 | if env.Depth() > int(params.CallCreateDepth.Int64()) { |
80 | | - caller.ReturnGas(self.Gas, self.price) |
| 60 | + caller.ReturnGas(gas, gasPrice) |
81 | 61 |
|
82 | | - return nil, vm.DepthError |
| 62 | + return nil, common.Address{}, vm.DepthError |
83 | 63 | } |
84 | 64 |
|
85 | | - if !env.CanTransfer(env.State().GetStateObject(caller.Address()), self.value) { |
86 | | - caller.ReturnGas(self.Gas, self.price) |
| 65 | + if !env.CanTransfer(caller.Address(), value) { |
| 66 | + caller.ReturnGas(gas, gasPrice) |
87 | 67 |
|
88 | | - return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, env.State().GetBalance(caller.Address())) |
| 68 | + return nil, common.Address{}, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", value, env.Db().GetBalance(caller.Address())) |
89 | 69 | } |
90 | 70 |
|
91 | 71 | var createAccount bool |
92 | | - if self.address == nil { |
| 72 | + if address == nil { |
93 | 73 | // Generate a new address |
94 | | - nonce := env.State().GetNonce(caller.Address()) |
95 | | - env.State().SetNonce(caller.Address(), nonce+1) |
| 74 | + nonce := env.Db().GetNonce(caller.Address()) |
| 75 | + env.Db().SetNonce(caller.Address(), nonce+1) |
96 | 76 |
|
97 | | - addr := crypto.CreateAddress(caller.Address(), nonce) |
| 77 | + addr = crypto.CreateAddress(caller.Address(), nonce) |
98 | 78 |
|
99 | | - self.address = &addr |
| 79 | + address = &addr |
100 | 80 | createAccount = true |
101 | 81 | } |
102 | | - snapshot := env.State().Copy() |
| 82 | + snapshot := env.MakeSnapshot() |
103 | 83 |
|
104 | 84 | var ( |
105 | | - from = env.State().GetStateObject(caller.Address()) |
106 | | - to *state.StateObject |
| 85 | + from = env.Db().GetAccount(caller.Address()) |
| 86 | + to vm.Account |
107 | 87 | ) |
108 | 88 | if createAccount { |
109 | | - to = env.State().CreateAccount(*self.address) |
| 89 | + to = env.Db().CreateAccount(*address) |
110 | 90 | } else { |
111 | | - to = env.State().GetOrNewStateObject(*self.address) |
| 91 | + if !env.Db().Exist(*address) { |
| 92 | + to = env.Db().CreateAccount(*address) |
| 93 | + } else { |
| 94 | + to = env.Db().GetAccount(*address) |
| 95 | + } |
112 | 96 | } |
113 | | - vm.Transfer(from, to, self.value) |
| 97 | + env.Transfer(from, to, value) |
114 | 98 |
|
115 | | - context := vm.NewContext(caller, to, self.value, self.Gas, self.price) |
116 | | - context.SetCallCode(contextAddr, code) |
| 99 | + contract := vm.NewContract(caller, to, value, gas, gasPrice) |
| 100 | + contract.SetCallCode(codeAddr, code) |
117 | 101 |
|
118 | | - ret, err = evm.Run(context, self.input) |
| 102 | + ret, err = evm.Run(contract, input) |
119 | 103 | if err != nil { |
120 | | - env.State().Set(snapshot) |
| 104 | + env.SetSnapshot(snapshot) //env.Db().Set(snapshot) |
121 | 105 | } |
122 | 106 |
|
123 | | - return |
| 107 | + return ret, addr, err |
| 108 | +} |
| 109 | + |
| 110 | +// generic transfer method |
| 111 | +func Transfer(from, to vm.Account, amount *big.Int) error { |
| 112 | + if from.Balance().Cmp(amount) < 0 { |
| 113 | + return errors.New("Insufficient balance in account") |
| 114 | + } |
| 115 | + |
| 116 | + from.SubBalance(amount) |
| 117 | + to.AddBalance(amount) |
| 118 | + |
| 119 | + return nil |
124 | 120 | } |
0 commit comments