aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2019-08-14 01:37:25 -0400
committerDeterminant <[email protected]>2019-08-14 01:37:25 -0400
commit592f21f5b97e5b1e714f194ae90ab83e6547cf41 (patch)
treec500cdf4ca4266af15703eca182df34d66715918
parentad886faec521f1edcb90f6f8eb4555608d085312 (diff)
finish a full chain example (with p2p network)
-rw-r--r--cmd/geth/config.go6
-rw-r--r--cmd/geth/consolecmd.go2
-rw-r--r--cmd/geth/main.go9
-rw-r--r--cmd/utils/cmd.go2
-rw-r--r--cmd/utils/flags.go88
-rw-r--r--consensus/dummy/consensus.go7
-rw-r--r--eth/backend.go19
-rw-r--r--eth/config.go32
-rw-r--r--ethstats/ethstats.go717
-rw-r--r--examples/fc/main.go14
-rw-r--r--miner/miner.go24
-rw-r--r--node/api.go317
-rw-r--r--node/node.go664
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(&ethereum); 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(&ethServ)
- 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(&ethServ); 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 := &params.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,
+ GasUsed: header.GasUsed,
+ GasLimit: header.GasLimit,
+ Diff: header.Difficulty.String(),
+ TotalDiff: td.String(),
+ Txs: txs,
+ TxHash: header.TxHash,
+ Root: header.Root,
+ Uncles: uncles,
+ }
+}
+
+// reportHistory retrieves the most recent batch of blocks and reports it to the
+// stats server.
+func (s *Service) reportHistory(conn *websocket.Conn, list []uint64) error {
+ // Figure out the indexes that need reporting
+ indexes := make([]uint64, 0, historyUpdateRange)
+ if len(list) > 0 {
+ // Specific indexes requested, send them back in particular
+ indexes = append(indexes, list...)
+ } else {
+ // No indexes requested, send back the top ones
+ var head int64
+ if s.eth != nil {
+ head = s.eth.BlockChain().CurrentHeader().Number.Int64()
+ } else {
+ head = s.les.BlockChain().CurrentHeader().Number.Int64()
+ }
+ start := head - historyUpdateRange + 1
+ if start < 0 {
+ start = 0
+ }
+ for i := uint64(start); i <= uint64(head); i++ {
+ indexes = append(indexes, i)
+ }
+ }
+ // Gather the batch of blocks to report
+ history := make([]*blockStats, len(indexes))
+ for i, number := range indexes {
+ // Retrieve the next block if it's known to us
+ var block *types.Block
+ if s.eth != nil {
+ block = s.eth.BlockChain().GetBlockByNumber(number)
+ } else {
+ if header := s.les.BlockChain().GetHeaderByNumber(number); header != nil {
+ block = types.NewBlockWithHeader(header)
+ }
+ }
+ // If we do have the block, add to the history and continue
+ if block != nil {
+ history[len(history)-1-i] = s.assembleBlockStats(block)
+ continue
+ }
+ // Ran out of blocks, cut the report short and send
+ history = history[len(history)-i:]
+ break
+ }
+ // Assemble the history report and send it to the server
+ if len(history) > 0 {
+ log.Trace("Sending historical blocks to ethstats", "first", history[0].Number, "last", history[len(history)-1].Number)
+ } else {
+ log.Trace("No history to send to stats server")
+ }
+ stats := map[string]interface{}{
+ "id": s.node,
+ "history": history,
+ }
+ report := map[string][]interface{}{
+ "emit": {"history", stats},
+ }
+ return conn.WriteJSON(report)
+}
+
+// pendStats is the information to report about pending transactions.
+type pendStats struct {
+ Pending int `json:"pending"`
+}
+
+// reportPending retrieves the current number of pending transactions and reports
+// it to the stats server.
+func (s *Service) reportPending(conn *websocket.Conn) error {
+ // Retrieve the pending count from the local blockchain
+ var pending int
+ if s.eth != nil {
+ pending, _ = s.eth.TxPool().Stats()
+ } else {
+ pending = s.les.TxPool().Stats()
+ }
+ // Assemble the transaction stats and send it to the server
+ log.Trace("Sending pending transactions to ethstats", "count", pending)
+
+ stats := map[string]interface{}{
+ "id": s.node,
+ "stats": &pendStats{
+ Pending: pending,
+ },
+ }
+ report := map[string][]interface{}{
+ "emit": {"pending", stats},
+ }
+ return conn.WriteJSON(report)
+}
+
+// nodeStats is the information to report about the local node.
+type nodeStats struct {
+ Active bool `json:"active"`
+ Syncing bool `json:"syncing"`
+ Mining bool `json:"mining"`
+ Hashrate int `json:"hashrate"`
+ Peers int `json:"peers"`
+ GasPrice int `json:"gasPrice"`
+ Uptime int `json:"uptime"`
+}
+
+// reportPending retrieves various stats about the node at the networking and
+// mining layer and reports it to the stats server.
+func (s *Service) reportStats(conn *websocket.Conn) error {
+ // Gather the syncing and mining infos from the local miner instance
+ var (
+ mining bool
+ hashrate int
+ syncing bool
+ gasprice int
+ )
+ if s.eth != nil {
+ mining = s.eth.Miner().Mining()
+ hashrate = int(s.eth.Miner().HashRate())
+
+ sync := s.eth.Downloader().Progress()
+ syncing = s.eth.BlockChain().CurrentHeader().Number.Uint64() >= sync.HighestBlock
+
+ price, _ := s.eth.APIBackend.SuggestPrice(context.Background())
+ gasprice = int(price.Uint64())
+ } else {
+ sync := s.les.Downloader().Progress()
+ syncing = s.les.BlockChain().CurrentHeader().Number.Uint64() >= sync.HighestBlock
+ }
+ // Assemble the node stats and send it to the server
+ log.Trace("Sending node details to ethstats")
+
+ stats := map[string]interface{}{
+ "id": s.node,
+ "stats": &nodeStats{
+ Active: true,
+ Mining: mining,
+ Hashrate: hashrate,
+ Peers: s.server.PeerCount(),
+ GasPrice: gasprice,
+ Syncing: syncing,
+ Uptime: 100,
+ },
+ }
+ report := map[string][]interface{}{
+ "emit": {"stats", stats},
+ }
+ return conn.WriteJSON(report)
+}
diff --git a/examples/fc/main.go b/examples/fc/main.go
new file mode 100644
index 0000000..f336ab5
--- /dev/null
+++ b/examples/fc/main.go
@@ -0,0 +1,14 @@
+package main
+
+import (
+ "os"
+ "github.com/Determinant/coreth/cmd/geth"
+)
+
+func checkError(err error) {
+ if err != nil { panic(err) }
+}
+
+func main() {
+ geth.App.Run(os.Args)
+}
diff --git a/miner/miner.go b/miner/miner.go
index 56440cd..969dceb 100644
--- a/miner/miner.go
+++ b/miner/miner.go
@@ -50,9 +50,6 @@ func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *even
}
}
-func (self *Miner) SetEtherbase(addr common.Address) {
- self.w.setEtherbase(addr)
-}
func (self *Miner) Start(coinbase common.Address) {
self.w.start()
@@ -62,6 +59,14 @@ func (self *Miner) Stop() {
self.w.stop()
}
+func (self *Miner) Mining() bool {
+ return false
+}
+
+func (self *Miner) HashRate() uint64 {
+ return 0
+}
+
func (self *Miner) SetExtra(extra []byte) error {
if uint64(len(extra)) > params.MaximumExtraDataSize {
return fmt.Errorf("Extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
@@ -70,20 +75,19 @@ func (self *Miner) SetExtra(extra []byte) error {
return nil
}
-func (self *Miner) Mining() bool {
- return false
+func (self *Miner) SetRecommitInterval(interval time.Duration) {
+ self.w.setRecommitInterval(interval)
}
-func (self *Miner) HashRate() uint64 {
- return 0
+func (self *Miner) Pending() (*types.Block, *state.StateDB) {
+ return self.w.pending()
}
-func (self *Miner) SetRecommitInterval(interval time.Duration) {}
func (self *Miner) PendingBlock() *types.Block {
return self.w.pendingBlock()
}
-func (self *Miner) Pending() (*types.Block, *state.StateDB) {
- return self.w.pending()
+func (self *Miner) SetEtherbase(addr common.Address) {
+ self.w.setEtherbase(addr)
}
diff --git a/node/api.go b/node/api.go
new file mode 100644
index 0000000..66cd1dd
--- /dev/null
+++ b/node/api.go
@@ -0,0 +1,317 @@
+// Copyright 2015 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 node
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/ethereum/go-ethereum/common/hexutil"
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/p2p"
+ "github.com/ethereum/go-ethereum/p2p/enode"
+ "github.com/ethereum/go-ethereum/rpc"
+)
+
+// PrivateAdminAPI is the collection of administrative API methods exposed only
+// over a secure RPC channel.
+type PrivateAdminAPI struct {
+ node *Node // Node interfaced by this API
+}
+
+// NewPrivateAdminAPI creates a new API definition for the private admin methods
+// of the node itself.
+func NewPrivateAdminAPI(node *Node) *PrivateAdminAPI {
+ return &PrivateAdminAPI{node: node}
+}
+
+// AddPeer requests connecting to a remote node, and also maintaining the new
+// connection at all times, even reconnecting if it is lost.
+func (api *PrivateAdminAPI) AddPeer(url string) (bool, error) {
+ // Make sure the server is running, fail otherwise
+ server := api.node.Server()
+ if server == nil {
+ return false, ErrNodeStopped
+ }
+ // Try to add the url as a static peer and return
+ node, err := enode.Parse(enode.ValidSchemes, url)
+ if err != nil {
+ return false, fmt.Errorf("invalid enode: %v", err)
+ }
+ server.AddPeer(node)
+ return true, nil
+}
+
+// RemovePeer disconnects from a remote node if the connection exists
+func (api *PrivateAdminAPI) RemovePeer(url string) (bool, error) {
+ // Make sure the server is running, fail otherwise
+ server := api.node.Server()
+ if server == nil {
+ return false, ErrNodeStopped
+ }
+ // Try to remove the url as a static peer and return
+ node, err := enode.Parse(enode.ValidSchemes, url)
+ if err != nil {
+ return false, fmt.Errorf("invalid enode: %v", err)
+ }
+ server.RemovePeer(node)
+ return true, nil
+}
+
+// AddTrustedPeer allows a remote node to always connect, even if slots are full
+func (api *PrivateAdminAPI) AddTrustedPeer(url string) (bool, error) {
+ // Make sure the server is running, fail otherwise
+ server := api.node.Server()
+ if server == nil {
+ return false, ErrNodeStopped
+ }
+ node, err := enode.Parse(enode.ValidSchemes, url)
+ if err != nil {
+ return false, fmt.Errorf("invalid enode: %v", err)
+ }
+ server.AddTrustedPeer(node)
+ return true, nil
+}
+
+// RemoveTrustedPeer removes a remote node from the trusted peer set, but it
+// does not disconnect it automatically.
+func (api *PrivateAdminAPI) RemoveTrustedPeer(url string) (bool, error) {
+ // Make sure the server is running, fail otherwise
+ server := api.node.Server()
+ if server == nil {
+ return false, ErrNodeStopped
+ }
+ node, err := enode.Parse(enode.ValidSchemes, url)
+ if err != nil {
+ return false, fmt.Errorf("invalid enode: %v", err)
+ }
+ server.RemoveTrustedPeer(node)
+ return true, nil
+}
+
+// PeerEvents creates an RPC subscription which receives peer events from the
+// node's p2p.Server
+func (api *PrivateAdminAPI) PeerEvents(ctx context.Context) (*rpc.Subscription, error) {
+ // Make sure the server is running, fail otherwise
+ server := api.node.Server()
+ if server == nil {
+ return nil, ErrNodeStopped
+ }
+
+ // Create the subscription
+ notifier, supported := rpc.NotifierFromContext(ctx)
+ if !supported {
+ return nil, rpc.ErrNotificationsUnsupported
+ }
+ rpcSub := notifier.CreateSubscription()
+
+ go func() {
+ events := make(chan *p2p.PeerEvent)
+ sub := server.SubscribeEvents(events)
+ defer sub.Unsubscribe()
+
+ for {
+ select {
+ case event := <-events:
+ notifier.Notify(rpcSub.ID, event)
+ case <-sub.Err():
+ return
+ case <-rpcSub.Err():
+ return
+ case <-notifier.Closed():
+ return
+ }
+ }
+ }()
+
+ return rpcSub, nil
+}
+
+// StartRPC starts the HTTP RPC API server.
+func (api *PrivateAdminAPI) StartRPC(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error) {
+ api.node.lock.Lock()
+ defer api.node.lock.Unlock()
+
+ if api.node.httpHandler != nil {
+ return false, fmt.Errorf("HTTP RPC already running on %s", api.node.httpEndpoint)
+ }
+
+ if host == nil {
+ h := DefaultHTTPHost
+ if api.node.config.HTTPHost != "" {
+ h = api.node.config.HTTPHost
+ }
+ host = &h
+ }
+ if port == nil {
+ port = &api.node.config.HTTPPort
+ }
+
+ allowedOrigins := api.node.config.HTTPCors
+ if cors != nil {
+ allowedOrigins = nil
+ for _, origin := range strings.Split(*cors, ",") {
+ allowedOrigins = append(allowedOrigins, strings.TrimSpace(origin))
+ }
+ }
+
+ allowedVHosts := api.node.config.HTTPVirtualHosts
+ if vhosts != nil {
+ allowedVHosts = nil
+ for _, vhost := range strings.Split(*host, ",") {
+ allowedVHosts = append(allowedVHosts, strings.TrimSpace(vhost))
+ }
+ }
+
+ modules := api.node.httpWhitelist
+ if apis != nil {
+ modules = nil
+ for _, m := range strings.Split(*apis, ",") {
+ modules = append(modules, strings.TrimSpace(m))
+ }
+ }
+
+ if err := api.node.startHTTP(fmt.Sprintf("%s:%d", *host, *port), api.node.rpcAPIs, modules, allowedOrigins, allowedVHosts, api.node.config.HTTPTimeouts); err != nil {
+ return false, err
+ }
+ return true, nil
+}
+
+// StopRPC terminates an already running HTTP RPC API endpoint.
+func (api *PrivateAdminAPI) StopRPC() (bool, error) {
+ api.node.lock.Lock()
+ defer api.node.lock.Unlock()
+
+ if api.node.httpHandler == nil {
+ return false, fmt.Errorf("HTTP RPC not running")
+ }
+ api.node.stopHTTP()
+ return true, nil
+}
+
+// StartWS starts the websocket RPC API server.
+func (api *PrivateAdminAPI) StartWS(host *string, port *int, allowedOrigins *string, apis *string) (bool, error) {
+ api.node.lock.Lock()
+ defer api.node.lock.Unlock()
+
+ if api.node.wsHandler != nil {
+ return false, fmt.Errorf("WebSocket RPC already running on %s", api.node.wsEndpoint)
+ }
+
+ if host == nil {
+ h := DefaultWSHost
+ if api.node.config.WSHost != "" {
+ h = api.node.config.WSHost
+ }
+ host = &h
+ }
+ if port == nil {
+ port = &api.node.config.WSPort
+ }
+
+ origins := api.node.config.WSOrigins
+ if allowedOrigins != nil {
+ origins = nil
+ for _, origin := range strings.Split(*allowedOrigins, ",") {
+ origins = append(origins, strings.TrimSpace(origin))
+ }
+ }
+
+ modules := api.node.config.WSModules
+ if apis != nil {
+ modules = nil
+ for _, m := range strings.Split(*apis, ",") {
+ modules = append(modules, strings.TrimSpace(m))
+ }
+ }
+
+ if err := api.node.startWS(fmt.Sprintf("%s:%d", *host, *port), api.node.rpcAPIs, modules, origins, api.node.config.WSExposeAll); err != nil {
+ return false, err
+ }
+ return true, nil
+}
+
+// StopWS terminates an already running websocket RPC API endpoint.
+func (api *PrivateAdminAPI) StopWS() (bool, error) {
+ api.node.lock.Lock()
+ defer api.node.lock.Unlock()
+
+ if api.node.wsHandler == nil {
+ return false, fmt.Errorf("WebSocket RPC not running")
+ }
+ api.node.stopWS()
+ return true, nil
+}
+
+// PublicAdminAPI is the collection of administrative API methods exposed over
+// both secure and unsecure RPC channels.
+type PublicAdminAPI struct {
+ node *Node // Node interfaced by this API
+}
+
+// NewPublicAdminAPI creates a new API definition for the public admin methods
+// of the node itself.
+func NewPublicAdminAPI(node *Node) *PublicAdminAPI {
+ return &PublicAdminAPI{node: node}
+}
+
+// Peers retrieves all the information we know about each individual peer at the
+// protocol granularity.
+func (api *PublicAdminAPI) Peers() ([]*p2p.PeerInfo, error) {
+ server := api.node.Server()
+ if server == nil {
+ return nil, ErrNodeStopped
+ }
+ return server.PeersInfo(), nil
+}
+
+// NodeInfo retrieves all the information we know about the host node at the
+// protocol granularity.
+func (api *PublicAdminAPI) NodeInfo() (*p2p.NodeInfo, error) {
+ server := api.node.Server()
+ if server == nil {
+ return nil, ErrNodeStopped
+ }
+ return server.NodeInfo(), nil
+}
+
+// Datadir retrieves the current data directory the node is using.
+func (api *PublicAdminAPI) Datadir() string {
+ return api.node.DataDir()
+}
+
+// PublicWeb3API offers helper utils
+type PublicWeb3API struct {
+ stack *Node
+}
+
+// NewPublicWeb3API creates a new Web3Service instance
+func NewPublicWeb3API(stack *Node) *PublicWeb3API {
+ return &PublicWeb3API{stack}
+}
+
+// ClientVersion returns the node name
+func (s *PublicWeb3API) ClientVersion() string {
+ return s.stack.Server().Name
+}
+
+// Sha3 applies the ethereum sha3 implementation on the input.
+// It assumes the input is hex encoded.
+func (s *PublicWeb3API) Sha3(input hexutil.Bytes) hexutil.Bytes {
+ return crypto.Keccak256(input)
+}
diff --git a/node/node.go b/node/node.go
new file mode 100644
index 0000000..bf496a4
--- /dev/null
+++ b/node/node.go
@@ -0,0 +1,664 @@
+// Copyright 2015 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 node
+
+import (
+ "errors"
+ "fmt"
+ "net"
+ "os"
+ "path/filepath"
+ "reflect"
+ "strings"
+ "sync"
+
+ "github.com/ethereum/go-ethereum/accounts"
+ "github.com/ethereum/go-ethereum/core/rawdb"
+ "github.com/ethereum/go-ethereum/ethdb"
+ "github.com/ethereum/go-ethereum/event"
+ "github.com/Determinant/coreth/internal/debug"
+ "github.com/ethereum/go-ethereum/log"
+ "github.com/ethereum/go-ethereum/p2p"
+ "github.com/ethereum/go-ethereum/rpc"
+ "github.com/prometheus/tsdb/fileutil"
+)
+
+// Node is a container on which services can be registered.
+type Node struct {
+ eventmux *event.TypeMux // Event multiplexer used between the services of a stack
+ config *Config
+ accman *accounts.Manager
+
+ ephemeralKeystore string // if non-empty, the key directory that will be removed by Stop
+ instanceDirLock fileutil.Releaser // prevents concurrent use of instance directory
+
+ serverConfig p2p.Config
+ server *p2p.Server // Currently running P2P networking layer
+
+ serviceFuncs []ServiceConstructor // Service constructors (in dependency order)
+ services map[reflect.Type]Service // Currently running services
+
+ rpcAPIs []rpc.API // List of APIs currently provided by the node
+ inprocHandler *rpc.Server // In-process RPC request handler to process the API requests
+
+ ipcEndpoint string // IPC endpoint to listen at (empty = IPC disabled)
+ ipcListener net.Listener // IPC RPC listener socket to serve API requests
+ ipcHandler *rpc.Server // IPC RPC request handler to process the API requests
+
+ httpEndpoint string // HTTP endpoint (interface + port) to listen at (empty = HTTP disabled)
+ httpWhitelist []string // HTTP RPC modules to allow through this endpoint
+ httpListener net.Listener // HTTP RPC listener socket to server API requests
+ httpHandler *rpc.Server // HTTP RPC request handler to process the API requests
+
+ wsEndpoint string // Websocket endpoint (interface + port) to listen at (empty = websocket disabled)
+ wsListener net.Listener // Websocket RPC listener socket to server API requests
+ wsHandler *rpc.Server // Websocket RPC request handler to process the API requests
+
+ stop chan struct{} // Channel to wait for termination notifications
+ lock sync.RWMutex
+
+ log log.Logger
+}
+
+// New creates a new P2P node, ready for protocol registration.
+func New(conf *Config) (*Node, error) {
+ // Copy config and resolve the datadir so future changes to the current
+ // working directory don't affect the node.
+ confCopy := *conf
+ conf = &confCopy
+ if conf.DataDir != "" {
+ absdatadir, err := filepath.Abs(conf.DataDir)
+ if err != nil {
+ return nil, err
+ }
+ conf.DataDir = absdatadir
+ }
+ // Ensure that the instance name doesn't cause weird conflicts with
+ // other files in the data directory.
+ if strings.ContainsAny(conf.Name, `/\`) {
+ return nil, errors.New(`Config.Name must not contain '/' or '\'`)
+ }
+ if conf.Name == datadirDefaultKeyStore {
+ return nil, errors.New(`Config.Name cannot be "` + datadirDefaultKeyStore + `"`)
+ }
+ if strings.HasSuffix(conf.Name, ".ipc") {
+ return nil, errors.New(`Config.Name cannot end in ".ipc"`)
+ }
+ // Ensure that the AccountManager method works before the node has started.
+ // We rely on this in cmd/geth.
+ am, ephemeralKeystore, err := makeAccountManager(conf)
+ if err != nil {
+ return nil, err
+ }
+ if conf.Logger == nil {
+ conf.Logger = log.New()
+ }
+ // Note: any interaction with Config that would create/touch files
+ // in the data directory or instance directory is delayed until Start.
+ return &Node{
+ accman: am,
+ ephemeralKeystore: ephemeralKeystore,
+ config: conf,
+ serviceFuncs: []ServiceConstructor{},
+ ipcEndpoint: conf.IPCEndpoint(),
+ httpEndpoint: conf.HTTPEndpoint(),
+ wsEndpoint: conf.WSEndpoint(),
+ eventmux: new(event.TypeMux),
+ log: conf.Logger,
+ }, nil
+}
+
+// Close stops the Node and releases resources acquired in
+// Node constructor New.
+func (n *Node) Close() error {
+ var errs []error
+
+ // Terminate all subsystems and collect any errors
+ if err := n.Stop(); err != nil && err != ErrNodeStopped {
+ errs = append(errs, err)
+ }
+ if err := n.accman.Close(); err != nil {
+ errs = append(errs, err)
+ }
+ // Report any errors that might have occurred
+ switch len(errs) {
+ case 0:
+ return nil
+ case 1:
+ return errs[0]
+ default:
+ return fmt.Errorf("%v", errs)
+ }
+}
+
+// Register injects a new service into the node's stack. The service created by
+// the passed constructor must be unique in its type with regard to sibling ones.
+func (n *Node) Register(constructor ServiceConstructor) error {
+ n.lock.Lock()
+ defer n.lock.Unlock()
+
+ if n.server != nil {
+ return ErrNodeRunning
+ }
+ n.serviceFuncs = append(n.serviceFuncs, constructor)
+ return nil
+}
+
+// Start create a live P2P node and starts running it.
+func (n *Node) Start() error {
+ n.lock.Lock()
+ defer n.lock.Unlock()
+
+ // Short circuit if the node's already running
+ if n.server != nil {
+ return ErrNodeRunning
+ }
+ if err := n.openDataDir(); err != nil {
+ return err
+ }
+
+ // Initialize the p2p server. This creates the node key and
+ // discovery databases.
+ n.serverConfig = n.config.P2P
+ n.serverConfig.PrivateKey = n.config.NodeKey()
+ n.serverConfig.Name = n.config.NodeName()
+ n.serverConfig.Logger = n.log
+ if n.serverConfig.StaticNodes == nil {
+ n.serverConfig.StaticNodes = n.config.StaticNodes()
+ }
+ if n.serverConfig.TrustedNodes == nil {
+ n.serverConfig.TrustedNodes = n.config.TrustedNodes()
+ }
+ if n.serverConfig.NodeDatabase == "" {
+ n.serverConfig.NodeDatabase = n.config.NodeDB()
+ }
+ running := &p2p.Server{Config: n.serverConfig}
+ n.log.Info("Starting peer-to-peer node", "instance", n.serverConfig.Name)
+
+ // Otherwise copy and specialize the P2P configuration
+ services := make(map[reflect.Type]Service)
+ for _, constructor := range n.serviceFuncs {
+ // Create a new context for the particular service
+ ctx := &ServiceContext{
+ config: n.config,
+ services: make(map[reflect.Type]Service),
+ EventMux: n.eventmux,
+ AccountManager: n.accman,
+ }
+ for kind, s := range services { // copy needed for threaded access
+ ctx.services[kind] = s
+ }
+ // Construct and save the service
+ service, err := constructor(ctx)
+ if err != nil {
+ return err
+ }
+ kind := reflect.TypeOf(service)
+ if _, exists := services[kind]; exists {
+ return &DuplicateServiceError{Kind: kind}
+ }
+ services[kind] = service
+ }
+ // Gather the protocols and start the freshly assembled P2P server
+ for _, service := range services {
+ running.Protocols = append(running.Protocols, service.Protocols()...)
+ }
+ if err := running.Start(); err != nil {
+ return convertFileLockError(err)
+ }
+ // Start each of the services
+ var started []reflect.Type
+ for kind, service := range services {
+ // Start the next service, stopping all previous upon failure
+ if err := service.Start(running); err != nil {
+ for _, kind := range started {
+ services[kind].Stop()
+ }
+ running.Stop()
+
+ return err
+ }
+ // Mark the service started for potential cleanup
+ started = append(started, kind)
+ }
+ // Lastly start the configured RPC interfaces
+ if err := n.startRPC(services); err != nil {
+ for _, service := range services {
+ service.Stop()
+ }
+ running.Stop()
+ return err
+ }
+ // Finish initializing the startup
+ n.services = services
+ n.server = running
+ n.stop = make(chan struct{})
+ return nil
+}
+
+// Config returns the configuration of node.
+func (n *Node) Config() *Config {
+ return n.config
+}
+
+func (n *Node) openDataDir() error {
+ if n.config.DataDir == "" {
+ return nil // ephemeral
+ }
+
+ instdir := filepath.Join(n.config.DataDir, n.config.name())
+ if err := os.MkdirAll(instdir, 0700); err != nil {
+ return err
+ }
+ // Lock the instance directory to prevent concurrent use by another instance as well as
+ // accidental use of the instance directory as a database.
+ release, _, err := fileutil.Flock(filepath.Join(instdir, "LOCK"))
+ if err != nil {
+ return convertFileLockError(err)
+ }
+ n.instanceDirLock = release
+ return nil
+}
+
+// startRPC is a helper method to start all the various RPC endpoint during node
+// startup. It's not meant to be called at any time afterwards as it makes certain
+// assumptions about the state of the node.
+func (n *Node) startRPC(services map[reflect.Type]Service) error {
+ // Gather all the possible APIs to surface
+ apis := n.apis()
+ for _, service := range services {
+ apis = append(apis, service.APIs()...)
+ }
+ // Start the various API endpoints, terminating all in case of errors
+ if err := n.startInProc(apis); err != nil {
+ return err
+ }
+ if err := n.startIPC(apis); err != nil {
+ n.stopInProc()
+ return err
+ }
+ if err := n.startHTTP(n.httpEndpoint, apis, n.config.HTTPModules, n.config.HTTPCors, n.config.HTTPVirtualHosts, n.config.HTTPTimeouts); err != nil {
+ n.stopIPC()
+ n.stopInProc()
+ return err
+ }
+ if err := n.startWS(n.wsEndpoint, apis, n.config.WSModules, n.config.WSOrigins, n.config.WSExposeAll); err != nil {
+ n.stopHTTP()
+ n.stopIPC()
+ n.stopInProc()
+ return err
+ }
+ // All API endpoints started successfully
+ n.rpcAPIs = apis
+ return nil
+}
+
+// startInProc initializes an in-process RPC endpoint.
+func (n *Node) startInProc(apis []rpc.API) error {
+ // Register all the APIs exposed by the services
+ handler := rpc.NewServer()
+ for _, api := range apis {
+ if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
+ return err
+ }
+ n.log.Debug("InProc registered", "namespace", api.Namespace)
+ }
+ n.inprocHandler = handler
+ return nil
+}
+
+// stopInProc terminates the in-process RPC endpoint.
+func (n *Node) stopInProc() {
+ if n.inprocHandler != nil {
+ n.inprocHandler.Stop()
+ n.inprocHandler = nil
+ }
+}
+
+// startIPC initializes and starts the IPC RPC endpoint.
+func (n *Node) startIPC(apis []rpc.API) error {
+ if n.ipcEndpoint == "" {
+ return nil // IPC disabled.
+ }
+ listener, handler, err := rpc.StartIPCEndpoint(n.ipcEndpoint, apis)
+ if err != nil {
+ return err
+ }
+ n.ipcListener = listener
+ n.ipcHandler = handler
+ n.log.Info("IPC endpoint opened", "url", n.ipcEndpoint)
+ return nil
+}
+
+// stopIPC terminates the IPC RPC endpoint.
+func (n *Node) stopIPC() {
+ if n.ipcListener != nil {
+ n.ipcListener.Close()
+ n.ipcListener = nil
+
+ n.log.Info("IPC endpoint closed", "url", n.ipcEndpoint)
+ }
+ if n.ipcHandler != nil {
+ n.ipcHandler.Stop()
+ n.ipcHandler = nil
+ }
+}
+
+// startHTTP initializes and starts the HTTP RPC endpoint.
+func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors []string, vhosts []string, timeouts rpc.HTTPTimeouts) error {
+ // Short circuit if the HTTP endpoint isn't being exposed
+ if endpoint == "" {
+ return nil
+ }
+ listener, handler, err := rpc.StartHTTPEndpoint(endpoint, apis, modules, cors, vhosts, timeouts)
+ if err != nil {
+ return err
+ }
+ n.log.Info("HTTP endpoint opened", "url", fmt.Sprintf("http://%s", endpoint), "cors", strings.Join(cors, ","), "vhosts", strings.Join(vhosts, ","))
+ // All listeners booted successfully
+ n.httpEndpoint = endpoint
+ n.httpListener = listener
+ n.httpHandler = handler
+
+ return nil
+}
+
+// stopHTTP terminates the HTTP RPC endpoint.
+func (n *Node) stopHTTP() {
+ if n.httpListener != nil {
+ n.httpListener.Close()
+ n.httpListener = nil
+
+ n.log.Info("HTTP endpoint closed", "url", fmt.Sprintf("http://%s", n.httpEndpoint))
+ }
+ if n.httpHandler != nil {
+ n.httpHandler.Stop()
+ n.httpHandler = nil
+ }
+}
+
+// startWS initializes and starts the websocket RPC endpoint.
+func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrigins []string, exposeAll bool) error {
+ // Short circuit if the WS endpoint isn't being exposed
+ if endpoint == "" {
+ return nil
+ }
+ listener, handler, err := rpc.StartWSEndpoint(endpoint, apis, modules, wsOrigins, exposeAll)
+ if err != nil {
+ return err
+ }
+ n.log.Info("WebSocket endpoint opened", "url", fmt.Sprintf("ws://%s", listener.Addr()))
+ // All listeners booted successfully
+ n.wsEndpoint = endpoint
+ n.wsListener = listener
+ n.wsHandler = handler
+
+ return nil
+}
+
+// stopWS terminates the websocket RPC endpoint.
+func (n *Node) stopWS() {
+ if n.wsListener != nil {
+ n.wsListener.Close()
+ n.wsListener = nil
+
+ n.log.Info("WebSocket endpoint closed", "url", fmt.Sprintf("ws://%s", n.wsEndpoint))
+ }
+ if n.wsHandler != nil {
+ n.wsHandler.Stop()
+ n.wsHandler = nil
+ }
+}
+
+// Stop terminates a running node along with all it's services. In the node was
+// not started, an error is returned.
+func (n *Node) Stop() error {
+ n.lock.Lock()
+ defer n.lock.Unlock()
+
+ // Short circuit if the node's not running
+ if n.server == nil {
+ return ErrNodeStopped
+ }
+
+ // Terminate the API, services and the p2p server.
+ n.stopWS()
+ n.stopHTTP()
+ n.stopIPC()
+ n.rpcAPIs = nil
+ failure := &StopError{
+ Services: make(map[reflect.Type]error),
+ }
+ for kind, service := range n.services {
+ if err := service.Stop(); err != nil {
+ failure.Services[kind] = err
+ }
+ }
+ n.server.Stop()
+ n.services = nil
+ n.server = nil
+
+ // Release instance directory lock.
+ if n.instanceDirLock != nil {
+ if err := n.instanceDirLock.Release(); err != nil {
+ n.log.Error("Can't release datadir lock", "err", err)
+ }
+ n.instanceDirLock = nil
+ }
+
+ // unblock n.Wait
+ close(n.stop)
+
+ // Remove the keystore if it was created ephemerally.
+ var keystoreErr error
+ if n.ephemeralKeystore != "" {
+ keystoreErr = os.RemoveAll(n.ephemeralKeystore)
+ }
+
+ if len(failure.Services) > 0 {
+ return failure
+ }
+ if keystoreErr != nil {
+ return keystoreErr
+ }
+ return nil
+}
+
+// Wait blocks the thread until the node is stopped. If the node is not running
+// at the time of invocation, the method immediately returns.
+func (n *Node) Wait() {
+ n.lock.RLock()
+ if n.server == nil {
+ n.lock.RUnlock()
+ return
+ }
+ stop := n.stop
+ n.lock.RUnlock()
+
+ <-stop
+}
+
+// Restart terminates a running node and boots up a new one in its place. If the
+// node isn't running, an error is returned.
+func (n *Node) Restart() error {
+ if err := n.Stop(); err != nil {
+ return err
+ }
+ if err := n.Start(); err != nil {
+ return err
+ }
+ return nil
+}
+
+// Attach creates an RPC client attached to an in-process API handler.
+func (n *Node) Attach() (*rpc.Client, error) {
+ n.lock.RLock()
+ defer n.lock.RUnlock()
+
+ if n.server == nil {
+ return nil, ErrNodeStopped
+ }
+ return rpc.DialInProc(n.inprocHandler), nil
+}
+
+// RPCHandler returns the in-process RPC request handler.
+func (n *Node) RPCHandler() (*rpc.Server, error) {
+ n.lock.RLock()
+ defer n.lock.RUnlock()
+
+ if n.inprocHandler == nil {
+ return nil, ErrNodeStopped
+ }
+ return n.inprocHandler, nil
+}
+
+// Server retrieves the currently running P2P network layer. This method is meant
+// only to inspect fields of the currently running server, life cycle management
+// should be left to this Node entity.
+func (n *Node) Server() *p2p.Server {
+ n.lock.RLock()
+ defer n.lock.RUnlock()
+
+ return n.server
+}
+
+// Service retrieves a currently running service registered of a specific type.
+func (n *Node) Service(service interface{}) error {
+ n.lock.RLock()
+ defer n.lock.RUnlock()
+
+ // Short circuit if the node's not running
+ if n.server == nil {
+ return ErrNodeStopped
+ }
+ // Otherwise try to find the service to return
+ element := reflect.ValueOf(service).Elem()
+ if running, ok := n.services[element.Type()]; ok {
+ element.Set(reflect.ValueOf(running))
+ return nil
+ }
+ return ErrServiceUnknown
+}
+
+// DataDir retrieves the current datadir used by the protocol stack.
+// Deprecated: No files should be stored in this directory, use InstanceDir instead.
+func (n *Node) DataDir() string {
+ return n.config.DataDir
+}
+
+// InstanceDir retrieves the instance directory used by the protocol stack.
+func (n *Node) InstanceDir() string {
+ return n.config.instanceDir()
+}
+
+// AccountManager retrieves the account manager used by the protocol stack.
+func (n *Node) AccountManager() *accounts.Manager {
+ return n.accman
+}
+
+// IPCEndpoint retrieves the current IPC endpoint used by the protocol stack.
+func (n *Node) IPCEndpoint() string {
+ return n.ipcEndpoint
+}
+
+// HTTPEndpoint retrieves the current HTTP endpoint used by the protocol stack.
+func (n *Node) HTTPEndpoint() string {
+ n.lock.Lock()
+ defer n.lock.Unlock()
+
+ if n.httpListener != nil {
+ return n.httpListener.Addr().String()
+ }
+ return n.httpEndpoint
+}
+
+// WSEndpoint retrieves the current WS endpoint used by the protocol stack.
+func (n *Node) WSEndpoint() string {
+ n.lock.Lock()
+ defer n.lock.Unlock()
+
+ if n.wsListener != nil {
+ return n.wsListener.Addr().String()
+ }
+ return n.wsEndpoint
+}
+
+// EventMux retrieves the event multiplexer used by all the network services in
+// the current protocol stack.
+func (n *Node) EventMux() *event.TypeMux {
+ return n.eventmux
+}
+
+// OpenDatabase opens an existing database with the given name (or creates one if no
+// previous can be found) from within the node's instance directory. If the node is
+// ephemeral, a memory database is returned.
+func (n *Node) OpenDatabase(name string, cache, handles int, namespace string) (ethdb.Database, error) {
+ if n.config.DataDir == "" {
+ return rawdb.NewMemoryDatabase(), nil
+ }
+ return rawdb.NewLevelDBDatabase(n.config.ResolvePath(name), cache, handles, namespace)
+}
+
+// OpenDatabaseWithFreezer opens an existing database with the given name (or
+// creates one if no previous can be found) from within the node's data directory,
+// also attaching a chain freezer to it that moves ancient chain data from the
+// database to immutable append-only files. If the node is an ephemeral one, a
+// memory database is returned.
+func (n *Node) OpenDatabaseWithFreezer(name string, cache, handles int, freezer, namespace string) (ethdb.Database, error) {
+ if n.config.DataDir == "" {
+ return rawdb.NewMemoryDatabase(), nil
+ }
+ root := n.config.ResolvePath(name)
+
+ switch {
+ case freezer == "":
+ freezer = filepath.Join(root, "ancient")
+ case !filepath.IsAbs(freezer):
+ freezer = n.config.ResolvePath(freezer)
+ }
+ return rawdb.NewLevelDBDatabaseWithFreezer(root, cache, handles, freezer, namespace)
+}
+
+// ResolvePath returns the absolute path of a resource in the instance directory.
+func (n *Node) ResolvePath(x string) string {
+ return n.config.ResolvePath(x)
+}
+
+// apis returns the collection of RPC descriptors this node offers.
+func (n *Node) apis() []rpc.API {
+ return []rpc.API{
+ {
+ Namespace: "admin",
+ Version: "1.0",
+ Service: NewPrivateAdminAPI(n),
+ }, {
+ Namespace: "admin",
+ Version: "1.0",
+ Service: NewPublicAdminAPI(n),
+ Public: true,
+ }, {
+ Namespace: "debug",
+ Version: "1.0",
+ Service: debug.Handler,
+ }, {
+ Namespace: "web3",
+ Version: "1.0",
+ Service: NewPublicWeb3API(n),
+ Public: true,
+ },
+ }
+}