aboutsummaryrefslogtreecommitdiffstats
path: root/core/consensus.go
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-08-13 15:13:41 +0800
committerGitHub <noreply@github.com>2018-08-13 15:13:41 +0800
commita8ebf501e54406c7449f71dba0c9af696bbc4e21 (patch)
tree37ae1a53769d6f7be82ed576b1fd65a476c11d17 /core/consensus.go
parent63c76e56169b6f14ef049490392a1b3b414f73c6 (diff)
downloaddexon-consensus-a8ebf501e54406c7449f71dba0c9af696bbc4e21.tar
dexon-consensus-a8ebf501e54406c7449f71dba0c9af696bbc4e21.tar.gz
dexon-consensus-a8ebf501e54406c7449f71dba0c9af696bbc4e21.tar.bz2
dexon-consensus-a8ebf501e54406c7449f71dba0c9af696bbc4e21.tar.lz
dexon-consensus-a8ebf501e54406c7449f71dba0c9af696bbc4e21.tar.xz
dexon-consensus-a8ebf501e54406c7449f71dba0c9af696bbc4e21.tar.zst
dexon-consensus-a8ebf501e54406c7449f71dba0c9af696bbc4e21.zip
core: Sign block in Consensus.PrepareBlock. (#50)
Diffstat (limited to 'core/consensus.go')
-rw-r--r--core/consensus.go35
1 files changed, 33 insertions, 2 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
}