aboutsummaryrefslogtreecommitdiffstats
path: root/simulation
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-08-13 16:19:32 +0800
committerGitHub <noreply@github.com>2018-08-13 16:19:32 +0800
commit2d3725842cb995cc748aeb6c05adb725f6ae18f8 (patch)
treefc0c90e1bd26f21a65066a49cb1f83aac8c7586b /simulation
parente22580beadb70991d95f13677f22701fe273791a (diff)
downloadtangerine-consensus-2d3725842cb995cc748aeb6c05adb725f6ae18f8.tar
tangerine-consensus-2d3725842cb995cc748aeb6c05adb725f6ae18f8.tar.gz
tangerine-consensus-2d3725842cb995cc748aeb6c05adb725f6ae18f8.tar.bz2
tangerine-consensus-2d3725842cb995cc748aeb6c05adb725f6ae18f8.tar.lz
tangerine-consensus-2d3725842cb995cc748aeb6c05adb725f6ae18f8.tar.xz
tangerine-consensus-2d3725842cb995cc748aeb6c05adb725f6ae18f8.tar.zst
tangerine-consensus-2d3725842cb995cc748aeb6c05adb725f6ae18f8.zip
simulation: Create Consensus core after Join. (#51)
Diffstat (limited to 'simulation')
-rw-r--r--simulation/fake-network.go9
-rw-r--r--simulation/network.go1
-rw-r--r--simulation/tcp-network.go9
-rw-r--r--simulation/validator.go44
4 files changed, 31 insertions, 32 deletions
diff --git a/simulation/fake-network.go b/simulation/fake-network.go
index 61394a9..99c504a 100644
--- a/simulation/fake-network.go
+++ b/simulation/fake-network.go
@@ -110,3 +110,12 @@ func (n *FakeNetwork) GetServerInfo() InfoMessage {
// TODO(jimmy-dexon): Implement this method.
return InfoMessage{}
}
+
+// Endpoints returns all validatorIDs.
+func (n *FakeNetwork) Endpoints() types.ValidatorIDs {
+ vIDs := make(types.ValidatorIDs, len(n.endpoints))
+ for vID := range n.endpoints {
+ vIDs = append(vIDs, vID)
+ }
+ return vIDs
+}
diff --git a/simulation/network.go b/simulation/network.go
index 0788ca1..672a664 100644
--- a/simulation/network.go
+++ b/simulation/network.go
@@ -80,6 +80,7 @@ type Network interface {
NumPeers() int
Join(endpoint Endpoint) chan interface{}
BroadcastBlock(block *types.Block)
+ Endpoints() types.ValidatorIDs
}
// PeerServerNetwork is the interface for peerServer network related functions
diff --git a/simulation/tcp-network.go b/simulation/tcp-network.go
index 6e2616b..f56cc53 100644
--- a/simulation/tcp-network.go
+++ b/simulation/tcp-network.go
@@ -349,3 +349,12 @@ func (n *TCPNetwork) GetServerInfo() InfoMessage {
}
return infoMsg
}
+
+// Endpoints returns all validatorIDs.
+func (n *TCPNetwork) Endpoints() types.ValidatorIDs {
+ vIDs := make(types.ValidatorIDs, 0, len(n.endpoints))
+ for vID := range n.endpoints {
+ vIDs = append(vIDs, vID)
+ }
+ return vIDs
+}
diff --git a/simulation/validator.go b/simulation/validator.go
index 17d6eee..7ebf06b 100644
--- a/simulation/validator.go
+++ b/simulation/validator.go
@@ -62,7 +62,6 @@ func NewValidator(
panic(err)
}
gov := newSimGovernance(config.Num, config.Consensus)
- gov.addValidator(id)
return &Validator{
ID: id,
prvKey: prvKey,
@@ -85,6 +84,12 @@ func (v *Validator) GetID() types.ValidatorID {
func (v *Validator) Run() {
v.msgChannel = v.network.Join(v)
+ for _, vID := range v.network.Endpoints() {
+ v.gov.addValidator(vID)
+ }
+ v.consensus = core.NewConsensus(
+ v.app, v.gov, v.db, v.prvKey, v.sigToPub)
+
genesisBlock := &types.Block{
ProposerID: v.ID,
ParentHash: common.Hash{},
@@ -99,8 +104,9 @@ func (v *Validator) Run() {
isShutdown := make(chan struct{})
v.app.addBlock(genesisBlock)
+ v.consensus.ProcessBlock(genesisBlock)
v.BroadcastGenesisBlock(genesisBlock)
- go v.MsgServer(isStopped, genesisBlock)
+ go v.MsgServer(isStopped)
go v.CheckServerInfo(isShutdown)
go v.BlockProposer(isStopped, isShutdown)
@@ -135,9 +141,8 @@ func (v *Validator) CheckServerInfo(isShutdown chan struct{}) {
// MsgServer listen to the network channel for message and handle it.
func (v *Validator) MsgServer(
- isStopped chan struct{}, genesisBlock *types.Block) {
+ isStopped chan struct{}) {
- pendingBlocks := []*types.Block{genesisBlock}
for {
var msg interface{}
select {
@@ -149,30 +154,9 @@ func (v *Validator) MsgServer(
switch val := msg.(type) {
case *types.Block:
v.app.addBlock(val)
- if v.consensus != nil {
- if err := v.consensus.ProcessBlock(val); err != nil {
- fmt.Println(err)
- //panic(err)
- }
- } else {
- pendingBlocks = append(pendingBlocks, val)
- if val.IsGenesis() {
- v.gov.addValidator(val.ProposerID)
- }
- validatorSet := v.gov.GetValidatorSet()
- if len(validatorSet) != v.config.Num {
- // We don't collect all validators yet.
- break
- }
- v.consensus = core.NewConsensus(
- v.app, v.gov, v.db, v.prvKey, v.sigToPub)
- for _, b := range pendingBlocks {
- if err := v.consensus.ProcessBlock(b); err != nil {
- fmt.Println(err)
- //panic(err)
- }
- }
- pendingBlocks = pendingBlocks[:0]
+ if err := v.consensus.ProcessBlock(val); err != nil {
+ fmt.Println(err)
+ //panic(err)
}
}
}
@@ -189,10 +173,6 @@ func (v *Validator) BroadcastGenesisBlock(genesisBlock *types.Block) {
// BlockProposer propose blocks to be send to the DEXON network.
func (v *Validator) BlockProposer(isStopped, isShutdown chan struct{}) {
- // Wait until all genesis blocks are received.
- for v.consensus == nil {
- time.Sleep(time.Second)
- }
model := &NormalNetwork{
Sigma: v.config.ProposeIntervalSigma,
Mean: v.config.ProposeIntervalMean,