@ -1,7 +1,6 @@
package main
import (
"flag"
"fmt"
"io/ioutil"
"math/big"
@ -10,32 +9,32 @@ import (
_ "net/http/pprof"
"os"
"os/signal"
"path"
"path/filepath "
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/harmony-one/harmony/internal/cli"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/numeric"
"github.com/spf13/cobra"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/api/service/syncing"
"github.com/harmony-one/harmony/consensus"
"github.com/harmony-one/harmony/consensus/quorum"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/internal/cli"
"github.com/harmony-one/harmony/internal/common"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
viperconfig "github.com/harmony-one/harmony/internal/configs/viper"
"github.com/harmony-one/harmony/internal/genesis"
"github.com/harmony-one/harmony/internal/shardchain"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/multibls"
"github.com/harmony-one/harmony/node"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/shard"
"github.com/harmony-one/harmony/webhooks"
@ -48,71 +47,315 @@ var (
initialAccounts = [ ] * genesis . DeployAccount { }
)
func printVersion ( ) {
fmt . Fprintln ( os . Stdout , nodeconfig . GetVersion ( ) )
os . Exit ( 0 )
var rootCmd = & cobra . Command {
// TODO: elaborate the usage
Use : "harmony" ,
Short : "run harmony node" ,
Long : "run harmony node" ,
Run : runHarmonyNode ,
}
//ip = flag.String("ip", "127.0.0.1", "ip of the node")
//port = flag.String("port", "9000", "port of the node.")
//logFolder = flag.String("log_folder", "latest", "the folder collecting the logs of this execution")
//logMaxSize = flag.Int("log_max_size", 100, "the max size in megabytes of the log file before it gets rotated")
//// Remove this flag
freshDB = flag . Bool ( "fresh_db" , false , "true means the existing disk based db will be removed" )
//pprof = flag.String("pprof", "", "what address and port the pprof profiling server should listen on")
//versionFlag = flag.Bool("version", false, "Output version info")
//dnsZone = flag.String("dns_zone", "", "if given and not empty, use peers from the zone (default: use libp2p peer discovery instead)")
//dnsFlag = flag.Bool("dns", true, "[deprecated] equivalent to -dns_zone t.hmny.io")
//dnsPort = flag.String("dns_port", "9000", "port of dns node")
////Leader needs to have a minimal number of peers to start consensus
//minPeers = flag.Int("min_peers", 32, "Minimal number of Peers in shard")
//// Key file to store the private key
//keyFile = flag.String("key", "./.hmykey", "the p2p key file of the harmony node")
//// isArchival indicates this node is an archival node that will save and archive current blockchain
//isArchival = flag.Bool("is_archival", false, "false will enable cached state pruning")
//// delayCommit is the commit-delay timer, used by Harmony nodes
//// TODO: check whether this field can be removed
//delayCommit = flag.String("delay_commit", "0ms", "how long to delay sending commit messages in consensus, ex: 500ms, 1s")
//// nodeType indicates the type of the node: validator, explorer
//nodeType = flag.String("node_type", "validator", "node type: validator, explorer")
//// networkType indicates the type of the network
//networkType = flag.String("network_type", "mainnet", "type of the network: mainnet, testnet, pangaea, partner, stressnet, devnet, localnet")
//// blockPeriod indicates the how long the leader waits to propose a new block.
//blockPeriod = flag.Int("block_period", 8, "how long in second the leader waits to propose a new block")
//// staking indicates whether the node is operating in staking mode.
//stakingFlag = flag.Bool("staking", false, "whether the node should operate in staking mode")
//// shardID indicates the shard ID of this node
//shardID = flag.Int("shard_id", -1, "the shard ID of this node")
//// Sharding configuration parameters for devnet
//devnetNumShards = flag.Uint("dn_num_shards", 2, "number of shards for -network_type=devnet (default: 2)")
//devnetShardSize = flag.Int("dn_shard_size", 10, "number of nodes per shard for -network_type=devnet (default 10)")
//devnetHarmonySize = flag.Int("dn_hmy_size", -1, "number of Harmony-operated nodes per shard for -network_type=devnet; negative (default) means equal to -dn_shard_size")
//// logging verbosity
//verbosity = flag.Int("verbosity", 5, "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail (default: 5)")
//// dbDir is the database directory.
//dbDir = flag.String("db_dir", "", "blockchain database directory")
//publicRPC = flag.Bool("public_rpc", false, "Enable Public RPC Access (default: false)")
//// Bad block revert
//// TODO: Seperate revert to a different command
//doRevertBefore = flag.Int("do_revert_before", 0, "If the current block is less than do_revert_before, revert all blocks until (including) revert_to block")
//revertTo = flag.Int("revert_to", 0, "The revert will rollback all blocks until and including block number revert_to")
//revertBeacon = flag.Bool("revert_beacon", false, "Whether to revert beacon chain or the chain this node is assigned to")
//// Blacklist of addresses
//blacklistPath = flag.String("blacklist", "./.hmy/blacklist.txt", "Path to newline delimited file of blacklisted wallet addresses")
//broadcastInvalidTx = flag.Bool("broadcast_invalid_tx", false, "Broadcast invalid transactions to sync pool state (default: false)")
//webHookYamlPath = flag.String(
// "webhook_yaml", "", "path for yaml config reporting double signing",
//)
var configFlag = cli . StringFlag {
Name : "config" ,
Usage : "load node config from the config toml file." ,
Shorthand : "c" ,
DefValue : "" ,
}
func init ( ) {
if err := registerRootCmdFlags ( ) ; err != nil {
// Only happens when code is wrong
panic ( err )
}
cli . SetParseErrorHandle ( func ( err error ) {
os . Exit ( 128 ) // 128 - invalid command line arguments
} )
rootCmd . AddCommand ( dumpConfigCmd )
rootCmd . AddCommand ( versionCmd )
if err := registerRootCmdFlags ( ) ; err != nil {
os . Exit ( 2 )
}
if err := registerDumpConfigFlags ( ) ; err != nil {
os . Exit ( 2 )
}
}
func main ( ) {
rootCmd . Execute ( )
}
func registerRootCmdFlags ( ) error {
var flags [ ] cli . Flag
flags = append ( flags , configFlag )
flags = append ( flags , generalFlags ... )
flags = append ( flags , networkFlags ... )
flags = append ( flags , p2pFlags ... )
flags = append ( flags , rpcFlags ... )
flags = append ( flags , wsFlags ... )
flags = append ( flags , blsFlags ... )
flags = append ( flags , consensusFlags ... )
flags = append ( flags , txPoolFlags ... )
flags = append ( flags , pprofFlags ... )
flags = append ( flags , logFlags ... )
flags = append ( flags , devnetFlags ... )
flags = append ( flags , revertFlags ... )
flags = append ( flags , legacyMiscFlags ... )
return cli . RegisterFlags ( rootCmd , flags )
}
func runHarmonyNode ( cmd * cobra . Command , args [ ] string ) {
prepareRootCmd ( cmd )
cfg , err := getHarmonyConfig ( cmd )
if err != nil {
fmt . Println ( err )
cmd . Help ( )
os . Exit ( 128 )
}
setupNodeLog ( cfg )
setupPprof ( cfg )
setupNodeAndRun ( cfg )
}
func prepareRootCmd ( cmd * cobra . Command ) {
// HACK Force usage of go implementation rather than the C based one. Do the right way, see the
// notes one line 66,67 of https://golang.org/src/net/net.go that say can make the decision at
// build time.
os . Setenv ( "GODEBUG" , "netdns=go" )
// Don't set higher than num of CPU. It will make go scheduler slower.
runtime . GOMAXPROCS ( runtime . NumCPU ( ) )
// Set up randomization seed.
rand . Seed ( int64 ( time . Now ( ) . Nanosecond ( ) ) )
}
func getHarmonyConfig ( cmd * cobra . Command ) ( harmonyConfig , error ) {
var (
config harmonyConfig
err error
)
if cli . IsFlagChanged ( cmd , configFlag ) {
configFile := cli . GetStringFlagValue ( cmd , configFlag )
config , err = loadHarmonyConfig ( configFile )
} else {
config = getDefaultHmyConfigCopy ( )
nt := getNetworkType ( cmd )
config . Network = getDefaultNetworkConfig ( nt )
}
if err != nil {
return harmonyConfig { } , err
}
// Misc flags shall be applied first since legacy ip / port is overwritten
// by new ip / port flags
applyLegacyMiscFlags ( cmd , & config )
applyGeneralFlags ( cmd , & config )
applyNetworkFlags ( cmd , & config )
applyP2PFlags ( cmd , & config )
applyRPCFlags ( cmd , & config )
applyWSFlags ( cmd , & config )
applyBLSFlags ( cmd , & config )
applyConsensusFlags ( cmd , & config )
applyTxPoolFlags ( cmd , & config )
applyPprofFlags ( cmd , & config )
applyLogFlags ( cmd , & config )
applyDevnetFlags ( cmd , & config )
applyRevertFlags ( cmd , & config )
if err := validateHarmonyConfig ( config ) ; err != nil {
return harmonyConfig { } , err
}
return config , nil
}
func setupNodeLog ( config harmonyConfig ) {
logPath := filepath . Join ( config . Log . Folder , config . Log . FileName )
rotateSize := config . Log . RotateSize
verbosity := config . Log . Verbosity
utils . AddLogFile ( logPath , rotateSize )
utils . SetLogVerbosity ( log . Lvl ( verbosity ) )
if config . Log . Context != nil {
ip := config . Log . Context . IP
port := config . Log . Context . Port
utils . SetLogContext ( ip , strconv . Itoa ( port ) )
}
}
func setupPprof ( config harmonyConfig ) {
enabled := config . Pprof . Enabled
addr := config . Pprof . ListenAddr
if enabled {
go func ( ) {
http . ListenAndServe ( addr , nil )
} ( )
}
}
func setupNodeAndRun ( hc harmonyConfig ) {
var err error
bootNodes := hc . Network . BootNodes
p2p . BootNodes , err = p2p . StringsToAddrs ( bootNodes )
if err != nil {
utils . FatalErrMsg ( err , "cannot parse bootnode list %#v" ,
bootNodes )
}
// TODO: seperate use of port rpc / p2p
nodeconfig . GetDefaultConfig ( ) . Port = strconv . Itoa ( hc . RPC . Port )
nodeconfig . GetDefaultConfig ( ) . IP = hc . RPC . IP
nodeconfig . SetShardingSchedule ( shard . Schedule )
nodeconfig . SetPublicRPC ( true )
nodeconfig . SetVersion ( getHarmonyVersion ( ) )
nodeconfigSetShardSchedule ( hc )
if hc . General . NodeType == "validator" {
var err error
if hc . General . IsStaking {
err = setupStakingNodeAccount ( hc )
} else {
err = setupLegacyNodeAccount ( hc )
}
if err != nil {
fmt . Fprintf ( os . Stderr , "cannot set up node account: %s\n" , err )
os . Exit ( 1 )
}
}
if hc . General . NodeType == "validator" {
fmt . Printf ( "%s mode; node key %s -> shard %d\n" ,
map [ bool ] string { false : "Legacy" , true : "Staking" } [ hc . General . IsStaking ] ,
nodeconfig . GetDefaultConfig ( ) . ConsensusPriKey . GetPublicKeys ( ) . SerializeToHexStr ( ) ,
initialAccounts [ 0 ] . ShardID )
}
if hc . General . NodeType != "validator" && hc . General . ShardID >= 0 {
for _ , initialAccount := range initialAccounts {
utils . Logger ( ) . Info ( ) .
Uint32 ( "original" , initialAccount . ShardID ) .
Int ( "override" , hc . General . ShardID ) .
Msg ( "ShardID Override" )
initialAccount . ShardID = uint32 ( hc . General . ShardID )
}
}
nodeConfig , err := createGlobalConfig ( hc )
if err != nil {
fmt . Fprintf ( os . Stderr , "ERROR cannot configure node: %s\n" , err )
os . Exit ( 1 )
}
currentNode := setupConsensusAndNode ( hc , nodeConfig )
nodeconfig . GetDefaultConfig ( ) . ShardID = nodeConfig . ShardID
// Prepare for graceful shutdown from os signals
osSignal := make ( chan os . Signal )
signal . Notify ( osSignal , os . Interrupt , syscall . SIGTERM )
go func ( ) {
for sig := range osSignal {
if sig == syscall . SIGTERM || sig == os . Interrupt {
const msg = "Got %s signal. Gracefully shutting down...\n"
utils . Logger ( ) . Printf ( msg , sig )
fmt . Printf ( msg , sig )
currentNode . ShutDown ( )
}
}
} ( )
if nodeConfig . ShardID != shard . BeaconChainShardID {
utils . Logger ( ) . Info ( ) .
Uint32 ( "shardID" , currentNode . Blockchain ( ) . ShardID ( ) ) .
Uint32 ( "shardID" , nodeConfig . ShardID ) . Msg ( "SupportBeaconSyncing" )
currentNode . SupportBeaconSyncing ( )
}
if hc . Revert != nil && hc . Revert . RevertBefore != - 1 && hc . Revert . RevertTo != - 1 {
chain := currentNode . Blockchain ( )
if hc . Revert . RevertBeacon {
chain = currentNode . Beaconchain ( )
}
curNum := chain . CurrentBlock ( ) . NumberU64 ( )
if curNum < uint64 ( hc . Revert . RevertBefore ) && curNum >= uint64 ( hc . Revert . RevertTo ) {
// Remove invalid blocks
for chain . CurrentBlock ( ) . NumberU64 ( ) >= uint64 ( hc . Revert . RevertTo ) {
curBlock := chain . CurrentBlock ( )
rollbacks := [ ] ethCommon . Hash { curBlock . Hash ( ) }
chain . Rollback ( rollbacks )
lastSig := curBlock . Header ( ) . LastCommitSignature ( )
sigAndBitMap := append ( lastSig [ : ] , curBlock . Header ( ) . LastCommitBitmap ( ) ... )
chain . WriteCommitSig ( curBlock . NumberU64 ( ) - 1 , sigAndBitMap )
}
}
}
startMsg := "==== New Harmony Node ===="
if hc . General . NodeType == nodeTypeExplorer {
startMsg = "==== New Explorer Node ===="
}
utils . Logger ( ) . Info ( ) .
Str ( "BLSPubKey" , nodeConfig . ConsensusPriKey . GetPublicKeys ( ) . SerializeToHexStr ( ) ) .
Uint32 ( "ShardID" , nodeConfig . ShardID ) .
Str ( "ShardGroupID" , nodeConfig . GetShardGroupID ( ) . String ( ) ) .
Str ( "BeaconGroupID" , nodeConfig . GetBeaconGroupID ( ) . String ( ) ) .
Str ( "ClientGroupID" , nodeConfig . GetClientGroupID ( ) . String ( ) ) .
Str ( "Role" , currentNode . NodeConfig . Role ( ) . String ( ) ) .
Str ( "multiaddress" ,
fmt . Sprintf ( "/ip4/%s/tcp/%s/p2p/%s" , hc . RPC . IP , hc . P2P . Port , myHost . GetID ( ) . Pretty ( ) ) ,
) .
Msg ( startMsg )
nodeconfig . SetPeerID ( myHost . GetID ( ) )
currentNode . SupportSyncing ( )
currentNode . ServiceManagerSetup ( )
currentNode . RunServices ( )
if err := currentNode . StartRPC ( strconv . Itoa ( hc . RPC . Port ) ) ; err != nil {
utils . Logger ( ) . Warn ( ) .
Err ( err ) .
Msg ( "StartRPC failed" )
}
if err := currentNode . BootstrapConsensus ( ) ; err != nil {
fmt . Println ( "could not bootstrap consensus" , err . Error ( ) )
os . Exit ( - 1 )
}
if err := currentNode . Start ( ) ; err != nil {
fmt . Println ( "could not begin network message handling for node" , err . Error ( ) )
os . Exit ( - 1 )
}
}
func nodeconfigSetShardSchedule ( config harmonyConfig ) {
switch config . Network . NetworkType {
case nodeconfig . Mainnet :
shard . Schedule = shardingconfig . MainnetSchedule
case nodeconfig . Testnet :
shard . Schedule = shardingconfig . TestnetSchedule
case nodeconfig . Pangaea :
shard . Schedule = shardingconfig . PangaeaSchedule
case nodeconfig . Localnet :
shard . Schedule = shardingconfig . LocalnetSchedule
case nodeconfig . Partner :
shard . Schedule = shardingconfig . PartnerSchedule
case nodeconfig . Stressnet :
shard . Schedule = shardingconfig . StressNetSchedule
case nodeconfig . Devnet :
var dnConfig devnetConfig
if config . Devnet != nil {
dnConfig = * config . Devnet
} else {
dnConfig = getDefaultDevnetConfigCopy ( )
}
// TODO (leo): use a passing list of accounts here
devnetConfig , err := shardingconfig . NewInstance (
uint32 ( dnConfig . NumShards ) , dnConfig . ShardSize , dnConfig . HmyNodeSize , numeric . OneDec ( ) , genesis . HarmonyAccounts , genesis . FoundationalNodeAccounts , nil , shardingconfig . VLBPE )
if err != nil {
_ , _ = fmt . Fprintf ( os . Stderr , "ERROR invalid devnet sharding config: %s" ,
err )
os . Exit ( 1 )
}
shard . Schedule = shardingconfig . NewFixedSchedule ( devnetConfig )
}
}
func findAccountsByPubKeys ( config shardingconfig . Instance , pubKeys multibls . PublicKeys ) {
@ -125,9 +368,9 @@ func findAccountsByPubKeys(config shardingconfig.Instance, pubKeys multibls.Publ
}
}
func setupLegacyNodeAccount ( ) error {
func setupLegacyNodeAccount ( hc harmonyConfig ) error {
genesisShardingConfig := shard . Schedule . InstanceForEpoch ( big . NewInt ( core . GenesisEpoch ) )
multiBLSPubKey := setupConsensusKeys ( nodeconfig . GetDefaultConfig ( ) )
multiBLSPubKey := setupConsensusKeys ( hc , nodeconfig . GetDefaultConfig ( ) )
reshardingEpoch := genesisShardingConfig . ReshardingEpoch ( )
if len ( reshardingEpoch ) > 0 {
@ -157,8 +400,8 @@ func setupLegacyNodeAccount() error {
return nil
}
func setupStakingNodeAccount ( ) error {
pubKeys := setupConsensusKeys ( nodeconfig . GetDefaultConfig ( ) )
func setupStakingNodeAccount ( hc harmonyConfig ) error {
pubKeys := setupConsensusKeys ( hc , nodeconfig . GetDefaultConfig ( ) )
shardID , err := nodeconfig . GetDefaultConfig ( ) . ShardIDFromConsensusKey ( )
if err != nil {
return errors . Wrap ( err , "cannot determine shard to join" )
@ -178,51 +421,36 @@ func setupStakingNodeAccount() error {
return nil
}
// setupConsensusKeys load bls keys and set the keys to nodeConfig. Return the loaded public keys.
func setupConsensusKeys ( config * nodeconfig . ConfigType ) multibls . PublicKeys {
onceLoadBLSKey . Do ( func ( ) {
var err error
multiBLSPriKey , err = loadBLSKeys ( )
if err != nil {
fmt . Fprintf ( os . Stderr , "ERROR when loading bls key: %v\n" , err )
os . Exit ( 100 )
}
fmt . Printf ( "Successfully loaded %v BLS keys\n" , len ( multiBLSPriKey ) )
} )
config . ConsensusPriKey = multiBLSPriKey
return multiBLSPriKey . GetPublicKeys ( )
}
func createGlobalConfig ( ) ( * nodeconfig . ConfigType , error ) {
func createGlobalConfig ( hc harmonyConfig ) ( * nodeconfig . ConfigType , error ) {
var err error
if len ( initialAccounts ) == 0 {
initialAccounts = append ( initialAccounts , & genesis . DeployAccount { ShardID : uint32 ( * s hardID) } )
initialAccounts = append ( initialAccounts , & genesis . DeployAccount { ShardID : uint32 ( hc . General . ShardID ) } )
}
nodeConfig := nodeconfig . GetShardConfig ( initialAccounts [ 0 ] . ShardID )
if * nodeType == "validator" {
if hc . General . NodeType == nodeTypeValidator {
// Set up consensus keys.
setupConsensusKeys ( nodeConfig )
setupConsensusKeys ( hc , nodeConfig )
} else {
// set dummy bls key for consensus object
nodeConfig . ConsensusPriKey = multibls . GetPrivateKeys ( & bls . SecretKey { } )
}
// Set network type
netType := nodeconfig . NetworkType ( * n etworkType)
netType := nodeconfig . NetworkType ( hc . Network . N etworkType)
nodeconfig . SetNetworkType ( netType ) // sets for both global and shard configs
nodeConfig . SetArchival ( * i sArchival)
nodeConfig . SetArchival ( hc . General . I sArchival)
// P2P private key is used for secure message transfer between p2p nodes.
nodeConfig . P2PPriKey , _ , err = utils . LoadKeyFromFile ( * k eyFile)
nodeConfig . P2PPriKey , _ , err = utils . LoadKeyFromFile ( hc . P2P . K eyFile)
if err != nil {
return nil , errors . Wrapf ( err , "cannot load or create P2P key at %#v" ,
* k eyFile)
hc . P2P . K eyFile)
}
selfPeer := p2p . Peer {
IP : * ip ,
Port : * port ,
IP : hc . P2P . IP ,
Port : strconv . Itoa ( hc . P2P . Port ) ,
ConsensusPubKey : nodeConfig . ConsensusPriKey [ 0 ] . Pub . Object ,
}
@ -231,9 +459,10 @@ func createGlobalConfig() (*nodeconfig.ConfigType, error) {
return nil , errors . Wrap ( err , "cannot create P2P network host" )
}
nodeConfig . DBDir = * db Dir
nodeConfig . DBDir = hc . General . Data Dir
if p := * webHookYamlPath ; p != "" {
if hc . Legacy != nil && len ( hc . Legacy . WebHookConfig ) != 0 {
p := hc . Legacy . WebHookConfig
config , err := webhooks . NewWebHooksFromPath ( p )
if err != nil {
fmt . Fprintf (
@ -247,10 +476,10 @@ func createGlobalConfig() (*nodeconfig.ConfigType, error) {
return nodeConfig , nil
}
func setupConsensusAndNode ( nodeConfig * nodeconfig . ConfigType ) * node . Node {
func setupConsensusAndNode ( hc harmonyConfig , nodeConfig * nodeconfig . ConfigType ) * node . Node {
// Consensus object.
// TODO: consensus object shouldn't start here
decider := quorum . NewDecider ( quorum . SuperMajorityVote , uint32 ( * s hardID) )
decider := quorum . NewDecider ( quorum . SuperMajorityVote , uint32 ( hc . General . S hardID) )
currentConsensus , err := consensus . New (
myHost , nodeConfig . ShardID , p2p . Peer { } , nodeConfig . ConsensusPriKey , decider ,
@ -263,15 +492,15 @@ func setupConsensusAndNode(nodeConfig *nodeconfig.ConfigType) *node.Node {
_ , _ = fmt . Fprintf ( os . Stderr , "Error :%v \n" , err )
os . Exit ( 1 )
}
commitDelay , err := time . ParseDuration ( * d elayCommit)
commitDelay , err := time . ParseDuration ( hc . Consensus . D elayCommit)
if err != nil || commitDelay < 0 {
_ , _ = fmt . Fprintf ( os . Stderr , "ERROR invalid commit delay %#v" , * d elayCommit)
_ , _ = fmt . Fprintf ( os . Stderr , "ERROR invalid commit delay %#v" , hc . Consensus . D elayCommit)
os . Exit ( 1 )
}
currentConsensus . SetCommitDelay ( commitDelay )
currentConsensus . MinPeers = * m inPeers
currentConsensus . MinPeers = hc . Consensus . M inPeers
blacklist , err := setupBlacklist ( )
blacklist , err := setupBlacklist ( hc )
if err != nil {
utils . Logger ( ) . Warn ( ) . Msgf ( "Blacklist setup error: %s" , err . Error ( ) )
}
@ -279,44 +508,37 @@ func setupConsensusAndNode(nodeConfig *nodeconfig.ConfigType) *node.Node {
// Current node.
chainDBFactory := & shardchain . LDBFactory { RootDir : nodeConfig . DBDir }
currentNode := node . New ( myHost , currentConsensus , chainDBFactory , blacklist , * i sArchival)
currentNode . BroadcastInvalidTx = * b roadcastInvalidTx
currentNode := node . New ( myHost , currentConsensus , chainDBFactory , blacklist , hc . General . I sArchival)
currentNode . BroadcastInvalidTx = hc . TxPool . B roadcastInvalidTx
switch {
case * networkType == nodeconfig . Localnet :
epochConfig := shard . Schedule . InstanceForEpoch ( ethCommon . Big0 )
selfPort , err := strconv . ParseUint ( * port , 10 , 16 )
if err != nil {
utils . Logger ( ) . Fatal ( ) .
Err ( err ) .
Str ( "self_port_string" , * port ) .
Msg ( "cannot convert self port string into port number" )
}
currentNode . SyncingPeerProvider = node . NewLocalSyncingPeerProvider (
6000 , uint16 ( selfPort ) , epochConfig . NumShards ( ) , uint32 ( epochConfig . NumNodesPerShard ( ) ) )
case * dnsZone != "" :
currentNode . SyncingPeerProvider = node . NewDNSSyncingPeerProvider ( * dnsZone , syncing . GetSyncingPort ( * dnsPort ) )
case * dnsFlag :
currentNode . SyncingPeerProvider = node . NewDNSSyncingPeerProvider ( "t.hmny.io" , syncing . GetSyncingPort ( * dnsPort ) )
default :
if hc . Network . LegacySyncing {
currentNode . SyncingPeerProvider = node . NewLegacySyncingPeerProvider ( currentNode )
} else {
if hc . Network . NetworkType == nodeconfig . Localnet {
epochConfig := shard . Schedule . InstanceForEpoch ( ethCommon . Big0 )
selfPort := hc . Network . DNSPort
currentNode . SyncingPeerProvider = node . NewLocalSyncingPeerProvider (
6000 , uint16 ( selfPort ) , epochConfig . NumShards ( ) , uint32 ( epochConfig . NumNodesPerShard ( ) ) )
} else {
currentNode . SyncingPeerProvider = node . NewDNSSyncingPeerProvider ( hc . Network . DNSZone , syncing . GetSyncingPort ( strconv . Itoa ( hc . Network . DNSPort ) ) )
}
}
// TODO: refactor the creation of blockchain out of node.New()
currentConsensus . ChainReader = currentNode . Blockchain ( )
currentNode . NodeConfig . DNSZone = * dnsZone
currentNode . NodeConfig . DNSZone = hc . Network . DNSZone
currentNode . NodeConfig . SetBeaconGroupID (
nodeconfig . NewGroupIDByShardID ( shard . BeaconChainShardID ) ,
)
nodeconfig . GetDefaultConfig ( ) . DBDir = nodeConfig . DBDir
switch * n odeType {
case "explorer" :
switch hc . General . N odeType {
case nodeTypeExplorer :
nodeconfig . SetDefaultRole ( nodeconfig . ExplorerNode )
currentNode . NodeConfig . SetRole ( nodeconfig . ExplorerNode )
case "validator" :
case nodeTypeValidator :
nodeconfig . SetDefaultRole ( nodeconfig . Validator )
currentNode . NodeConfig . SetRole ( nodeconfig . Validator )
}
@ -327,7 +549,7 @@ func setupConsensusAndNode(nodeConfig *nodeconfig.ConfigType) *node.Node {
// This needs to be executed after consensus setup
if err := currentNode . InitConsensusWithValidators ( ) ; err != nil {
utils . Logger ( ) . Warn ( ) .
Int ( "shardID" , * s hardID) .
Int ( "shardID" , hc . General . S hardID) .
Err ( err ) .
Msg ( "InitConsensusWithMembers failed" )
}
@ -345,14 +567,19 @@ func setupConsensusAndNode(nodeConfig *nodeconfig.ConfigType) *node.Node {
// update consensus information based on the blockchain
currentConsensus . SetMode ( currentConsensus . UpdateConsensusInformation ( ) )
// Setup block period and block due time.
currentConsensus . BlockPeriod = time . Duration ( * blockPeriod ) * time . Second
// TODO: move the error check to validation
currentConsensus . BlockPeriod , err = time . ParseDuration ( hc . Consensus . BlockTime )
if err != nil {
fmt . Printf ( "Unknown block period: %v\n" , hc . Consensus . BlockTime )
os . Exit ( 128 )
}
currentConsensus . NextBlockDue = time . Now ( )
return currentNode
}
func setupBlacklist ( ) ( map [ ethCommon . Address ] struct { } , error ) {
utils . Logger ( ) . Debug ( ) . Msgf ( "Using blacklist file at `%s`" , * blacklistPath )
dat , err := ioutil . ReadFile ( * blacklistPath )
func setupBlacklist ( hc harmonyConfig ) ( map [ ethCommon . Address ] struct { } , error ) {
utils . Logger ( ) . Debug ( ) . Msgf ( "Using blacklist file at `%s`" , hc . TxPool . BlacklistFile )
dat , err := ioutil . ReadFile ( hc . TxPool . BlacklistFile )
if err != nil {
return nil , err
}
@ -369,120 +596,3 @@ func setupBlacklist() (map[ethCommon.Address]struct{}, error) {
}
return addrMap , nil
}
func main ( ) {
if * nodeType == "validator" {
var err error
if * stakingFlag {
err = setupStakingNodeAccount ( )
} else {
err = setupLegacyNodeAccount ( )
}
if err != nil {
fmt . Fprintf ( os . Stderr , "cannot set up node account: %s\n" , err )
os . Exit ( 1 )
}
}
if * nodeType == "validator" {
fmt . Printf ( "%s mode; node key %s -> shard %d\n" ,
map [ bool ] string { false : "Legacy" , true : "Staking" } [ * stakingFlag ] ,
nodeconfig . GetDefaultConfig ( ) . ConsensusPriKey . GetPublicKeys ( ) . SerializeToHexStr ( ) ,
initialAccounts [ 0 ] . ShardID )
}
if * nodeType != "validator" && * shardID >= 0 {
for _ , initialAccount := range initialAccounts {
utils . Logger ( ) . Info ( ) .
Uint32 ( "original" , initialAccount . ShardID ) .
Int ( "override" , * shardID ) .
Msg ( "ShardID Override" )
initialAccount . ShardID = uint32 ( * shardID )
}
}
nodeConfig , err := createGlobalConfig ( )
if err != nil {
fmt . Fprintf ( os . Stderr , "ERROR cannot configure node: %s\n" , err )
os . Exit ( 1 )
}
currentNode := setupConsensusAndNode ( nodeConfig )
nodeconfig . GetDefaultConfig ( ) . ShardID = nodeConfig . ShardID
// Prepare for graceful shutdown from os signals
osSignal := make ( chan os . Signal )
signal . Notify ( osSignal , os . Interrupt , syscall . SIGTERM )
go func ( ) {
for sig := range osSignal {
if sig == syscall . SIGTERM || sig == os . Interrupt {
const msg = "Got %s signal. Gracefully shutting down...\n"
utils . Logger ( ) . Printf ( msg , sig )
fmt . Printf ( msg , sig )
currentNode . ShutDown ( )
}
}
} ( )
if nodeConfig . ShardID != shard . BeaconChainShardID {
utils . Logger ( ) . Info ( ) .
Uint32 ( "shardID" , currentNode . Blockchain ( ) . ShardID ( ) ) .
Uint32 ( "shardID" , nodeConfig . ShardID ) . Msg ( "SupportBeaconSyncing" )
currentNode . SupportBeaconSyncing ( )
}
if uint64 ( * doRevertBefore ) != 0 && uint64 ( * revertTo ) != 0 {
chain := currentNode . Blockchain ( )
if * revertBeacon {
chain = currentNode . Beaconchain ( )
}
curNum := chain . CurrentBlock ( ) . NumberU64 ( )
if curNum < uint64 ( * doRevertBefore ) && curNum >= uint64 ( * revertTo ) {
// Remove invalid blocks
for chain . CurrentBlock ( ) . NumberU64 ( ) >= uint64 ( * revertTo ) {
curBlock := chain . CurrentBlock ( )
rollbacks := [ ] ethCommon . Hash { curBlock . Hash ( ) }
chain . Rollback ( rollbacks )
lastSig := curBlock . Header ( ) . LastCommitSignature ( )
sigAndBitMap := append ( lastSig [ : ] , curBlock . Header ( ) . LastCommitBitmap ( ) ... )
chain . WriteCommitSig ( curBlock . NumberU64 ( ) - 1 , sigAndBitMap )
}
}
}
startMsg := "==== New Harmony Node ===="
if * nodeType == "explorer" {
startMsg = "==== New Explorer Node ===="
}
utils . Logger ( ) . Info ( ) .
Str ( "BLSPubKey" , nodeConfig . ConsensusPriKey . GetPublicKeys ( ) . SerializeToHexStr ( ) ) .
Uint32 ( "ShardID" , nodeConfig . ShardID ) .
Str ( "ShardGroupID" , nodeConfig . GetShardGroupID ( ) . String ( ) ) .
Str ( "BeaconGroupID" , nodeConfig . GetBeaconGroupID ( ) . String ( ) ) .
Str ( "ClientGroupID" , nodeConfig . GetClientGroupID ( ) . String ( ) ) .
Str ( "Role" , currentNode . NodeConfig . Role ( ) . String ( ) ) .
Str ( "multiaddress" ,
fmt . Sprintf ( "/ip4/%s/tcp/%s/p2p/%s" , * ip , * port , myHost . GetID ( ) . Pretty ( ) ) ,
) .
Msg ( startMsg )
nodeconfig . SetPeerID ( myHost . GetID ( ) )
currentNode . SupportSyncing ( )
currentNode . ServiceManagerSetup ( )
currentNode . RunServices ( )
if err := currentNode . StartRPC ( * port ) ; err != nil {
utils . Logger ( ) . Warn ( ) .
Err ( err ) .
Msg ( "StartRPC failed" )
}
if err := currentNode . BootstrapConsensus ( ) ; err != nil {
fmt . Println ( "could not bootstrap consensus" , err . Error ( ) )
os . Exit ( - 1 )
}
if err := currentNode . Start ( ) ; err != nil {
fmt . Println ( "could not begin network message handling for node" , err . Error ( ) )
os . Exit ( - 1 )
}
}