OSDN Git Service

modify format
authormars <mars@bytom.io>
Thu, 18 Jul 2019 11:43:24 +0000 (19:43 +0800)
committermars <mars@bytom.io>
Thu, 18 Jul 2019 11:43:24 +0000 (19:43 +0800)
toolbar/reward/reward.go
toolbar/reward/reward/vote_reward.go
toolbar/reward/synchron/block_keeper.go

index 15bc258..c0b8098 100644 (file)
@@ -20,8 +20,8 @@ type Reward struct {
        cfg                *config.Config
        db                 *gorm.DB
        countReward        CountReward
-       VoteInfoCh         chan instance.VoteInfo
-       OverReadCh         chan struct{}
+       voteInfoCh         chan instance.VoteInfo
+       overReadCh         chan struct{}
        period             uint64
        roundVoteBlockNums uint64
 }
@@ -44,8 +44,8 @@ func NewReward(db *gorm.DB, cfg *config.Config, period uint64, quit chan struct{
                cfg:                cfg,
                db:                 db,
                countReward:        countReward,
-               VoteInfoCh:         voteInfoCh,
-               OverReadCh:         overReadCh,
+               voteInfoCh:         voteInfoCh,
+               overReadCh:         overReadCh,
                period:             period,
                roundVoteBlockNums: consensus.ActiveNetParams.DPOSConfig.RoundVoteBlockNums,
        }
@@ -99,7 +99,7 @@ func (r *Reward) readVoteInfo() error {
                        voteBlockNum = r.roundVoteBlockNums*r.period - voteHeight
                }
 
-               r.VoteInfoCh <- instance.VoteInfo{
+               r.voteInfoCh <- instance.VoteInfo{
                        XPub:         xpub,
                        Address:      address,
                        VoteNum:      voteNum,
@@ -109,11 +109,11 @@ func (r *Reward) readVoteInfo() error {
                }
        }
 
-       close(r.OverReadCh)
+       close(r.overReadCh)
        return nil
 }
 
 func (r *Reward) Start() {
        go r.readVoteInfo()
-       r.countReward.Start()
+       go r.countReward.Start()
 }
index 8314c68..b880904 100644 (file)
@@ -18,8 +18,8 @@ type voterReward struct {
 }
 
 type voteResult struct {
-       Votes     map[string]*big.Int
-       VoteTotal *big.Int
+       votes     map[string]*big.Int
+       voteTotal *big.Int
 }
 
 type coinBaseReward struct {
@@ -123,23 +123,23 @@ out:
                        bigVoteNum.Mul(bigVoteNum, bigBlockNum)
 
                        if value, ok := v.voteResults[voteInfo.XPub]; ok {
-                               if vote, ok := value.Votes[voteInfo.Address]; ok {
+                               if vote, ok := value.votes[voteInfo.Address]; ok {
                                        vote.Add(vote, bigVoteNum)
                                } else {
-                                       value.Votes[voteInfo.Address] = bigVoteNum
+                                       value.votes[voteInfo.Address] = bigVoteNum
                                }
                        } else {
                                voteResult := &voteResult{
-                                       Votes:     make(map[string]*big.Int),
-                                       VoteTotal: big.NewInt(0),
+                                       votes:     make(map[string]*big.Int),
+                                       voteTotal: big.NewInt(0),
                                }
 
-                               voteResult.Votes[voteInfo.Address] = bigVoteNum
+                               voteResult.votes[voteInfo.Address] = bigVoteNum
                                v.voteResults[voteInfo.XPub] = voteResult
                        }
-                       voteTotal := v.voteResults[voteInfo.XPub].VoteTotal
+                       voteTotal := v.voteResults[voteInfo.XPub].voteTotal
                        voteTotal.Add(voteTotal, bigVoteNum)
-                       v.voteResults[voteInfo.XPub].VoteTotal = voteTotal
+                       v.voteResults[voteInfo.XPub].voteTotal = voteTotal
                case <-v.overReadCH:
                        break out
                }
@@ -154,11 +154,11 @@ func (v *Vote) countReward() {
                        continue
                }
 
-               for address, vote := range votes.Votes {
+               for address, vote := range votes.votes {
                        if value, ok := v.voterRewards[xpub]; ok {
                                mul := vote.Mul(vote, coinBaseReward.voteTotalReward)
                                amount := big.NewInt(0)
-                               amount.Div(mul, votes.VoteTotal)
+                               amount.Div(mul, votes.voteTotal)
 
                                value.rewards[address] = amount
                        } else {
@@ -168,7 +168,7 @@ func (v *Vote) countReward() {
 
                                mul := vote.Mul(vote, coinBaseReward.voteTotalReward)
                                amount := big.NewInt(0)
-                               amount.Div(mul, votes.VoteTotal)
+                               amount.Div(mul, votes.voteTotal)
                                if amount.Uint64() > 0 {
                                        reward.rewards[address] = amount
                                        v.voterRewards[xpub] = reward
index 2c990fe..5a5505c 100644 (file)
@@ -207,7 +207,17 @@ func (c *ChainKeeper) DetachBlock(block *types.Block, txStatus *bc.TransactionSt
                return err
        }
 
-       if err := c.updateBlockState(ormDB, block); err != nil {
+       preBlockStr, _, err := c.node.GetBlockByHeight(block.Height + 1)
+       if err != nil {
+               return err
+       }
+
+       preBlock := &types.Block{}
+       if err := preBlock.UnmarshalText([]byte(preBlockStr)); err != nil {
+               return errors.New("Unmarshal preBlock")
+       }
+
+       if err := c.updateBlockState(ormDB, preBlock); err != nil {
                return err
        }