state_transition.go 5.8 KB
Newer Older
O
obscuren 已提交
1
package core
2 3 4

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

7 8
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/ethereum/go-ethereum/ethutil"
O
obscuren 已提交
9
	"github.com/ethereum/go-ethereum/state"
10
	"github.com/ethereum/go-ethereum/vm"
11 12
)

O
obscuren 已提交
13 14
const tryJit = false

O
obscuren 已提交
15
var ()
O
obscuren 已提交
16

O
obscuren 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * The State transitioning model
 *
 * A state transition is a change made when a transaction is applied to the current world state
 * The state transitioning model does all all the necessary work to work out a valid new state root.
 * 1) Nonce handling
 * 2) Pre pay / buy gas of the coinbase (miner)
 * 3) Create a new state object if the recipient is \0*32
 * 4) Value transfer
 * == If contract creation ==
 * 4a) Attempt to run transaction data
 * 4b) If valid, use result as code for the new state object
 * == end ==
 * 5) Run Script section
 * 6) Derive new state root
 */
33
type StateTransition struct {
34 35 36 37 38 39 40
	coinbase      []byte
	msg           Message
	gas, gasPrice *big.Int
	initialGas    *big.Int
	value         *big.Int
	data          []byte
	state         *state.StateDB
41

O
obscuren 已提交
42
	cb, rec, sen *state.StateObject
43

44
	env vm.Environment
45 46 47 48 49 50 51 52 53 54 55 56
}

type Message interface {
	From() []byte
	To() []byte

	GasPrice() *big.Int
	Gas() *big.Int
	Value() *big.Int

	Nonce() uint64
	Data() []byte
57 58
}

59 60 61 62 63
func AddressFromMessage(msg Message) []byte {
	// Generate a new address
	return crypto.Sha3(ethutil.NewValue([]interface{}{msg.From(), msg.Nonce()}).Encode())[12:]
}

O
obscuren 已提交
64 65 66 67
func MessageCreatesContract(msg Message) bool {
	return len(msg.To()) == 0
}

O
obscuren 已提交
68 69 70 71
func MessageGasValue(msg Message) *big.Int {
	return new(big.Int).Mul(msg.Gas(), msg.GasPrice())
}

72 73 74 75 76 77 78 79 80 81 82 83
func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateObject) *StateTransition {
	return &StateTransition{
		coinbase:   coinbase.Address(),
		env:        env,
		msg:        msg,
		gas:        new(big.Int),
		gasPrice:   new(big.Int).Set(msg.GasPrice()),
		initialGas: new(big.Int),
		value:      msg.Value(),
		data:       msg.Data(),
		state:      env.State(),
		cb:         coinbase,
84
	}
85 86
}

O
obscuren 已提交
87
func (self *StateTransition) Coinbase() *state.StateObject {
O
obscuren 已提交
88
	return self.state.GetOrNewStateObject(self.coinbase)
89
}
90
func (self *StateTransition) From() *state.StateObject {
O
obscuren 已提交
91
	return self.state.GetOrNewStateObject(self.msg.From())
92
}
93
func (self *StateTransition) To() *state.StateObject {
O
obscuren 已提交
94
	if self.msg != nil && MessageCreatesContract(self.msg) {
95 96
		return nil
	}
O
obscuren 已提交
97
	return self.state.GetOrNewStateObject(self.msg.To())
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
}

func (self *StateTransition) UseGas(amount *big.Int) error {
	if self.gas.Cmp(amount) < 0 {
		return OutOfGasError()
	}
	self.gas.Sub(self.gas, amount)

	return nil
}

func (self *StateTransition) AddGas(amount *big.Int) {
	self.gas.Add(self.gas, amount)
}

func (self *StateTransition) BuyGas() error {
	var err error

116
	sender := self.From()
O
obscuren 已提交
117
	if sender.Balance().Cmp(MessageGasValue(self.msg)) < 0 {
118
		return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address()[:4], MessageGasValue(self.msg), sender.Balance())
119 120 121
	}

	coinbase := self.Coinbase()
122
	err = coinbase.BuyGas(self.msg.Gas(), self.msg.GasPrice())
123 124 125 126
	if err != nil {
		return err
	}

127
	self.AddGas(self.msg.Gas())
O
obscuren 已提交
128
	self.initialGas.Set(self.msg.Gas())
O
obscuren 已提交
129
	sender.SubBalance(MessageGasValue(self.msg))
130 131 132 133

	return nil
}

O
obscuren 已提交
134 135
func (self *StateTransition) preCheck() (err error) {
	var (
136 137
		msg    = self.msg
		sender = self.From()
O
obscuren 已提交
138 139 140
	)

	// Make sure this transaction's nonce is correct
141 142
	if sender.Nonce() != msg.Nonce() {
		return NonceError(msg.Nonce(), sender.Nonce())
O
obscuren 已提交
143 144 145 146
	}

	// Pre-pay gas / Buy gas of the coinbase account
	if err = self.BuyGas(); err != nil {
O
obscuren 已提交
147
		return InvalidTxError(err)
O
obscuren 已提交
148 149 150 151 152
	}

	return nil
}

O
obscuren 已提交
153
func (self *StateTransition) TransitionState() (ret []byte, err error) {
154
	// statelogger.Debugf("(~) %x\n", self.msg.Hash())
155

O
obscuren 已提交
156 157 158 159 160
	// XXX Transactions after this point are considered valid.
	if err = self.preCheck(); err != nil {
		return
	}

161
	var (
162 163
		msg    = self.msg
		sender = self.From()
164 165
	)

O
obscuren 已提交
166 167
	defer self.RefundGas()

O
obscuren 已提交
168
	// Transaction gas
O
obscuren 已提交
169
	if err = self.UseGas(vm.GasTx); err != nil {
O
obscuren 已提交
170
		return nil, InvalidTxError(err)
171 172
	}

O
obscuren 已提交
173 174 175 176
	// Increment the nonce for the next transaction
	self.state.SetNonce(sender.Address(), sender.Nonce()+1)
	//sender.Nonce += 1

O
obscuren 已提交
177
	// Pay data gas
O
obscuren 已提交
178 179 180
	var dgas int64
	for _, byt := range self.data {
		if byt != 0 {
O
obscuren 已提交
181
			dgas += vm.GasTxDataNonzeroByte.Int64()
O
obscuren 已提交
182
		} else {
O
obscuren 已提交
183
			dgas += vm.GasTxDataZeroByte.Int64()
O
obscuren 已提交
184 185 186
		}
	}
	if err = self.UseGas(big.NewInt(dgas)); err != nil {
187
		println("2")
O
obscuren 已提交
188
		return nil, InvalidTxError(err)
189 190
	}

O
obscuren 已提交
191
	//stateCopy := self.env.State().Copy()
192
	vmenv := self.env
O
obscuren 已提交
193
	var ref vm.ContextRef
O
obscuren 已提交
194
	if MessageCreatesContract(msg) {
195 196
		contract := MakeContract(msg, self.state)
		ret, err, ref = vmenv.Create(sender, contract.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value)
197 198 199
		if err == nil {
			dataGas := big.NewInt(int64(len(ret)))
			dataGas.Mul(dataGas, vm.GasCreateByte)
O
obscuren 已提交
200
			if err := self.UseGas(dataGas); err == nil {
201 202 203
				ref.SetCode(ret)
			}
		}
O
obscuren 已提交
204
	} else {
205
		ret, err = vmenv.Call(self.From(), self.To().Address(), self.msg.Data(), self.gas, self.gasPrice, self.value)
O
obscuren 已提交
206
	}
207

O
obscuren 已提交
208 209
	if err != nil && IsValueTransferErr(err) {
		return nil, InvalidTxError(err)
210 211 212 213
	}

	return
}
O
obscuren 已提交
214 215

// Converts an transaction in to a state object
216 217
func MakeContract(msg Message, state *state.StateDB) *state.StateObject {
	addr := AddressFromMessage(msg)
O
obscuren 已提交
218

O
obscuren 已提交
219
	contract := state.GetOrNewStateObject(addr)
220
	contract.SetInitCode(msg.Data())
O
obscuren 已提交
221

O
obscuren 已提交
222
	return contract
O
obscuren 已提交
223
}
O
obscuren 已提交
224 225

func (self *StateTransition) RefundGas() {
226 227 228
	coinbase, sender := self.Coinbase(), self.From()
	// Return remaining gas
	remaining := new(big.Int).Mul(self.gas, self.msg.GasPrice())
O
obscuren 已提交
229
	sender.AddBalance(remaining)
230

O
obscuren 已提交
231
	uhalf := new(big.Int).Div(self.GasUsed(), ethutil.Big2)
O
obscuren 已提交
232 233
	for addr, ref := range self.state.Refunds() {
		refund := ethutil.BigMin(uhalf, ref)
234
		self.gas.Add(self.gas, refund)
O
Bump  
obscuren 已提交
235
		self.state.AddBalance([]byte(addr), refund.Mul(refund, self.msg.GasPrice()))
O
obscuren 已提交
236 237
	}

238
	coinbase.RefundGas(self.gas, self.msg.GasPrice())
O
obscuren 已提交
239 240 241 242 243
}

func (self *StateTransition) GasUsed() *big.Int {
	return new(big.Int).Sub(self.initialGas, self.gas)
}