diff options
author | Determinant <ted.sybil@gmail.com> | 2019-08-14 01:37:25 -0400 |
---|---|---|
committer | Determinant <ted.sybil@gmail.com> | 2019-08-14 01:37:25 -0400 |
commit | 592f21f5b97e5b1e714f194ae90ab83e6547cf41 (patch) | |
tree | c500cdf4ca4266af15703eca182df34d66715918 | |
parent | ad886faec521f1edcb90f6f8eb4555608d085312 (diff) |
finish a full chain example (with p2p network)
-rw-r--r-- | cmd/geth/config.go | 6 | ||||
-rw-r--r-- | cmd/geth/consolecmd.go | 2 | ||||
-rw-r--r-- | cmd/geth/main.go | 9 | ||||
-rw-r--r-- | cmd/utils/cmd.go | 2 | ||||
-rw-r--r-- | cmd/utils/flags.go | 88 | ||||
-rw-r--r-- | consensus/dummy/consensus.go | 7 | ||||
-rw-r--r-- | eth/backend.go | 19 | ||||
-rw-r--r-- | eth/config.go | 32 | ||||
-rw-r--r-- | ethstats/ethstats.go | 717 | ||||
-rw-r--r-- | examples/fc/main.go | 14 | ||||
-rw-r--r-- | miner/miner.go | 24 | ||||
-rw-r--r-- | node/api.go | 317 | ||||
-rw-r--r-- | node/node.go | 664 |
13 files changed, 1808 insertions, 93 deletions
diff --git a/cmd/geth/config.go b/cmd/geth/config.go index e33b367..d7484ab 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -28,8 +28,8 @@ import ( "github.com/Determinant/coreth/cmd/utils" "github.com/ethereum/go-ethereum/dashboard" - "github.com/ethereum/go-ethereum/eth" - "github.com/ethereum/go-ethereum/node" + "github.com/Determinant/coreth/eth" + "github.com/Determinant/coreth/node" "github.com/ethereum/go-ethereum/params" whisper "github.com/ethereum/go-ethereum/whisper/whisperv6" "github.com/naoina/toml" @@ -109,7 +109,7 @@ func defaultNodeConfig() node.Config { func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { // Load defaults. cfg := gethConfig{ - Eth: eth.DefaultConfig, + Eth: eth.MyDefaultConfig(), Shh: whisper.DefaultConfig, Node: defaultNodeConfig(), Dashboard: dashboard.DefaultConfig, diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index 0c0881b..3cf9192 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -26,7 +26,7 @@ import ( "github.com/Determinant/coreth/cmd/utils" "github.com/ethereum/go-ethereum/console" - "github.com/ethereum/go-ethereum/node" + "github.com/Determinant/coreth/node" "github.com/ethereum/go-ethereum/rpc" "gopkg.in/urfave/cli.v1" ) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index a8ddd44..56b838d 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -31,14 +31,14 @@ import ( "github.com/Determinant/coreth/cmd/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/console" - "github.com/ethereum/go-ethereum/eth" + "github.com/Determinant/coreth/eth" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/ethclient" "github.com/Determinant/coreth/internal/debug" "github.com/ethereum/go-ethereum/les" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" - "github.com/ethereum/go-ethereum/node" + "github.com/Determinant/coreth/node" cli "gopkg.in/urfave/cli.v1" ) @@ -380,6 +380,11 @@ func startNode(ctx *cli.Context, stack *node.Node) { if err := stack.Service(ðereum); err != nil { utils.Fatalf("Ethereum service not running: %v", err) } + etherBase := &common.Address { + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + } + ethereum.SetEtherbase(*etherBase) // Set the gas price to the limits from the CLI and start mining gasprice := utils.GlobalBig(ctx, utils.MinerLegacyGasPriceFlag.Name) if ctx.IsSet(utils.MinerGasPriceFlag.Name) { diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 97597bb..91b1bb5 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -35,7 +35,7 @@ import ( "github.com/ethereum/go-ethereum/ethdb" "github.com/Determinant/coreth/internal/debug" "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/node" + "github.com/Determinant/coreth/node" "github.com/ethereum/go-ethereum/rlp" ) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 7e28dff..54dc9cd 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -40,18 +40,17 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/dashboard" - "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/gasprice" + "github.com/Determinant/coreth/eth" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/ethstats" + "github.com/Determinant/coreth/ethstats" "github.com/ethereum/go-ethereum/graphql" - "github.com/ethereum/go-ethereum/les" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics/influxdb" "github.com/ethereum/go-ethereum/miner" - "github.com/ethereum/go-ethereum/node" + "github.com/Determinant/coreth/node" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discv5" "github.com/ethereum/go-ethereum/p2p/enode" @@ -960,41 +959,6 @@ func setIPC(ctx *cli.Context, cfg *node.Config) { } } -// setLes configures the les server and ultra light client settings from the command line flags. -func setLes(ctx *cli.Context, cfg *eth.Config) { - if ctx.GlobalIsSet(LightLegacyServFlag.Name) { - cfg.LightServ = ctx.GlobalInt(LightLegacyServFlag.Name) - } - if ctx.GlobalIsSet(LightServeFlag.Name) { - cfg.LightServ = ctx.GlobalInt(LightServeFlag.Name) - } - if ctx.GlobalIsSet(LightIngressFlag.Name) { - cfg.LightIngress = ctx.GlobalInt(LightIngressFlag.Name) - } - if ctx.GlobalIsSet(LightEgressFlag.Name) { - cfg.LightEgress = ctx.GlobalInt(LightEgressFlag.Name) - } - if ctx.GlobalIsSet(LightLegacyPeersFlag.Name) { - cfg.LightPeers = ctx.GlobalInt(LightLegacyPeersFlag.Name) - } - if ctx.GlobalIsSet(LightMaxPeersFlag.Name) { - cfg.LightPeers = ctx.GlobalInt(LightMaxPeersFlag.Name) - } - if ctx.GlobalIsSet(UltraLightServersFlag.Name) { - cfg.UltraLightServers = strings.Split(ctx.GlobalString(UltraLightServersFlag.Name), ",") - } - if ctx.GlobalIsSet(UltraLightFractionFlag.Name) { - cfg.UltraLightFraction = ctx.GlobalInt(UltraLightFractionFlag.Name) - } - if cfg.UltraLightFraction <= 0 && cfg.UltraLightFraction > 100 { - log.Error("Ultra light fraction is invalid", "had", cfg.UltraLightFraction, "updated", eth.DefaultConfig.UltraLightFraction) - cfg.UltraLightFraction = eth.DefaultConfig.UltraLightFraction - } - if ctx.GlobalIsSet(UltraLightOnlyAnnounceFlag.Name) { - cfg.UltraLightOnlyAnnounce = ctx.GlobalBool(UltraLightOnlyAnnounceFlag.Name) - } -} - // makeDatabaseHandles raises out the number of allowed file handles per process // for Geth and returns half of the allowance to assign to the database. func makeDatabaseHandles() int { @@ -1411,7 +1375,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { setEthash(ctx, cfg) setMiner(ctx, &cfg.Miner) setWhitelist(ctx, cfg) - setLes(ctx, cfg) if ctx.GlobalIsSet(SyncModeFlag.Name) { cfg.SyncMode = *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode) @@ -1513,24 +1476,21 @@ func SetDashboardConfig(ctx *cli.Context, cfg *dashboard.Config) { // RegisterEthService adds an Ethereum client to the stack. func RegisterEthService(stack *node.Node, cfg *eth.Config) { - var err error - if cfg.SyncMode == downloader.LightSync { - err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { - return les.New(ctx, cfg) - }) - } else { - err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { - fullNode, err := eth.New(ctx, cfg) - if fullNode != nil && cfg.LightServ > 0 { - ls, _ := les.NewLesServer(fullNode, cfg) - fullNode.AddLesServer(ls) - } - return fullNode, err - }) - } - if err != nil { - Fatalf("Failed to register the Ethereum service: %v", err) - } + var err error + if cfg.SyncMode == downloader.LightSync { + panic("not supported") + } else { + err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { + fullNode, err := eth.New(ctx, cfg) + if fullNode != nil && cfg.LightServ > 0 { + panic("not supported") + } + return fullNode, err + }) + } + if err != nil { + Fatalf("Failed to register the Ethereum service: %v", err) + } } // RegisterDashboardService adds a dashboard to the stack. @@ -1553,15 +1513,12 @@ func RegisterShhService(stack *node.Node, cfg *whisper.Config) { // the given node. func RegisterEthStatsService(stack *node.Node, url string) { if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { - // Retrieve both eth and les services + // Retrieve both eth service var ethServ *eth.Ethereum ctx.Service(ðServ) - var lesServ *les.LightEthereum - ctx.Service(&lesServ) - // Let ethstats use whichever is not nil - return ethstats.New(url, ethServ, lesServ) + return ethstats.New(url, ethServ, nil) }); err != nil { Fatalf("Failed to register the Ethereum Stats service: %v", err) } @@ -1575,11 +1532,6 @@ func RegisterGraphQLService(stack *node.Node, endpoint string, cors, vhosts []st if err := ctx.Service(ðServ); err == nil { return graphql.New(ethServ.APIBackend, endpoint, cors, vhosts, timeouts) } - // Try to construct the GraphQL service backed by a light node - var lesServ *les.LightEthereum - if err := ctx.Service(&lesServ); err == nil { - return graphql.New(lesServ.ApiBackend, endpoint, cors, vhosts, timeouts) - } // Well, this should not have happened, bail out return nil, errors.New("no Ethereum service") }); err != nil { diff --git a/consensus/dummy/consensus.go b/consensus/dummy/consensus.go index a496262..2a9441d 100644 --- a/consensus/dummy/consensus.go +++ b/consensus/dummy/consensus.go @@ -93,8 +93,7 @@ func (self *DummyEngine) verifyHeaderWorker(chain consensus.ChainReader, headers } func (self *DummyEngine) Author(header *types.Header) (common.Address, error) { - addr := common.Address{} - return addr, nil + return header.Coinbase, nil } func (self *DummyEngine) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error { @@ -173,7 +172,7 @@ func (self *DummyEngine) VerifySeal(chain consensus.ChainReader, header *types.H } func (self *DummyEngine) Prepare(chain consensus.ChainReader, header *types.Header) error { - header.Difficulty = big.NewInt(0) + header.Difficulty = big.NewInt(1) return nil } @@ -222,7 +221,7 @@ func (self *DummyEngine) SealHash(header *types.Header) (hash common.Hash) { } func (self *DummyEngine) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int { - return big.NewInt(0) + return big.NewInt(1) } func (self *DummyEngine) APIs(chain consensus.ChainReader) []rpc.API { diff --git a/eth/backend.go b/eth/backend.go index b5f590e..12041de 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -409,6 +409,17 @@ func (s *Ethereum) SetEtherbase(etherbase common.Address) { // is already running, this method adjust the number of threads allowed to use // and updates the minimum price required by the transaction pool. func (s *Ethereum) StartMining(threads int) error { + // Update the thread count within the consensus engine + type threaded interface { + SetThreads(threads int) + } + if th, ok := s.engine.(threaded); ok { + log.Info("Updated mining threads", "threads", threads) + if threads == 0 { + threads = -1 // Disable the miner from within + } + th.SetThreads(threads) + } // If the miner was not running, initialize it if !s.IsMining() { // Propagate the initial price point to the transaction pool @@ -509,10 +520,10 @@ func (s *Ethereum) Stop() error { s.bloomIndexer.Close() s.blockchain.Stop() s.engine.Close() - //s.protocolManager.Stop() - //if s.lesServer != nil { - // s.lesServer.Stop() - //} + s.protocolManager.Stop() + if s.lesServer != nil { + s.lesServer.Stop() + } s.txPool.Stop() s.miner.Stop() s.eventMux.Stop() diff --git a/eth/config.go b/eth/config.go index 6887872..3149c6f 100644 --- a/eth/config.go +++ b/eth/config.go @@ -24,6 +24,7 @@ import ( "runtime" "time" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core" @@ -155,3 +156,34 @@ type Config struct { // CheckpointOracle is the configuration for checkpoint oracle. CheckpointOracle *params.CheckpointOracleConfig `toml:",omitempty"` } + +func MyDefaultConfig() Config { + config := DefaultConfig + chainConfig := ¶ms.ChainConfig { + ChainID: big.NewInt(42222), + HomesteadBlock: big.NewInt(0), + DAOForkBlock: big.NewInt(0), + DAOForkSupport: true, + EIP150Block: big.NewInt(0), + EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: nil, + Ethash: nil, + } + genBalance := big.NewInt(1000000000000000000) + + config.Genesis = &core.Genesis{ + Config: chainConfig, + Nonce: 0, + Number: 0, + ExtraData: hexutil.MustDecode("0x00"), + GasLimit: 100000000, + Difficulty: big.NewInt(0), + Alloc: core.GenesisAlloc{ common.Address{}: { Balance: genBalance }}, + } + return config +} diff --git a/ethstats/ethstats.go b/ethstats/ethstats.go new file mode 100644 index 0000000..e680b87 --- /dev/null +++ b/ethstats/ethstats.go @@ -0,0 +1,717 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +// Package ethstats implements the network stats reporting service. +package ethstats + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "math/big" + "net/http" + "regexp" + "runtime" + "strconv" + "strings" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/mclock" + "github.com/ethereum/go-ethereum/consensus" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/Determinant/coreth/eth" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/les" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/rpc" + "github.com/gorilla/websocket" +) + +const ( + // historyUpdateRange is the number of blocks a node should report upon login or + // history request. + historyUpdateRange = 50 + + // txChanSize is the size of channel listening to NewTxsEvent. + // The number is referenced from the size of tx pool. + txChanSize = 4096 + // chainHeadChanSize is the size of channel listening to ChainHeadEvent. + chainHeadChanSize = 10 +) + +type txPool interface { + // SubscribeNewTxsEvent should return an event subscription of + // NewTxsEvent and send events to the given channel. + SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription +} + +type blockChain interface { + SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription +} + +// Service implements an Ethereum netstats reporting daemon that pushes local +// chain statistics up to a monitoring server. +type Service struct { + server *p2p.Server // Peer-to-peer server to retrieve networking infos + eth *eth.Ethereum // Full Ethereum service if monitoring a full node + les *les.LightEthereum // Light Ethereum service if monitoring a light node + engine consensus.Engine // Consensus engine to retrieve variadic block fields + + node string // Name of the node to display on the monitoring page + pass string // Password to authorize access to the monitoring page + host string // Remote address of the monitoring service + + pongCh chan struct{} // Pong notifications are fed into this channel + histCh chan []uint64 // History request block numbers are fed into this channel +} + +// New returns a monitoring service ready for stats reporting. +func New(url string, ethServ *eth.Ethereum, lesServ *les.LightEthereum) (*Service, error) { + // Parse the netstats connection url + re := regexp.MustCompile("([^:@]*)(:([^@]*))?@(.+)") + parts := re.FindStringSubmatch(url) + if len(parts) != 5 { + return nil, fmt.Errorf("invalid netstats url: \"%s\", should be nodename:secret@host:port", url) + } + // Assemble and return the stats service + var engine consensus.Engine + if ethServ != nil { + engine = ethServ.Engine() + } else { + engine = lesServ.Engine() + } + return &Service{ + eth: ethServ, + les: lesServ, + engine: engine, + node: parts[1], + pass: parts[3], + host: parts[4], + pongCh: make(chan struct{}), + histCh: make(chan []uint64, 1), + }, nil +} + +// Protocols implements node.Service, returning the P2P network protocols used +// by the stats service (nil as it doesn't use the devp2p overlay network). +func (s *Service) Protocols() []p2p.Protocol { return nil } + +// APIs implements node.Service, returning the RPC API endpoints provided by the +// stats service (nil as it doesn't provide any user callable APIs). +func (s *Service) APIs() []rpc.API { return nil } + +// Start implements node.Service, starting up the monitoring and reporting daemon. +func (s *Service) Start(server *p2p.Server) error { + s.server = server + go s.loop() + + log.Info("Stats daemon started") + return nil +} + +// Stop implements node.Service, terminating the monitoring and reporting daemon. +func (s *Service) Stop() error { + log.Info("Stats daemon stopped") + return nil +} + +// loop keeps trying to connect to the netstats server, reporting chain events +// until termination. +func (s *Service) loop() { + // Subscribe to chain events to execute updates on + var blockchain blockChain + var txpool txPool + if s.eth != nil { + blockchain = s.eth.BlockChain() + txpool = s.eth.TxPool() + } else { + blockchain = s.les.BlockChain() + txpool = s.les.TxPool() + } + + chainHeadCh := make(chan core.ChainHeadEvent, chainHeadChanSize) + headSub := blockchain.SubscribeChainHeadEvent(chainHeadCh) + defer headSub.Unsubscribe() + + txEventCh := make(chan core.NewTxsEvent, txChanSize) + txSub := txpool.SubscribeNewTxsEvent(txEventCh) + defer txSub.Unsubscribe() + + // Start a goroutine that exhausts the subsciptions to avoid events piling up + var ( + quitCh = make(chan struct{}) + headCh = make(chan *types.Block, 1) + txCh = make(chan struct{}, 1) + ) + go func() { + var lastTx mclock.AbsTime + + HandleLoop: + for { + select { + // Notify of chain head events, but drop if too frequent + case head := <-chainHeadCh: + select { + case headCh <- head.Block: + default: + } + + // Notify of new transaction events, but drop if too frequent + case <-txEventCh: + if time.Duration(mclock.Now()-lastTx) < time.Second { + continue + } + lastTx = mclock.Now() + + select { + case txCh <- struct{}{}: + default: + } + + // node stopped + case <-txSub.Err(): + break HandleLoop + case <-headSub.Err(): + break HandleLoop + } + } + close(quitCh) + }() + // Loop reporting until termination + for { + // Resolve the URL, defaulting to TLS, but falling back to none too + path := fmt.Sprintf("%s/api", s.host) + urls := []string{path} + + // url.Parse and url.IsAbs is unsuitable (https://github.com/golang/go/issues/19779) + if !strings.Contains(path, "://") { + urls = []string{"wss://" + path, "ws://" + path} + } + // Establish a websocket connection to the server on any supported URL + var ( + conn *websocket.Conn + err error + ) + dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second} + header := make(http.Header) + header.Set("origin", "http://localhost") + for _, url := range urls { + conn, _, err = dialer.Dial(url, header) + if err == nil { + break + } + } + if err != nil { + log.Warn("Stats server unreachable", "err", err) + time.Sleep(10 * time.Second) + continue + } + // Authenticate the client with the server + if err = s.login(conn); err != nil { + log.Warn("Stats login failed", "err", err) + conn.Close() + time.Sleep(10 * time.Second) + continue + } + go s.readLoop(conn) + + // Send the initial stats so our node looks decent from the get go + if err = s.report(conn); err != nil { + log.Warn("Initial stats report failed", "err", err) + conn.Close() + continue + } + // Keep sending status updates until the connection breaks + fullReport := time.NewTicker(15 * time.Second) + + for err == nil { + select { + case <-quitCh: + conn.Close() + return + + case <-fullReport.C: + if err = s.report(conn); err != nil { + log.Warn("Full stats report failed", "err", err) + } + case list := <-s.histCh: + if err = s.reportHistory(conn, list); err != nil { + log.Warn("Requested history report failed", "err", err) + } + case head := <-headCh: + if err = s.reportBlock(conn, head); err != nil { + log.Warn("Block stats report failed", "err", err) + } + if err = s.reportPending(conn); err != nil { + log.Warn("Post-block transaction stats report failed", "err", err) + } + case <-txCh: + if err = s.reportPending(conn); err != nil { + log.Warn("Transaction stats report failed", "err", err) + } + } + } + // Make sure the connection is closed + conn.Close() + } +} + +// readLoop loops as long as the connection is alive and retrieves data packets +// from the network socket. If any of them match an active request, it forwards +// it, if they themselves are requests it initiates a reply, and lastly it drops +// unknown packets. +func (s *Service) readLoop(conn *websocket.Conn) { + // If the read loop exists, close the connection + defer conn.Close() + + for { + // Retrieve the next generic network packet and bail out on error + var msg map[string][]interface{} + if err := conn.ReadJSON(&msg); err != nil { + log.Warn("Failed to decode stats server message", "err", err) + return + } + log.Trace("Received message from stats server", "msg", msg) + if len(msg["emit"]) == 0 { + log.Warn("Stats server sent non-broadcast", "msg", msg) + return + } + command, ok := msg["emit"][0].(string) + if !ok { + log.Warn("Invalid stats server message type", "type", msg["emit"][0]) + return + } + // If the message is a ping reply, deliver (someone must be listening!) + if len(msg["emit"]) == 2 && command == "node-pong" { + select { + case s.pongCh <- struct{}{}: + // Pong delivered, continue listening + continue + default: + // Ping routine dead, abort + log.Warn("Stats server pinger seems to have died") + return + } + } + // If the message is a history request, forward to the event processor + if len(msg["emit"]) == 2 && command == "history" { + // Make sure the request is valid and doesn't crash us + request, ok := msg["emit"][1].(map[string]interface{}) + if !ok { + log.Warn("Invalid stats history request", "msg", msg["emit"][1]) + s.histCh <- nil + continue // Ethstats sometime sends invalid history requests, ignore those + } + list, ok := request["list"].([]interface{}) + if !ok { + log.Warn("Invalid stats history block list", "list", request["list"]) + return + } + // Convert the block number list to an integer list + numbers := make([]uint64, len(list)) + for i, num := range list { + n, ok := num.(float64) + if !ok { + log.Warn("Invalid stats history block number", "number", num) + return + } + numbers[i] = uint64(n) + } + select { + case s.histCh <- numbers: + continue + default: + } + } + // Report anything else and continue + log.Info("Unknown stats message", "msg", msg) + } +} + +// nodeInfo is the collection of metainformation about a node that is displayed +// on the monitoring page. +type nodeInfo struct { + Name string `json:"name"` + Node string `json:"node"` + Port int `json:"port"` + Network string `json:"net"` + Protocol string `json:"protocol"` + API string `json:"api"` + Os string `json:"os"` + OsVer string `json:"os_v"` + Client string `json:"client"` + History bool `json:"canUpdateHistory"` +} + +// authMsg is the authentication infos needed to login to a monitoring server. +type authMsg struct { + ID string `json:"id"` + Info nodeInfo `json:"info"` + Secret string `json:"secret"` +} + +// login tries to authorize the client at the remote server. +func (s *Service) login(conn *websocket.Conn) error { + // Construct and send the login authentication + infos := s.server.NodeInfo() + + var network, protocol string + if info := infos.Protocols["eth"]; info != nil { + network = fmt.Sprintf("%d", info.(*eth.NodeInfo).Network) + protocol = fmt.Sprintf("eth/%d", eth.ProtocolVersions[0]) + } else { + network = fmt.Sprintf("%d", infos.Protocols["les"].(*les.NodeInfo).Network) + protocol = fmt.Sprintf("les/%d", les.ClientProtocolVersions[0]) + } + auth := &authMsg{ + ID: s.node, + Info: nodeInfo{ + Name: s.node, + Node: infos.Name, + Port: infos.Ports.Listener, + Network: network, + Protocol: protocol, + API: "No", + Os: runtime.GOOS, + OsVer: runtime.GOARCH, + Client: "0.1.1", + History: true, + }, + Secret: s.pass, + } + login := map[string][]interface{}{ + "emit": {"hello", auth}, + } + if err := conn.WriteJSON(login); err != nil { + return err + } + // Retrieve the remote ack or connection termination + var ack map[string][]string + if err := conn.ReadJSON(&ack); err != nil || len(ack["emit"]) != 1 || ack["emit"][0] != "ready" { + return errors.New("unauthorized") + } + return nil +} + +// report collects all possible data to report and send it to the stats server. +// This should only be used on reconnects or rarely to avoid overloading the +// server. Use the individual methods for reporting subscribed events. +func (s *Service) report(conn *websocket.Conn) error { + if err := s.reportLatency(conn); err != nil { + return err + } + if err := s.reportBlock(conn, nil); err != nil { + return err + } + if err := s.reportPending(conn); err != nil { + return err + } + if err := s.reportStats(conn); err != nil { + return err + } + return nil +} + +// reportLatency sends a ping request to the server, measures the RTT time and +// finally sends a latency update. +func (s *Service) reportLatency(conn *websocket.Conn) error { + // Send the current time to the ethstats server + start := time.Now() + + ping := map[string][]interface{}{ + "emit": {"node-ping", map[string]string{ + "id": s.node, + "clientTime": start.String(), + }}, + } + if err := conn.WriteJSON(ping); err != nil { + return err + } + // Wait for the pong request to arrive back + select { + case <-s.pongCh: + // Pong delivered, report the latency + case <-time.After(5 * time.Second): + // Ping timeout, abort + return errors.New("ping timed out") + } + latency := strconv.Itoa(int((time.Since(start) / time.Duration(2)).Nanoseconds() / 1000000)) + + // Send back the measured latency + log.Trace("Sending measured latency to ethstats", "latency", latency) + + stats := map[string][]interface{}{ + "emit": {"latency", map[string]string{ + "id": s.node, + "latency": latency, + }}, + } + return conn.WriteJSON(stats) +} + +// blockStats is the information to report about individual blocks. +type blockStats struct { + Number *big.Int `json:"number"` + Hash common.Hash `json:"hash"` + ParentHash common.Hash `json:"parentHash"` + Timestamp *big.Int `json:"timestamp"` + Miner common.Address `json:"miner"` + GasUsed uint64 `json:"gasUsed"` + GasLimit uint64 `json:"gasLimit"` + Diff string `json:"difficulty"` + TotalDiff string `json:"totalDifficulty"` + Txs []txStats `json:"transactions"` + TxHash common.Hash `json:"transactionsRoot"` + Root common.Hash `json:"stateRoot"` + Uncles uncleStats `json:"uncles"` +} + +// txStats is the information to report about individual transactions. +type txStats struct { + Hash common.Hash `json:"hash"` +} + +// uncleStats is a custom wrapper around an uncle array to force serializing +// empty arrays instead of returning null for them. +type uncleStats []*types.Header + +func (s uncleStats) MarshalJSON() ([]byte, error) { + if uncles := ([]*types.Header)(s); len(uncles) > 0 { + return json.Marshal(uncles) + } + return []byte("[]"), nil +} + +// reportBlock retrieves the current chain head and reports it to the stats server. +func (s *Service) reportBlock(conn *websocket.Conn, block *types.Block) error { + // Gather the block details from the header or block chain + details := s.assembleBlockStats(block) + + // Assemble the block report and send it to the server + log.Trace("Sending new block to ethstats", "number", details.Number, "hash", details.Hash) + + stats := map[string]interface{}{ + "id": s.node, + "block": details, + } + report := map[string][]interface{}{ + "emit": {"block", stats}, + } + return conn.WriteJSON(report) +} + +// assembleBlockStats retrieves any required metadata to report a single block +// and assembles the block stats. If block is nil, the current head is processed. +func (s *Service) assembleBlockStats(block *types.Block) *blockStats { + // Gather the block infos from the local blockchain + var ( + header *types.Header + td *big.Int + txs []txStats + uncles []*types.Header + ) + if s.eth != nil { + // Full nodes have all needed information available + if block == nil { + block = s.eth.BlockChain().CurrentBlock() + } + header = block.Header() + td = s.eth.BlockChain().GetTd(header.Hash(), header.Number.Uint64()) + + txs = make([]txStats, len(block.Transactions())) + for i, tx := range block.Transactions() { + txs[i].Hash = tx.Hash() + } + uncles = block.Uncles() + } else { + // Light nodes would need on-demand lookups for transactions/uncles, skip + if block != nil { + header = block.Header() + } else { + header = s.les.BlockChain().CurrentHeader() + } + td = s.les.BlockChain().GetTd(header.Hash(), header.Number.Uint64()) + txs = []txStats{} + } + // Assemble and return the block stats + author, _ := s.engine.Author(header) + + return &blockStats{ + Number: header.Number, + Hash: header.Hash(), + ParentHash: header.ParentHash, + Timestamp: new(big.Int).SetUint64(header.Time), + Miner: author, < |