From 1865f28131dea6cd9f0f7b3fda182fb9ace23d98 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Mon, 6 Dec 2021 16:25:21 +0530 Subject: [PATCH] Added sypnosis/help + clean code --- command/chain_watch.go | 36 +++++++++++++++++------------------- command/server/service.go | 2 -- core/blockchain.go | 8 ++++---- docs/cli/README.md | 2 ++ docs/cli/chain_watch.md | 3 +++ 5 files changed, 26 insertions(+), 25 deletions(-) create mode 100644 docs/cli/chain_watch.md diff --git a/command/chain_watch.go b/command/chain_watch.go index 05e52d2bc4..8e40631ccd 100644 --- a/command/chain_watch.go +++ b/command/chain_watch.go @@ -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 diff --git a/command/server/service.go b/command/server/service.go index 2a06460bbf..a2bcd55187 100644 --- a/command/server/service.go +++ b/command/server/service.go @@ -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 diff --git a/core/blockchain.go b/core/blockchain.go index a58f94852f..5bd2f93561 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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, }) diff --git a/docs/cli/README.md b/docs/cli/README.md index c82bbbe047..052116fde8 100644 --- a/docs/cli/README.md +++ b/docs/cli/README.md @@ -15,4 +15,6 @@ - [```account import```](./account_import.md) +- [```chain watch```](./chain_watch.md) + - [```version```](./version.md) diff --git a/docs/cli/chain_watch.md b/docs/cli/chain_watch.md new file mode 100644 index 0000000000..4844bb7618 --- /dev/null +++ b/docs/cli/chain_watch.md @@ -0,0 +1,3 @@ +# Chain watch + +The ```chain watch``` command is used to view the chainHead, reorg and fork events in real-time.