diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/consensus.go | 35 | ||||
-rw-r--r-- | core/consensus_test.go | 5 | ||||
-rw-r--r-- | core/crypto.go | 9 | ||||
-rw-r--r-- | core/crypto_test.go | 2 | ||||
-rw-r--r-- | core/types/validator.go | 5 |
5 files changed, 43 insertions, 13 deletions
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 } diff --git a/core/consensus_test.go b/core/consensus_test.go index c26e7e8..522c566 100644 --- a/core/consensus_test.go +++ b/core/consensus_test.go @@ -26,6 +26,7 @@ import ( "github.com/dexon-foundation/dexon-consensus-core/common" "github.com/dexon-foundation/dexon-consensus-core/core/test" "github.com/dexon-foundation/dexon-consensus-core/core/types" + "github.com/dexon-foundation/dexon-consensus-core/crypto/eth" "github.com/stretchr/testify/suite" ) @@ -60,7 +61,9 @@ func (s *ConsensusTestSuite) prepareConsensus(gov *test.Governance) ( app := test.NewApp() db, err := blockdb.NewMemBackedBlockDB() s.Require().Nil(err) - con := NewConsensus(app, gov, db) + prv, err := eth.NewPrivateKey() + s.Require().Nil(err) + con := NewConsensus(app, gov, db, prv, eth.SigToPub) return app, con } diff --git a/core/crypto.go b/core/crypto.go index abfc8fb..564ad38 100644 --- a/core/crypto.go +++ b/core/crypto.go @@ -101,15 +101,6 @@ func hashBlock(blockConv types.BlockConverter) (common.Hash, error) { return hash, nil } -func signBlock(blockConv types.BlockConverter, - prv crypto.PrivateKey) (crypto.Signature, error) { - hash, err := hashBlock(blockConv) - if err != nil { - return crypto.Signature{}, err - } - return prv.Sign(hash) -} - func verifyBlockSignature(pubkey crypto.PublicKey, blockConv types.BlockConverter, sig crypto.Signature) (bool, error) { hash, err := hashBlock(blockConv) diff --git a/core/crypto_test.go b/core/crypto_test.go index 06f4905..595d0e9 100644 --- a/core/crypto_test.go +++ b/core/crypto_test.go @@ -154,7 +154,7 @@ func (s *CryptoTestSuite) generateBlockChain( block := s.newBlock(prevBlock) blocks[idx] = block var err error - block.Signature, err = signBlock(&simpleBlock{block: block}, prv) + block.Signature, err = prv.Sign(block.Hash) s.Require().Nil(err) } return blocks diff --git a/core/types/validator.go b/core/types/validator.go index 27a9acd..5151a6d 100644 --- a/core/types/validator.go +++ b/core/types/validator.go @@ -35,6 +35,11 @@ func NewValidatorID(pubKey crypto.PublicKey) ValidatorID { return ValidatorID{Hash: crypto.Keccak256Hash(pubKey.Bytes())} } +// Equal checks if the hash representation is the same ValidatorID. +func (v ValidatorID) Equal(hash common.Hash) bool { + return v.Hash == hash +} + // ValidatorIDs implements sort.Interface for ValidatorID. type ValidatorIDs []ValidatorID |