aboutsummaryrefslogtreecommitdiffstats
path: root/simulation/validator.go
blob: 8a672c555264bb894654d0b8056ea40b3ec5acac (plain) (blame)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// 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"
    "sort"
    "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/crypto"
    "github.com/dexon-foundation/dexon-consensus-core/simulation/config"
)

// Validator represents a validator in DexCon.
type Validator struct {
    network Network
    app     *simApp
    gov     *simGovernance
    db      blockdb.BlockDatabase

    config     config.Validator
    msgChannel <-chan interface{}
    isFinished chan struct{}

    ID              types.ValidatorID
    chainID         uint64
    prvKey          crypto.PrivateKey
    sigToPub        core.SigToPubFn
    consensus       *core.Consensus
    compactionChain *core.BlockChain
}

// NewValidator returns a new empty validator.
func NewValidator(
    prvKey crypto.PrivateKey,
    sigToPub core.SigToPubFn,
    config config.Validator,
    network Network) *Validator {

    id := types.NewValidatorID(prvKey.PublicKey())

    db, err := blockdb.NewMemBackedBlockDB(
        id.String() + ".blockdb")
    if err != nil {
        panic(err)
    }
    gov := newSimGovernance(config.Num, config.Consensus)
    return &Validator{
        ID:         id,
        prvKey:     prvKey,
        sigToPub:   sigToPub,
        config:     config,
        network:    network,
        app:        newSimApp(id, network),
        gov:        gov,
        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.network.Join(v)
    v.msgChannel = v.network.ReceiveChan()

    hashes := make(common.Hashes, 0, v.network.NumPeers())
    for _, vID := range v.network.Endpoints() {
        v.gov.addValidator(vID)
        hashes = append(hashes, vID.Hash)
    }
    sort.Sort(hashes)
    for i, hash := range hashes {
        if hash == v.ID.Hash {
            v.chainID = uint64(i)
            break
        }
    }
    v.consensus = core.NewConsensus(
        v.app, v.gov, v.db, v.prvKey, v.sigToPub)

    genesisBlock := &types.Block{
        ProposerID: v.ID,
        ChainID:    v.chainID,
    }
    err := v.consensus.PrepareGenesisBlock(genesisBlock, time.Now().UTC())
    if err != nil {
        panic(err)
    }
    isStopped := make(chan struct{}, 2)
    isShutdown := make(chan struct{})

    v.app.addBlock(genesisBlock)
    v.consensus.ProcessBlock(genesisBlock)
    v.BroadcastGenesisBlock(genesisBlock)
    go v.MsgServer(isStopped)
    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(
    isStopped chan struct{}) {

    for {
        var msg interface{}
        select {
        case msg = <-v.msgChannel:
        case <-isStopped:
            return
        }

        switch val := msg.(type) {
        case *types.Block:
            v.app.addBlock(val)
            if err := v.consensus.ProcessBlock(val); err != nil {
                fmt.Println(err)
            }
            types.RecycleBlock(val)
        case *types.NotaryAck:
            if err := v.consensus.ProcessNotaryAck(val); err != nil {
                fmt.Println(err)
            }
        case *types.Vote:
            if err := v.consensus.ProcessVote(val); err != nil {
                fmt.Println(err)
            }
        }
    }
}

// BroadcastGenesisBlock broadcasts genesis block to all peers.
func (v *Validator) BroadcastGenesisBlock(genesisBlock *types.Block) {
    // Wait until all peer joined the network.
    for v.network.NumPeers() != v.config.Num {
        time.Sleep(time.Second)
    }
    v.network.BroadcastBlock(genesisBlock)
}

// BlockProposer propose blocks to be send to the DEXON network.
func (v *Validator) BlockProposer(isStopped, isShutdown chan struct{}) {
    model := &NormalNetwork{
        Sigma: v.config.ProposeIntervalSigma,
        Mean:  v.config.ProposeIntervalMean,
    }
ProposingBlockLoop:
    for {
        time.Sleep(model.Delay())

        block := &types.Block{
            ProposerID: v.ID,
            ChainID:    v.chainID,
            Hash:       common.NewRandomHash(),
        }
        if err := v.consensus.PrepareBlock(block, time.Now().UTC()); err != nil {
            panic(err)
        }
        v.app.addBlock(block)
        if err := v.consensus.ProcessBlock(block); err != nil {
            fmt.Println(err)
            //panic(err)
        }
        v.network.BroadcastBlock(block)
        select {
        case <-isShutdown:
            isStopped <- struct{}{}
            isStopped <- struct{}{}
            break ProposingBlockLoop
        default:
            break
        }
    }
}