2018-02-09 16:08:50 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2019-02-19 11:48:48 +00:00
|
|
|
"context"
|
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-25 10:45:54 +00:00
|
|
|
"github.com/CityOfZion/neo-go/config"
|
2018-03-14 09:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
2019-09-10 14:22:21 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2019-10-21 05:41:05 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-02-09 16:08:50 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/network"
|
2019-10-29 17:51:17 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/network/metrics"
|
2018-03-23 20:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/rpc"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2019-10-19 20:58:45 +00:00
|
|
|
// NewCommands returns 'node' command.
|
|
|
|
func NewCommands() []cli.Command {
|
2019-10-21 05:41:05 +00:00
|
|
|
var cfgFlags = []cli.Flag{
|
|
|
|
cli.StringFlag{Name: "config-path"},
|
|
|
|
cli.BoolFlag{Name: "privnet, p"},
|
|
|
|
cli.BoolFlag{Name: "mainnet, m"},
|
|
|
|
cli.BoolFlag{Name: "testnet, t"},
|
|
|
|
cli.BoolFlag{Name: "debug, d"},
|
|
|
|
}
|
|
|
|
var cfgWithCountFlags = make([]cli.Flag, len(cfgFlags))
|
|
|
|
copy(cfgWithCountFlags, cfgFlags)
|
|
|
|
cfgWithCountFlags = append(cfgWithCountFlags,
|
|
|
|
cli.UintFlag{
|
|
|
|
Name: "count, c",
|
|
|
|
Usage: "number of blocks to be processed (default or 0: all chain)",
|
|
|
|
},
|
|
|
|
cli.UintFlag{
|
|
|
|
Name: "skip, s",
|
|
|
|
Usage: "number of blocks to skip (default: 0)",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
var cfgCountOutFlags = make([]cli.Flag, len(cfgWithCountFlags))
|
|
|
|
copy(cfgCountOutFlags, cfgWithCountFlags)
|
|
|
|
cfgCountOutFlags = append(cfgCountOutFlags, cli.StringFlag{
|
|
|
|
Name: "out, o",
|
|
|
|
Usage: "Output file (stdout if not given)",
|
|
|
|
})
|
|
|
|
var cfgCountInFlags = make([]cli.Flag, len(cfgWithCountFlags))
|
|
|
|
copy(cfgCountInFlags, cfgWithCountFlags)
|
|
|
|
cfgCountInFlags = append(cfgCountInFlags, cli.StringFlag{
|
|
|
|
Name: "in, i",
|
|
|
|
Usage: "Input file (stdin if not given)",
|
|
|
|
})
|
|
|
|
return []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "node",
|
|
|
|
Usage: "start a NEO node",
|
|
|
|
Action: startServer,
|
|
|
|
Flags: cfgFlags,
|
2018-02-09 16:08:50 +00:00
|
|
|
},
|
2019-10-21 05:41:05 +00:00
|
|
|
{
|
|
|
|
Name: "db",
|
|
|
|
Usage: "database manipulations",
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "dump",
|
|
|
|
Usage: "dump blocks (starting with block #1) to the file",
|
|
|
|
Action: dumpDB,
|
|
|
|
Flags: cfgCountOutFlags,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "restore",
|
|
|
|
Usage: "restore blocks from the file",
|
|
|
|
Action: restoreDB,
|
|
|
|
Flags: cfgCountInFlags,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-02-09 16:08:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 11:48:48 +00:00
|
|
|
func newGraceContext() context.Context {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(stop, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
<-stop
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2019-10-21 05:41:05 +00:00
|
|
|
// getConfigFromContext looks at path and mode flags in the given config and
|
|
|
|
// returns appropriate config.
|
|
|
|
func getConfigFromContext(ctx *cli.Context) (config.Config, error) {
|
|
|
|
var net = config.ModePrivNet
|
2018-02-09 16:08:50 +00:00
|
|
|
if ctx.Bool("testnet") {
|
2018-03-25 10:45:54 +00:00
|
|
|
net = config.ModeTestNet
|
2018-02-09 16:08:50 +00:00
|
|
|
}
|
|
|
|
if ctx.Bool("mainnet") {
|
2018-03-25 10:45:54 +00:00
|
|
|
net = config.ModeMainNet
|
2018-02-09 16:08:50 +00:00
|
|
|
}
|
2019-08-30 08:22:11 +00:00
|
|
|
configPath := "./config"
|
2018-07-22 06:46:49 +00:00
|
|
|
if argCp := ctx.String("config-path"); argCp != "" {
|
|
|
|
configPath = argCp
|
|
|
|
}
|
2019-10-21 05:41:05 +00:00
|
|
|
return config.Load(configPath, net)
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:22:07 +00:00
|
|
|
// handleLoggingParams reads logging parameters.
|
|
|
|
// If user selected debug level -- function enables it.
|
|
|
|
// If logPath is configured -- function creates dir and file for logging.
|
|
|
|
func handleLoggingParams(ctx *cli.Context, cfg config.ApplicationConfiguration) error {
|
2019-10-21 05:41:05 +00:00
|
|
|
if ctx.Bool("debug") {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
2019-11-05 12:22:07 +00:00
|
|
|
|
|
|
|
if logPath := cfg.LogPath; logPath != "" {
|
2019-11-06 14:12:33 +00:00
|
|
|
if err := io.MakeDirForFile(logPath, "logger"); err != nil {
|
2019-11-05 12:22:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.Create(logPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.SetOutput(f)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-21 05:41:05 +00:00
|
|
|
func getCountAndSkipFromContext(ctx *cli.Context) (uint32, uint32) {
|
|
|
|
count := uint32(ctx.Uint("count"))
|
|
|
|
skip := uint32(ctx.Uint("skip"))
|
|
|
|
return count, skip
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpDB(ctx *cli.Context) error {
|
|
|
|
cfg, err := getConfigFromContext(ctx)
|
2018-03-15 20:45:37 +00:00
|
|
|
if err != nil {
|
2018-03-17 11:53:21 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
2019-11-05 12:22:07 +00:00
|
|
|
if err := handleLoggingParams(ctx, cfg.ApplicationConfiguration); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2019-10-21 05:41:05 +00:00
|
|
|
count, skip := getCountAndSkipFromContext(ctx)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2019-10-21 05:41:05 +00:00
|
|
|
var outStream = os.Stdout
|
|
|
|
if out := ctx.String("out"); out != "" {
|
|
|
|
outStream, err = os.Create(out)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer outStream.Close()
|
|
|
|
writer := io.NewBinWriterFromIO(outStream)
|
|
|
|
|
2019-09-17 12:27:40 +00:00
|
|
|
chain, err := initBlockChain(cfg)
|
2019-10-21 05:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2019-11-07 17:47:48 +00:00
|
|
|
go chain.Run()
|
2019-10-21 05:41:05 +00:00
|
|
|
|
|
|
|
chainHeight := chain.BlockHeight()
|
|
|
|
if skip+count > chainHeight {
|
|
|
|
return cli.NewExitError(fmt.Errorf("chain is not that high (%d) to dump %d blocks starting from %d", chainHeight, count, skip), 1)
|
|
|
|
}
|
|
|
|
if count == 0 {
|
|
|
|
count = chainHeight - skip
|
|
|
|
}
|
2019-12-12 15:52:23 +00:00
|
|
|
writer.WriteU32LE(count)
|
2019-11-08 13:56:54 +00:00
|
|
|
for i := skip + 1; i <= skip+count; i++ {
|
2019-10-21 05:41:05 +00:00
|
|
|
bh := chain.GetHeaderHash(int(i))
|
|
|
|
b, err := chain.GetBlock(bh)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("failed to get block %d: %s", i, err), 1)
|
|
|
|
}
|
2019-11-11 10:15:34 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
b.EncodeBinary(buf.BinWriter)
|
|
|
|
bytes := buf.Bytes()
|
2019-11-22 10:34:06 +00:00
|
|
|
writer.WriteVarBytes(bytes)
|
2019-10-21 05:41:05 +00:00
|
|
|
if writer.Err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 17:47:48 +00:00
|
|
|
chain.Close()
|
2019-10-21 05:41:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func restoreDB(ctx *cli.Context) error {
|
|
|
|
cfg, err := getConfigFromContext(ctx)
|
2018-03-14 09:36:59 +00:00
|
|
|
if err != nil {
|
2019-09-10 14:22:21 +00:00
|
|
|
return err
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2019-11-05 12:22:07 +00:00
|
|
|
if err := handleLoggingParams(ctx, cfg.ApplicationConfiguration); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2019-10-21 05:41:05 +00:00
|
|
|
count, skip := getCountAndSkipFromContext(ctx)
|
2018-03-14 09:36:59 +00:00
|
|
|
|
2019-10-21 05:41:05 +00:00
|
|
|
var inStream = os.Stdin
|
|
|
|
if in := ctx.String("in"); in != "" {
|
|
|
|
inStream, err = os.Open(in)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer inStream.Close()
|
|
|
|
reader := io.NewBinReaderFromIO(inStream)
|
|
|
|
|
|
|
|
chain, err := initBlockChain(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-07 17:47:48 +00:00
|
|
|
go chain.Run()
|
2019-10-21 05:41:05 +00:00
|
|
|
|
2019-12-12 15:52:23 +00:00
|
|
|
var allBlocks = reader.ReadU32LE()
|
2019-10-21 05:41:05 +00:00
|
|
|
if reader.Err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
if skip+count > allBlocks {
|
|
|
|
return cli.NewExitError(fmt.Errorf("input file has only %d blocks, can't read %d starting from %d", allBlocks, count, skip), 1)
|
|
|
|
}
|
|
|
|
if count == 0 {
|
|
|
|
count = allBlocks
|
|
|
|
}
|
|
|
|
i := uint32(0)
|
|
|
|
for ; i < skip; i++ {
|
2019-11-11 10:15:34 +00:00
|
|
|
_, err := readBlock(reader)
|
|
|
|
if err != nil {
|
2019-10-21 05:41:05 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 13:56:54 +00:00
|
|
|
for ; i < skip+count; i++ {
|
2019-11-11 10:15:34 +00:00
|
|
|
bytes, err := readBlock(reader)
|
|
|
|
block := &core.Block{}
|
|
|
|
newReader := io.NewBinReaderFromBuf(bytes)
|
|
|
|
block.DecodeBinary(newReader)
|
|
|
|
if err != nil {
|
2019-10-21 05:41:05 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2019-11-11 10:15:34 +00:00
|
|
|
err = chain.AddBlock(block)
|
2019-10-21 05:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("failed to add block %d: %s", i, err), 1)
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 17:47:48 +00:00
|
|
|
chain.Close()
|
2019-10-21 05:41:05 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-11 10:15:34 +00:00
|
|
|
// readBlock performs reading of block size and then bytes with the length equal to that size.
|
|
|
|
func readBlock(reader *io.BinReader) ([]byte, error) {
|
2019-12-12 15:52:23 +00:00
|
|
|
var size = reader.ReadU32LE()
|
2019-11-11 10:15:34 +00:00
|
|
|
bytes := make([]byte, size)
|
2019-12-12 15:52:23 +00:00
|
|
|
reader.ReadBytes(bytes)
|
2019-11-11 10:15:34 +00:00
|
|
|
if reader.Err != nil {
|
|
|
|
return nil, reader.Err
|
|
|
|
}
|
|
|
|
return bytes, nil
|
|
|
|
}
|
|
|
|
|
2019-10-21 05:41:05 +00:00
|
|
|
func startServer(ctx *cli.Context) error {
|
|
|
|
cfg, err := getConfigFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-05 12:22:07 +00:00
|
|
|
if err := handleLoggingParams(ctx, cfg.ApplicationConfiguration); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-21 05:41:05 +00:00
|
|
|
|
|
|
|
grace, cancel := context.WithCancel(newGraceContext())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
serverConfig := network.NewServerConfig(cfg)
|
|
|
|
|
|
|
|
chain, err := initBlockChain(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 12:22:07 +00:00
|
|
|
configureAddresses(cfg.ApplicationConfiguration)
|
2018-03-23 20:36:59 +00:00
|
|
|
server := network.NewServer(serverConfig, chain)
|
2019-11-01 10:23:46 +00:00
|
|
|
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server)
|
2018-03-23 20:36:59 +00:00
|
|
|
errChan := make(chan error)
|
2019-10-29 17:51:17 +00:00
|
|
|
monitoring := metrics.NewMetricsService(cfg.ApplicationConfiguration.Monitoring)
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2019-11-07 17:47:48 +00:00
|
|
|
go chain.Run()
|
2018-03-23 20:36:59 +00:00
|
|
|
go server.Start(errChan)
|
|
|
|
go rpcServer.Start(errChan)
|
2019-10-29 17:51:17 +00:00
|
|
|
go monitoring.Start()
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
fmt.Println(logo())
|
|
|
|
fmt.Println(server.UserAgent)
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
var shutdownErr error
|
2018-03-23 20:36:59 +00:00
|
|
|
Main:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
shutdownErr = errors.Wrap(err, "Error encountered by server")
|
2019-02-19 11:48:48 +00:00
|
|
|
cancel()
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2019-02-19 11:48:48 +00:00
|
|
|
case <-grace.Done():
|
2018-03-23 20:36:59 +00:00
|
|
|
server.Shutdown()
|
|
|
|
if serverErr := rpcServer.Shutdown(); serverErr != nil {
|
|
|
|
shutdownErr = errors.Wrap(serverErr, "Error encountered whilst shutting down server")
|
|
|
|
}
|
2019-10-29 17:51:17 +00:00
|
|
|
monitoring.ShutDown()
|
2019-11-07 17:47:48 +00:00
|
|
|
chain.Close()
|
2018-03-23 20:36:59 +00:00
|
|
|
break Main
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shutdownErr != nil {
|
|
|
|
return cli.NewExitError(shutdownErr, 1)
|
|
|
|
}
|
|
|
|
|
2018-02-09 16:08:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:22:07 +00:00
|
|
|
// configureAddresses sets up addresses for RPC and Monitoring depending from the provided config.
|
|
|
|
// In case RPC or Monitoring Address provided each of them will use it.
|
|
|
|
// In case global Address (of the node) provided and RPC/Monitoring don't have configured addresses they will
|
|
|
|
// use global one. So Node and RPC and Monitoring will run on one address.
|
|
|
|
func configureAddresses(cfg config.ApplicationConfiguration) {
|
|
|
|
if cfg.Address != "" {
|
|
|
|
if cfg.RPC.Address == "" {
|
|
|
|
cfg.RPC.Address = cfg.Address
|
|
|
|
}
|
|
|
|
if cfg.Monitoring.Address == "" {
|
|
|
|
cfg.Monitoring.Address = cfg.Address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 14:22:21 +00:00
|
|
|
// initBlockChain initializes BlockChain with preselected DB.
|
2019-09-17 12:27:40 +00:00
|
|
|
func initBlockChain(cfg config.Config) (*core.Blockchain, error) {
|
2019-09-16 15:52:47 +00:00
|
|
|
store, err := storage.NewStore(cfg.ApplicationConfiguration.DBConfiguration)
|
2019-09-10 14:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, cli.NewExitError(fmt.Errorf("could not initialize storage: %s", err), 1)
|
|
|
|
}
|
|
|
|
|
2019-09-17 12:27:40 +00:00
|
|
|
chain, err := core.NewBlockchain(store, cfg.ProtocolConfiguration)
|
2019-09-10 14:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, cli.NewExitError(fmt.Errorf("could not initialize blockchain: %s", err), 1)
|
|
|
|
}
|
|
|
|
return chain, nil
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
func logo() string {
|
|
|
|
return `
|
|
|
|
_ ____________ __________
|
|
|
|
/ | / / ____/ __ \ / ____/ __ \
|
|
|
|
/ |/ / __/ / / / /_____/ / __/ / / /
|
|
|
|
/ /| / /___/ /_/ /_____/ /_/ / /_/ /
|
|
|
|
/_/ |_/_____/\____/ \____/\____/
|
|
|
|
`
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|