From a8ebf501e54406c7449f71dba0c9af696bbc4e21 Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Mon, 13 Aug 2018 15:13:41 +0800 Subject: core: Sign block in Consensus.PrepareBlock. (#50) --- core/consensus.go | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'core/consensus.go') diff --git a/core/consensus.go b/core/consensus.go index b1b1973..c8b10a0 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -25,8 +25,13 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/blockdb" "github.com/dexon-foundation/dexon-consensus-core/common" "github.com/dexon-foundation/dexon-consensus-core/core/types" + "github.com/dexon-foundation/dexon-consensus-core/crypto" ) +// SigToPubFn is a function to recover public key from signature. +type SigToPubFn func(hash common.Hash, signature crypto.Signature) ( + crypto.PublicKey, error) + // ErrMissingBlockInfo would be reported if some information is missing when // calling PrepareBlock. It implements error interface. type ErrMissingBlockInfo struct { @@ -39,7 +44,8 @@ func (e *ErrMissingBlockInfo) Error() string { // Errors for sanity check error. var ( - ErrIncorrectHash = fmt.Errorf("hash of block is incorrect") + ErrIncorrectHash = fmt.Errorf("hash of block is incorrect") + ErrIncorrectSignature = fmt.Errorf("signature of block is incorrect") ) // Consensus implements DEXON Consensus algorithm. @@ -50,6 +56,8 @@ type Consensus struct { toModule *totalOrdering ctModule *consensusTimestamp db blockdb.BlockDatabase + prvKey crypto.PrivateKey + sigToPub SigToPubFn lock sync.RWMutex } @@ -57,7 +65,9 @@ type Consensus struct { func NewConsensus( app Application, gov Governance, - db blockdb.BlockDatabase) *Consensus { + db blockdb.BlockDatabase, + prv crypto.PrivateKey, + sigToPub SigToPubFn) *Consensus { validatorSet := gov.GetValidatorSet() // Setup acking by information returned from Governace. @@ -79,6 +89,8 @@ func NewConsensus( app: app, gov: gov, db: db, + prvKey: prv, + sigToPub: sigToPub, } } @@ -91,6 +103,18 @@ func (con *Consensus) sanityCheck(blockConv types.BlockConverter) (err error) { return ErrIncorrectHash } + /* Disable these check before the implmentation of the signature for + * genesis block is finished. + // Check the signer. + pubKey, err := con.sigToPub(b.Hash, b.Signature) + if err != nil { + return err + } + if !b.ProposerID.Equal(crypto.Keccak256Hash(pubKey.Bytes())) { + return ErrIncorrectSignature + } + */ + return nil } @@ -166,6 +190,13 @@ func (con *Consensus) PrepareBlock(blockConv types.BlockConverter, con.rbModule.prepareBlock(b) b.Timestamps[b.ProposerID] = proposeTime b.Hash, err = hashBlock(b) + if err != nil { + return + } + b.Signature, err = con.prvKey.Sign(b.Hash) + if err != nil { + return + } blockConv.SetBlock(b) return } -- cgit v1.2.3