From 15706b3b9eee46359fa82f091b66ecb90822bf3c Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Fri, 22 Mar 2019 19:23:47 -0700 Subject: [PATCH] Do not update staking list if StakeInfo query failed Previously, CurrentStakes were getting cleared if stakeInfoReturnValue == nil, which means that the stake information query has failed. We still process non-nil but empty stake query result. --- node/staking.go | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/node/staking.go b/node/staking.go index 9ee213175..2dba4772b 100644 --- a/node/staking.go +++ b/node/staking.go @@ -26,28 +26,29 @@ const ( lockPeriodInEpochs = 3 // This should be in sync with contracts/StakeLockContract.sol ) -// UpdateStakingList updates staking information by querying the staking smart contract. +// UpdateStakingList updates staking list from the given StakeInfo query result. func (node *Node) UpdateStakingList(stakeInfoReturnValue *structs.StakeInfoReturnValue) { + if stakeInfoReturnValue == nil { + return + } node.CurrentStakes = make(map[common.Address]*structs.StakeInfo) - if stakeInfoReturnValue != nil { - for i, addr := range stakeInfoReturnValue.LockedAddresses { - blockNum := stakeInfoReturnValue.BlockNums[i] - lockPeriodCount := stakeInfoReturnValue.LockPeriodCounts[i] + for i, addr := range stakeInfoReturnValue.LockedAddresses { + blockNum := stakeInfoReturnValue.BlockNums[i] + lockPeriodCount := stakeInfoReturnValue.LockPeriodCounts[i] - startEpoch := core.GetEpochFromBlockNumber(blockNum.Uint64()) - curEpoch := core.GetEpochFromBlockNumber(node.blockchain.CurrentBlock().NumberU64()) + startEpoch := core.GetEpochFromBlockNumber(blockNum.Uint64()) + curEpoch := core.GetEpochFromBlockNumber(node.blockchain.CurrentBlock().NumberU64()) - if startEpoch == curEpoch { - continue // The token are counted into stakes at the beginning of next epoch. - } - // True if the token is still staked within the locking period. - if curEpoch-startEpoch <= lockPeriodCount.Uint64()*lockPeriodInEpochs { - node.CurrentStakes[addr] = &structs.StakeInfo{ - stakeInfoReturnValue.BlsAddresses[i], - blockNum, - lockPeriodCount, - stakeInfoReturnValue.Amounts[i], - } + if startEpoch == curEpoch { + continue // The token are counted into stakes at the beginning of next epoch. + } + // True if the token is still staked within the locking period. + if curEpoch-startEpoch <= lockPeriodCount.Uint64()*lockPeriodInEpochs { + node.CurrentStakes[addr] = &structs.StakeInfo{ + stakeInfoReturnValue.BlsAddresses[i], + blockNum, + lockPeriodCount, + stakeInfoReturnValue.Amounts[i], } } }