Skip to content

Commit 8fd6c3d

Browse files
committed
core, eth, internal/ethapi: unify chain and chain head events
1 parent 0ca8941 commit 8fd6c3d

File tree

9 files changed

+16
-45
lines changed

9 files changed

+16
-45
lines changed

core/blockchain.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ type BlockChain struct {
223223

224224
hc *HeaderChain
225225
rmLogsFeed event.Feed
226-
chainFeed event.Feed
227-
chainSideFeed event.Feed
228226
chainHeadFeed event.Feed
229227
logsFeed event.Feed
230228
blockProcFeed event.Feed
@@ -1534,7 +1532,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
15341532

15351533
// writeBlockAndSetHead is the internal implementation of WriteBlockAndSetHead.
15361534
// This function expects the chain mutex to be held.
1537-
func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
1535+
func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB) (status WriteStatus, err error) {
15381536
if err := bc.writeBlockWithState(block, receipts, state); err != nil {
15391537
return NonStatTy, err
15401538
}
@@ -1547,21 +1545,15 @@ func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types
15471545
}
15481546
}
15491547

1550-
// Set new head.
1548+
// Set new head
15511549
bc.writeHeadBlock(block)
15521550

1553-
bc.chainFeed.Send(ChainEvent{Header: block.Header()})
15541551
if len(logs) > 0 {
15551552
bc.logsFeed.Send(logs)
15561553
}
1557-
// In theory, we should fire a ChainHeadEvent when we inject
1558-
// a canonical block, but sometimes we can insert a batch of
1559-
// canonical blocks. Avoid firing too many ChainHeadEvents,
1560-
// we will fire an accumulated ChainHeadEvent and disable fire
1561-
// event here.
1562-
if emitHeadEvent {
1563-
bc.chainHeadFeed.Send(ChainHeadEvent{Header: block.Header()})
1564-
}
1554+
// No chain head event is emitted as this method is called when importing a
1555+
// batch of blocks, so there will be one final event emitted at the end
1556+
15651557
return CanonStatTy, nil
15661558
}
15671559

@@ -1968,7 +1960,7 @@ func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, s
19681960
// Don't set the head, only insert the block
19691961
err = bc.writeBlockWithState(block, res.Receipts, statedb)
19701962
} else {
1971-
status, err = bc.writeBlockAndSetHead(block, res.Receipts, res.Logs, statedb, false)
1963+
status, err = bc.writeBlockAndSetHead(block, res.Receipts, res.Logs, statedb)
19721964
}
19731965
if err != nil {
19741966
return nil, err
@@ -2398,7 +2390,6 @@ func (bc *BlockChain) SetCanonical(head *types.Block) (common.Hash, error) {
23982390

23992391
// Emit events
24002392
logs := bc.collectLogs(head, false)
2401-
bc.chainFeed.Send(ChainEvent{Header: head.Header()})
24022393
if len(logs) > 0 {
24032394
bc.logsFeed.Send(logs)
24042395
}

core/blockchain_reader.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,6 @@ func (bc *BlockChain) SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) even
420420
return bc.scope.Track(bc.rmLogsFeed.Subscribe(ch))
421421
}
422422

423-
// SubscribeChainEvent registers a subscription of ChainEvent.
424-
func (bc *BlockChain) SubscribeChainEvent(ch chan<- ChainEvent) event.Subscription {
425-
return bc.scope.Track(bc.chainFeed.Subscribe(ch))
426-
}
427-
428423
// SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.
429424
func (bc *BlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription {
430425
return bc.scope.Track(bc.chainHeadFeed.Subscribe(ch))

core/events.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,10 @@ import (
2323
// NewTxsEvent is posted when a batch of transactions enter the transaction pool.
2424
type NewTxsEvent struct{ Txs []*types.Transaction }
2525

26-
// NewMinedBlockEvent is posted when a block has been imported.
27-
type NewMinedBlockEvent struct{ Block *types.Block }
28-
29-
// RemovedLogsEvent is posted when a reorg happens
26+
// RemovedLogsEvent is posted when a reorg happens.
3027
type RemovedLogsEvent struct{ Logs []*types.Log }
3128

32-
type ChainEvent struct {
33-
Header *types.Header
34-
}
35-
29+
// ChainHeadEvent is posted when the chain head is updated.
3630
type ChainHeadEvent struct {
3731
Header *types.Header
3832
}

eth/api_backend.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,6 @@ func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEven
267267
return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
268268
}
269269

270-
func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
271-
return b.eth.BlockChain().SubscribeChainEvent(ch)
272-
}
273-
274270
func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
275271
return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
276272
}

eth/filters/filter_system.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type Backend interface {
6565
CurrentHeader() *types.Header
6666
ChainConfig() *params.ChainConfig
6767
SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
68-
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
68+
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
6969
SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
7070
SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
7171

@@ -200,7 +200,7 @@ type EventSystem struct {
200200
txsCh chan core.NewTxsEvent // Channel to receive new transactions event
201201
logsCh chan []*types.Log // Channel to receive new log event
202202
rmLogsCh chan core.RemovedLogsEvent // Channel to receive removed log event
203-
chainCh chan core.ChainEvent // Channel to receive new chain event
203+
chainCh chan core.ChainHeadEvent // Channel to receive new chain head event
204204
}
205205

206206
// NewEventSystem creates a new manager that listens for event on the given mux,
@@ -218,14 +218,14 @@ func NewEventSystem(sys *FilterSystem) *EventSystem {
218218
txsCh: make(chan core.NewTxsEvent, txChanSize),
219219
logsCh: make(chan []*types.Log, logsChanSize),
220220
rmLogsCh: make(chan core.RemovedLogsEvent, rmLogsChanSize),
221-
chainCh: make(chan core.ChainEvent, chainEvChanSize),
221+
chainCh: make(chan core.ChainHeadEvent, chainEvChanSize),
222222
}
223223

224224
// Subscribe events
225225
m.txsSub = m.backend.SubscribeNewTxsEvent(m.txsCh)
226226
m.logsSub = m.backend.SubscribeLogsEvent(m.logsCh)
227227
m.rmLogsSub = m.backend.SubscribeRemovedLogsEvent(m.rmLogsCh)
228-
m.chainSub = m.backend.SubscribeChainEvent(m.chainCh)
228+
m.chainSub = m.backend.SubscribeChainHeadEvent(m.chainCh)
229229

230230
// Make sure none of the subscriptions are empty
231231
if m.txsSub == nil || m.logsSub == nil || m.rmLogsSub == nil || m.chainSub == nil {
@@ -389,7 +389,7 @@ func (es *EventSystem) handleTxsEvent(filters filterIndex, ev core.NewTxsEvent)
389389
}
390390
}
391391

392-
func (es *EventSystem) handleChainEvent(filters filterIndex, ev core.ChainEvent) {
392+
func (es *EventSystem) handleChainEvent(filters filterIndex, ev core.ChainHeadEvent) {
393393
for _, f := range filters[BlocksSubscription] {
394394
f.headers <- ev.Header
395395
}

eth/filters/filter_system_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (b *testBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscript
133133
return b.logsFeed.Subscribe(ch)
134134
}
135135

136-
func (b *testBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
136+
func (b *testBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
137137
return b.chainFeed.Subscribe(ch)
138138
}
139139

@@ -196,11 +196,11 @@ func TestBlockSubscription(t *testing.T) {
196196
BaseFee: big.NewInt(params.InitialBaseFee),
197197
}
198198
_, chain, _ = core.GenerateChainWithGenesis(genesis, ethash.NewFaker(), 10, func(i int, gen *core.BlockGen) {})
199-
chainEvents []core.ChainEvent
199+
chainEvents []core.ChainHeadEvent
200200
)
201201

202202
for _, blk := range chain {
203-
chainEvents = append(chainEvents, core.ChainEvent{Header: blk.Header()})
203+
chainEvents = append(chainEvents, core.ChainHeadEvent{Header: blk.Header()})
204204
}
205205

206206
chan0 := make(chan *types.Header)

internal/ethapi/api_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,6 @@ func (b testBackend) GetEVM(ctx context.Context, msg *core.Message, state *state
581581
}
582582
return vm.NewEVM(context, txContext, state, b.chain.Config(), *vmConfig)
583583
}
584-
func (b testBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
585-
panic("implement me")
586-
}
587584
func (b testBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
588585
panic("implement me")
589586
}

internal/ethapi/backend.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ type Backend interface {
6969
Pending() (*types.Block, types.Receipts, *state.StateDB)
7070
GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
7171
GetEVM(ctx context.Context, msg *core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config, blockCtx *vm.BlockContext) *vm.EVM
72-
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
7372
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
7473

7574
// Transaction pool API

internal/ethapi/transaction_args_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ func (b *backendMock) GetTd(ctx context.Context, hash common.Hash) *big.Int { re
373373
func (b *backendMock) GetEVM(ctx context.Context, msg *core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config, blockCtx *vm.BlockContext) *vm.EVM {
374374
return nil
375375
}
376-
func (b *backendMock) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { return nil }
377376
func (b *backendMock) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
378377
return nil
379378
}

0 commit comments

Comments
 (0)