aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/types/validator.go7
-rw-r--r--crypto/eth/eth.go5
-rw-r--r--crypto/interfaces.go3
-rw-r--r--simulation/simulation.go24
-rw-r--r--simulation/validator.go6
5 files changed, 36 insertions, 9 deletions
diff --git a/core/types/validator.go b/core/types/validator.go
index 86c3acc..27a9acd 100644
--- a/core/types/validator.go
+++ b/core/types/validator.go
@@ -21,6 +21,7 @@ import (
"bytes"
"github.com/dexon-foundation/dexon-consensus-core/common"
+ "github.com/dexon-foundation/dexon-consensus-core/crypto"
)
// ValidatorID is the ID type for validators.
@@ -28,6 +29,12 @@ type ValidatorID struct {
common.Hash
}
+// NewValidatorID returns a ValidatorID with Hash set to the hash value of
+// public key.
+func NewValidatorID(pubKey crypto.PublicKey) ValidatorID {
+ return ValidatorID{Hash: crypto.Keccak256Hash(pubKey.Bytes())}
+}
+
// ValidatorIDs implements sort.Interface for ValidatorID.
type ValidatorIDs []ValidatorID
diff --git a/crypto/eth/eth.go b/crypto/eth/eth.go
index edf50d3..975d8a8 100644
--- a/crypto/eth/eth.go
+++ b/crypto/eth/eth.go
@@ -104,6 +104,11 @@ func (pub PublicKey) Compress() []byte {
return pub.publicKey
}
+// Bytes returns the []byte representation of public key.
+func (pub PublicKey) Bytes() []byte {
+ return pub.Compress()
+}
+
// SigToPub returns the PublicKey that created the given signature.
func SigToPub(
hash common.Hash, signature crypto.Signature) (PublicKey, error) {
diff --git a/crypto/interfaces.go b/crypto/interfaces.go
index 015ff40..280082e 100644
--- a/crypto/interfaces.go
+++ b/crypto/interfaces.go
@@ -39,4 +39,7 @@ type PrivateKey interface {
type PublicKey interface {
// VerifySignature checks that the given public key created signature over hash.
VerifySignature(hash common.Hash, signature Signature) bool
+
+ // Bytes returns the []byte representation of public key.
+ Bytes() []byte
}
diff --git a/simulation/simulation.go b/simulation/simulation.go
index 27e31d2..6730b96 100644
--- a/simulation/simulation.go
+++ b/simulation/simulation.go
@@ -20,8 +20,7 @@ package simulation
import (
"fmt"
- "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/eth"
"github.com/dexon-foundation/dexon-consensus-core/simulation/config"
)
@@ -50,15 +49,21 @@ func Run(configPath string) {
network = NewFakeNetwork(networkModel)
for i := 0; i < cfg.Validator.Num; i++ {
- id := types.ValidatorID{Hash: common.NewRandomHash()}
- vs = append(vs, NewValidator(id, cfg.Validator, network))
+ prv, err := eth.NewPrivateKey()
+ if err != nil {
+ panic(err)
+ }
+ vs = append(vs, NewValidator(prv, cfg.Validator, network))
}
} else if networkType == config.NetworkTypeTCPLocal {
for i := 0; i < cfg.Validator.Num; i++ {
- id := types.ValidatorID{Hash: common.NewRandomHash()}
+ prv, err := eth.NewPrivateKey()
+ if err != nil {
+ panic(err)
+ }
network := NewTCPNetwork(true, cfg.Networking.PeerServer)
go network.Start()
- vs = append(vs, NewValidator(id, cfg.Validator, network))
+ vs = append(vs, NewValidator(prv, cfg.Validator, network))
}
}
@@ -67,10 +72,13 @@ func Run(configPath string) {
go vs[i].Run()
}
} else if networkType == config.NetworkTypeTCP {
- id := types.ValidatorID{Hash: common.NewRandomHash()}
+ prv, err := eth.NewPrivateKey()
+ if err != nil {
+ panic(err)
+ }
network := NewTCPNetwork(false, cfg.Networking.PeerServer)
go network.Start()
- v := NewValidator(id, cfg.Validator, network)
+ v := NewValidator(prv, cfg.Validator, network)
go v.Run()
vs = append(vs, v)
}
diff --git a/simulation/validator.go b/simulation/validator.go
index 246d0d3..c5bbe15 100644
--- a/simulation/validator.go
+++ b/simulation/validator.go
@@ -25,6 +25,7 @@ import (
"github.com/dexon-foundation/dexon-consensus-core/common"
"github.com/dexon-foundation/dexon-consensus-core/core"
"github.com/dexon-foundation/dexon-consensus-core/core/types"
+ "github.com/dexon-foundation/dexon-consensus-core/crypto"
"github.com/dexon-foundation/dexon-consensus-core/simulation/config"
)
@@ -40,16 +41,19 @@ type Validator struct {
isFinished chan struct{}
ID types.ValidatorID
+ privateKey crypto.PrivateKey
consensus *core.Consensus
compactionChain *core.BlockChain
}
// NewValidator returns a new empty validator.
func NewValidator(
- id types.ValidatorID,
+ prvKey crypto.PrivateKey,
config config.Validator,
network Network) *Validator {
+ id := types.NewValidatorID(prvKey.PublicKey())
+
db, err := blockdb.NewMemBackedBlockDB(
id.String() + ".blockdb")
if err != nil {