2018-02-09 16:08:50 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2018-03-14 09:36:59 +00:00
|
|
|
"fmt"
|
2018-03-23 20:36:59 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-02-09 16:08:50 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
2018-03-17 11:53:21 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2018-02-09 16:08:50 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/network"
|
2018-03-23 20:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/rpc"
|
2018-03-14 09:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2018-03-23 20:36:59 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-03-17 11:53:21 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-02-09 16:08:50 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
// NewCommand creates a new Node command.
|
2018-02-09 16:08:50 +00:00
|
|
|
func NewCommand() cli.Command {
|
|
|
|
return cli.Command{
|
|
|
|
Name: "node",
|
|
|
|
Usage: "start a NEO node",
|
|
|
|
Action: startServer,
|
|
|
|
Flags: []cli.Flag{
|
2018-03-15 20:45:37 +00:00
|
|
|
cli.StringFlag{Name: "config-path"},
|
2018-02-09 16:08:50 +00:00
|
|
|
cli.BoolFlag{Name: "privnet, p"},
|
|
|
|
cli.BoolFlag{Name: "mainnet, m"},
|
|
|
|
cli.BoolFlag{Name: "testnet, t"},
|
2018-03-17 11:53:21 +00:00
|
|
|
cli.BoolFlag{Name: "debug, d"},
|
2018-02-09 16:08:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startServer(ctx *cli.Context) error {
|
|
|
|
net := network.ModePrivNet
|
|
|
|
if ctx.Bool("testnet") {
|
|
|
|
net = network.ModeTestNet
|
|
|
|
}
|
|
|
|
if ctx.Bool("mainnet") {
|
|
|
|
net = network.ModeMainNet
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:45:37 +00:00
|
|
|
configPath := "./config"
|
|
|
|
configPath = ctx.String("config-path")
|
|
|
|
config, err := network.LoadConfig(configPath, net)
|
|
|
|
if err != nil {
|
2018-03-17 11:53:21 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
interruptChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interruptChan, os.Interrupt)
|
|
|
|
|
2018-03-15 20:45:37 +00:00
|
|
|
serverConfig := network.NewServerConfig(config)
|
|
|
|
chain, err := newBlockchain(net, config.ApplicationConfiguration.DataDirectoryPath)
|
2018-03-14 09:36:59 +00:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("could not initialize blockhain: %s", err)
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
if ctx.Bool("debug") {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(logo())
|
2018-03-23 20:36:59 +00:00
|
|
|
server := network.NewServer(serverConfig, chain)
|
|
|
|
rpcServer := rpc.NewServer(chain, config.ApplicationConfiguration.RPCPort, server)
|
|
|
|
errChan := make(chan error)
|
|
|
|
|
|
|
|
go server.Start(errChan)
|
|
|
|
go rpcServer.Start(errChan)
|
|
|
|
var shutdownErr error
|
|
|
|
|
|
|
|
Main:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
shutdownErr = errors.Wrap(err, "Error encountered by server")
|
|
|
|
interruptChan <- os.Kill
|
|
|
|
|
|
|
|
case <-interruptChan:
|
|
|
|
server.Shutdown()
|
|
|
|
if serverErr := rpcServer.Shutdown(); serverErr != nil {
|
|
|
|
shutdownErr = errors.Wrap(serverErr, "Error encountered whilst shutting down server")
|
|
|
|
}
|
|
|
|
break Main
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shutdownErr != nil {
|
|
|
|
return cli.NewExitError(shutdownErr, 1)
|
|
|
|
}
|
|
|
|
|
2018-02-09 16:08:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
func newBlockchain(net network.NetMode, path string) (*core.Blockchain, error) {
|
|
|
|
var startHash util.Uint256
|
|
|
|
if net == network.ModePrivNet {
|
|
|
|
startHash = core.GenesisHashPrivNet()
|
|
|
|
}
|
|
|
|
if net == network.ModeTestNet {
|
|
|
|
startHash = core.GenesisHashTestNet()
|
|
|
|
}
|
|
|
|
if net == network.ModeMainNet {
|
|
|
|
startHash = core.GenesisHashMainNet()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hardcoded for now.
|
2018-03-17 11:53:21 +00:00
|
|
|
store, err := storage.NewLevelDBStore(path, nil)
|
2018-03-14 09:36:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
return core.NewBlockchain(store, startHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func logo() string {
|
|
|
|
return `
|
|
|
|
_ ____________ __________
|
|
|
|
/ | / / ____/ __ \ / ____/ __ \
|
|
|
|
/ |/ / __/ / / / /_____/ / __/ / / /
|
|
|
|
/ /| / /___/ /_/ /_____/ /_/ / /_/ /
|
|
|
|
/_/ |_/_____/\____/ \____/\____/
|
|
|
|
`
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|