2016-09-11 11:44:14 +00:00
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-12-07 21:07:08 +00:00
|
|
|
"io"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
2016-10-11 11:33:37 +00:00
|
|
|
"path/filepath"
|
2016-12-07 21:07:08 +00:00
|
|
|
"reflect"
|
2016-09-11 11:44:14 +00:00
|
|
|
"runtime"
|
2016-12-07 21:07:08 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-12-07 21:07:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/les"
|
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2016-11-25 08:06:47 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv2"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-12-07 21:07:08 +00:00
|
|
|
ClientIdentifier = "StatusIM" // Client identifier to advertise over the network
|
|
|
|
VersionMajor = 1 // Major version component of the current release
|
|
|
|
VersionMinor = 1 // Minor version component of the current release
|
|
|
|
VersionPatch = 0 // Patch version component of the current release
|
|
|
|
VersionMeta = "unstable" // Version metadata to append to the version string
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
RPCPort = 8545 // RPC port (replaced in unit tests)
|
|
|
|
NetworkPort = 30303
|
|
|
|
MaxPeers = 25
|
|
|
|
MaxLightPeers = 20
|
|
|
|
MaxPendingPeers = 0
|
|
|
|
|
|
|
|
ProcessFileDescriptorLimit = uint64(2048)
|
|
|
|
DatabaseCacheSize = 128 // Megabytes of memory allocated to internal caching (min 16MB / database forced)
|
2016-09-15 03:08:06 +00:00
|
|
|
|
|
|
|
EventNodeStarted = "node.started"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// Gas price settings
|
2016-09-11 11:44:14 +00:00
|
|
|
var (
|
2016-12-07 21:07:08 +00:00
|
|
|
GasPrice = new(big.Int).Mul(big.NewInt(20), common.Shannon) // Minimal gas price to accept for mining a transactions
|
|
|
|
GpoMinGasPrice = new(big.Int).Mul(big.NewInt(20), common.Shannon) // Minimum suggested gas price
|
|
|
|
GpoMaxGasPrice = new(big.Int).Mul(big.NewInt(500), common.Shannon) // Maximum suggested gas price
|
|
|
|
GpoFullBlockRatio = 80 // Full block threshold for gas price calculation (%)
|
|
|
|
GpobaseStepDown = 10 // Suggested gas price base step down ratio (1/1000)
|
|
|
|
GpobaseStepUp = 100 // Suggested gas price base step up ratio (1/1000)
|
|
|
|
GpobaseCorrectionFactor = 110 // Suggested gas price base correction factor (%)
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// default node configuration options
|
2016-09-11 11:44:14 +00:00
|
|
|
var (
|
2016-12-07 21:07:08 +00:00
|
|
|
UseTestnetFlag = "true" // to be overridden via -ldflags '-X geth.UseTestnetFlag'
|
|
|
|
UseTestnet = false
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
func init() {
|
|
|
|
if UseTestnetFlag == "true" { // set at compile time, here we make sure to set corresponding boolean flag
|
|
|
|
UseTestnet = true
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// node-related errors
|
|
|
|
var (
|
|
|
|
ErrRLimitRaiseFailure = errors.New("failed to register the whisper service")
|
|
|
|
ErrDatabaseAccessFailure = errors.New("could not open database")
|
|
|
|
ErrChainConfigurationFailure = errors.New("could not make chain configuration")
|
|
|
|
ErrEthServiceRegistrationFailure = errors.New("failed to register the Ethereum service")
|
|
|
|
ErrSshServiceRegistrationFailure = errors.New("failed to register the Whisper service")
|
|
|
|
ErrLightEthRegistrationFailure = errors.New("failed to register the LES service")
|
|
|
|
)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
type Node struct {
|
|
|
|
geth *node.Node // reference to the running Geth node
|
|
|
|
started chan struct{} // channel to wait for node to start
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// Inited checks whether status node has been properly initialized
|
|
|
|
func (n *Node) Inited() bool {
|
|
|
|
return n != nil && n.geth != nil
|
2016-09-15 03:08:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// MakeNode create a geth node entity
|
|
|
|
func MakeNode(dataDir string, rpcPort int) *Node {
|
|
|
|
glog.CopyStandardLogTo("INFO")
|
|
|
|
glog.SetToStderr(true)
|
|
|
|
|
|
|
|
bootstrapNodes := params.MainnetBootnodes
|
|
|
|
if UseTestnet {
|
|
|
|
dataDir = filepath.Join(dataDir, "testnet")
|
|
|
|
bootstrapNodes = params.TestnetBootnodes
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// configure required node (should you need to update node's config, e.g. add bootstrap nodes, see node.Config)
|
|
|
|
config := &node.Config{
|
|
|
|
DataDir: dataDir,
|
|
|
|
UseLightweightKDF: true,
|
|
|
|
Name: ClientIdentifier,
|
|
|
|
Version: fmt.Sprintf("%d.%d.%d-%s", VersionMajor, VersionMinor, VersionPatch, VersionMeta),
|
|
|
|
NoDiscovery: true,
|
|
|
|
DiscoveryV5: true,
|
|
|
|
DiscoveryV5Addr: fmt.Sprintf(":%d", NetworkPort+1),
|
|
|
|
BootstrapNodes: bootstrapNodes,
|
|
|
|
BootstrapNodesV5: params.DiscoveryV5Bootnodes,
|
|
|
|
ListenAddr: fmt.Sprintf(":%d", NetworkPort),
|
|
|
|
MaxPeers: MaxPeers,
|
|
|
|
MaxPendingPeers: MaxPendingPeers,
|
|
|
|
HTTPHost: node.DefaultHTTPHost,
|
|
|
|
HTTPPort: rpcPort,
|
|
|
|
HTTPCors: "*",
|
|
|
|
HTTPModules: strings.Split("db,eth,net,web3,shh,personal,admin", ","), // TODO remove "admin" on main net
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
stack, err := node.New(config)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2016-12-07 21:07:08 +00:00
|
|
|
Fatalf(ErrNodeMakeFailure)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// start Ethereum service
|
|
|
|
if err := activateEthService(stack, makeDefaultExtra()); err != nil {
|
|
|
|
Fatalf(fmt.Errorf("%v: %v", ErrEthServiceRegistrationFailure, err))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// start Whisper service
|
|
|
|
if err := activateShhService(stack); err != nil {
|
|
|
|
Fatalf(fmt.Errorf("%v: %v", ErrSshServiceRegistrationFailure, err))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
return &Node{
|
|
|
|
geth: stack,
|
|
|
|
started: make(chan struct{}),
|
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// activateEthService configures and registers the eth.Ethereum service with a given node.
|
|
|
|
func activateEthService(stack *node.Node, extra []byte) error {
|
|
|
|
ethConf := ð.Config{
|
|
|
|
Etherbase: common.Address{},
|
|
|
|
ChainConfig: makeChainConfig(stack),
|
|
|
|
FastSync: false,
|
|
|
|
LightMode: true,
|
|
|
|
LightServ: 60,
|
|
|
|
LightPeers: MaxLightPeers,
|
|
|
|
MaxPeers: MaxPeers,
|
|
|
|
DatabaseCache: DatabaseCacheSize,
|
|
|
|
DatabaseHandles: makeDatabaseHandles(),
|
|
|
|
NetworkId: 1, // Olympic
|
|
|
|
MinerThreads: runtime.NumCPU(),
|
|
|
|
GasPrice: GasPrice,
|
|
|
|
GpoMinGasPrice: GpoMinGasPrice,
|
|
|
|
GpoMaxGasPrice: GpoMaxGasPrice,
|
|
|
|
GpoFullBlockRatio: GpoFullBlockRatio,
|
|
|
|
GpobaseStepDown: GpobaseStepDown,
|
|
|
|
GpobaseStepUp: GpobaseStepUp,
|
|
|
|
GpobaseCorrectionFactor: GpobaseCorrectionFactor,
|
|
|
|
SolcPath: "solc",
|
|
|
|
AutoDAG: false,
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
if UseTestnet {
|
|
|
|
ethConf.NetworkId = 3
|
|
|
|
ethConf.Genesis = core.DefaultTestnetGenesisBlock()
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
|
|
|
return les.New(ctx, ethConf)
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("%v: %v", ErrLightEthRegistrationFailure, err)
|
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
return nil
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// activateShhService configures Whisper and adds it to the given node.
|
|
|
|
func activateShhService(stack *node.Node) error {
|
|
|
|
serviceConstructor := func(*node.ServiceContext) (node.Service, error) {
|
|
|
|
return whisper.New(), nil
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
if err := stack.Register(serviceConstructor); err != nil {
|
|
|
|
return err
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
return nil
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// makeChainConfig reads the chain configuration from the database in the datadir.
|
|
|
|
func makeChainConfig(stack *node.Node) *params.ChainConfig {
|
|
|
|
config := new(params.ChainConfig)
|
|
|
|
|
|
|
|
if UseTestnet {
|
|
|
|
config = params.TestnetChainConfig
|
|
|
|
} else {
|
|
|
|
// Homestead fork
|
|
|
|
config.HomesteadBlock = params.MainNetHomesteadBlock
|
|
|
|
// DAO fork
|
|
|
|
config.DAOForkBlock = params.MainNetDAOForkBlock
|
|
|
|
config.DAOForkSupport = true
|
|
|
|
|
|
|
|
// DoS reprice fork
|
|
|
|
config.EIP150Block = params.MainNetHomesteadGasRepriceBlock
|
|
|
|
config.EIP150Hash = params.MainNetHomesteadGasRepriceHash
|
|
|
|
|
|
|
|
// DoS state cleanup fork
|
|
|
|
config.EIP155Block = params.MainNetSpuriousDragon
|
|
|
|
config.EIP158Block = params.MainNetSpuriousDragon
|
|
|
|
config.ChainId = params.MainNetChainID
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// makeDatabaseHandles makes sure that enough file descriptors are available to the process
|
|
|
|
// (and returns half of them for node's database to use)
|
|
|
|
func makeDatabaseHandles() int {
|
|
|
|
// current limit
|
|
|
|
var limit syscall.Rlimit
|
|
|
|
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
|
|
|
|
Fatalf(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// increase limit
|
|
|
|
limit.Cur = limit.Max
|
|
|
|
if limit.Cur > ProcessFileDescriptorLimit {
|
|
|
|
limit.Cur = ProcessFileDescriptorLimit
|
|
|
|
}
|
|
|
|
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
|
|
|
|
Fatalf(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// re-query limit
|
|
|
|
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
|
|
|
|
Fatalf(err)
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// cap limit
|
|
|
|
if limit.Cur > ProcessFileDescriptorLimit {
|
|
|
|
limit.Cur = ProcessFileDescriptorLimit
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
return int(limit.Cur) / 2
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
func makeDefaultExtra() []byte {
|
|
|
|
var clientInfo = struct {
|
|
|
|
Version uint
|
|
|
|
Name string
|
|
|
|
GoVersion string
|
|
|
|
Os string
|
2016-12-07 21:07:08 +00:00
|
|
|
}{uint(VersionMajor<<16 | VersionMinor<<8 | VersionPatch), ClientIdentifier, runtime.Version(), runtime.GOOS}
|
2016-09-11 11:44:14 +00:00
|
|
|
extra, err := rlp.EncodeToBytes(clientInfo)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
|
|
|
|
glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
|
|
|
|
glog.V(logger.Debug).Infof("extra: %x\n", extra)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return extra
|
|
|
|
}
|
2016-10-11 11:33:37 +00:00
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
func Fatalf(reason interface{}, args ...interface{}) {
|
|
|
|
// decide on output stream
|
|
|
|
w := io.MultiWriter(os.Stdout, os.Stderr)
|
|
|
|
outf, _ := os.Stdout.Stat()
|
|
|
|
errf, _ := os.Stderr.Stat()
|
|
|
|
if outf != nil && errf != nil && os.SameFile(outf, errf) {
|
|
|
|
w = os.Stderr
|
2016-10-11 11:33:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// find out whether error or string has been passed as a reason
|
|
|
|
r := reflect.ValueOf(reason)
|
|
|
|
if r.Kind() == reflect.String {
|
|
|
|
fmt.Fprintf(w, "Fatal Failure: "+reason.(string)+"\n", args)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "Fatal Failure: %v\n", reason.(error))
|
2016-10-11 11:33:37 +00:00
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
|
|
|
|
os.Exit(1)
|
2016-10-11 11:33:37 +00:00
|
|
|
}
|