1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
// Copyright 2018 The dexon-consensus-core Authors
// This file is part of the dexon-consensus-core library.
//
// The dexon-consensus-core library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus-core library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus-core library. If not, see
// <http://www.gnu.org/licenses/>.
package simulation
import (
"fmt"
"time"
"github.com/dexon-foundation/dexon-consensus-core/blockdb"
"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/simulation/config"
)
// Validator represents a validator in DexCon.
type Validator struct {
network Network
app *SimApp
gov *simGov
db blockdb.BlockDatabase
config config.Validator
msgChannel chan interface{}
isFinished chan struct{}
ID types.ValidatorID
consensus *core.Consensus
compactionChain *core.BlockChain
genesis *types.Block
current *types.Block
}
// NewValidator returns a new empty validator.
func NewValidator(
id types.ValidatorID,
config config.Validator,
network Network) *Validator {
db, err := blockdb.NewMemBackedBlockDB(
id.String() + ".blockdb")
if err != nil {
panic(err)
}
return &Validator{
ID: id,
config: config,
network: network,
app: NewSimApp(id, network),
gov: newSimGov(config.Num),
db: db,
isFinished: make(chan struct{}),
}
}
// GetID returns the ID of validator.
func (v *Validator) GetID() types.ValidatorID {
return v.ID
}
// Run starts the validator.
func (v *Validator) Run() {
v.msgChannel = v.network.Join(v)
isStopped := make(chan struct{})
isShutdown := make(chan struct{})
v.BroadcastGenesisBlock()
go v.MsgServer()
go v.CheckServerInfo(isShutdown)
go v.BlockProposer(isStopped, isShutdown)
// Blocks forever.
<-isStopped
if err := v.db.Close(); err != nil {
fmt.Println(err)
}
v.network.NotifyServer(Message{
Type: shutdownAck,
})
v.isFinished <- struct{}{}
}
// Wait for the validator to stop (if peerServer told it to).
func (v *Validator) Wait() {
<-v.isFinished
}
// CheckServerInfo will check the info from the peerServer and update
// validator's status if needed.
func (v *Validator) CheckServerInfo(isShutdown chan struct{}) {
for {
infoMsg := v.network.GetServerInfo()
if infoMsg.Status == statusShutdown {
isShutdown <- struct{}{}
break
}
time.Sleep(250 * time.Millisecond)
}
}
// MsgServer listen to the network channel for message and handle it.
func (v *Validator) MsgServer() {
var pendingBlocks []*types.Block
for {
msg := <-v.msgChannel
switch val := msg.(type) {
case *types.Block:
if v.consensus != nil {
if err := v.consensus.ProcessBlock(val); err != nil {
fmt.Println(err)
//panic(err)
}
} else {
pendingBlocks = append(pendingBlocks, val)
if val.ParentHash == val.Hash {
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)
for _, b := range pendingBlocks {
if err := v.consensus.ProcessBlock(b); err != nil {
fmt.Println(err)
//panic(err)
}
}
pendingBlocks = pendingBlocks[:0]
}
}
}
}
// BroadcastGenesisBlock broadcasts genesis block to all peers.
func (v *Validator) BroadcastGenesisBlock() {
// Wait until all peer joined the network.
for v.network.NumPeers() != v.config.Num {
time.Sleep(time.Second)
}
hash := common.NewRandomHash()
b := &types.Block{
ProposerID: v.ID,
ParentHash: hash,
Hash: hash,
Height: 0,
Acks: map[common.Hash]struct{}{},
Timestamps: map[types.ValidatorID]time.Time{
v.ID: time.Now().UTC(),
},
}
v.network.BroadcastBlock(b)
}
// 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,
}
ProposingBlockLoop:
for {
time.Sleep(model.Delay())
block := &types.Block{
ProposerID: v.ID,
Hash: common.NewRandomHash(),
}
if err := v.consensus.PrepareBlock(block); err != nil {
panic(err)
}
if err := v.consensus.ProcessBlock(block); err != nil {
fmt.Println(err)
//panic(err)
}
v.network.BroadcastBlock(block)
select {
case <-isShutdown:
isStopped <- struct{}{}
break ProposingBlockLoop
default:
break
}
}
}
|