From daf3bab93c323b173345811adc9a334dad4a7094 Mon Sep 17 00:00:00 2001 From: haoping-ku Date: Thu, 29 Nov 2018 14:30:02 +0800 Subject: core: syncer: add syncer (#346) --- core/utils.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'core/utils.go') diff --git a/core/utils.go b/core/utils.go index 441aac1..bc5e336 100644 --- a/core/utils.go +++ b/core/utils.go @@ -167,6 +167,51 @@ func VerifyBlock(b *types.Block) (err error) { return } +// VerifyAgreementResult perform sanity check against a types.AgreementResult +// instance. +func VerifyAgreementResult( + res *types.AgreementResult, cache *utils.NodeSetCache) error { + notarySet, err := cache.GetNotarySet( + res.Position.Round, res.Position.ChainID) + if err != nil { + return err + } + if len(res.Votes) < len(notarySet)/3*2+1 { + return ErrNotEnoughVotes + } + if len(res.Votes) > len(notarySet) { + return ErrIncorrectVoteProposer + } + for _, vote := range res.Votes { + if res.IsEmptyBlock { + if (vote.BlockHash != common.Hash{}) { + return ErrIncorrectVoteBlockHash + } + } else { + if vote.BlockHash != res.BlockHash { + return ErrIncorrectVoteBlockHash + } + } + if vote.Type != types.VoteCom { + return ErrIncorrectVoteType + } + if vote.Position != res.Position { + return ErrIncorrectVotePosition + } + if _, exist := notarySet[vote.ProposerID]; !exist { + return ErrIncorrectVoteProposer + } + ok, err := verifyVoteSignature(&vote) + if err != nil { + return err + } + if !ok { + return ErrIncorrectVoteSignature + } + } + return nil +} + // DiffUint64 calculates difference between two uint64. func DiffUint64(a, b uint64) uint64 { if a > b { -- cgit