package commands
import (
- "fmt"
+ "strings"
+ "time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
- "strings"
-
"github.com/bytom/node"
)
RootCmd.AddCommand(runNodeCmd)
}
-func getLogLevel(level string) log.Level {
+func setLogLevel(level string) {
switch strings.ToLower(level) {
case "debug":
- return log.DebugLevel
+ log.SetLevel(log.DebugLevel)
case "info":
- return log.InfoLevel
+ log.SetLevel(log.InfoLevel)
case "warn":
- return log.WarnLevel
+ log.SetLevel(log.WarnLevel)
case "error":
- return log.ErrorLevel
+ log.SetLevel(log.ErrorLevel)
case "fatal":
- return log.FatalLevel
+ log.SetLevel(log.FatalLevel)
default:
- return log.InfoLevel
+ log.SetLevel(log.InfoLevel)
}
}
func runNode(cmd *cobra.Command, args []string) error {
- // Set log level by config.LogLevel
- log.SetLevel(getLogLevel(config.LogLevel))
+ startTime := time.Now()
+ setLogLevel(config.LogLevel)
// Create & start node
n := node.NewNode(config)
if _, err := n.Start(); err != nil {
- return fmt.Errorf("Failed to start node: %v", err)
- } else {
- log.Info("Start node ", n.SyncManager().NodeInfo())
+ log.WithField("err", err).Fatal("failed to start node")
}
+ nodeInfo := n.SyncManager().NodeInfo()
+ log.WithFields(log.Fields{
+ "version": nodeInfo.Version,
+ "network": nodeInfo.Network,
+ "duration": time.Since(startTime),
+ }).Info("start node complete")
+
// Trap signal, run forever.
n.RunForever()
-
return nil
}
import (
"encoding/binary"
"encoding/json"
+ "time"
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
"github.com/bytom/protocol/state"
)
+const logModule = "leveldb"
+
var (
blockStoreKey = []byte("blockStore")
blockPrefix = []byte("B:")
}
func (s *Store) LoadBlockIndex(stateBestHeight uint64) (*state.BlockIndex, error) {
+ startTime := time.Now()
blockIndex := state.NewBlockIndex()
bhIter := s.db.IteratorPrefix(blockHeaderPrefix)
defer bhIter.Release()
if err := bh.UnmarshalText(bhIter.Value()); err != nil {
return nil, err
}
-
+
// If a block with a height greater than the best height of state is added to the index,
// It may cause a bug that the new block cant not be process properly.
if bh.Height > stateBestHeight {
lastNode = node
}
+ log.WithFields(log.Fields{
+ "module": logModule,
+ "height": stateBestHeight,
+ "duration": time.Since(startTime),
+ }).Debug("initialize load history block index from database")
return blockIndex, nil
}
// SaveBlock persists a new block in the protocol.
func (s *Store) SaveBlock(block *types.Block, ts *bc.TransactionStatus) error {
+ startTime := time.Now()
binaryBlock, err := block.MarshalText()
if err != nil {
return errors.Wrap(err, "Marshal block meta")
batch.Set(calcTxStatusKey(&blockHash), binaryTxStatus)
batch.Write()
- log.WithFields(log.Fields{"height": block.Height, "hash": blockHash.String()}).Info("block saved on disk")
+ log.WithFields(log.Fields{
+ "module": logModule,
+ "height": block.Height,
+ "hash": blockHash.String(),
+ "duration": time.Since(startTime),
+ }).Info("block saved on disk")
return nil
}
const (
bannedPeerKey = "BannedPeer"
defaultBanDuration = time.Hour * 1
- minNumOutboundPeers = 5
+ minNumOutboundPeers = 3
)
//pre-define errors for connecting fail
import (
"time"
+ log "github.com/sirupsen/logrus"
+
"github.com/bytom/consensus"
"github.com/bytom/consensus/difficulty"
"github.com/bytom/errors"
"github.com/bytom/protocol/state"
)
+const logModule = "leveldb"
+
var (
errBadTimestamp = errors.New("block timestamp is not in the valid range")
errBadBits = errors.New("block bits is invalid")
// ValidateBlock validates a block and the transactions within.
func ValidateBlock(b *bc.Block, parent *state.BlockNode) error {
+ startTime := time.Now()
if err := ValidateBlockHeader(b, parent); err != nil {
return err
}
if txStatusHash != *b.TransactionStatusHash {
return errors.WithDetailf(errMismatchedMerkleRoot, "transaction status merkle root")
}
+
+ log.WithFields(log.Fields{
+ "module": logModule,
+ "height": b.Height,
+ "hash": b.ID.String(),
+ "duration": time.Since(startTime),
+ }).Debug("finish validate block")
return nil
}