forked from protolambda/rumor
/
block_page.go
79 lines (68 loc) · 2.23 KB
/
block_page.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package visualizer
import (
"fmt"
"strconv"
"github.com/olekukonko/tablewriter"
// "html/template"
"net/http"
bdb "github.com/cortze/rumor/chain/db/blocks"
)
// BeaconBlock Page Struct
type BeaconBlockPage struct {
ChainVisualizer *ChainVisualizer
ReqPath string
Title string
HtmlFolder string
}
// Generate a new BeaconBlockPage
func NewBeaconBlockPage(cv *ChainVisualizer, reqpath string, title string) (*BeaconBlockPage, error) {
if cv == nil {
return nil, fmt.Errorf("No ChainVisualizer has been given, Empty pointer")
}
bbp := &BeaconBlockPage{
ChainVisualizer: cv,
ReqPath: reqpath,
Title: title,
HtmlFolder: cv.HtmlFolder,
}
return bbp, nil
}
// Request Handler function for the new page
func (c *BeaconBlockPage) HandlerFunction(w http.ResponseWriter, r *http.Request) {
// Get the Length of the BeaconBlock Buffer
buffLen := c.ChainVisualizer.BlocksBuffer.Len
// Set the header of the block page
typeStr := "Type"
rootStr := "Root"
slotStr := "Slot"
parentStr := "Parent Root"
stateStr := "State Root"
proposerStr := "Proposer ID"
// Autorefresh the webpage every 12 seconds (everytime I get a new block)
// var metadata string = "<meta http-equiv=\"refresh\" content=\"3\" />"
// fmt.Fprintln(w, metadata)
header := "Latest " + strconv.Itoa(buffLen) + " Beacon Blocks From the Mainnet Chain"
fmt.Fprintln(w, header)
fmt.Fprintln(w, "")
tw := tablewriter.NewWriter(w)
tableHeader := []string{typeStr, slotStr, rootStr, proposerStr, stateStr, parentStr}
tw.SetHeader(tableHeader)
for _, block := range c.ChainVisualizer.BlocksBuffer.Items {
if block == nil {
break
}
bwr := block.(*bdb.BlockWithRoot)
blockMsg := []string{"Block", bwr.Block.Message.Slot.String(), bwr.Root.String(), bwr.Block.Message.ProposerIndex.String(), bwr.Block.Message.StateRoot.String(), bwr.Block.Message.ParentRoot.String()}
tw.Append(blockMsg)
}
tw.SetBorder(false)
tw.Render()
}
// Return the info regarding the BeaconBlockPage
func (c *BeaconBlockPage) Info() string {
return "Short Summary of the last Blocks Viewed in the Chain (Latest BeaconBlocks)"
}
// Return the RequestPath for the BeaconBlockPage
func (c *BeaconBlockPage) RequestPath() string {
return c.ReqPath
}