Skip to content

Commit

Permalink
Organizes several loggers to print to different files
Browse files Browse the repository at this point in the history
  • Loading branch information
robschleusner committed Jan 5, 2024
1 parent efafdb6 commit e0a573c
Show file tree
Hide file tree
Showing 41 changed files with 648 additions and 562 deletions.
3 changes: 2 additions & 1 deletion cmd/go-quai/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ func runStart(cmd *cobra.Command, args []string) error {
log.Fatalf("error creating node: %s", err)
}

logLevel := cmd.Flag(utils.LogLevelFlag.Name).Value.String()
// create instance of consensus backend
consensus, err := utils.StartQuaiBackend()
consensus, err := utils.StartQuaiBackend(logLevel)
if err != nil {
log.Fatalf("error creating consensus backend: %s", err)
}
Expand Down
51 changes: 30 additions & 21 deletions cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package utils

import (
"fmt"
"io"
"os"
"runtime"
"time"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core/vm"
Expand All @@ -13,10 +17,6 @@ import (
"github.com/dominant-strategies/go-quai/quai/quaiconfig"
"github.com/dominant-strategies/go-quai/quaistats"
"github.com/spf13/viper"
"io"
"os"
"runtime"
"time"
)

type quaistatsConfig struct {
Expand All @@ -30,12 +30,15 @@ type quaiConfig struct {
}

// Create a new instance of the QuaiBackend consensus service
func StartQuaiBackend() (*quai.QuaiBackend, error) {

func StartQuaiBackend(logLevel string) (*quai.QuaiBackend, error) {
var logger *log.LogWrapper
// Make full node
go func() {
log.Info("Starting Prime")
stackPrime := makeFullNode(nil)
// Create the prime logger with the log file path
// TODO: Save log level as global variable and set it
logger = log.New("nodelogs/prime.log", logLevel)
logger.Info("Starting Prime")
stackPrime := makeFullNode(nil, logger)
defer stackPrime.Close()
StartNode(stackPrime)
stackPrime.Wait()
Expand All @@ -44,8 +47,11 @@ func StartQuaiBackend() (*quai.QuaiBackend, error) {
time.Sleep(2 * time.Second)

go func() {
log.Info("Starting Region")
stackRegion := makeFullNode(common.Location{0})
// Create the prime logger with the log file path
// TODO: Save log level as global variable and set it
logger = log.New("nodelogs/region-0.log", logLevel)
logger.Info("Starting Region")
stackRegion := makeFullNode(common.Location{0}, logger)
defer stackRegion.Close()
StartNode(stackRegion)
stackRegion.Wait()
Expand All @@ -54,8 +60,11 @@ func StartQuaiBackend() (*quai.QuaiBackend, error) {
time.Sleep(2 * time.Second)

go func() {
// Create the prime logger with the log file path
// TODO: Save log level as global variable and set it
logger = log.New("nodelogs/zone-0-0.log", logLevel)
log.Info("Starting Zone")
stackZone := makeFullNode(common.Location{0, 0})
stackZone := makeFullNode(common.Location{0, 0}, logger)
defer stackZone.Close()
StartNode(stackZone)
stackZone.Wait()
Expand All @@ -72,7 +81,7 @@ func StartNode(stack *node.Node) {
}

// makeConfigNode loads quai configuration and creates a blank node instance.
func makeConfigNode(nodeLocation common.Location) (*node.Node, quaiConfig) {
func makeConfigNode(nodeLocation common.Location, logger log.Logger) (*node.Node, quaiConfig) {
// Load defaults.
cfg := quaiConfig{
Quai: quaiconfig.Defaults,
Expand All @@ -81,15 +90,15 @@ func makeConfigNode(nodeLocation common.Location) (*node.Node, quaiConfig) {

// Apply flags.
// set the node location
log.Info("Node", "Location", nodeLocation)
logger.Info("Node", "Location", nodeLocation)
cfg.Node.NodeLocation = nodeLocation

SetNodeConfig(&cfg.Node, nodeLocation)
stack, err := node.New(&cfg.Node)
SetNodeConfig(&cfg.Node, nodeLocation, logger)
stack, err := node.New(&cfg.Node, logger)
if err != nil {
Fatalf("Failed to create the protocol stack: %v", err)
}
SetQuaiConfig(stack, &cfg.Quai, nodeLocation)
SetQuaiConfig(stack, &cfg.Quai, nodeLocation, logger)

// TODO: Apply stats
if viper.IsSet(QuaiStatsURLFlag.Name) {
Expand All @@ -114,9 +123,9 @@ func defaultNodeConfig() node.Config {
}

// makeFullNode loads quai configuration and creates the Quai backend.
func makeFullNode(nodeLocation common.Location) *node.Node {
stack, cfg := makeConfigNode(nodeLocation)
backend, _ := RegisterQuaiService(stack, cfg.Quai, cfg.Node.NodeLocation.Context())
func makeFullNode(nodeLocation common.Location, logger log.Logger) *node.Node {
stack, cfg := makeConfigNode(nodeLocation, logger)
backend, _ := RegisterQuaiService(stack, cfg.Quai, cfg.Node.NodeLocation.Context(), logger)
sendfullstats := viper.GetBool(SendFullStatsFlag.Name)
// Add the Quai Stats daemon if requested.
if cfg.Ethstats.URL != "" {
Expand All @@ -128,8 +137,8 @@ func makeFullNode(nodeLocation common.Location) *node.Node {
// RegisterQuaiService adds a Quai client to the stack.
// The second return value is the full node instance, which may be nil if the
// node is running as a light client.
func RegisterQuaiService(stack *node.Node, cfg quaiconfig.Config, nodeCtx int) (quaiapi.Backend, error) {
backend, err := quai.New(stack, &cfg, nodeCtx)
func RegisterQuaiService(stack *node.Node, cfg quaiconfig.Config, nodeCtx int, logger log.Logger) (quaiapi.Backend, error) {
backend, err := quai.New(stack, &cfg, nodeCtx, logger)
if err != nil {
Fatalf("Failed to register the Quai service: %v", err)
}
Expand Down
32 changes: 16 additions & 16 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ func setWS(cfg *node.Config, nodeLocation common.Location) {
}

// setDomUrl sets the dominant chain websocket url.
func setDomUrl(cfg *quaiconfig.Config, nodeLocation common.Location) {
func setDomUrl(cfg *quaiconfig.Config, nodeLocation common.Location, logger log.Logger) {
// only set the dom url if the node is not prime
if nodeLocation != nil {
if len(nodeLocation) == 1 {
Expand All @@ -948,7 +948,7 @@ func setDomUrl(cfg *quaiconfig.Config, nodeLocation common.Location) {
cfg.DomUrl = "ws://127.0.0.1:8002"
}
}
log.Info("Node", "Location", nodeLocation, "domurl", cfg.DomUrl)
logger.Info("Node", "Location", nodeLocation, "domurl", cfg.DomUrl)
}

// setSubUrls sets the subordinate chain urls
Expand Down Expand Up @@ -1069,7 +1069,7 @@ func MakePasswordList() []string {
}

// SetNodeConfig applies node-related command line flags to the config.
func SetNodeConfig(cfg *node.Config, nodeLocation common.Location) {
func SetNodeConfig(cfg *node.Config, nodeLocation common.Location, logger log.Logger) {
setHTTP(cfg, nodeLocation)
setWS(cfg, nodeLocation)
setNodeUserIdent(cfg)
Expand All @@ -1086,7 +1086,7 @@ func SetNodeConfig(cfg *node.Config, nodeLocation common.Location) {
cfg.UseLightweightKDF = true
}
if viper.IsSet(NoUSBFlag.Name) || cfg.NoUSB {
log.Warn("Option nousb is deprecated and USB is deactivated by default. Use --usb to enable")
logger.Warn("Option nousb is deprecated and USB is deactivated by default. Use --usb to enable")
}
if viper.IsSet(USBFlag.Name) {
cfg.USB = viper.GetBool(USBFlag.Name)
Expand All @@ -1099,7 +1099,7 @@ func SetNodeConfig(cfg *node.Config, nodeLocation common.Location) {
if dbEngine != "leveldb" && dbEngine != "pebble" {
Fatalf("Invalid choice for db.engine '%s', allowed 'leveldb' or 'pebble'", dbEngine)
}
log.Info(fmt.Sprintf("Using %s as db engine", dbEngine))
logger.Info(fmt.Sprintf("Using %s as db engine", dbEngine))
cfg.DBEngine = dbEngine
}
}
Expand Down Expand Up @@ -1329,15 +1329,15 @@ func CheckExclusive(args ...interface{}) {
}

// SetQuaiConfig applies quai-related command line flags to the config.
func SetQuaiConfig(stack *node.Node, cfg *quaiconfig.Config, nodeLocation common.Location) {
func SetQuaiConfig(stack *node.Node, cfg *quaiconfig.Config, nodeLocation common.Location, logger log.Logger) {
// Avoid conflicting network flags
CheckExclusive(ColosseumFlag, DeveloperFlag, GardenFlag, OrchardFlag, LocalFlag, LighthouseFlag)
CheckExclusive(DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer

if viper.GetString(GCModeFlag.Name) == "archive" && viper.GetUint64(TxLookupLimitFlag.Name) != 0 {
// TODO: see what this is supposed to do
viper.IsSet(TxLookupLimitFlag.Name)
log.Warn("Disable transaction unindexing for archive node")
logger.Warn("Disable transaction unindexing for archive node")
}

cfg.NodeLocation = nodeLocation
Expand All @@ -1359,7 +1359,7 @@ func SetQuaiConfig(stack *node.Node, cfg *quaiconfig.Config, nodeLocation common
setWhitelist(cfg)

// set the dominant chain websocket url
setDomUrl(cfg, nodeLocation)
setDomUrl(cfg, nodeLocation, logger)

// set the subordinate chain websocket urls
setSubUrls(cfg, nodeLocation)
Expand All @@ -1374,20 +1374,20 @@ func SetQuaiConfig(stack *node.Node, cfg *quaiconfig.Config, nodeLocation common
mem, err := gopsutil.VirtualMemory()
if err == nil {
if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 {
log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024)
logger.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024)
mem.Total = 2 * 1024 * 1024 * 1024
}
allowance := int(mem.Total / 1024 / 1024 / 3)
if cache := viper.GetInt(CacheFlag.Name); cache > allowance {
log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance)
logger.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance)
viper.GetViper().Set(CacheFlag.Name, strconv.Itoa(allowance))
}
}
// Ensure Go's GC ignores the database cache for trigger percentage
cache := viper.GetInt(CacheFlag.Name)
gogc := math.Max(20, math.Min(100, 100/(float64(cache)/1024)))

log.Debug("Sanitizing Go's GC trigger", "percent", int(gogc))
logger.Debug("Sanitizing Go's GC trigger", "percent", int(gogc))
godebug.SetGCPercent(int(gogc))

if viper.IsSet(NetworkIdFlag.Name) {
Expand All @@ -1414,7 +1414,7 @@ func SetQuaiConfig(stack *node.Node, cfg *quaiconfig.Config, nodeLocation common
cfg.Preimages = viper.GetBool(CachePreimagesFlag.Name)
if cfg.NoPruning && !cfg.Preimages {
cfg.Preimages = true
log.Info("Enabling recording of key preimages since archive mode is used")
logger.Info("Enabling recording of key preimages since archive mode is used")
}
if viper.IsSet(TxLookupLimitFlag.Name) {
cfg.TxLookupLimit = viper.GetUint64(TxLookupLimitFlag.Name)
Expand Down Expand Up @@ -1450,9 +1450,9 @@ func SetQuaiConfig(stack *node.Node, cfg *quaiconfig.Config, nodeLocation common
cfg.RPCGasCap = viper.GetUint64(RPCGlobalGasCapFlag.Name)
}
if cfg.RPCGasCap != 0 {
log.Info("Set global gas cap", "cap", cfg.RPCGasCap)
logger.Info("Set global gas cap", "cap", cfg.RPCGasCap)
} else {
log.Info("Global gas cap disabled")
logger.Info("Global gas cap disabled")
}
if viper.IsSet(RPCGlobalTxFeeCapFlag.Name) {
cfg.RPCTxFeeCap = viper.GetFloat64(RPCGlobalTxFeeCapFlag.Name)
Expand Down Expand Up @@ -1516,9 +1516,9 @@ func SetQuaiConfig(stack *node.Node, cfg *quaiconfig.Config, nodeLocation common
cfg.Genesis.Nonce = viper.GetUint64(GenesisNonceFlag.Name)
}

log.Info("Setting genesis Location", "node", nodeLocation)
logger.Info("Setting genesis Location", "node", nodeLocation)
cfg.Genesis.Config.Location = nodeLocation
log.Info("Location after setting", "genesis", cfg.Genesis.Config.Location)
logger.Info("Location after setting", "genesis", cfg.Genesis.Config.Location)
}

func SplitTagsFlag(tagsFlag string) map[string]string {
Expand Down
9 changes: 6 additions & 3 deletions consensus/blake3pow/blake3pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func init() {
sharedConfig := Config{
PowMode: ModeNormal,
}
sharedBlake3pow = New(sharedConfig, nil, false)
sharedBlake3pow = New(sharedConfig, nil, false, log.New("nodelogs/shared-blake3pow.log", "info"))
}

// Mode defines the type and amount of PoW verification a blake3pow engine makes.
Expand Down Expand Up @@ -71,15 +71,18 @@ type Blake3pow struct {

lock sync.Mutex // Ensures thread safety for the in-memory caches and mining fields
closeOnce sync.Once // Ensures exit channel will not be closed twice.

logger log.Logger
}

// New creates a full sized blake3pow PoW scheme and starts a background thread for
// remote mining, also optionally notifying a batch of remote services of new work
// packages.
func New(config Config, notify []string, noverify bool) *Blake3pow {
func New(config Config, notify []string, noverify bool, logger log.Logger) *Blake3pow {
blake3pow := &Blake3pow{
config: config,
update: make(chan struct{}),
logger: logger,
}
if config.PowMode == ModeShared {
blake3pow.shared = sharedBlake3pow
Expand All @@ -91,7 +94,7 @@ func New(config Config, notify []string, noverify bool) *Blake3pow {
// NewTester creates a small sized blake3pow PoW scheme useful only for testing
// purposes.
func NewTester(notify []string, noverify bool) *Blake3pow {
return New(Config{PowMode: ModeTest}, notify, noverify)
return New(Config{PowMode: ModeTest}, notify, noverify, log.New("nodelogs/test-blake3pow.log", "info"))
}

// NewFaker creates a blake3pow consensus engine with a fake PoW scheme that accepts
Expand Down
16 changes: 8 additions & 8 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (blake3pow *Blake3pow) CalcDifficulty(chain consensus.ChainHeaderReader, pa
nodeCtx := blake3pow.config.NodeLocation.Context()

if nodeCtx != common.ZONE_CTX {
log.Error("Cannot CalcDifficulty for", "context", nodeCtx)
blake3pow.logger.Error("Cannot CalcDifficulty for", "context", nodeCtx)
return nil
}

Expand Down Expand Up @@ -431,17 +431,17 @@ func (blake3pow *Blake3pow) Finalize(chain consensus.ChainHeaderReader, header *
nodeLocation := blake3pow.config.NodeLocation
nodeCtx := blake3pow.config.NodeLocation.Context()
// Accumulate any block and uncle rewards and commit the final state root
accumulateRewards(chain.Config(), state, header, uncles)
accumulateRewards(chain.Config(), state, header, uncles, blake3pow.logger)

if nodeCtx == common.ZONE_CTX && header.ParentHash(nodeCtx) == chain.Config().GenesisHash {
alloc := core.ReadGenesisAlloc("genallocs/gen_alloc_" + nodeLocation.Name() + ".json")
log.Info("Allocating genesis accounts", "num", len(alloc))
alloc := core.ReadGenesisAlloc("genallocs/gen_alloc_"+nodeLocation.Name()+".json", blake3pow.logger)
blake3pow.logger.Info("Allocating genesis accounts", "num", len(alloc))

for addressString, account := range alloc {
addr := common.HexToAddress(addressString, nodeLocation)
internal, err := addr.InternalAddress()
if err != nil {
log.Error("Provided address in genesis block is out of scope")
blake3pow.logger.Error("Provided address in genesis block is out of scope")
}
state.AddBalance(internal, account.Balance)
state.SetCode(internal, account.Code)
Expand Down Expand Up @@ -475,14 +475,14 @@ func (blake3pow *Blake3pow) ComputePowLight(header *types.Header) (common.Hash,
// AccumulateRewards credits the coinbase of the given block with the mining
// reward. The total reward consists of the static block reward and rewards for
// included uncles. The coinbase of each uncle block is also rewarded.
func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header, logger log.Logger) {
nodeCtx := config.Location.Context()
// Select the correct block reward based on chain progression
blockReward := misc.CalculateReward(header)

coinbase, err := header.Coinbase().InternalAddress()
if err != nil {
log.Error("Block has out of scope coinbase, skipping block reward", "Address", header.Coinbase().String(), "Hash", header.Hash().String())
logger.Error("Block has out of scope coinbase, skipping block reward", "Address", header.Coinbase().String(), "Hash", header.Hash().String())
return
}

Expand All @@ -492,7 +492,7 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
for _, uncle := range uncles {
coinbase, err := uncle.Coinbase().InternalAddress()
if err != nil {
log.Error("Found uncle with out of scope coinbase, skipping reward", "Address", uncle.Coinbase().String(), "Hash", uncle.Hash().String())
logger.Error("Found uncle with out of scope coinbase, skipping reward", "Address", uncle.Coinbase().String(), "Hash", uncle.Hash().String())
continue
}
r.Add(uncle.Number(nodeCtx), big8)
Expand Down
Loading

0 comments on commit e0a573c

Please sign in to comment.