Added sypnosis/help + clean code

This commit is contained in:
Shivam Sharma 2021-12-06 16:25:21 +05:30
parent a6f014c2c9
commit 1865f28131
5 changed files with 26 additions and 25 deletions

View file

@ -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 {
@ -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
@ -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

View file

@ -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

View file

@ -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},
})
@ -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},
})
}
@ -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},
})
}
@ -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,
})

View file

@ -15,4 +15,6 @@
- [```account import```](./account_import.md)
- [```chain watch```](./chain_watch.md)
- [```version```](./version.md)

3
docs/cli/chain_watch.md Normal file
View file

@ -0,0 +1,3 @@
# Chain watch
The ```chain watch``` command is used to view the chainHead, reorg and fork events in real-time.