Minor Refactor

This commit is contained in:
Shivam Sharma 2021-12-03 17:28:53 +05:30
parent dcb6cabb77
commit beaf6da26d
5 changed files with 30 additions and 20 deletions

View file

@ -54,8 +54,17 @@ func (c *ChainWatchCommand) Run(args []string) int {
// if err == EOF if finished on the other side // if err == EOF if finished on the other side
panic(err) panic(err)
} }
fmt.Println("message received") if msg.Type == "head" {
fmt.Println(msg) 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 return 0

View file

@ -126,7 +126,7 @@ func ConvertBlockToBlockStub(blocks []*types.Block) []*proto.BlockStub {
} }
func (s *Server) ChainWatch(req *proto.ChainWatchRequest, reply proto.Bor_ChainWatchServer) error { 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 // 2. for each event send a proto.ChainWatchResponse
chain2HeadChanSize := 10 chain2HeadChanSize := 10
@ -136,7 +136,7 @@ func (s *Server) ChainWatch(req *proto.ChainWatchRequest, reply proto.Bor_ChainW
for { for {
msg := <-chain2HeadCh msg := <-chain2HeadCh
fmt.Print(msg)
reply.Send(&proto.ChainWatchResponse{Type: msg.Type, reply.Send(&proto.ChainWatchResponse{Type: msg.Type,
Newchain: ConvertBlockToBlockStub(msg.NewChain), Newchain: ConvertBlockToBlockStub(msg.NewChain),
Oldchain: ConvertBlockToBlockStub(msg.OldChain), Oldchain: ConvertBlockToBlockStub(msg.OldChain),

View file

@ -175,16 +175,15 @@ type BlockChain struct {
// * nil: disable tx reindexer/deleter, but still index new blocks // * nil: disable tx reindexer/deleter, but still index new blocks
txLookupLimit uint64 txLookupLimit uint64
hc *HeaderChain hc *HeaderChain
rmLogsFeed event.Feed rmLogsFeed event.Feed
chainFeed event.Feed chainFeed event.Feed
chainSideFeed event.Feed chainSideFeed event.Feed
chainHeadFeed event.Feed chainHeadFeed event.Feed
chain2HeadFeed event.Feed logsFeed event.Feed
logsFeed event.Feed blockProcFeed event.Feed
blockProcFeed event.Feed scope event.SubscriptionScope
scope event.SubscriptionScope genesisBlock *types.Block
genesisBlock *types.Block
// This mutex synchronizes chain write operations. // This mutex synchronizes chain write operations.
// Readers don't need to take it, they can just read the database. // 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 borReceiptsCache *lru.Cache // Cache for the most recent bor receipt receipts per block
stateSyncData []*types.StateSyncData // State sync data stateSyncData []*types.StateSyncData // State sync data
stateSyncFeed event.Feed // State sync feed stateSyncFeed event.Feed // State sync feed
chain2HeadFeed event.Feed // Reorg/NewHead/Fork data feed
} }
// NewBlockChain returns a fully initialised block chain using information // NewBlockChain returns a fully initialised block chain using information

View file

@ -8,3 +8,10 @@ import (
type StateSyncEvent struct { type StateSyncEvent struct {
Data *types.StateSyncData Data *types.StateSyncData
} }
// For tracking reorgs related information
type Chain2HeadEvent struct {
NewChain []*types.Block
OldChain []*types.Block
Type string
}

View file

@ -41,9 +41,3 @@ type ChainSideEvent struct {
} }
type ChainHeadEvent struct{ Block *types.Block } type ChainHeadEvent struct{ Block *types.Block }
type Chain2HeadEvent struct {
NewChain []*types.Block
OldChain []*types.Block
Type string
}