status-go/src/main.go

178 lines
5.6 KiB
Go
Raw Normal View History

package main
import (
"errors"
2016-06-20 14:47:10 +00:00
"flag"
"fmt"
"github.com/ethereum/go-ethereum/accounts"
2016-06-20 14:47:10 +00:00
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/les"
2016-06-20 14:47:10 +00:00
"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/release"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
2016-07-12 18:10:37 +00:00
"github.com/ethereum/go-ethereum/whisper"
2016-07-03 19:44:31 +00:00
"gopkg.in/urfave/cli.v1"
"io"
"os"
"path"
"path/filepath"
"runtime"
)
2016-06-20 14:47:10 +00:00
const (
clientIdentifier = "Geth" // Client identifier to advertise over the network
versionMajor = 1 // Major version component of the current release
versionMinor = 5 // 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
versionOracle = "0xfa7b9770ca4cb04296cac84f37736d4041251cdf" // Ethereum address of the Geth release oracle
)
var (
vString string // Combined textual representation of the version
rConfig release.Config // Structured version information and release oracle config
currentNode *node.Node // currently running geth node
c *cli.Context // the CLI context used to start the geth node
accountSync *[]node.Service // the object used to sync accounts between geth services
lightEthereum *les.LightEthereum // LES service
accountManager *accounts.Manager // the account manager attached to the currentNode
whisperService *whisper.Whisper // whisper service
datadir string // data directory for geth
rpcport int = 8545 // RPC port (replaced in unit tests)
2016-08-03 05:49:56 +00:00
client rpc.Client
2016-06-20 14:47:10 +00:00
)
var (
ErrDataDirPreprocessingFailed = errors.New("Failed to pre-process data directory")
)
2016-06-20 15:21:45 +00:00
func main() {
// Placeholder for anything we want to run by default
fmt.Println("You are running statusgo!")
}
2016-06-21 14:35:43 +00:00
// MakeNode create a geth node entity
func MakeNode(inputDir string) *node.Node {
datadir := inputDir
2016-06-30 13:54:36 +00:00
// TODO remove admin rpcapi flag
2016-06-20 14:47:10 +00:00
set := flag.NewFlagSet("test", 0)
set.Bool("lightkdf", true, "Reduce key-derivation RAM & CPU usage at some expense of KDF strength")
2016-06-20 14:47:10 +00:00
set.Bool("shh", true, "whisper")
2016-07-03 19:44:31 +00:00
set.Bool("light", true, "disable eth")
set.Bool("testnet", true, "light test network")
set.Bool("rpc", true, "enable rpc")
2016-06-30 14:32:06 +00:00
set.String("rpcaddr", "localhost", "host for RPC")
set.Int("rpcport", rpcport, "rpc port")
2016-08-04 06:36:33 +00:00
set.String("rpccorsdomain", "*", "allow all domains")
set.String("verbosity", "3", "verbosity level")
set.String("rpcapi", "db,eth,net,web3,shh,personal,admin", "rpc api(s)")
2016-06-20 15:21:45 +00:00
set.String("datadir", datadir, "data directory for geth")
2016-07-01 15:56:41 +00:00
set.String("logdir", datadir, "log dir for glog")
2016-06-20 14:47:10 +00:00
c = cli.NewContext(nil, set, nil)
2016-06-20 14:47:10 +00:00
// Construct the textual version string from the individual components
vString = fmt.Sprintf("%d.%d.%d", versionMajor, versionMinor, versionPatch)
// Construct the version release oracle configuration
rConfig.Oracle = common.HexToAddress(versionOracle)
rConfig.Major = uint32(versionMajor)
rConfig.Minor = uint32(versionMinor)
rConfig.Patch = uint32(versionPatch)
2016-06-29 22:48:21 +00:00
utils.DebugSetup(c)
2016-06-21 18:29:38 +00:00
currentNode, accountSync = utils.MakeSystemNode(clientIdentifier, vString, rConfig, makeDefaultExtra(), c)
2016-06-20 15:21:45 +00:00
return currentNode
}
2016-06-21 14:35:43 +00:00
// StartNode starts a geth node entity
2016-06-29 11:32:04 +00:00
func RunNode(nodeIn *node.Node) {
2016-06-21 18:29:38 +00:00
utils.StartNode(nodeIn)
if err := nodeIn.Service(&accountManager); err != nil {
glog.V(logger.Warn).Infoln("cannot get account manager:", err)
2016-07-12 18:10:37 +00:00
}
if err := nodeIn.Service(&whisperService); err != nil {
glog.V(logger.Warn).Infoln("cannot get whisper service:", err)
}
if err := nodeIn.Service(&lightEthereum); err != nil {
glog.V(logger.Warn).Infoln("cannot get light ethereum service:", err)
}
lightEthereum.StatusBackend.SetTransactionQueueHandler(onSendTransactionRequest)
2016-08-03 05:49:56 +00:00
client, _ = nodeIn.Attach()
2016-06-21 18:29:38 +00:00
nodeIn.Wait()
2016-06-20 14:47:10 +00:00
}
func makeDefaultExtra() []byte {
var clientInfo = struct {
Version uint
Name string
GoVersion string
Os string
}{uint(versionMajor<<16 | versionMinor<<8 | versionPatch), clientIdentifier, runtime.Version(), runtime.GOOS}
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
}
2016-06-20 14:47:10 +00:00
return extra
}
func preprocessDataDir(dataDir string) (string, error) {
testDataDir := path.Join(dataDir, "testnet")
if _, err := os.Stat(testDataDir); os.IsNotExist(err) {
if err := os.MkdirAll(testDataDir, 0755); err != nil {
return dataDir, ErrDataDirPreprocessingFailed
}
}
// copy over static peer nodes list (LES auto-discovery is not stable yet)
dst := filepath.Join(testDataDir, "static-nodes.json")
if _, err := os.Stat(dst); os.IsNotExist(err) {
src := filepath.Join("data", "static-nodes.json")
if err := copyFile(dst, src); err != nil {
return dataDir, err
}
}
return dataDir, nil
}
func copyFile(dst, src string) error {
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
d, err := os.Create(dst)
if err != nil {
return err
}
defer d.Close()
if _, err := io.Copy(d, s); err != nil {
return err
}
return nil
}