2018-02-09 16:08:50 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2019-02-19 11:48:48 +00:00
|
|
|
"context"
|
2022-01-12 20:21:09 +00:00
|
|
|
"errors"
|
2018-03-14 09:36:59 +00:00
|
|
|
"fmt"
|
2022-02-07 10:36:42 +00:00
|
|
|
"net/url"
|
2018-03-23 20:36:59 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-02-07 10:36:42 +00:00
|
|
|
"runtime"
|
2021-04-30 12:53:27 +00:00
|
|
|
"syscall"
|
2018-02-09 16:08:50 +00:00
|
|
|
|
2020-06-17 18:13:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/options"
|
2020-03-25 15:30:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2022-01-12 20:04:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/consensus"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-11-24 08:36:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/chaindump"
|
2022-01-13 23:09:16 +00:00
|
|
|
corestate "github.com/nspcc-dev/neo-go/pkg/core/stateroot"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2022-01-12 20:21:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/metrics"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/server"
|
2022-01-12 20:21:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/services/notary"
|
2022-01-12 02:01:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/services/oracle"
|
2022-01-12 18:09:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/services/stateroot"
|
2018-02-09 16:08:50 +00:00
|
|
|
"github.com/urfave/cli"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2018-02-09 16:08:50 +00:00
|
|
|
)
|
|
|
|
|
2022-02-07 15:51:54 +00:00
|
|
|
var (
|
|
|
|
// _winfileSinkRegistered denotes whether zap has registered
|
|
|
|
// user-supplied factory for all sinks with `winfile`-prefixed scheme.
|
|
|
|
_winfileSinkRegistered bool
|
|
|
|
_winfileSinkCloser func() error
|
|
|
|
)
|
2022-02-07 10:36:42 +00:00
|
|
|
|
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: "debug, d"},
|
|
|
|
}
|
2020-06-17 18:13:37 +00:00
|
|
|
cfgFlags = append(cfgFlags, options.Network...)
|
2019-10-21 05:41:05 +00:00
|
|
|
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)",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
var cfgCountOutFlags = make([]cli.Flag, len(cfgWithCountFlags))
|
|
|
|
copy(cfgCountOutFlags, cfgWithCountFlags)
|
2019-12-27 09:11:57 +00:00
|
|
|
cfgCountOutFlags = append(cfgCountOutFlags,
|
|
|
|
cli.UintFlag{
|
|
|
|
Name: "start, s",
|
|
|
|
Usage: "block number to start from (default: 0)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "out, o",
|
|
|
|
Usage: "Output file (stdout if not given)",
|
|
|
|
},
|
|
|
|
)
|
2019-10-21 05:41:05 +00:00
|
|
|
var cfgCountInFlags = make([]cli.Flag, len(cfgWithCountFlags))
|
|
|
|
copy(cfgCountInFlags, cfgWithCountFlags)
|
2019-12-27 09:11:57 +00:00
|
|
|
cfgCountInFlags = append(cfgCountInFlags,
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "in, i",
|
|
|
|
Usage: "Input file (stdin if not given)",
|
|
|
|
},
|
2020-02-06 15:47:03 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "dump",
|
|
|
|
Usage: "directory for storing JSON dumps",
|
|
|
|
},
|
2021-07-12 14:59:28 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "incremental, n",
|
|
|
|
Usage: "use if dump is incremental",
|
|
|
|
},
|
2019-12-27 09:11:57 +00:00
|
|
|
)
|
2019-10-21 05:41:05 +00:00
|
|
|
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) {
|
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
|
|
|
|
}
|
2020-06-17 18:13:37 +00:00
|
|
|
return config.Load(configPath, options.GetNetwork(ctx))
|
2019-10-21 05:41:05 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2022-02-07 15:51:54 +00:00
|
|
|
// If logPath is configured on Windows -- function returns closer to be
|
|
|
|
// able to close sink for opened log output file.
|
|
|
|
func handleLoggingParams(ctx *cli.Context, cfg config.ApplicationConfiguration) (*zap.Logger, func() error, error) {
|
2019-12-30 07:43:05 +00:00
|
|
|
level := zapcore.InfoLevel
|
2019-10-21 05:41:05 +00:00
|
|
|
if ctx.Bool("debug") {
|
2019-12-30 07:43:05 +00:00
|
|
|
level = zapcore.DebugLevel
|
2019-10-21 05:41:05 +00:00
|
|
|
}
|
2019-11-05 12:22:07 +00:00
|
|
|
|
2019-12-30 07:43:05 +00:00
|
|
|
cc := zap.NewProductionConfig()
|
|
|
|
cc.DisableCaller = true
|
|
|
|
cc.DisableStacktrace = true
|
|
|
|
cc.EncoderConfig.EncodeDuration = zapcore.StringDurationEncoder
|
|
|
|
cc.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
|
|
|
cc.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
|
|
cc.Encoding = "console"
|
|
|
|
cc.Level = zap.NewAtomicLevelAt(level)
|
2020-01-13 13:44:12 +00:00
|
|
|
cc.Sampling = nil
|
2019-12-30 07:43:05 +00:00
|
|
|
|
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 {
|
2022-02-07 15:51:54 +00:00
|
|
|
return nil, nil, err
|
2019-11-05 12:22:07 +00:00
|
|
|
}
|
2019-12-30 07:43:05 +00:00
|
|
|
|
2022-02-07 10:36:42 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if !_winfileSinkRegistered {
|
|
|
|
// See https://github.com/uber-go/zap/issues/621.
|
|
|
|
err := zap.RegisterSink("winfile", func(u *url.URL) (zap.Sink, error) {
|
|
|
|
if u.User != nil {
|
|
|
|
return nil, fmt.Errorf("user and password not allowed with file URLs: got %v", u)
|
|
|
|
}
|
|
|
|
if u.Fragment != "" {
|
|
|
|
return nil, fmt.Errorf("fragments not allowed with file URLs: got %v", u)
|
|
|
|
}
|
|
|
|
if u.RawQuery != "" {
|
|
|
|
return nil, fmt.Errorf("query parameters not allowed with file URLs: got %v", u)
|
|
|
|
}
|
|
|
|
// Error messages are better if we check hostname and port separately.
|
|
|
|
if u.Port() != "" {
|
|
|
|
return nil, fmt.Errorf("ports not allowed with file URLs: got %v", u)
|
|
|
|
}
|
|
|
|
if hn := u.Hostname(); hn != "" && hn != "localhost" {
|
|
|
|
return nil, fmt.Errorf("file URLs must leave host empty or use localhost: got %v", u)
|
|
|
|
}
|
|
|
|
switch u.Path {
|
|
|
|
case "stdout":
|
|
|
|
return os.Stdout, nil
|
|
|
|
case "stderr":
|
|
|
|
return os.Stderr, nil
|
|
|
|
}
|
2022-02-07 15:51:54 +00:00
|
|
|
f, err := os.OpenFile(u.Path[1:], // Remove leading slash left after url.Parse.
|
2022-02-07 10:36:42 +00:00
|
|
|
os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
|
2022-02-07 15:51:54 +00:00
|
|
|
_winfileSinkCloser = func() error {
|
|
|
|
_winfileSinkCloser = nil
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
return f, err
|
2022-02-07 10:36:42 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-02-07 15:51:54 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to register windows-specific sinc: %w", err)
|
2022-02-07 10:36:42 +00:00
|
|
|
}
|
|
|
|
_winfileSinkRegistered = true
|
|
|
|
}
|
|
|
|
logPath = "winfile:///" + logPath
|
|
|
|
}
|
|
|
|
|
2019-12-30 07:43:05 +00:00
|
|
|
cc.OutputPaths = []string{logPath}
|
2019-11-05 12:22:07 +00:00
|
|
|
}
|
2019-12-30 07:43:05 +00:00
|
|
|
|
2022-02-07 15:51:54 +00:00
|
|
|
log, err := cc.Build()
|
|
|
|
return log, _winfileSinkCloser, err
|
2019-11-05 12:22:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 07:43:05 +00:00
|
|
|
func initBCWithMetrics(cfg config.Config, log *zap.Logger) (*core.Blockchain, *metrics.Service, *metrics.Service, error) {
|
|
|
|
chain, err := initBlockChain(cfg, log)
|
2019-12-16 14:04:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-08-25 12:41:50 +00:00
|
|
|
configureAddresses(&cfg.ApplicationConfiguration)
|
2019-12-30 07:43:05 +00:00
|
|
|
prometheus := metrics.NewPrometheusService(cfg.ApplicationConfiguration.Prometheus, log)
|
|
|
|
pprof := metrics.NewPprofService(cfg.ApplicationConfiguration.Pprof, log)
|
2019-12-16 14:04:18 +00:00
|
|
|
|
|
|
|
go chain.Run()
|
|
|
|
go prometheus.Start()
|
|
|
|
go pprof.Start()
|
|
|
|
|
|
|
|
return chain, prometheus, pprof, nil
|
|
|
|
}
|
|
|
|
|
2019-10-21 05:41:05 +00:00
|
|
|
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
|
|
|
}
|
2022-02-07 15:51:54 +00:00
|
|
|
log, logCloser, err := handleLoggingParams(ctx, cfg.ApplicationConfiguration)
|
2019-12-30 07:43:05 +00:00
|
|
|
if err != nil {
|
2019-11-05 12:22:07 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2022-02-07 15:51:54 +00:00
|
|
|
if logCloser != nil {
|
|
|
|
defer func() { _ = logCloser() }()
|
|
|
|
}
|
2019-12-27 09:11:57 +00:00
|
|
|
count := uint32(ctx.Uint("count"))
|
|
|
|
start := uint32(ctx.Uint("start"))
|
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-12-30 07:43:05 +00:00
|
|
|
chain, prometheus, pprof, err := initBCWithMetrics(cfg, log)
|
2019-10-21 05:41:05 +00:00
|
|
|
if err != nil {
|
2019-12-16 14:04:18 +00:00
|
|
|
return err
|
2019-10-21 05:41:05 +00:00
|
|
|
}
|
2021-11-17 14:33:37 +00:00
|
|
|
defer func() {
|
|
|
|
pprof.ShutDown()
|
|
|
|
prometheus.ShutDown()
|
|
|
|
chain.Close()
|
|
|
|
}()
|
2019-10-21 05:41:05 +00:00
|
|
|
|
2019-12-27 09:11:57 +00:00
|
|
|
chainCount := chain.BlockHeight() + 1
|
|
|
|
if start+count > chainCount {
|
|
|
|
return cli.NewExitError(fmt.Errorf("chain is not that high (%d) to dump %d blocks starting from %d", chainCount-1, count, start), 1)
|
2019-10-21 05:41:05 +00:00
|
|
|
}
|
|
|
|
if count == 0 {
|
2019-12-27 09:11:57 +00:00
|
|
|
count = chainCount - start
|
2019-10-21 05:41:05 +00:00
|
|
|
}
|
2019-12-12 15:52:23 +00:00
|
|
|
writer.WriteU32LE(count)
|
2020-11-24 08:36:09 +00:00
|
|
|
err = chaindump.Dump(chain, writer, start, count)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err.Error(), 1)
|
2019-10-21 05:41:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-16 14:04:18 +00:00
|
|
|
|
2019-10-21 05:41:05 +00:00
|
|
|
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
|
|
|
}
|
2022-02-07 15:51:54 +00:00
|
|
|
log, logCloser, err := handleLoggingParams(ctx, cfg.ApplicationConfiguration)
|
2019-12-30 07:43:05 +00:00
|
|
|
if err != nil {
|
2019-11-05 12:22:07 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2022-02-07 15:51:54 +00:00
|
|
|
if logCloser != nil {
|
|
|
|
defer func() { _ = logCloser() }()
|
|
|
|
}
|
2019-12-27 09:11:57 +00:00
|
|
|
count := uint32(ctx.Uint("count"))
|
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)
|
|
|
|
|
2020-02-06 15:47:03 +00:00
|
|
|
dumpDir := ctx.String("dump")
|
|
|
|
if dumpDir != "" {
|
|
|
|
cfg.ProtocolConfiguration.SaveStorageBatch = true
|
|
|
|
}
|
|
|
|
|
2019-12-30 07:43:05 +00:00
|
|
|
chain, prometheus, pprof, err := initBCWithMetrics(cfg, log)
|
2019-10-21 05:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-08 11:02:46 +00:00
|
|
|
defer func() {
|
|
|
|
pprof.ShutDown()
|
|
|
|
prometheus.ShutDown()
|
|
|
|
chain.Close()
|
|
|
|
}()
|
2019-10-21 05:41:05 +00:00
|
|
|
|
2021-07-12 14:59:28 +00:00
|
|
|
var start uint32
|
|
|
|
if ctx.Bool("incremental") {
|
|
|
|
start = reader.ReadU32LE()
|
|
|
|
if chain.BlockHeight()+1 < start {
|
|
|
|
return cli.NewExitError(fmt.Errorf("expected height: %d, dump starts at %d",
|
|
|
|
chain.BlockHeight()+1, start), 1)
|
|
|
|
}
|
2021-07-16 07:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var skip uint32
|
|
|
|
if chain.BlockHeight() != 0 {
|
2021-07-12 14:59:28 +00:00
|
|
|
skip = chain.BlockHeight() + 1 - start
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-12-27 09:15:47 +00:00
|
|
|
count = allBlocks - skip
|
2019-10-21 05:41:05 +00:00
|
|
|
}
|
2021-07-16 07:45:34 +00:00
|
|
|
log.Info("initialize restore",
|
|
|
|
zap.Uint32("start", start),
|
|
|
|
zap.Uint32("height", chain.BlockHeight()),
|
|
|
|
zap.Uint32("skip", skip),
|
|
|
|
zap.Uint32("count", count))
|
2020-02-06 15:47:03 +00:00
|
|
|
|
2020-03-16 09:00:22 +00:00
|
|
|
gctx := newGraceContext()
|
|
|
|
var lastIndex uint32
|
2020-02-06 15:47:03 +00:00
|
|
|
dump := newDump()
|
2020-03-16 09:00:22 +00:00
|
|
|
defer func() {
|
|
|
|
_ = dump.tryPersist(dumpDir, lastIndex)
|
|
|
|
}()
|
2020-02-06 15:47:03 +00:00
|
|
|
|
2020-11-24 08:36:09 +00:00
|
|
|
var f = func(b *block.Block) error {
|
2020-03-16 09:00:22 +00:00
|
|
|
select {
|
|
|
|
case <-gctx.Done():
|
2020-11-24 08:36:09 +00:00
|
|
|
return gctx.Err()
|
2020-03-16 09:00:22 +00:00
|
|
|
default:
|
2020-11-24 08:36:09 +00:00
|
|
|
return nil
|
2020-03-16 09:00:22 +00:00
|
|
|
}
|
2020-11-24 08:36:09 +00:00
|
|
|
}
|
|
|
|
if dumpDir != "" {
|
|
|
|
f = func(b *block.Block) error {
|
|
|
|
select {
|
|
|
|
case <-gctx.Done():
|
|
|
|
return gctx.Err()
|
|
|
|
default:
|
2019-12-27 09:25:39 +00:00
|
|
|
}
|
2020-02-06 15:47:03 +00:00
|
|
|
batch := chain.LastBatch()
|
2020-06-24 13:09:54 +00:00
|
|
|
// The genesis block may already be persisted, so LastBatch() will return nil.
|
2020-11-24 08:36:09 +00:00
|
|
|
if batch == nil && b.Index == 0 {
|
|
|
|
return nil
|
2020-06-24 13:09:54 +00:00
|
|
|
}
|
2020-11-24 08:36:09 +00:00
|
|
|
dump.add(b.Index, batch)
|
|
|
|
lastIndex = b.Index
|
|
|
|
if b.Index%1000 == 0 {
|
|
|
|
if err := dump.tryPersist(dumpDir, b.Index); err != nil {
|
|
|
|
return fmt.Errorf("can't dump storage to file: %w", err)
|
2020-03-16 09:00:22 +00:00
|
|
|
}
|
2020-02-06 15:47:03 +00:00
|
|
|
}
|
2020-11-24 08:36:09 +00:00
|
|
|
return nil
|
2020-02-06 15:47:03 +00:00
|
|
|
}
|
2019-10-21 05:41:05 +00:00
|
|
|
}
|
2020-11-24 08:36:09 +00:00
|
|
|
|
|
|
|
err = chaindump.Restore(chain, reader, skip, count, f)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2019-10-21 05:41:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 02:01:34 +00:00
|
|
|
func mkOracle(config network.ServerConfig, chain *core.Blockchain, serv *network.Server, log *zap.Logger) (*oracle.Oracle, error) {
|
|
|
|
if !config.OracleCfg.Enabled {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
orcCfg := oracle.Config{
|
|
|
|
Log: log,
|
|
|
|
Network: config.Net,
|
|
|
|
MainCfg: config.OracleCfg,
|
|
|
|
Chain: chain,
|
|
|
|
OnTransaction: serv.RelayTxn,
|
|
|
|
}
|
|
|
|
orc, err := oracle.NewOracle(orcCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't initialize Oracle module: %w", err)
|
|
|
|
}
|
|
|
|
chain.SetOracle(orc)
|
|
|
|
serv.AddService(orc)
|
|
|
|
return orc, nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 20:04:07 +00:00
|
|
|
func mkConsensus(config network.ServerConfig, chain *core.Blockchain, serv *network.Server, log *zap.Logger) (consensus.Service, error) {
|
|
|
|
if config.Wallet == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
srv, err := consensus.NewService(consensus.Config{
|
|
|
|
Logger: log,
|
|
|
|
Broadcast: serv.BroadcastExtensible,
|
|
|
|
Chain: chain,
|
|
|
|
ProtocolConfiguration: chain.GetConfig(),
|
|
|
|
RequestTx: serv.RequestTx,
|
|
|
|
Wallet: config.Wallet,
|
|
|
|
TimePerBlock: config.TimePerBlock,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't initialize Consensus module: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
serv.AddExtensibleHPService(srv, consensus.Category, srv.OnPayload, srv.OnTransaction)
|
|
|
|
return srv, nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 20:21:09 +00:00
|
|
|
func mkP2PNotary(config network.ServerConfig, chain *core.Blockchain, serv *network.Server, log *zap.Logger) (*notary.Notary, error) {
|
|
|
|
if !config.P2PNotaryCfg.Enabled {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if !chain.P2PSigExtensionsEnabled() {
|
|
|
|
return nil, errors.New("P2PSigExtensions are disabled, but Notary service is enabled")
|
|
|
|
}
|
|
|
|
cfg := notary.Config{
|
|
|
|
MainCfg: config.P2PNotaryCfg,
|
|
|
|
Chain: chain,
|
|
|
|
Log: log,
|
|
|
|
}
|
|
|
|
n, err := notary.NewNotary(cfg, serv.Net, serv.GetNotaryPool(), func(tx *transaction.Transaction) error {
|
2022-04-05 14:19:56 +00:00
|
|
|
err := serv.RelayTxn(tx)
|
|
|
|
if err != nil && !errors.Is(err, core.ErrAlreadyExists) {
|
2022-01-12 20:21:09 +00:00
|
|
|
return fmt.Errorf("can't relay completed notary transaction: hash %s, error: %w", tx.Hash().StringLE(), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create Notary module: %w", err)
|
|
|
|
}
|
|
|
|
serv.AddService(n)
|
|
|
|
chain.SetNotary(n)
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2019-10-21 05:41:05 +00:00
|
|
|
func startServer(ctx *cli.Context) error {
|
|
|
|
cfg, err := getConfigFromContext(ctx)
|
|
|
|
if err != nil {
|
2022-01-31 13:20:14 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2019-10-21 05:41:05 +00:00
|
|
|
}
|
2022-02-07 15:51:54 +00:00
|
|
|
log, logCloser, err := handleLoggingParams(ctx, cfg.ApplicationConfiguration)
|
2019-12-30 07:43:05 +00:00
|
|
|
if err != nil {
|
2022-01-31 13:20:14 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2019-11-05 12:22:07 +00:00
|
|
|
}
|
2022-02-07 15:51:54 +00:00
|
|
|
if logCloser != nil {
|
|
|
|
defer func() { _ = logCloser() }()
|
|
|
|
}
|
2019-10-21 05:41:05 +00:00
|
|
|
|
|
|
|
grace, cancel := context.WithCancel(newGraceContext())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
serverConfig := network.NewServerConfig(cfg)
|
|
|
|
|
2019-12-30 07:43:05 +00:00
|
|
|
chain, prometheus, pprof, err := initBCWithMetrics(cfg, log)
|
2019-10-21 05:41:05 +00:00
|
|
|
if err != nil {
|
2022-01-31 13:20:14 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
2022-02-08 11:02:46 +00:00
|
|
|
defer func() {
|
|
|
|
pprof.ShutDown()
|
|
|
|
prometheus.ShutDown()
|
|
|
|
chain.Close()
|
|
|
|
}()
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2022-01-12 21:20:03 +00:00
|
|
|
serv, err := network.NewServer(serverConfig, chain, chain.GetStateSyncModule(), log)
|
2020-01-22 08:17:51 +00:00
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to create network server: %w", err), 1)
|
2020-01-22 08:17:51 +00:00
|
|
|
}
|
2022-01-13 23:09:16 +00:00
|
|
|
srMod := chain.GetStateModule().(*corestate.Module) // Take full responsibility here.
|
|
|
|
sr, err := stateroot.New(serverConfig.StateRootCfg, srMod, log, chain, serv.BroadcastExtensible)
|
2022-01-12 18:09:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't initialize StateRoot service: %w", err), 1)
|
|
|
|
}
|
|
|
|
serv.AddExtensibleService(sr, stateroot.Category, sr.OnPayload)
|
|
|
|
|
2022-01-12 02:01:34 +00:00
|
|
|
oracleSrv, err := mkOracle(serverConfig, chain, serv, log)
|
|
|
|
if err != nil {
|
2022-01-31 13:20:14 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2022-01-12 02:01:34 +00:00
|
|
|
}
|
2022-01-12 20:04:07 +00:00
|
|
|
_, err = mkConsensus(serverConfig, chain, serv, log)
|
|
|
|
if err != nil {
|
2022-01-31 13:20:14 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2022-01-12 20:04:07 +00:00
|
|
|
}
|
2022-01-12 20:21:09 +00:00
|
|
|
_, err = mkP2PNotary(serverConfig, chain, serv, log)
|
|
|
|
if err != nil {
|
2022-01-31 13:20:14 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2022-01-12 20:21:09 +00:00
|
|
|
}
|
2022-01-12 02:01:34 +00:00
|
|
|
rpcServer := server.New(chain, cfg.ApplicationConfiguration.RPC, serv, oracleSrv, log)
|
2018-03-23 20:36:59 +00:00
|
|
|
errChan := make(chan error)
|
|
|
|
|
2020-02-17 12:17:02 +00:00
|
|
|
go serv.Start(errChan)
|
2020-08-31 12:35:55 +00:00
|
|
|
rpcServer.Start(errChan)
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2021-04-30 12:53:27 +00:00
|
|
|
sighupCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sighupCh, syscall.SIGHUP)
|
|
|
|
|
2022-01-31 13:20:14 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, Logo())
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, serv.UserAgent)
|
|
|
|
fmt.Fprintln(ctx.App.Writer)
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
var shutdownErr error
|
2018-03-23 20:36:59 +00:00
|
|
|
Main:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
2020-08-06 14:44:08 +00:00
|
|
|
shutdownErr = fmt.Errorf("server error: %w", err)
|
2019-02-19 11:48:48 +00:00
|
|
|
cancel()
|
2021-04-30 12:53:27 +00:00
|
|
|
case sig := <-sighupCh:
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGHUP:
|
|
|
|
log.Info("SIGHUP received, restarting rpc-server")
|
|
|
|
serverErr := rpcServer.Shutdown()
|
|
|
|
if serverErr != nil {
|
|
|
|
errChan <- fmt.Errorf("error while restarting rpc-server: %w", serverErr)
|
|
|
|
break
|
|
|
|
}
|
2022-01-12 02:01:34 +00:00
|
|
|
rpcServer = server.New(chain, cfg.ApplicationConfiguration.RPC, serv, oracleSrv, log)
|
2021-04-30 12:53:27 +00:00
|
|
|
rpcServer.Start(errChan)
|
|
|
|
}
|
2019-02-19 11:48:48 +00:00
|
|
|
case <-grace.Done():
|
2021-04-30 12:53:27 +00:00
|
|
|
signal.Stop(sighupCh)
|
2020-02-17 12:17:02 +00:00
|
|
|
serv.Shutdown()
|
2018-03-23 20:36:59 +00:00
|
|
|
if serverErr := rpcServer.Shutdown(); serverErr != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
shutdownErr = fmt.Errorf("error on shutdown: %w", serverErr)
|
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-12-04 06:48:32 +00:00
|
|
|
// configureAddresses sets up addresses for RPC, Prometheus and Pprof depending from the provided config.
|
|
|
|
// In case RPC or Prometheus or Pprof Address provided each of them will use it.
|
|
|
|
// In case global Address (of the node) provided and RPC/Prometheus/Pprof don't have configured addresses they will
|
|
|
|
// use global one. So Node and RPC and Prometheus and Pprof will run on one address.
|
2020-08-25 12:41:50 +00:00
|
|
|
func configureAddresses(cfg *config.ApplicationConfiguration) {
|
2019-11-05 12:22:07 +00:00
|
|
|
if cfg.Address != "" {
|
|
|
|
if cfg.RPC.Address == "" {
|
|
|
|
cfg.RPC.Address = cfg.Address
|
|
|
|
}
|
2019-12-04 06:48:32 +00:00
|
|
|
if cfg.Prometheus.Address == "" {
|
|
|
|
cfg.Prometheus.Address = cfg.Address
|
|
|
|
}
|
|
|
|
if cfg.Pprof.Address == "" {
|
|
|
|
cfg.Pprof.Address = cfg.Address
|
2019-11-05 12:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 14:22:21 +00:00
|
|
|
// initBlockChain initializes BlockChain with preselected DB.
|
2019-12-30 07:43:05 +00:00
|
|
|
func initBlockChain(cfg config.Config, log *zap.Logger) (*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 {
|
2020-08-06 16:09:57 +00:00
|
|
|
return nil, cli.NewExitError(fmt.Errorf("could not initialize storage: %w", err), 1)
|
2019-09-10 14:22:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 07:43:05 +00:00
|
|
|
chain, err := core.NewBlockchain(store, cfg.ProtocolConfiguration, log)
|
2019-09-10 14:22:21 +00:00
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return nil, cli.NewExitError(fmt.Errorf("could not initialize blockchain: %w", err), 1)
|
2019-09-10 14:22:21 +00:00
|
|
|
}
|
|
|
|
return chain, nil
|
|
|
|
}
|
|
|
|
|
2022-01-31 13:20:14 +00:00
|
|
|
// Logo returns Neo-Go logo.
|
|
|
|
func Logo() string {
|
2018-03-17 11:53:21 +00:00
|
|
|
return `
|
|
|
|
_ ____________ __________
|
|
|
|
/ | / / ____/ __ \ / ____/ __ \
|
|
|
|
/ |/ / __/ / / / /_____/ / __/ / / /
|
|
|
|
/ /| / /___/ /_/ /_____/ /_/ / /_/ /
|
|
|
|
/_/ |_/_____/\____/ \____/\____/
|
|
|
|
`
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|