Documentation ¶
Index ¶
- Constants
- Variables
- func CompareBlocks(a, b *Block) int
- func IsUnexpectedCloseError(err error) bool
- type APIMessage
- type Block
- type BlockMessageData
- type BlockPropagationStat
- type BlockPropagationStats
- type Blockchain
- func (bc *Blockchain) AvgBlockPropagation() int64
- func (bc *Blockchain) AvgBlockTime() float64
- func (bc *Blockchain) BlockNumberIndex(number int) int
- func (bc *Blockchain) BlockPropagation() *BlockPropagationStats
- func (bc *Blockchain) Chart() *Chart
- func (bc *Blockchain) HasHash(hash string) bool
- func (bc *Blockchain) Len() int
- func (bc *Blockchain) MaxBlockNumber() int
- func (bc *Blockchain) MinBlockNumber() int
- func (bc *Blockchain) MinersCount() []*ChartMiner
- func (bc *Blockchain) NodePropagation(id string) []int64
- func (bc *Blockchain) Trim(n int)
- type Chart
- type ChartMiner
- type ClientLatencyMessageData
- type ClientPingMessageData
- type Conn
- type DB
- func (db *DB) AddBlock(ctx context.Context, nodeID string, block *Block) error
- func (db *DB) AddBlocks(ctx context.Context, nodeID string, blocks []*Block) error
- func (db *DB) BestBlockNumber(ctx context.Context) int
- func (db *DB) BestBlockNumberNotify() <-chan struct{}
- func (db *DB) Blocks(ctx context.Context) ([]*Block, error)
- func (db *DB) Chart() *Chart
- func (db *DB) ChartNotify() <-chan struct{}
- func (db *DB) CreateNodeIfNotExists(ctx context.Context, node *Node) error
- func (db *DB) FindNodeByID(ctx context.Context, id string) (*Node, error)
- func (db *DB) MaxBlockNumber(ctx context.Context) int
- func (db *DB) MinBlockNumber(ctx context.Context) int
- func (db *DB) Nodes(ctx context.Context) ([]*Node, error)
- func (db *DB) RemoveOldNodes()
- func (db *DB) SetInactive(ctx context.Context, id string) error
- func (db *DB) UpdateLatency(ctx context.Context, id string, latency int64) error
- func (db *DB) UpdatePending(ctx context.Context, nodeID string, pending int) error
- func (db *DB) UpdateStats(ctx context.Context, nodeID string, stats Stats) error
- type Geo
- type GeoByIP
- type GeoService
- type Handler
- type HelloMessageData
- type HistoryMessageData
- type LatencyMessageData
- type Message
- type Node
- type NodeInfo
- type NodePingMessageData
- type NodePongMessageData
- type NopGeoService
- type PendingMessageData
- type Propagation
- type PublishMessage
- type RequestHistoryMessageData
- type Stats
- type StatsMessageData
- type Transaction
- type UpdateMessageData
- type Uptime
Constants ¶
const ( MaxHistory = 40 MaxPeerPropagation = 40 MaxInactiveTime = 4 * 60 * 60 * 1000 // Histogram constants MaxBins = 40 MinPropagationRange = 0 MaxPropagationRange = 10000 )
const (
HistoryRequestInterval = int64((2 * time.Minute) / time.Millisecond)
)
Variables ¶
var ( ErrNodeNotFound = errors.New("node not found") ErrBlockRequired = errors.New("block required") )
var ErrIPNotFound = errors.New("ip address not found")
ErrIPNotFound is returned by GeoService.GeoByIP if an IP address is not found.
Functions ¶
func CompareBlocks ¶
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 ¶
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"` }
type BlockMessageData ¶
type BlockPropagationStat ¶
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 int `json:"avgTransactionRate"` Propagation *BlockPropagationStats `json:"propagation"` }
type ChartMiner ¶
type ClientLatencyMessageData ¶
type ClientLatencyMessageData struct {
Latency int64 `json:"latency"`
}
type ClientPingMessageData ¶
type ClientPingMessageData struct {
ServerTime int64 `json:"serverTime"`
}
type DB ¶
type DB struct { // Set of geolocations for trusted IPs. Must be set before using DB. Trusted GeoByIP // If true, only allow trusted IPs. Strict bool // Geolocation service used for IPs not in GeoByIP. GeoService GeoService // Now returns the current time, in milliseconds. Now func() int64 // contains filtered or unexported fields }
func (*DB) AddBlock ¶
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) BestBlockNumber ¶
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) ChartNotify ¶
func (db *DB) ChartNotify() <-chan struct{}
ChartNotify returns a channel that closes when new charts are available.
func (*DB) CreateNodeIfNotExists ¶
CreateNodeIfNotExists adds the node to the database if it is not already registered.
func (*DB) FindNodeByID ¶
FindNodeByID returns a node by node ID. Returns ErrNodeNotFound if node does not exist.
func (*DB) MaxBlockNumber ¶
MaxBlockNumber returns the block number of the highest block.
func (*DB) MinBlockNumber ¶
MinBlockNumber returns the block number of the lowest block.
func (*DB) RemoveOldNodes ¶
func (db *DB) RemoveOldNodes()
RemoveOldNodes removes nodes that aren't active and haven't been updated recently.
func (*DB) SetInactive ¶
SetInactive marks a given node as inactive.
func (*DB) UpdateLatency ¶
UpdateLatency sets the latency stat for a given node.
func (*DB) UpdatePending ¶
UpdatePending updates the pending stat for a given node.
type Geo ¶
type Geo struct { City string `json:"city"` Country string `json:"country"` LL []float64 `json:"ll"` Metro int `json:"metro"` Range []int `json:"range"` Region string `json:"region"` Zip int `json:"zip"` }
func (*Geo) MarshalLogObject ¶
func (g *Geo) MarshalLogObject(oe zapcore.ObjectEncoder) error
type GeoByIP ¶
func (GeoByIP) MarshalLogObject ¶
func (gs GeoByIP) MarshalLogObject(oe zapcore.ObjectEncoder) error
type GeoService ¶
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 ¶
NewHandler returns a new instance of Handler.
func (*Handler) IsValidAPISecret ¶
type HelloMessageData ¶
type HistoryMessageData ¶
type LatencyMessageData ¶
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 (*Node) BlockNumber ¶
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"` }
type NodePingMessageData ¶
type NodePingMessageData struct{}
type NodePongMessageData ¶
type NodePongMessageData struct{}
type PendingMessageData ¶
type Propagation ¶
func PropagationByNodeID ¶
func PropagationByNodeID(a []*Propagation, nodeID string) *Propagation
type PublishMessage ¶
type PublishMessage struct { Action string `json:"action"` Data interface{} `json:"data"` }
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"` }
type StatsMessageData ¶
type Transaction ¶
type Transaction struct {
Hash string `json:"hash"`
}
func (*Transaction) Clone ¶
func (t *Transaction) Clone() *Transaction