@@ -79,10 +79,16 @@ const (
7979type 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.
13861402func (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