From 9266273b790d4fa8c68e2d0fce290fe58923187b Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Thu, 16 Aug 2018 12:02:51 +0800 Subject: Verify consensus info and the acking's signature (#63) --- core/consensus.go | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'core') diff --git a/core/consensus.go b/core/consensus.go index 36e54ec..d5ce923 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -44,9 +44,14 @@ func (e *ErrMissingBlockInfo) Error() string { // Errors for consensus core. var ( - ErrIncorrectHash = fmt.Errorf("hash of block is incorrect") - ErrIncorrectSignature = fmt.Errorf("signature of block is incorrect") - ErrGenesisBlockNotEmpty = fmt.Errorf("genesis block should be empty") + ErrIncorrectHash = fmt.Errorf( + "hash of block is incorrect") + ErrIncorrectSignature = fmt.Errorf( + "signature of block is incorrect") + ErrIncorrectCompactionChainAck = fmt.Errorf( + "compaction chain ack of block is incorrect") + ErrGenesisBlockNotEmpty = fmt.Errorf( + "genesis block should be empty") ) // Consensus implements DEXON Consensus algorithm. @@ -115,11 +120,32 @@ func (con *Consensus) sanityCheck(blockConv types.BlockConverter) (err error) { return ErrIncorrectSignature } + // Check the compaction chain info. + if ackingBlockHash := + b.CompactionChainAck.AckingBlockHash; (ackingBlockHash != common.Hash{}) { + ackingBlock, err := con.db.Get(ackingBlockHash) + if err != nil { + return err + } + hash, err := hashConsensusInfo(&ackingBlock) + if err != nil { + return err + } + pubKey, err := con.sigToPub(hash, + b.CompactionChainAck.ConsensusInfoSignature) + if err != nil { + return err + } + if !b.ProposerID.Equal(crypto.Keccak256Hash(pubKey.Bytes())) { + return ErrIncorrectCompactionChainAck + } + } return nil } // ProcessBlock is the entry point to submit one block to a Consensus instance. func (con *Consensus) ProcessBlock(blockConv types.BlockConverter) (err error) { + // TODO(jimmy-dexon): BlockConverter.Block() is called twice in this method. if err := con.sanityCheck(blockConv); err != nil { return err } -- cgit