Skip to content

Commit

Permalink
Calculated blockNumber in a separate go-routine for logging (#1025)
Browse files Browse the repository at this point in the history
* Cached block number in logger

* Revert "Cached block number in logger"

This reverts commit 760e3ad.

* Added block/block.go for calculating latestBlock in a seperate goroutine

* Fetched block number from block package

* log corrections
  • Loading branch information
Yashk767 authored Nov 18, 2022
1 parent 2449c03 commit 351a6a8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
34 changes: 34 additions & 0 deletions block/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package block

import (
"context"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/sirupsen/logrus"
"razor/core"
"time"
)

var latestBlock *types.Header

func GetLatestBlock() *types.Header {
return latestBlock
}

func SetLatestBlock(block *types.Header) {
latestBlock = block
}

func CalculateLatestBlock(client *ethclient.Client) {
for {
if client != nil {
latestHeader, err := client.HeaderByNumber(context.Background(), nil)
if err != nil {
logrus.Error("CalculateBlockNumber: Error in fetching block: ", err)
continue
}
SetLatestBlock(latestHeader)
}
time.Sleep(time.Second * time.Duration(core.BlockNumberInterval))
}
}
4 changes: 2 additions & 2 deletions cmd/addStake.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (*UtilsStruct) ExecuteStake(flagSet *pflag.FlagSet) {
Config: config,
}

log.Debugf("ExecuteStake: Calling Approve() with transaction arguments: %+v", txnArgs)
log.Debug("ExecuteStake: Calling Approve() for amount: ", txnArgs.Amount)
approveTxnHash, err := cmdUtils.Approve(txnArgs)
utils.CheckError("Approve error: ", err)

Expand All @@ -92,7 +92,7 @@ func (*UtilsStruct) ExecuteStake(flagSet *pflag.FlagSet) {
utils.CheckError("Error in WaitForBlockCompletion for approve: ", err)
}

log.Debugf("ExecuteStake: Calling StakeCoins() with transaction arguments: %+v", txnArgs)
log.Debug("ExecuteStake: Calling StakeCoins() for amount: ", txnArgs.Amount)
stakeTxnHash, err := cmdUtils.StakeCoins(txnArgs)
utils.CheckError("Stake error: ", err)

Expand Down
2 changes: 1 addition & 1 deletion cmd/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (*UtilsStruct) GetLocalMediansData(client *ethclient.Client, account types.
}
log.Debugf("GetLocalMediansData: Proposed data from file: %+v", proposedData)
if proposedData.Epoch != epoch {
log.Errorf("File %s doesn't contain latest median data: %v", fileName, err)
log.Errorf("File %s doesn't contain latest median data", fileName)
goto CalculateMedian
}
log.Debug("Updating global proposed data struct...")
Expand Down
3 changes: 3 additions & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ var DefaultWaitTime = 1
var DefaultGasLimit = 2
var DefaultRPCTimeout = 10
var DefaultLogLevel = ""

//BlockNumberInterval is the interval in seconds after which blockNumber needs to be calculated again
var BlockNumberInterval = 5
17 changes: 7 additions & 10 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package logger

import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/razor-network/goInfo"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"math/big"
"os"
"razor/block"
"razor/core"
"razor/path"
"runtime"
Expand Down Expand Up @@ -169,19 +168,17 @@ func (logger *StandardLogger) Fatalf(format string, args ...interface{}) {

func SetEpochAndBlockNumber(client *ethclient.Client) {
if client != nil {
latestHeader, err := client.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Error("Error in fetching block: ", err)
return
latestBlock := block.GetLatestBlock()
if latestBlock != nil {
BlockNumber = latestBlock.Number
epoch := latestBlock.Time / uint64(core.EpochLength)
Epoch = uint32(epoch)
}
BlockNumber = latestHeader.Number

epoch := latestHeader.Time / uint64(core.EpochLength)
Epoch = uint32(epoch)
}
}

func SetLoggerParameters(client *ethclient.Client, address string) {
Address = address
Client = client
go block.CalculateLatestBlock(client)
}

0 comments on commit 351a6a8

Please sign in to comment.