Skip to content

Commit

Permalink
Added sypnosis/help + clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsharma committed Dec 6, 2021
1 parent a6f014c commit 1865f28
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
36 changes: 17 additions & 19 deletions command/chain_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ type ChainWatchCommand struct {

// Help implements the cli.Command interface
func (c *ChainWatchCommand) Help() string {
return ``
return `Usage: bor chain watch
This command is used to view the chainHead, reorg and fork events in real-time`
}

func (c *ChainWatchCommand) Flags() *flagset.Flagset {
Expand All @@ -30,7 +32,19 @@ func (c *ChainWatchCommand) Flags() *flagset.Flagset {

// Synopsis implements the cli.Command interface
func (c *ChainWatchCommand) Synopsis() string {
return ""
return "Watch the chainHead, reorg and fork events in real-time"
}

func printEvent(msg *proto.ChainWatchResponse) string {
var out string
if msg.Type == core.Chain2HeadCanonicalEvent {
out = fmt.Sprintf("Block Added : %v", msg.Newchain)
} else if msg.Type == core.Chain2HeadForkEvent {
out = fmt.Sprintf("New Fork Block : %v", msg.Newchain)
} else if msg.Type == core.Chain2HeadReorgEvent {
out = fmt.Sprintf("Reorg Detected \nAdded : %v \nRemoved : %v", msg.Newchain, msg.Oldchain)
}
return out
}

// Run implements the cli.Command interface
Expand Down Expand Up @@ -68,23 +82,7 @@ func (c *ChainWatchCommand) Run(args []string) int {
c.UI.Output(err.Error())
break
}
if msg.Type == core.Chain2HeadCanonicalEvent {
out := fmt.Sprintf("Block Added : %v", msg.Newchain)
c.UI.Output(out)
} else if msg.Type == core.Chain2HeadForkEvent {
out := fmt.Sprintf("New Fork Block : %v", msg.Newchain)
c.UI.Output(out)
} else if msg.Type == core.Chain2HeadReorgEvent {
c.UI.Output("Reorg Detected")

out := fmt.Sprintf("Added : %v", msg.Newchain)
c.UI.Output(out)

out = fmt.Sprintf("Removed : %v", msg.Oldchain)
c.UI.Output(out)
}

// fmt.Println(msg)
c.UI.Output(printEvent(msg))
}

return 0
Expand Down
2 changes: 0 additions & 2 deletions command/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ 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 blockchain events
// 2. for each event send a proto.ChainWatchResponse

chain2HeadChanSize := 10

Expand Down
8 changes: 4 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
}

bc.chain2HeadFeed.Send(Chain2HeadEvent{
Type: "head",
Type: Chain2HeadCanonicalEvent,
NewChain: []*types.Block{block},
})

Expand All @@ -1653,7 +1653,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
bc.chainSideFeed.Send(ChainSideEvent{Block: block})

bc.chain2HeadFeed.Send(Chain2HeadEvent{
Type: "fork",
Type: Chain2HeadForkEvent,
NewChain: []*types.Block{block},
})
}
Expand Down Expand Up @@ -1751,7 +1751,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
bc.chainHeadFeed.Send(ChainHeadEvent{lastCanon})

bc.chain2HeadFeed.Send(Chain2HeadEvent{
Type: "head",
Type: Chain2HeadCanonicalEvent,
NewChain: []*types.Block{lastCanon},
})
}
Expand Down Expand Up @@ -2281,7 +2281,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
if len(oldChain) > 0 && len(newChain) > 0 {

bc.chain2HeadFeed.Send(Chain2HeadEvent{
Type: "reorg",
Type: Chain2HeadReorgEvent,
NewChain: newChain,
OldChain: oldChain,
})
Expand Down
2 changes: 2 additions & 0 deletions docs/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@

- [```account import```](./account_import.md)

- [```chain watch```](./chain_watch.md)

- [```version```](./version.md)
3 changes: 3 additions & 0 deletions docs/cli/chain_watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Chain watch

The ```chain watch``` command is used to view the chainHead, reorg and fork events in real-time.

0 comments on commit 1865f28

Please sign in to comment.