diff --git a/block/block.go b/block/block.go new file mode 100644 index 000000000..5169371a2 --- /dev/null +++ b/block/block.go @@ -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)) + } +} diff --git a/cmd/addStake.go b/cmd/addStake.go index e7c517ed6..4b02e5d3f 100644 --- a/cmd/addStake.go +++ b/cmd/addStake.go @@ -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) @@ -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) diff --git a/cmd/dispute.go b/cmd/dispute.go index f16fb4e46..f11b55f0a 100644 --- a/cmd/dispute.go +++ b/cmd/dispute.go @@ -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...") diff --git a/core/constants.go b/core/constants.go index d9cbf3fef..3abc6faa9 100644 --- a/core/constants.go +++ b/core/constants.go @@ -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 diff --git a/logger/logger.go b/logger/logger.go index eb5b46a64..374420091 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -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" @@ -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) }