diff --git a/command/chain_watch.go b/command/chain_watch.go index f61a28c877..2ed37be9f6 100644 --- a/command/chain_watch.go +++ b/command/chain_watch.go @@ -54,8 +54,17 @@ func (c *ChainWatchCommand) Run(args []string) int { // if err == EOF if finished on the other side panic(err) } - fmt.Println("message received") - fmt.Println(msg) + if msg.Type == "head" { + fmt.Println("Block Added : ", msg.Newchain) + } else if msg.Type == "fork" { + fmt.Println("New Fork Block :", msg.Newchain) + } else if msg.Type == "reorg" { + fmt.Println("Reorg Detected") + fmt.Println("Added :", msg.Newchain) + fmt.Println("Removed :", msg.Oldchain) + } + + // fmt.Println(msg) } return 0 diff --git a/command/server/service.go b/command/server/service.go index 526d4b66bf..4c01facf14 100644 --- a/command/server/service.go +++ b/command/server/service.go @@ -126,7 +126,7 @@ func ConvertBlockToBlockStub(blocks []*types.Block) []*proto.BlockStub { } func (s *Server) ChainWatch(req *proto.ChainWatchRequest, reply proto.Bor_ChainWatchServer) error { - // 1. start the feed to the blcokchain events + // 1. start the feed to the blockchain events // 2. for each event send a proto.ChainWatchResponse chain2HeadChanSize := 10 @@ -136,7 +136,7 @@ func (s *Server) ChainWatch(req *proto.ChainWatchRequest, reply proto.Bor_ChainW for { msg := <-chain2HeadCh - fmt.Print(msg) + reply.Send(&proto.ChainWatchResponse{Type: msg.Type, Newchain: ConvertBlockToBlockStub(msg.NewChain), Oldchain: ConvertBlockToBlockStub(msg.OldChain), diff --git a/core/blockchain.go b/core/blockchain.go index 22b56dc0b5..a58f94852f 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -175,16 +175,15 @@ type BlockChain struct { // * nil: disable tx reindexer/deleter, but still index new blocks txLookupLimit uint64 - hc *HeaderChain - rmLogsFeed event.Feed - chainFeed event.Feed - chainSideFeed event.Feed - chainHeadFeed event.Feed - chain2HeadFeed event.Feed - logsFeed event.Feed - blockProcFeed event.Feed - scope event.SubscriptionScope - genesisBlock *types.Block + hc *HeaderChain + rmLogsFeed event.Feed + chainFeed event.Feed + chainSideFeed event.Feed + chainHeadFeed event.Feed + logsFeed event.Feed + blockProcFeed event.Feed + scope event.SubscriptionScope + genesisBlock *types.Block // This mutex synchronizes chain write operations. // Readers don't need to take it, they can just read the database. @@ -218,6 +217,7 @@ type BlockChain struct { borReceiptsCache *lru.Cache // Cache for the most recent bor receipt receipts per block stateSyncData []*types.StateSyncData // State sync data stateSyncFeed event.Feed // State sync feed + chain2HeadFeed event.Feed // Reorg/NewHead/Fork data feed } // NewBlockChain returns a fully initialised block chain using information diff --git a/core/bor_events.go b/core/bor_events.go index d8a5b38d0e..e47afba1a5 100644 --- a/core/bor_events.go +++ b/core/bor_events.go @@ -8,3 +8,10 @@ import ( type StateSyncEvent struct { Data *types.StateSyncData } + +// For tracking reorgs related information +type Chain2HeadEvent struct { + NewChain []*types.Block + OldChain []*types.Block + Type string +} diff --git a/core/events.go b/core/events.go index 648546ea36..ac935a137f 100644 --- a/core/events.go +++ b/core/events.go @@ -41,9 +41,3 @@ type ChainSideEvent struct { } type ChainHeadEvent struct{ Block *types.Block } - -type Chain2HeadEvent struct { - NewChain []*types.Block - OldChain []*types.Block - Type string -}