Skip to content

Commit db49417

Browse files
committed
Created generic message (easy for testing)
1 parent 9e286e1 commit db49417

File tree

10 files changed

+159
-102
lines changed

10 files changed

+159
-102
lines changed

cmd/mist/gui.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,13 @@ func (gui *Gui) insertTransaction(window string, tx *types.Transaction) {
305305

306306
var (
307307
ptx = xeth.NewJSTx(tx, pipe.World().State())
308-
send = nameReg.Storage(tx.Sender())
309-
rec = nameReg.Storage(tx.Recipient)
308+
send = nameReg.Storage(tx.From())
309+
rec = nameReg.Storage(tx.To())
310310
s, r string
311311
)
312312

313313
if tx.CreatesContract() {
314-
rec = nameReg.Storage(tx.CreationAddress(pipe.World().State()))
314+
rec = nameReg.Storage(core.AddressFromMessage(tx))
315315
}
316316

317317
if send.Len() != 0 {
@@ -323,9 +323,9 @@ func (gui *Gui) insertTransaction(window string, tx *types.Transaction) {
323323
r = strings.Trim(rec.Str(), "\x00")
324324
} else {
325325
if tx.CreatesContract() {
326-
r = ethutil.Bytes2Hex(tx.CreationAddress(pipe.World().State()))
326+
r = ethutil.Bytes2Hex(core.AddressFromMessage(tx))
327327
} else {
328-
r = ethutil.Bytes2Hex(tx.Recipient)
328+
r = ethutil.Bytes2Hex(tx.To())
329329
}
330330
}
331331
ptx.Sender = s
@@ -449,11 +449,11 @@ func (gui *Gui) update() {
449449
object := state.GetAccount(gui.address())
450450

451451
if bytes.Compare(tx.Sender(), gui.address()) == 0 {
452-
object.SubAmount(tx.Value)
452+
object.SubAmount(tx.Value())
453453

454454
gui.txDb.Put(tx.Hash(), tx.RlpEncode())
455-
} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
456-
object.AddAmount(tx.Value)
455+
} else if bytes.Compare(tx.To(), gui.address()) == 0 {
456+
object.AddAmount(tx.Value())
457457

458458
gui.txDb.Put(tx.Hash(), tx.RlpEncode())
459459
}

core/block_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ done:
111111
// If we are mining this block and validating we want to set the logs back to 0
112112
state.EmptyLogs()
113113

114-
txGas := new(big.Int).Set(tx.Gas)
114+
txGas := new(big.Int).Set(tx.Gas())
115115

116116
cb := state.GetStateObject(coinbase.Address())
117117
st := NewStateTransition(cb, tx, state, block)
@@ -134,7 +134,7 @@ done:
134134
}
135135

136136
txGas.Sub(txGas, st.gas)
137-
cumulativeSum.Add(cumulativeSum, new(big.Int).Mul(txGas, tx.GasPrice))
137+
cumulativeSum.Add(cumulativeSum, new(big.Int).Mul(txGas, tx.GasPrice()))
138138

139139
// Update the state with pending changes
140140
state.Update(txGas)

core/state_transition.go

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"math/big"
66

77
"github.com/ethereum/go-ethereum/core/types"
8+
"github.com/ethereum/go-ethereum/crypto"
9+
"github.com/ethereum/go-ethereum/ethutil"
810
"github.com/ethereum/go-ethereum/state"
911
"github.com/ethereum/go-ethereum/vm"
1012
)
@@ -27,18 +29,50 @@ import (
2729
*/
2830
type StateTransition struct {
2931
coinbase, receiver []byte
30-
tx *types.Transaction
32+
msg Message
3133
gas, gasPrice *big.Int
3234
value *big.Int
3335
data []byte
3436
state *state.StateDB
3537
block *types.Block
3638

3739
cb, rec, sen *state.StateObject
40+
41+
Env vm.Environment
42+
}
43+
44+
type Message interface {
45+
Hash() []byte
46+
47+
CreatesContract() bool
48+
49+
From() []byte
50+
To() []byte
51+
52+
GasValue() *big.Int
53+
GasPrice() *big.Int
54+
Gas() *big.Int
55+
Value() *big.Int
56+
57+
Nonce() uint64
58+
Data() []byte
3859
}
3960

40-
func NewStateTransition(coinbase *state.StateObject, tx *types.Transaction, state *state.StateDB, block *types.Block) *StateTransition {
41-
return &StateTransition{coinbase.Address(), tx.Recipient, tx, new(big.Int), new(big.Int).Set(tx.GasPrice), tx.Value, tx.Data, state, block, coinbase, nil, nil}
61+
func AddressFromMessage(msg Message) []byte {
62+
// Generate a new address
63+
return crypto.Sha3(ethutil.NewValue([]interface{}{msg.From(), msg.Nonce()}).Encode())[12:]
64+
}
65+
66+
func NewStateTransition(coinbase *state.StateObject, msg Message, state *state.StateDB, block *types.Block) *StateTransition {
67+
return &StateTransition{coinbase.Address(), msg.To(), msg, new(big.Int), new(big.Int).Set(msg.GasPrice()), msg.Value(), msg.Data(), state, block, coinbase, nil, nil, nil}
68+
}
69+
70+
func (self *StateTransition) VmEnv() vm.Environment {
71+
if self.Env == nil {
72+
self.Env = NewEnv(self.state, self.msg, self.block)
73+
}
74+
75+
return self.Env
4276
}
4377

4478
func (self *StateTransition) Coinbase() *state.StateObject {
@@ -49,25 +83,25 @@ func (self *StateTransition) Coinbase() *state.StateObject {
4983
self.cb = self.state.GetOrNewStateObject(self.coinbase)
5084
return self.cb
5185
}
52-
func (self *StateTransition) Sender() *state.StateObject {
86+
func (self *StateTransition) From() *state.StateObject {
5387
if self.sen != nil {
5488
return self.sen
5589
}
5690

57-
self.sen = self.state.GetOrNewStateObject(self.tx.Sender())
91+
self.sen = self.state.GetOrNewStateObject(self.msg.From())
5892

5993
return self.sen
6094
}
61-
func (self *StateTransition) Receiver() *state.StateObject {
62-
if self.tx != nil && self.tx.CreatesContract() {
95+
func (self *StateTransition) To() *state.StateObject {
96+
if self.msg != nil && self.msg.CreatesContract() {
6397
return nil
6498
}
6599

66100
if self.rec != nil {
67101
return self.rec
68102
}
69103

70-
self.rec = self.state.GetOrNewStateObject(self.tx.Recipient)
104+
self.rec = self.state.GetOrNewStateObject(self.msg.To())
71105
return self.rec
72106
}
73107

@@ -87,41 +121,41 @@ func (self *StateTransition) AddGas(amount *big.Int) {
87121
func (self *StateTransition) BuyGas() error {
88122
var err error
89123

90-
sender := self.Sender()
91-
if sender.Balance().Cmp(self.tx.GasValue()) < 0 {
92-
return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.tx.GasValue(), sender.Balance())
124+
sender := self.From()
125+
if sender.Balance().Cmp(self.msg.GasValue()) < 0 {
126+
return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.msg.GasValue(), sender.Balance())
93127
}
94128

95129
coinbase := self.Coinbase()
96-
err = coinbase.BuyGas(self.tx.Gas, self.tx.GasPrice)
130+
err = coinbase.BuyGas(self.msg.Gas(), self.msg.GasPrice())
97131
if err != nil {
98132
return err
99133
}
100134

101-
self.AddGas(self.tx.Gas)
102-
sender.SubAmount(self.tx.GasValue())
135+
self.AddGas(self.msg.Gas())
136+
sender.SubAmount(self.msg.GasValue())
103137

104138
return nil
105139
}
106140

107141
func (self *StateTransition) RefundGas() {
108-
coinbase, sender := self.Coinbase(), self.Sender()
109-
coinbase.RefundGas(self.gas, self.tx.GasPrice)
142+
coinbase, sender := self.Coinbase(), self.From()
143+
coinbase.RefundGas(self.gas, self.msg.GasPrice())
110144

111145
// Return remaining gas
112-
remaining := new(big.Int).Mul(self.gas, self.tx.GasPrice)
146+
remaining := new(big.Int).Mul(self.gas, self.msg.GasPrice())
113147
sender.AddAmount(remaining)
114148
}
115149

116150
func (self *StateTransition) preCheck() (err error) {
117151
var (
118-
tx = self.tx
119-
sender = self.Sender()
152+
msg = self.msg
153+
sender = self.From()
120154
)
121155

122156
// Make sure this transaction's nonce is correct
123-
if sender.Nonce != tx.Nonce {
124-
return NonceError(tx.Nonce, sender.Nonce)
157+
if sender.Nonce != msg.Nonce() {
158+
return NonceError(msg.Nonce(), sender.Nonce)
125159
}
126160

127161
// Pre-pay gas / Buy gas of the coinbase account
@@ -133,16 +167,16 @@ func (self *StateTransition) preCheck() (err error) {
133167
}
134168

135169
func (self *StateTransition) TransitionState() (err error) {
136-
statelogger.Debugf("(~) %x\n", self.tx.Hash())
170+
statelogger.Debugf("(~) %x\n", self.msg.Hash())
137171

138172
// XXX Transactions after this point are considered valid.
139173
if err = self.preCheck(); err != nil {
140174
return
141175
}
142176

143177
var (
144-
tx = self.tx
145-
sender = self.Sender()
178+
msg = self.msg
179+
sender = self.From()
146180
)
147181

148182
defer self.RefundGas()
@@ -169,15 +203,15 @@ func (self *StateTransition) TransitionState() (err error) {
169203
}
170204

171205
var ret []byte
172-
vmenv := NewEnv(self.state, self.tx, self.block)
206+
vmenv := self.VmEnv()
173207
var ref vm.ClosureRef
174-
if tx.CreatesContract() {
175-
self.rec = MakeContract(tx, self.state)
208+
if msg.CreatesContract() {
209+
self.rec = MakeContract(msg, self.state)
176210

177-
ret, err, ref = vmenv.Create(sender, self.rec.Address(), self.tx.Data, self.gas, self.gasPrice, self.value)
211+
ret, err, ref = vmenv.Create(sender, self.rec.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value)
178212
ref.SetCode(ret)
179213
} else {
180-
ret, err = vmenv.Call(self.Sender(), self.Receiver().Address(), self.tx.Data, self.gas, self.gasPrice, self.value)
214+
ret, err = vmenv.Call(self.From(), self.To().Address(), self.msg.Data(), self.gas, self.gasPrice, self.value)
181215
}
182216
if err != nil {
183217
statelogger.Debugln(err)
@@ -187,11 +221,11 @@ func (self *StateTransition) TransitionState() (err error) {
187221
}
188222

189223
// Converts an transaction in to a state object
190-
func MakeContract(tx *types.Transaction, state *state.StateDB) *state.StateObject {
191-
addr := tx.CreationAddress(state)
224+
func MakeContract(msg Message, state *state.StateDB) *state.StateObject {
225+
addr := AddressFromMessage(msg)
192226

193227
contract := state.GetOrNewStateObject(addr)
194-
contract.InitCode = tx.Data
228+
contract.InitCode = msg.Data()
195229

196230
return contract
197231
}

core/transaction_pool.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
112112
return fmt.Errorf("No last block on the block chain")
113113
}
114114

115-
if len(tx.Recipient) != 0 && len(tx.Recipient) != 20 {
116-
return fmt.Errorf("Invalid recipient. len = %d", len(tx.Recipient))
115+
if len(tx.To()) != 0 && len(tx.To()) != 20 {
116+
return fmt.Errorf("Invalid recipient. len = %d", len(tx.To()))
117117
}
118118

119119
v, _, _ := tx.Curve()
@@ -124,15 +124,15 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
124124
// Get the sender
125125
sender := pool.chainManager.State().GetAccount(tx.Sender())
126126

127-
totAmount := new(big.Int).Set(tx.Value)
127+
totAmount := new(big.Int).Set(tx.Value())
128128
// Make sure there's enough in the sender's account. Having insufficient
129129
// funds won't invalidate this transaction but simple ignores it.
130130
if sender.Balance().Cmp(totAmount) < 0 {
131-
return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.Sender())
131+
return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.From())
132132
}
133133

134134
if tx.IsContract() {
135-
if tx.GasPrice.Cmp(big.NewInt(minGasPrice)) < 0 {
135+
if tx.GasPrice().Cmp(big.NewInt(minGasPrice)) < 0 {
136136
return fmt.Errorf("Gasprice too low, %s given should be at least %d.", tx.GasPrice, minGasPrice)
137137
}
138138
}
@@ -160,10 +160,7 @@ func (self *TxPool) Add(tx *types.Transaction) error {
160160

161161
self.addTransaction(tx)
162162

163-
tmp := make([]byte, 4)
164-
copy(tmp, tx.Recipient)
165-
166-
txplogger.Debugf("(t) %x => %x (%v) %x\n", tx.Sender()[:4], tmp, tx.Value, tx.Hash())
163+
txplogger.Debugf("(t) %x => %x (%v) %x\n", tx.From()[:4], tx.To()[:4], tx.Value, tx.Hash())
167164

168165
// Notify the subscribers
169166
go self.eventMux.Post(TxPreEvent{tx})
@@ -200,7 +197,7 @@ func (pool *TxPool) RemoveInvalid(state *state.StateDB) {
200197
tx := e.Value.(*types.Transaction)
201198
sender := state.GetAccount(tx.Sender())
202199
err := pool.ValidateTransaction(tx)
203-
if err != nil || sender.Nonce >= tx.Nonce {
200+
if err != nil || sender.Nonce >= tx.Nonce() {
204201
pool.pool.Remove(e)
205202
}
206203
}

0 commit comments

Comments
 (0)