From f7e2219e11c2f9aa52db4d7f4281f42fc12be39f Mon Sep 17 00:00:00 2001 From: muscle_boy Date: Wed, 29 May 2019 18:24:08 +0800 Subject: [PATCH] fix dpos (#96) * fix dpos * modify func name --- protocol/consensus_node_manager.go | 36 ++++++++++++++++++------------------ protocol/state/blockindex.go | 32 ++++++++++++++------------------ 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/protocol/consensus_node_manager.go b/protocol/consensus_node_manager.go index 33cc83c9..7667e46d 100644 --- a/protocol/consensus_node_manager.go +++ b/protocol/consensus_node_manager.go @@ -144,8 +144,8 @@ func getLastBlockTimeInTimeRange(startTimestamp, endTimestamp, order uint64) uin func (c *consensusNodeManager) getPrevRoundVoteLastBlock(blockNode *state.BlockNode) (*state.BlockNode, error) { prevVoteRoundLastBlockHeight := blockNode.Height/roundVoteBlockNums*roundVoteBlockNums - 1 - lastBlockNode := c.blockIndex.NodeByHeightInSameChain(&blockNode.Hash, prevVoteRoundLastBlockHeight) - if blockNode == nil { + lastBlockNode := blockNode.GetParent(prevVoteRoundLastBlockHeight) + if lastBlockNode == nil { return nil, errNotFoundBlockNode } return lastBlockNode, nil @@ -208,36 +208,36 @@ func (c *consensusNodeManager) reorganizeVoteResult(voteResult *state.VoteResult } } - var attachBlocks []*types.Block - var detachBlocks []*types.Block + var attachNodes []*state.BlockNode + var detachNodes []*state.BlockNode for forkChainNode.Hash != mainChainNode.Hash && forkChainNode.Height >= (voteResult.Seq-1)*roundVoteBlockNums { - attachBlock, err := c.store.GetBlock(&forkChainNode.Hash) - if err != nil { - return err - } - - attachBlocks = append([]*types.Block{attachBlock}, attachBlocks...) + attachNodes = append([]*state.BlockNode{forkChainNode}, attachNodes...) forkChainNode = forkChainNode.Parent if mainChainNode != nil && forkChainNode.Height == mainChainNode.Height { - detachBlock, err := c.store.GetBlock(&mainChainNode.Hash) - if err != nil { - return err - } - - detachBlocks = append(detachBlocks, detachBlock) + detachNodes = append(detachNodes, mainChainNode) mainChainNode = mainChainNode.Parent } } - for _, block := range detachBlocks { + for _, node := range detachNodes { + block, err := c.store.GetBlock(&node.Hash) + if err != nil { + return err + } + if err := c.detachBlock(map[uint64]*state.VoteResult{voteResult.Seq: voteResult}, block); err != nil { return err } } - for _, block := range attachBlocks { + for _, node := range attachNodes { + block, err := c.store.GetBlock(&node.Hash) + if err != nil { + return err + } + if err := c.applyBlock(map[uint64]*state.VoteResult{voteResult.Seq: voteResult}, block); err != nil { return err } diff --git a/protocol/state/blockindex.go b/protocol/state/blockindex.go index 3bccd841..c2e39637 100644 --- a/protocol/state/blockindex.go +++ b/protocol/state/blockindex.go @@ -85,6 +85,20 @@ func (node *BlockNode) CalcPastMedianTime() uint64 { return timestamps[len(timestamps)/2] } +// GetParent return the node of specified height +// And the node satisfies the same chain as current node +// Height of current node must greater than height parameter +func (node *BlockNode) GetParent(height uint64) *BlockNode { + prevBlockNode := node + for prevBlockNode != nil && prevBlockNode.Height != height { + if prevBlockNode.Height < height { + return nil + } + prevBlockNode = prevBlockNode.Parent + } + return prevBlockNode +} + // BlockIndex is the struct for help chain trace block chain as tree type BlockIndex struct { sync.RWMutex @@ -118,24 +132,6 @@ func (bi *BlockIndex) GetNode(hash *bc.Hash) *BlockNode { return bi.index[*hash] } -// NodeByHeightInSameChain return the node of specified height -// And the node satisfies the same chain as the node that specifies the hash -// Height of the block of the specified node hash must greater than height parameter -func (bi *BlockIndex) NodeByHeightInSameChain(nodeHash *bc.Hash, height uint64) *BlockNode { - bi.RLock() - defer bi.RUnlock() - - blockNode := bi.index[*nodeHash] - prevBlockNode := blockNode - for prevBlockNode != nil && prevBlockNode.Height != height { - if prevBlockNode.Height < height { - return nil - } - prevBlockNode = bi.index[prevBlockNode.Parent.Hash] - } - return prevBlockNode -} - func (bi *BlockIndex) BestNode() *BlockNode { bi.RLock() defer bi.RUnlock() -- 2.11.0