From 3aba8f4c26c7351cb435c3e5fc7c7d7246c2a895 Mon Sep 17 00:00:00 2001 From: Nguyen Sy Thanh Son Date: Mon, 7 Jan 2019 07:22:41 +0000 Subject: [PATCH] fix unit test --- consensus/posv/posv.go | 2 +- contracts/utils.go | 17 ++++++++++++----- contracts/validator/validator_test.go | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/consensus/posv/posv.go b/consensus/posv/posv.go index cff6bf6c9c..d981050555 100644 --- a/consensus/posv/posv.go +++ b/consensus/posv/posv.go @@ -903,7 +903,7 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state } if c.HookReward != nil && number%rCheckpoint == 0 { - if !c.EnableCache && c.BlockSigners.Len() >= 2700 { + if !c.EnableCache && uint64(c.BlockSigners.Len()) >= (rCheckpoint*3) { fmt.Println("EnableCache true c.BlockSigners.Len()", c.BlockSigners.Len()) c.EnableCache = true } diff --git a/contracts/utils.go b/contracts/utils.go index a91f6c2fc1..991f71bd6a 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -350,7 +350,7 @@ func GetRewardForCheckpoint(c *posv.Posv, chain consensus.ChainReader, blockSign } else { var wg sync.WaitGroup squeue := make(chan []common.Address, 1) - wg.Add(900) + wg.Add(int(rCheckpoint)) for i := startBlockNumber; i <= endBlockNumber; i++ { go func(i uint64) { @@ -484,15 +484,22 @@ func GetRewardBalancesRate(c *posv.Posv, foudationWalletAddr common.Address, mas vote.Masternode = masterAddr vote.Voter = voteAddr - if vCap, ok := c.Votes.Get(vote); ok { - voterCap = vCap.(*big.Int) + if c != nil { + if vCap, ok := c.Votes.Get(vote); ok { + voterCap = vCap.(*big.Int) + } else { + voterCap, err = validator.GetVoterCap(opts, masterAddr, voteAddr) + if err != nil { + log.Crit("Fail to get vote capacity", "error", err) + } + fmt.Println("Add to Votes cache", vote.Masternode.String(), vote.Voter.String(), voterCap.String()) + c.Votes.Add(vote, voterCap) + } } else { voterCap, err = validator.GetVoterCap(opts, masterAddr, voteAddr) if err != nil { log.Crit("Fail to get vote capacity", "error", err) } - fmt.Println("Add to Votes cache", vote.Masternode.String(), vote.Voter.String(), voterCap.String()) - c.Votes.Add(vote, voterCap) } totalCap.Add(totalCap, voterCap) diff --git a/contracts/validator/validator_test.go b/contracts/validator/validator_test.go index f3896db4b8..e5df78d42f 100644 --- a/contracts/validator/validator_test.go +++ b/contracts/validator/validator_test.go @@ -144,7 +144,7 @@ func TestRewardBalance(t *testing.T) { foundationAddr := common.HexToAddress(common.FoudationAddr) totalReward := new(big.Int).SetInt64(15 * 1000) - rewards, err := contracts.GetRewardBalancesRate(foundationAddr, acc3Addr, totalReward, baseValidator) + rewards, err := contracts.GetRewardBalancesRate(nil, foundationAddr, acc3Addr, totalReward, baseValidator) if err != nil { t.Error("Fail to get reward balances rate.", err) }