Skip to content

Commit bf1e263

Browse files
tailingchenfjl
authored andcommitted
core, light: send chain events using event.Feed (#14865)
1 parent a4da841 commit bf1e263

37 files changed

Lines changed: 787 additions & 373 deletions

‎accounts/abi/bind/backends/simulated.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/ethereum/go-ethereum/core/types"
3434
"github.com/ethereum/go-ethereum/core/vm"
3535
"github.com/ethereum/go-ethereum/ethdb"
36-
"github.com/ethereum/go-ethereum/event"
3736
"github.com/ethereum/go-ethereum/params"
3837
)
3938

@@ -61,7 +60,7 @@ func NewSimulatedBackend(alloc core.GenesisAlloc) *SimulatedBackend {
6160
database, _ := ethdb.NewMemDatabase()
6261
genesis := core.Genesis{Config: params.AllProtocolChanges, Alloc: alloc}
6362
genesis.MustCommit(database)
64-
blockchain, _ := core.NewBlockChain(database, genesis.Config, ethash.NewFaker(), new(event.TypeMux), vm.Config{})
63+
blockchain, _ := core.NewBlockChain(database, genesis.Config, ethash.NewFaker(), vm.Config{})
6564
backend := &SimulatedBackend{database: database, blockchain: blockchain, config: genesis.Config}
6665
backend.rollback()
6766
return backend

‎cmd/utils/flags.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import (
4141
"github.com/ethereum/go-ethereum/eth/gasprice"
4242
"github.com/ethereum/go-ethereum/ethdb"
4343
"github.com/ethereum/go-ethereum/ethstats"
44-
"github.com/ethereum/go-ethereum/event"
4544
"github.com/ethereum/go-ethereum/les"
4645
"github.com/ethereum/go-ethereum/log"
4746
"github.com/ethereum/go-ethereum/metrics"
@@ -1103,7 +1102,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
11031102
Fatalf("%v", err)
11041103
}
11051104
vmcfg := vm.Config{EnablePreimageRecording: ctx.GlobalBool(VMEnableDebugFlag.Name)}
1106-
chain, err = core.NewBlockChain(chainDb, config, engine, new(event.TypeMux), vmcfg)
1105+
chain, err = core.NewBlockChain(chainDb, config, engine, vmcfg)
11071106
if err != nil {
11081107
Fatalf("Can't create BlockChain: %v", err)
11091108
}

‎core/bench_test.go‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/ethereum/go-ethereum/core/vm"
3131
"github.com/ethereum/go-ethereum/crypto"
3232
"github.com/ethereum/go-ethereum/ethdb"
33-
"github.com/ethereum/go-ethereum/event"
3433
"github.com/ethereum/go-ethereum/params"
3534
)
3635

@@ -175,8 +174,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
175174

176175
// Time the insertion of the new chain.
177176
// State and blocks are stored in the same DB.
178-
evmux := new(event.TypeMux)
179-
chainman, _ := NewBlockChain(db, gspec.Config, ethash.NewFaker(), evmux, vm.Config{})
177+
chainman, _ := NewBlockChain(db, gspec.Config, ethash.NewFaker(), vm.Config{})
180178
defer chainman.Stop()
181179
b.ReportAllocs()
182180
b.ResetTimer()
@@ -286,7 +284,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
286284
if err != nil {
287285
b.Fatalf("error opening database at %v: %v", dir, err)
288286
}
289-
chain, err := NewBlockChain(db, params.TestChainConfig, ethash.NewFaker(), new(event.TypeMux), vm.Config{})
287+
chain, err := NewBlockChain(db, params.TestChainConfig, ethash.NewFaker(), vm.Config{})
290288
if err != nil {
291289
b.Fatalf("error creating chain: %v", err)
292290
}

‎core/block_validator_test.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/ethereum/go-ethereum/core/types"
2626
"github.com/ethereum/go-ethereum/core/vm"
2727
"github.com/ethereum/go-ethereum/ethdb"
28-
"github.com/ethereum/go-ethereum/event"
2928
"github.com/ethereum/go-ethereum/params"
3029
)
3130

@@ -43,7 +42,7 @@ func TestHeaderVerification(t *testing.T) {
4342
headers[i] = block.Header()
4443
}
4544
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
46-
chain, _ := NewBlockChain(testdb, params.TestChainConfig, ethash.NewFaker(), new(event.TypeMux), vm.Config{})
45+
chain, _ := NewBlockChain(testdb, params.TestChainConfig, ethash.NewFaker(), vm.Config{})
4746
defer chain.Stop()
4847

4948
for i := 0; i < len(blocks); i++ {
@@ -107,11 +106,11 @@ func testHeaderConcurrentVerification(t *testing.T, threads int) {
107106
var results <-chan error
108107

109108
if valid {
110-
chain, _ := NewBlockChain(testdb, params.TestChainConfig, ethash.NewFaker(), new(event.TypeMux), vm.Config{})
109+
chain, _ := NewBlockChain(testdb, params.TestChainConfig, ethash.NewFaker(), vm.Config{})
111110
_, results = chain.engine.VerifyHeaders(chain, headers, seals)
112111
chain.Stop()
113112
} else {
114-
chain, _ := NewBlockChain(testdb, params.TestChainConfig, ethash.NewFakeFailer(uint64(len(headers)-1)), new(event.TypeMux), vm.Config{})
113+
chain, _ := NewBlockChain(testdb, params.TestChainConfig, ethash.NewFakeFailer(uint64(len(headers)-1)), vm.Config{})
115114
_, results = chain.engine.VerifyHeaders(chain, headers, seals)
116115
chain.Stop()
117116
}
@@ -174,7 +173,7 @@ func testHeaderConcurrentAbortion(t *testing.T, threads int) {
174173
defer runtime.GOMAXPROCS(old)
175174

176175
// Start the verifications and immediately abort
177-
chain, _ := NewBlockChain(testdb, params.TestChainConfig, ethash.NewFakeDelayer(time.Millisecond), new(event.TypeMux), vm.Config{})
176+
chain, _ := NewBlockChain(testdb, params.TestChainConfig, ethash.NewFakeDelayer(time.Millisecond), vm.Config{})
178177
defer chain.Stop()
179178

180179
abort, results := chain.engine.VerifyHeaders(chain, headers, seals)

‎core/blockchain.go‎

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,16 @@ const (
7979
type BlockChain struct {
8080
config *params.ChainConfig // chain & network configuration
8181

82-
hc *HeaderChain
83-
chainDb ethdb.Database
84-
eventMux *event.TypeMux
85-
genesisBlock *types.Block
82+
hc *HeaderChain
83+
chainDb ethdb.Database
84+
rmTxFeed event.Feed
85+
rmLogsFeed event.Feed
86+
chainFeed event.Feed
87+
chainSideFeed event.Feed
88+
chainHeadFeed event.Feed
89+
logsFeed event.Feed
90+
scope event.SubscriptionScope
91+
genesisBlock *types.Block
8692

8793
mu sync.RWMutex // global mutex for locking chain operations
8894
chainmu sync.RWMutex // blockchain insertion lock
@@ -115,7 +121,7 @@ type BlockChain struct {
115121
// NewBlockChain returns a fully initialised block chain using information
116122
// available in the database. It initialises the default Ethereum Validator and
117123
// Processor.
118-
func NewBlockChain(chainDb ethdb.Database, config *params.ChainConfig, engine consensus.Engine, mux *event.TypeMux, vmConfig vm.Config) (*BlockChain, error) {
124+
func NewBlockChain(chainDb ethdb.Database, config *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) {
119125
bodyCache, _ := lru.New(bodyCacheLimit)
120126
bodyRLPCache, _ := lru.New(bodyCacheLimit)
121127
blockCache, _ := lru.New(blockCacheLimit)
@@ -126,7 +132,6 @@ func NewBlockChain(chainDb ethdb.Database, config *params.ChainConfig, engine co
126132
config: config,
127133
chainDb: chainDb,
128134
stateCache: state.NewDatabase(chainDb),
129-
eventMux: mux,
130135
quit: make(chan struct{}),
131136
bodyCache: bodyCache,
132137
bodyRLPCache: bodyRLPCache,
@@ -594,6 +599,8 @@ func (bc *BlockChain) Stop() {
594599
if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) {
595600
return
596601
}
602+
// Unsubscribe all subscriptions registered from blockchain
603+
bc.scope.Close()
597604
close(bc.quit)
598605
atomic.StoreInt32(&bc.procInterrupt, 1)
599606

@@ -1000,6 +1007,12 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
10001007

10011008
blockInsertTimer.UpdateSince(bstart)
10021009
events = append(events, ChainEvent{block, block.Hash(), logs})
1010+
// We need some control over the mining operation. Acquiring locks and waiting
1011+
// for the miner to create new block takes too long and in most cases isn't
1012+
// even necessary.
1013+
if bc.LastBlockHash() == block.Hash() {
1014+
events = append(events, ChainHeadEvent{block})
1015+
}
10031016

10041017
// Write the positional metadata for transaction and receipt lookups
10051018
if err := WriteTxLookupEntries(bc.chainDb, block); err != nil {
@@ -1024,7 +1037,7 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
10241037
stats.usedGas += usedGas.Uint64()
10251038
stats.report(chain, i)
10261039
}
1027-
go bc.postChainEvents(events, coalescedLogs)
1040+
go bc.PostChainEvents(events, coalescedLogs)
10281041

10291042
return 0, nil
10301043
}
@@ -1184,39 +1197,42 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
11841197
// Must be posted in a goroutine because of the transaction pool trying
11851198
// to acquire the chain manager lock
11861199
if len(diff) > 0 {
1187-
go bc.eventMux.Post(RemovedTransactionEvent{diff})
1200+
go bc.rmTxFeed.Send(RemovedTransactionEvent{diff})
11881201
}
11891202
if len(deletedLogs) > 0 {
1190-
go bc.eventMux.Post(RemovedLogsEvent{deletedLogs})
1203+
go bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs})
11911204
}
11921205

11931206
if len(oldChain) > 0 {
11941207
go func() {
11951208
for _, block := range oldChain {
1196-
bc.eventMux.Post(ChainSideEvent{Block: block})
1209+
bc.chainSideFeed.Send(ChainSideEvent{Block: block})
11971210
}
11981211
}()
11991212
}
12001213

12011214
return nil
12021215
}
12031216

1204-
// postChainEvents iterates over the events generated by a chain insertion and
1205-
// posts them into the event mux.
1206-
func (bc *BlockChain) postChainEvents(events []interface{}, logs []*types.Log) {
1217+
// PostChainEvents iterates over the events generated by a chain insertion and
1218+
// posts them into the event feed.
1219+
// TODO: Should not expose PostChainEvents. The chain events should be posted in WriteBlock.
1220+
func (bc *BlockChain) PostChainEvents(events []interface{}, logs []*types.Log) {
12071221
// post event logs for further processing
1208-
bc.eventMux.Post(logs)
1222+
if logs != nil {
1223+
bc.logsFeed.Send(logs)
1224+
}
12091225
for _, event := range events {
1210-
if event, ok := event.(ChainEvent); ok {
1211-
// We need some control over the mining operation. Acquiring locks and waiting
1212-
// for the miner to create new block takes too long and in most cases isn't
1213-
// even necessary.
1214-
if bc.LastBlockHash() == event.Hash {
1215-
bc.eventMux.Post(ChainHeadEvent{event.Block})
1216-
}
1226+
switch ev := event.(type) {
1227+
case ChainEvent:
1228+
bc.chainFeed.Send(ev)
1229+
1230+
case ChainHeadEvent:
1231+
bc.chainHeadFeed.Send(ev)
1232+
1233+
case ChainSideEvent:
1234+
bc.chainSideFeed.Send(ev)
12171235
}
1218-
// Fire the insertion events individually too
1219-
bc.eventMux.Post(event)
12201236
}
12211237
}
12221238

@@ -1384,3 +1400,33 @@ func (bc *BlockChain) Config() *params.ChainConfig { return bc.config }
13841400

13851401
// Engine retrieves the blockchain's consensus engine.
13861402
func (bc *BlockChain) Engine() consensus.Engine { return bc.engine }
1403+
1404+
// SubscribeRemovedTxEvent registers a subscription of RemovedTransactionEvent.
1405+
func (bc *BlockChain) SubscribeRemovedTxEvent(ch chan<- RemovedTransactionEvent) event.Subscription {
1406+
return bc.scope.Track(bc.rmTxFeed.Subscribe(ch))
1407+
}
1408+
1409+
// SubscribeRemovedLogsEvent registers a subscription of RemovedLogsEvent.
1410+
func (bc *BlockChain) SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) event.Subscription {
1411+
return bc.scope.Track(bc.rmLogsFeed.Subscribe(ch))
1412+
}
1413+
1414+
// SubscribeChainEvent registers a subscription of ChainEvent.
1415+
func (bc *BlockChain) SubscribeChainEvent(ch chan<- ChainEvent) event.Subscription {
1416+
return bc.scope.Track(bc.chainFeed.Subscribe(ch))
1417+
}
1418+
1419+
// SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.
1420+
func (bc *BlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription {
1421+
return bc.scope.Track(bc.chainHeadFeed.Subscribe(ch))
1422+
}
1423+
1424+
// SubscribeChainSideEvent registers a subscription of ChainSideEvent.
1425+
func (bc *BlockChain) SubscribeChainSideEvent(ch chan<- ChainSideEvent) event.Subscription {
1426+
return bc.scope.Track(bc.chainSideFeed.Subscribe(ch))
1427+
}
1428+
1429+
// SubscribeLogsEvent registers a subscription of []*types.Log.
1430+
func (bc *BlockChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
1431+
return bc.scope.Track(bc.logsFeed.Subscribe(ch))
1432+
}

0 commit comments

Comments
 (0)