diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-03-10 11:58:38 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:57 +0800 |
commit | eb7487b9318954404895fbe0d3a0acaabe3558a0 (patch) | |
tree | b27df91774d30e995539cd5141f5c7a94db14b27 /vendor/github.com/dexon-foundation/dexon-consensus | |
parent | 057357dc977b47ec31a91636aa11aac341f97ae9 (diff) | |
download | dexon-eb7487b9318954404895fbe0d3a0acaabe3558a0.tar dexon-eb7487b9318954404895fbe0d3a0acaabe3558a0.tar.gz dexon-eb7487b9318954404895fbe0d3a0acaabe3558a0.tar.bz2 dexon-eb7487b9318954404895fbe0d3a0acaabe3558a0.tar.lz dexon-eb7487b9318954404895fbe0d3a0acaabe3558a0.tar.xz dexon-eb7487b9318954404895fbe0d3a0acaabe3558a0.tar.zst dexon-eb7487b9318954404895fbe0d3a0acaabe3558a0.zip |
vendor: sync to latest core (#237)
Diffstat (limited to 'vendor/github.com/dexon-foundation/dexon-consensus')
5 files changed, 75 insertions, 13 deletions
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/blockchain.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/blockchain.go index 30895f8ec..aacb65de2 100644 --- a/vendor/github.com/dexon-foundation/dexon-consensus/core/blockchain.go +++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/blockchain.go @@ -63,6 +63,13 @@ func (pb *pendingBlockRecords) insert(p pendingBlockRecord) error { *pb = append(*pb, p) default: if (*pb)[idx].position.Equal(p.position) { + // Allow to overwrite pending block record for empty blocks, we may + // need to pull that block from others when its parent is not found + // locally. + if (*pb)[idx].block == nil && p.block != nil { + (*pb)[idx].block = p.block + return nil + } return ErrDuplicatedPendingBlock } // Insert the value to that index. @@ -275,8 +282,7 @@ func (bc *blockChain) addEmptyBlock(position types.Position) ( } else if position.Height == 0 && position.Round == 0 { return add(), nil } - bc.addPendingBlockRecord(pendingBlockRecord{position, nil}) - return nil, nil + return nil, bc.addPendingBlockRecord(pendingBlockRecord{position, nil}) } // addBlock should be called when the block is confirmed by BA, we won't perform @@ -298,11 +304,10 @@ func (bc *blockChain) addBlock(b *types.Block) error { confirmed = true } if !confirmed { - bc.addPendingBlockRecord(pendingBlockRecord{b.Position, b}) - } else { - bc.confirmBlock(b) - bc.checkIfBlocksConfirmed() + return bc.addPendingBlockRecord(pendingBlockRecord{b.Position, b}) } + bc.confirmBlock(b) + bc.checkIfBlocksConfirmed() return nil } @@ -449,18 +454,19 @@ func (bc *blockChain) findPendingBlock(p types.Position) *types.Block { return pendingRec.block } -func (bc *blockChain) addPendingBlockRecord(p pendingBlockRecord) { +func (bc *blockChain) addPendingBlockRecord(p pendingBlockRecord) error { if err := bc.pendingBlocks.insert(p); err != nil { if err == ErrDuplicatedPendingBlock { - // TODO(mission): panic directly once our BA can confirm blocks - // uniquely and in sequence. - return + // TODO(mission): stop ignoreing this error once our BA can confirm + // blocks uniquely and sequentially. + err = nil } - panic(err) + return err } if p.block != nil { bc.setRandomnessFromPending(p.block) } + return nil } func (bc *blockChain) checkIfBlocksConfirmed() { diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/consensus.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/consensus.go index 0f4daa766..050bfe7e5 100644 --- a/vendor/github.com/dexon-foundation/dexon-consensus/core/consensus.go +++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/consensus.go @@ -127,7 +127,15 @@ func (recv *consensusBAReceiver) ConfirmBlock( return } if block == nil { - panic(fmt.Errorf("empty block should be proposed directly: %s", aID)) + // The empty block's parent is not found locally, thus we can't + // propose it at this moment. + // + // We can only rely on block pulling upon receiving + // types.AgreementResult from the next position. + recv.consensus.logger.Warn( + "An empty block is confirmed without its parent", + "position", aID) + return } } else { var exist bool @@ -1075,6 +1083,14 @@ func (con *Consensus) ProcessBlockRandomnessResult( // preProcessBlock performs Byzantine Agreement on the block. func (con *Consensus) preProcessBlock(b *types.Block) (err error) { + var exist bool + exist, err = con.nodeSetCache.Exists(b.Position.Round, b.ProposerID) + if err != nil { + return + } + if !exist { + return ErrProposerNotInNodeSet + } err = con.baMgr.processBlock(b) if err == nil && con.debugApp != nil { con.debugApp.BlockReceived(b.Hash) diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/dkg-tsig-protocol.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/dkg-tsig-protocol.go index 4f15a74ac..45fe822e1 100644 --- a/vendor/github.com/dexon-foundation/dexon-consensus/core/dkg-tsig-protocol.go +++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/dkg-tsig-protocol.go @@ -55,6 +55,12 @@ var ( "cache of round already been purged") ErrTSigNotReady = fmt.Errorf( "tsig not ready") + ErrSelfMPKNotRegister = fmt.Errorf( + "self mpk not registered") + ErrUnableGetSelfPrvShare = fmt.Errorf( + "unable to get self DKG PrivateShare") + ErrSelfPrvShareMismatch = fmt.Errorf( + "self privateShare does not match mpk registered") ) type dkgReceiver interface { @@ -186,6 +192,9 @@ func (d *dkgProtocol) processMasterPublicKeys( ids[i] = mpks[i].DKGID } d.masterPrivateShare.SetParticipants(ids) + if err = d.verifySelfPrvShare(); err != nil { + return + } for _, mpk := range mpks { share, ok := d.masterPrivateShare.Share(mpk.DKGID) if !ok { @@ -201,6 +210,26 @@ func (d *dkgProtocol) processMasterPublicKeys( return } +func (d *dkgProtocol) verifySelfPrvShare() error { + selfMPK, exist := d.mpkMap[d.ID] + if !exist { + return ErrSelfMPKNotRegister + } + share, ok := d.masterPrivateShare.Share(d.idMap[d.ID]) + if !ok { + return ErrUnableGetSelfPrvShare + } + ok, err := selfMPK.VerifyPrvShare( + d.idMap[d.ID], share) + if err != nil { + return err + } + if !ok { + return ErrSelfPrvShareMismatch + } + return nil +} + func (d *dkgProtocol) proposeNackComplaints() { for nID := range d.mpkMap { if _, exist := d.prvSharesReceived[nID]; exist { @@ -218,6 +247,9 @@ func (d *dkgProtocol) proposeNackComplaints() { func (d *dkgProtocol) processNackComplaints(complaints []*typesDKG.Complaint) ( err error) { + if err = d.verifySelfPrvShare(); err != nil { + return + } for _, complaint := range complaints { if !complaint.IsNack() { continue diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/syncer/consensus.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/syncer/consensus.go index 7ba659f27..305339687 100644 --- a/vendor/github.com/dexon-foundation/dexon-consensus/core/syncer/consensus.go +++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/syncer/consensus.go @@ -85,6 +85,7 @@ type Consensus struct { dummyCancel context.CancelFunc dummyFinished <-chan struct{} dummyMsgBuffer []interface{} + initChainTipHeight uint64 } // NewConsensus creates an instance for Consensus (syncer consensus). @@ -116,6 +117,7 @@ func NewConsensus( randomnessResults: make(map[common.Hash]*types.BlockRandomnessResult), } con.ctx, con.ctxCancel = context.WithCancel(context.Background()) + _, con.initChainTipHeight = db.GetCompactionChainTipInfo() con.agreementModule = newAgreement( con.receiveChan, con.pullChan, con.nodeSetCache, con.logger) con.agreementWaitGroup.Add(1) @@ -466,6 +468,12 @@ func (con *Consensus) startNetwork() { switch v := val.(type) { case *types.Block: case *types.AgreementResult: + // Avoid byzantine nodes attack by broadcasting older + // agreement results. Normal nodes might report 'synced' + // while still fall behind other nodes. + if v.Position.Height <= con.initChainTipHeight { + continue loop + } case *types.BlockRandomnessResult: con.cacheRandomnessResult(v) continue loop diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go index 203f57fc2..220240c5a 100644 --- a/vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go +++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go @@ -98,7 +98,7 @@ func VerifyDKGComplaint( if err != nil { return false, err } - return ok, nil + return !ok, nil } // LaunchDummyReceiver launches a go routine to receive from the receive |