2018-02-09 16:08:50 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2018-03-14 09:36:59 +00:00
|
|
|
"fmt"
|
2018-02-09 16:08:50 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
2018-02-09 16:08:50 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/network"
|
2018-03-14 09:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
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"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return err
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
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-15 20:45:37 +00:00
|
|
|
s := network.NewServer(serverConfig, chain)
|
2018-03-09 15:55:25 +00:00
|
|
|
s.Start()
|
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.
|
|
|
|
store, err := core.NewLevelDBStore(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return core.NewBlockchain(
|
|
|
|
store,
|
|
|
|
startHash,
|
|
|
|
), nil
|
|
|
|
}
|