Skip to content

Commit fa890c8

Browse files
committed
Merge pull request bnb-chain#167 from fjl/feature/split-tx-event
Split TxEvent type for tx pre/post
2 parents ce2ec19 + 6b3f5fb commit fa890c8

5 files changed

Lines changed: 48 additions & 56 deletions

File tree

‎cmd/mist/gui.go‎

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ func (gui *Gui) update() {
408408
eth.ChainSyncEvent{},
409409
eth.PeerListEvent{},
410410
ethchain.NewBlockEvent{},
411-
ethchain.TxEvent{},
411+
ethchain.TxPreEvent{},
412+
ethchain.TxPostEvent{},
412413
ethminer.Event{},
413414
)
414415

@@ -430,40 +431,38 @@ func (gui *Gui) update() {
430431
gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.address()).Balance(), nil)
431432
}
432433

433-
case ethchain.TxEvent:
434+
case ethchain.TxPreEvent:
434435
tx := ev.Tx
435-
if ev.Type == ethchain.TxPre {
436-
object := state.GetAccount(gui.address())
436+
object := state.GetAccount(gui.address())
437437

438-
if bytes.Compare(tx.Sender(), gui.address()) == 0 {
439-
unconfirmedFunds.Sub(unconfirmedFunds, tx.Value)
440-
} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
441-
unconfirmedFunds.Add(unconfirmedFunds, tx.Value)
442-
}
443-
444-
gui.setWalletValue(object.Balance(), unconfirmedFunds)
445-
446-
gui.insertTransaction("pre", tx)
438+
if bytes.Compare(tx.Sender(), gui.address()) == 0 {
439+
unconfirmedFunds.Sub(unconfirmedFunds, tx.Value)
440+
} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
441+
unconfirmedFunds.Add(unconfirmedFunds, tx.Value)
442+
}
447443

448-
} else if ev.Type == ethchain.TxPost {
449-
object := state.GetAccount(gui.address())
450-
if bytes.Compare(tx.Sender(), gui.address()) == 0 {
451-
object.SubAmount(tx.Value)
444+
gui.setWalletValue(object.Balance(), unconfirmedFunds)
445+
gui.insertTransaction("pre", tx)
452446

453-
//gui.getObjectByName("transactionView").Call("addTx", ethpipe.NewJSTx(tx), "send")
454-
gui.txDb.Put(tx.Hash(), tx.RlpEncode())
455-
} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
456-
object.AddAmount(tx.Value)
447+
case ethchain.TxPostEvent:
448+
tx := ev.Tx
449+
object := state.GetAccount(gui.address())
457450

458-
//gui.getObjectByName("transactionView").Call("addTx", ethpipe.NewJSTx(tx), "recv")
459-
gui.txDb.Put(tx.Hash(), tx.RlpEncode())
460-
}
451+
if bytes.Compare(tx.Sender(), gui.address()) == 0 {
452+
object.SubAmount(tx.Value)
461453

462-
gui.setWalletValue(object.Balance(), nil)
454+
//gui.getObjectByName("transactionView").Call("addTx", ethpipe.NewJSTx(tx), "send")
455+
gui.txDb.Put(tx.Hash(), tx.RlpEncode())
456+
} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
457+
object.AddAmount(tx.Value)
463458

464-
state.UpdateStateObject(object)
459+
//gui.getObjectByName("transactionView").Call("addTx", ethpipe.NewJSTx(tx), "recv")
460+
gui.txDb.Put(tx.Hash(), tx.RlpEncode())
465461
}
466462

463+
gui.setWalletValue(object.Balance(), nil)
464+
state.UpdateStateObject(object)
465+
467466
// case object:
468467
// gui.loadAddressBook()
469468

‎ethchain/events.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package ethchain
22

3-
type TxEvent struct {
4-
Type int // TxPre || TxPost
5-
Tx *Transaction
6-
}
3+
// TxPreEvent is posted when a transaction enters the transaction pool.
4+
type TxPreEvent struct{ Tx *Transaction }
75

8-
type NewBlockEvent struct {
9-
Block *Block
10-
}
6+
// TxPostEvent is posted when a transaction has been processed.
7+
type TxPostEvent struct{ Tx *Transaction }
8+
9+
// NewBlockEvent is posted when a block has been imported.
10+
type NewBlockEvent struct{ Block *Block }

‎ethchain/state_manager.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ done:
191191
}
192192

193193
// Notify all subscribers
194-
self.eth.EventMux().Post(TxEvent{TxPost, tx})
194+
self.eth.EventMux().Post(TxPostEvent{tx})
195195

196196
receipts = append(receipts, receipt)
197197
handled = append(handled, tx)

‎ethchain/transaction_pool.go‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@ import (
1414

1515
var txplogger = ethlog.NewLogger("TXP")
1616

17-
const (
18-
txPoolQueueSize = 50
19-
)
17+
const txPoolQueueSize = 50
2018

2119
type TxPoolHook chan *Transaction
2220
type TxMsgTy byte
2321

2422
const (
25-
TxPre = iota
26-
TxPost
27-
2823
minGasPrice = 1000000
2924
)
3025

@@ -169,7 +164,7 @@ out:
169164
txplogger.Debugf("(t) %x => %x (%v) %x\n", tx.Sender()[:4], tmp, tx.Value, tx.Hash())
170165

171166
// Notify the subscribers
172-
pool.Ethereum.EventMux().Post(TxEvent{TxPre, tx})
167+
pool.Ethereum.EventMux().Post(TxPreEvent{tx})
173168
}
174169
case <-pool.quit:
175170
break out

‎ethminer/miner.go‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (miner *Miner) Start() {
6464
miner.block = miner.ethereum.ChainManager().NewBlock(miner.coinbase)
6565

6666
mux := miner.ethereum.EventMux()
67-
miner.events = mux.Subscribe(ethchain.NewBlockEvent{}, ethchain.TxEvent{})
67+
miner.events = mux.Subscribe(ethchain.NewBlockEvent{}, ethchain.TxPreEvent{})
6868

6969
// Prepare inital block
7070
//miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
@@ -118,25 +118,23 @@ func (miner *Miner) listener() {
118118
}
119119
miner.startMining()
120120

121-
case ethchain.TxEvent:
122-
if event.Type == ethchain.TxPre {
123-
miner.stopMining()
121+
case ethchain.TxPreEvent:
122+
miner.stopMining()
124123

125-
found := false
126-
for _, ctx := range miner.txs {
127-
if found = bytes.Compare(ctx.Hash(), event.Tx.Hash()) == 0; found {
128-
break
129-
}
130-
}
131-
if found == false {
132-
// Undo all previous commits
133-
miner.block.Undo()
134-
// Apply new transactions
135-
miner.txs = append(miner.txs, event.Tx)
124+
found := false
125+
for _, ctx := range miner.txs {
126+
if found = bytes.Compare(ctx.Hash(), event.Tx.Hash()) == 0; found {
127+
break
136128
}
137129

138130
miner.startMining()
139131
}
132+
if found == false {
133+
// Undo all previous commits
134+
miner.block.Undo()
135+
// Apply new transactions
136+
miner.txs = append(miner.txs, event.Tx)
137+
}
140138
}
141139

142140
case <-miner.powDone:

0 commit comments

Comments
 (0)