Skip to content

Commit

Permalink
Merge branch 'dev-testing' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
yilunzhang authored Sep 7, 2018
2 parents a7e8f4d + 8f3bf68 commit 0002a5d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions consensus/ising/proposerservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ func (ps *ProposerService) ChangeProposerRoutine() {
}

func (ps *ProposerService) BlockSyncingFinished(v interface{}) {
log.Infof("process blocks, from height: %d, to height: %d, consensus height: %d, start height: %d",
ps.syncCache.startHeight, ps.syncCache.nextHeight-1,
ps.syncCache.consensusHeight, ps.syncCache.consensusHeight+1)
for i := ps.syncCache.startHeight; i < ps.syncCache.nextHeight; i++ {
if i > ps.syncCache.consensusHeight {
vBlock, err := ps.syncCache.GetBlockFromSyncCache(i)
Expand Down
15 changes: 15 additions & 0 deletions consensus/ising/synccache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

. "github.com/nknorg/nkn/common"
"github.com/nknorg/nkn/core/ledger"
"github.com/nknorg/nkn/util/log"
"github.com/syndtr/goleveldb/leveldb/errors"
)

Expand Down Expand Up @@ -122,9 +123,13 @@ func (sc *SyncCache) AddBlockToSyncCache(block *ledger.Block) error {
}
// analyse cached votes when add new block
if voteInfo, ok := sc.voteCache[blockHeight]; ok {
log.Infof("AddBlockToSyncCache: receive block: %s, %d has voted first",
BytesToHexString(hash.ToArrayReverse()), len(sc.voteCache[blockHeight]))
for _, h := range voteInfo {
if hash.CompareTo(h) == 0 {
blockInfo.votes++
log.Infof("append vote for block: %s, totally got %d votes: %d",
BytesToHexString(hash.ToArrayReverse()), blockInfo.votes)
if 2*blockInfo.votes > len(sc.voteCache[blockHeight]) {
if _, ok := sc.blockCache[blockHeight]; !ok {
sc.blockCache[blockHeight] = &BlockWithVotes{}
Expand All @@ -133,6 +138,9 @@ func (sc *SyncCache) AddBlockToSyncCache(block *ledger.Block) error {
}
}
}
} else {
log.Infof("AddBlockToSyncCache: receive block: %s, have not received vote for it",
BytesToHexString(hash.ToArrayReverse()))
}

// cache new block
Expand Down Expand Up @@ -187,14 +195,21 @@ func (sc *SyncCache) AddVoteForBlock(hash Uint256, height uint32, voter uint64)
if _, ok := sc.voteCache[height]; !ok {
sc.voteCache[height] = make(map[uint64]Uint256)
}
log.Infof("AddVoteForBlock: receive vote for block: %s, height: %d, voter: %d",
BytesToHexString(hash.ToArrayReverse()), height, voter)
sc.voteCache[height][voter] = hash

if blockInfo, ok := sc.BlockInSyncCache(hash, height); ok {
// if voted block existed in cache then increase vote
blockInfo.votes++
log.Infof("AddVoteForBlock: block: %s already exist, block got %d votes, votes for height %d: %d",
BytesToHexString(hash.ToArrayReverse()), blockInfo.votes, height, len(sc.voteCache[height]))
if 2*blockInfo.votes > len(sc.voteCache[height]) {
sc.blockCache[height].bestBlock = blockInfo
}
} else {
log.Infof("AddVoteForBlock: block: %s doesn't exist, cache vote only",
BytesToHexString(hash.ToArrayReverse()))
}

return nil
Expand Down

0 comments on commit 0002a5d

Please sign in to comment.