From c143aabb0c9fcd354918cc1aeabbf2ee3163c36e Mon Sep 17 00:00:00 2001 From: oscar Date: Wed, 29 Aug 2018 14:34:21 +0800 Subject: [PATCH 1/2] add more logs to synccache for debugging Signed-off-by: oscar --- consensus/ising/proposerservice.go | 3 +++ consensus/ising/synccache.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/consensus/ising/proposerservice.go b/consensus/ising/proposerservice.go index 62b6db20b..75aed136c 100644 --- a/consensus/ising/proposerservice.go +++ b/consensus/ising/proposerservice.go @@ -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) diff --git a/consensus/ising/synccache.go b/consensus/ising/synccache.go index 14b14b230..5255dedab 100644 --- a/consensus/ising/synccache.go +++ b/consensus/ising/synccache.go @@ -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" ) @@ -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{} @@ -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 @@ -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 From 8f3bf6873dc2a41f84559258653298e0ce8772e0 Mon Sep 17 00:00:00 2001 From: Yilun Date: Fri, 7 Sep 2018 09:53:58 -0700 Subject: [PATCH 2/2] Fix nil pointer error in FindSuccessors Signed-off-by: Yilun --- net/chord/net.go | 9 ++++++--- net/chord/transport.go | 7 +++++-- net/chord/vnode.go | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/net/chord/net.go b/net/chord/net.go index 4edf1ff6d..ceec942d3 100644 --- a/net/chord/net.go +++ b/net/chord/net.go @@ -119,14 +119,17 @@ func InitTCPTransport(listen string, timeout time.Duration) (*TCPTransport, erro // Checks for a local vnode func (t *TCPTransport) get(vn *Vnode) (VnodeRPC, bool) { + if vn == nil { + return nil, false + } key := vn.String() t.lock.RLock() defer t.lock.RUnlock() w, ok := t.local[key] - if ok { + if ok && w != nil && w.obj != nil { return w.obj, ok } else { - return nil, ok + return nil, false } } @@ -768,7 +771,7 @@ func (t *TCPTransport) handleConn(conn *net.TCPConn) { obj, ok := t.get(body.Target) resp := tcpBodyVnodeListError{} sendResp = &resp - if ok { + if ok && obj != nil { nodes, err := obj.FindSuccessors(body.Num, body.Key) resp.Vnodes = trimSlice(nodes) resp.Err = err diff --git a/net/chord/transport.go b/net/chord/transport.go index ecafa2f76..40364f414 100644 --- a/net/chord/transport.go +++ b/net/chord/transport.go @@ -34,14 +34,17 @@ func InitLocalTransport(remote Transport) Transport { // Checks for a local vnode func (lt *LocalTransport) get(vn *Vnode) (VnodeRPC, bool) { + if vn == nil { + return nil, false + } key := vn.String() lt.lock.RLock() defer lt.lock.RUnlock() w, ok := lt.local[key] - if ok { + if ok && w != nil && w.obj != nil { return w.obj, ok } else { - return nil, ok + return nil, false } } diff --git a/net/chord/vnode.go b/net/chord/vnode.go index f8ccfac9b..6da2371db 100644 --- a/net/chord/vnode.go +++ b/net/chord/vnode.go @@ -315,7 +315,7 @@ func (vn *localVnode) checkPredecessor() error { // Finds next N successors. N must be <= NumSuccessors func (vn *localVnode) FindSuccessors(n int, key []byte) ([]*Vnode, error) { // Check if we are the immediate predecessor - if betweenRightIncl(vn.Id, vn.successors[0].Id, key) { + if len(vn.successors) > 0 && vn.successors[0] != nil && betweenRightIncl(vn.Id, vn.successors[0].Id, key) { return vn.successors[:n], nil } @@ -343,7 +343,7 @@ func (vn *localVnode) FindSuccessors(n int, key []byte) ([]*Vnode, error) { // Check if the ID is between us and any non-immediate successors for i := 1; i <= successors-n; i++ { - if betweenRightIncl(vn.Id, vn.successors[i].Id, key) { + if vn.successors[i] != nil && betweenRightIncl(vn.Id, vn.successors[i].Id, key) { remain := vn.successors[i:] if len(remain) > n { remain = remain[:n]