netstats

package module
v0.0.0-...-e13b794 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2023 License: GPL-3.0 Imports: 19 Imported by: 0

README

GoStats - GoChain Network Stats Dashboard

CircleCI

This is a visual interface for tracking GoChain network status. It uses WebSockets to receive stats from running nodes and output them through an angular interface.

Screenshot

Prequisites

  • Docker

Run

export WS_SECRET=YOURSECRET
make docker
WS_SECRET=YOOOOO make run

Check it out at http://localhost:3000

Running outside Docker

Prerequisite

  • node
  • npm

Installation

Make sure you have node.js and npm installed.

Clone the repository and install the dependencies

git clone https://github.com/gochain-io/netstats
cd netstats
npm install
sudo npm install -g grunt-cli

Build the resources

NetStats features two versions: the full version and the lite version. In order to build the static files you have to run grunt tasks which will generate dist or dist-lite directories containing the js and css files, fonts and images.

To build the full version run

grunt

To build the lite version run

grunt lite

If you want to build both versions run

grunt all

##Run

npm start

see the interface at http://localhost:3000

Documentation

Index

Constants

View Source
const (
	MaxHistory         = 40
	MaxPeerPropagation = 40
	MaxInactiveTime    = 4 * 60 * 60 * 1000

	// Histogram constants
	MaxBins             = 40
	MinPropagationRange = 0
	MaxPropagationRange = 10000
)
View Source
const (
	HistoryRequestInterval = int64((2 * time.Minute) / time.Millisecond)
)

Variables

View Source
var (
	ErrNodeNotFound  = errors.New("node not found")
	ErrBlockRequired = errors.New("block required")
)
View Source
var ErrIPNotFound = errors.New("ip address not found")

ErrIPNotFound is returned by GeoService.GeoByIP if an IP address is not found.

Functions

func CompareBlocks

func CompareBlocks(a, b *Block) int

CompareBlocks compares two blocks by difficulty, number, & gas used. Returns 0 if a==b, -1 if a < b, and +1 if a > b.

func IsUnexpectedCloseError

func IsUnexpectedCloseError(err error) bool

Types

type APIMessage

type APIMessage struct {
	Emit []json.RawMessage `json:"emit,omitempty"`
}

type Block

type Block struct {
	Number           int            `json:"number"`
	Hash             string         `json:"hash"`
	ParentHash       string         `json:"parentHash"`
	Timestamp        int            `json:"timestamp"`
	Miner            string         `json:"miner"`
	GasUsed          int            `json:"gasUsed"`
	GasLimit         int            `json:"gasLimit"`
	Difficulty       int            `json:"difficulty,string"`
	TotalDifficulty  int            `json:"totalDifficulty,string"`
	Transactions     []*Transaction `json:"transactions"`
	TransactionCount int            `json:"transactionCount"`
	TransactionsRoot string         `json:"transactionsRoot"`
	StateRoot        string         `json:"stateRoot"`
	Uncles           []interface{}  `json:"uncles"`
	Trusted          bool           `json:"trusted"`
	Arrived          int64          `json:"arrived"`
	Received         int64          `json:"received"`
	Propagation      int64          `json:"propagation"`
	Time             int64          `json:"time"` // Time since last block (ms)
	Fork             int            `json:"fork"`
}

func (*Block) Clone

func (b *Block) Clone() *Block

type BlockMessageData

type BlockMessageData struct {
	ID    string `json:"id"`
	Block *Block `json:"block"`
}

type BlockPropagationStat

type BlockPropagationStat struct {
	X          int     `json:"x"`
	Dx         int     `json:"dx"`
	Y          float64 `json:"y"`
	Frequency  int     `json:"frequency"`
	Cumulative int     `json:"cumulative"`
	Cumpercent float64 `json:"cumpercent"`
}

type BlockPropagationStats

type BlockPropagationStats struct {
	Histogram []*BlockPropagationStat `json:"histogram"`
	Avg       int64                   `json:"avg"`
}

type Blockchain

type Blockchain struct {
	// contains filtered or unexported fields
}

func NewBlockchain

func NewBlockchain() *Blockchain

func (*Blockchain) AvgBlockPropagation

func (bc *Blockchain) AvgBlockPropagation() int64

func (*Blockchain) AvgBlockTime

func (bc *Blockchain) AvgBlockTime() float64

AvgBlockTime returns the average block time in seconds.

func (*Blockchain) BlockNumberIndex

func (bc *Blockchain) BlockNumberIndex(number int) int

func (*Blockchain) BlockPropagation

func (bc *Blockchain) BlockPropagation() *BlockPropagationStats

func (*Blockchain) Chart

func (bc *Blockchain) Chart() *Chart

func (*Blockchain) HasHash

func (bc *Blockchain) HasHash(hash string) bool

HasHash returns true if a block in the chain exists with the given hash.

func (*Blockchain) Len

func (bc *Blockchain) Len() int

func (*Blockchain) MaxBlockNumber

func (bc *Blockchain) MaxBlockNumber() int

func (*Blockchain) MinBlockNumber

func (bc *Blockchain) MinBlockNumber() int

func (*Blockchain) MinersCount

func (bc *Blockchain) MinersCount() []*ChartMiner

func (*Blockchain) NodePropagation

func (bc *Blockchain) NodePropagation(id string) []int64

func (*Blockchain) Trim

func (bc *Blockchain) Trim(n int)

Trim ensures that the chain length is not greater than n blocks.

type Chart

type Chart struct {
	NetworkName        string        `json:"networkName"`
	Height             []int         `json:"height"`
	BlockTime          []float64     `json:"blocktime"`
	AvgBlockTime       float64       `json:"avgBlocktime"` // Average block time (s)
	Difficulty         []string      `json:"difficulty"`
	Uncles             []int         `json:"uncles"`
	Transactions       []int         `json:"transactions"`
	TransactionRate    []string      `json:"transactionrate"`
	GasSpending        []int         `json:"gasSpending"`
	GasLimit           []int         `json:"gasLimit"`
	Miners             []*ChartMiner `json:"miners"`
	UncleCount         []int         `json:"uncleCount"`
	AvgHashRate        int           `json:"avgHashrate"`
	AvgTransactionRate json.Number   `json:"avgTransactionRate"`

	Propagation *BlockPropagationStats `json:"propagation"`
}

func NewChart

func NewChart() *Chart

type ChartMiner

type ChartMiner struct {
	Miner  string `json:"miner"`
	Name   bool   `json:"name"`
	Blocks int    `json:"blocks"`
}

type ClientLatencyMessageData

type ClientLatencyMessageData struct {
	Latency int64 `json:"latency"`
}

type ClientPingMessageData

type ClientPingMessageData struct {
	ServerTime int64 `json:"serverTime"`
}

type Conn

type Conn struct {
	*websocket.Conn
	// contains filtered or unexported fields
}

Conn is a websocket connection with an attached mutex.

func (*Conn) WriteJSON

func (c *Conn) WriteJSON(v interface{}) error

func (*Conn) WriteMessage

func (c *Conn) WriteMessage(messageType int, data []byte) error

type DB

type DB struct {

	// Set of data for trusted IPs. Must be set before using DB.
	Trusted TrustedByIP
	// If true, only allow trusted IPs.
	Strict bool

	// Geolocation service used for IPs not in TrustedByIP.
	GeoService GeoService

	// Now returns the current time, in milliseconds.
	Now func() int64
	// contains filtered or unexported fields
}

func NewDB

func NewDB(networkName string, lgr *zap.Logger) *DB

func (*DB) AddBlock

func (db *DB) AddBlock(ctx context.Context, nodeID string, block *Block) error

AddBlock adds a block to the database on behalf of a node. Updates the propagation stats for the block & node based on the current time.

func (*DB) AddBlocks

func (db *DB) AddBlocks(ctx context.Context, nodeID string, blocks []*Block) error

AddBlocks adds one or more blocks on behalf of a node.

func (*DB) BestBlockNumber

func (db *DB) BestBlockNumber(ctx context.Context) int

BestBlockNumber returns the block number of the best block.

func (*DB) BestBlockNumberNotify

func (db *DB) BestBlockNumberNotify() <-chan struct{}

BestBlockNumberNotify returns a channel that closes when a new best block number increases.

func (*DB) Blocks

func (db *DB) Blocks(ctx context.Context) ([]*Block, error)

Blocks returns a list of all blocks.

func (*DB) Chart

func (db *DB) Chart() *Chart

Chart returns the last cached chart data.

func (*DB) ChartNotify

func (db *DB) ChartNotify() <-chan struct{}

ChartNotify returns a channel that closes when new charts are available.

func (*DB) CreateNodeIfNotExists

func (db *DB) CreateNodeIfNotExists(ctx context.Context, node *Node) (string, error)

CreateNodeIfNotExists adds the node to the database if it is not already registered. Returns the node's ID, which may be updated if the node is trusted.

func (*DB) FindNodeByID

func (db *DB) FindNodeByID(ctx context.Context, id string) (*Node, error)

FindNodeByID returns a node by node ID. Returns ErrNodeNotFound if node does not exist.

func (*DB) MaxBlockNumber

func (db *DB) MaxBlockNumber(ctx context.Context) int

MaxBlockNumber returns the block number of the highest block.

func (*DB) MinBlockNumber

func (db *DB) MinBlockNumber(ctx context.Context) int

MinBlockNumber returns the block number of the lowest block.

func (*DB) Nodes

func (db *DB) Nodes(ctx context.Context) ([]*Node, error)

Nodes returns a list of all nodes, sorted by id.

func (*DB) RemoveOldNodes

func (db *DB) RemoveOldNodes()

RemoveOldNodes removes nodes that aren't active and haven't been updated recently.

func (*DB) SetInactive

func (db *DB) SetInactive(ctx context.Context, id string) error

SetInactive marks a given node as inactive.

func (*DB) UpdateLatency

func (db *DB) UpdateLatency(ctx context.Context, id string, latency int64) error

UpdateLatency sets the latency stat for a given node.

func (*DB) UpdatePending

func (db *DB) UpdatePending(ctx context.Context, nodeID string, pending int) error

UpdatePending updates the pending stat for a given node.

func (*DB) UpdateStats

func (db *DB) UpdateStats(ctx context.Context, nodeID string, stats Stats) error

UpdateStats updates basic stats for a given node.

type Geo

type Geo struct {
	City    string         `json:"city"`
	Country string         `json:"country"`
	LL      [2]json.Number `json:"ll"`
	Metro   int            `json:"metro"`
	Range   []int          `json:"range"`
	Region  string         `json:"region"`
	Zip     int            `json:"zip"`
}

func (*Geo) Clone

func (g *Geo) Clone() *Geo

Clone returns a deep copy of g.

func (*Geo) MarshalLogObject

func (g *Geo) MarshalLogObject(oe zapcore.ObjectEncoder) error

type GeoService

type GeoService interface {
	GeoByIP(ctx context.Context, ip string) (*Geo, error)
}

GeoService represents a service for computing geolocation by IP address.

type Handler

type Handler struct {

	// Underlying database.
	DB *DB

	// Secret used to authorize node.
	APISecrets []string
	// contains filtered or unexported fields
}

func NewHandler

func NewHandler(lgr *zap.Logger) *Handler

NewHandler returns a new instance of Handler.

func (*Handler) IsValidAPISecret

func (h *Handler) IsValidAPISecret(s string) bool

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type HelloMessageData

type HelloMessageData struct {
	ID      string    `json:"id"`
	Secret  string    `json:"secret"`
	Info    *NodeInfo `json:"info"`
	Latency int64     `json:"latency,string"`
}

type HistoryMessageData

type HistoryMessageData struct {
	ID      string   `json:"id"`
	History []*Block `json:"history"`
}

type LatencyMessageData

type LatencyMessageData struct {
	ID      string `json:"id"`
	Latency int64  `json:"latency,string"`
}

type Message

type Message struct {
	Action         string          `json:"action,omitempty"`
	PropagationAvg uint64          `json:"propagationAvg,omitempty"`
	History        []int64         `json:"history,omitempty"`
	Emit           []interface{}   `json:"emit,omitempty"` // primus-emit
	Data           json.RawMessage `json:"data,omitempty"`
}

type Node

type Node struct {
	ID             string    `json:"id,omitempty"`
	Geo            *Geo      `json:"geo,omitempty"`
	Block          *Block    `json:"block,omitempty"`
	History        []int64   `json:"history,omitempty"`
	Info           *NodeInfo `json:"info,omitempty"`
	Stats          *Stats    `json:"stats,omitempty"`
	Trusted        bool      `json:"trusted,omitempty"`
	PropagationAvg int64     `json:"propagationAvg,omitempty"`
	Uptime         *Uptime   `json:"uptime,omitempty"`
}

func NewNode

func NewNode(timestamp int64) *Node

func (*Node) BlockNumber

func (n *Node) BlockNumber() int

func (*Node) Clone

func (n *Node) Clone() *Node

Clone returns a deep copy of n.

type NodeInfo

type NodeInfo struct {
	API              string `json:"api,omitempty"`
	CanUpdateHistory bool   `json:"canUpdateHistory,omitempty"`
	Client           string `json:"client,omitempty"`
	IP               string `json:"ip,omitempty"`
	Name             string `json:"name,omitempty"`
	Net              string `json:"net,omitempty"`
	Node             string `json:"node,omitempty"`
	OS               string `json:"os,omitempty"`
	OS_V             string `json:"os_v,omitempty"`
	Port             int    `json:"port,omitempty"`
	Protocol         string `json:"protocol,omitempty"`
}

This data is reported by the node and forwarded to clients, but the Name may be modified for trusted nodes.

func (*NodeInfo) Clone

func (ni *NodeInfo) Clone() *NodeInfo

Clone returns a deep copy of ni.

type NodePingMessageData

type NodePingMessageData struct{}

type NodePongMessageData

type NodePongMessageData struct{}

type NopGeoService

type NopGeoService struct{}

NopGeoService is a GeoService that does nothing.

func (*NopGeoService) GeoByIP

func (s *NopGeoService) GeoByIP(ctx context.Context, ip string) (*Geo, error)

GeoByIP always returns ErrIPNotFound.

type PendingMessageData

type PendingMessageData struct {
	ID    string `json:"id"`
	Stats *Stats `json:"stats"`
}

type Propagation

type Propagation struct {
	NodeID      string
	Received    int64
	Propagation int64
}

func PropagationByNodeID

func PropagationByNodeID(a []*Propagation, nodeID string) *Propagation

type PublishMessage

type PublishMessage struct {
	Action string      `json:"action"`
	Data   interface{} `json:"data"`
}

type RequestHistoryMessageData

type RequestHistoryMessageData struct {
	Max  int   `json:"max"`
	Min  int   `json:"min"`
	List []int `json:"list"`
}

type Stats

type Stats struct {
	Active         bool   `json:"active"`
	Mining         bool   `json:"mining"`
	Syncing        bool   `json:"syncing"`
	Hashrate       int    `json:"hashrate"`
	Peers          int    `json:"peers"`
	GasPrice       int    `json:"gasPrice"`
	Uptime         int64  `json:"uptime"`
	Pending        int    `json:"pending"`
	Latency        int64  `json:"latency,string"`
	PropagationAvg int64  `json:"propagationAvg"`
	Block          *Block `json:"block,omitempty"`
}

func (*Stats) Clone

func (s *Stats) Clone() *Stats

Clone returns a deep copy of s.

type StatsMessageData

type StatsMessageData struct {
	ID    string `json:"id"`
	Stats Stats  `json:"stats"`
}

type Transaction

type Transaction struct {
	Hash string `json:"hash"`
}

func (*Transaction) Clone

func (t *Transaction) Clone() *Transaction

type Trusted

type Trusted struct {
	ID string `json:"id"`
	Geo
}

type TrustedByIP

type TrustedByIP map[string]*Trusted

func (TrustedByIP) MarshalLogObject

func (tp TrustedByIP) MarshalLogObject(oe zapcore.ObjectEncoder) error

type UpdateMessageData

type UpdateMessageData struct {
	ID    string `json:"id"`
	Stats *Stats `json:"stats"`
}

type Uptime

type Uptime struct {
	Down       int64 `json:"down"`
	LastStatus bool  `json:"lastStatus"`
	LastUpdate int64 `json:"lastUpdate"`
	Started    int64 `json:"started"`
	Up         int64 `json:"up"`
}

func (*Uptime) Clone

func (u *Uptime) Clone() *Uptime

Clone returns a deep copy of ni.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL