Skip to content

Commit

Permalink
Minor Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsharma committed Dec 3, 2021
1 parent dcb6cab commit beaf6da
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
13 changes: 11 additions & 2 deletions command/chain_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions command/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
20 changes: 10 additions & 10 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions core/bor_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 0 additions & 6 deletions core/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,3 @@ type ChainSideEvent struct {
}

type ChainHeadEvent struct{ Block *types.Block }

type Chain2HeadEvent struct {
NewChain []*types.Block
OldChain []*types.Block
Type string
}

0 comments on commit beaf6da

Please sign in to comment.