aboutsummaryrefslogtreecommitdiffstats
path: root/core/test/blocks-generator.go
blob: 92271f70fc5d42c516319254afd7a0351fb1a3a6 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 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 test

import (
    "errors"
    "math"
    "math/rand"
    "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/types"
)

// ErrParentNotAcked would be raised when some block doesn't
// ack its parent block.
var ErrParentNotAcked = errors.New("parent is not acked")

// validatorStatus is a state holder for each validator
// during generating blocks.
type validatorStatus struct {
    blocks           []*types.Block
    lastAckingHeight map[types.ValidatorID]uint64
}

type hashBlockFn func(*types.Block) (common.Hash, error)

// getAckedBlockHash would randomly pick one block between
// last acked one to current head.
func (vs *validatorStatus) getAckedBlockHash(
    ackedVID types.ValidatorID,
    ackedValidator *validatorStatus,
    randGen *rand.Rand) (
    hash common.Hash, ok bool) {

    baseAckingHeight, exists := vs.lastAckingHeight[ackedVID]
    if exists {
        // Do not ack the same block(height) twice.
        baseAckingHeight++
    }
    totalBlockCount := uint64(len(ackedValidator.blocks))
    if totalBlockCount <= baseAckingHeight {
        // There is no new block to ack.
        return
    }
    ackableRange := totalBlockCount - baseAckingHeight
    height := uint64((randGen.Uint64() % ackableRange) + baseAckingHeight)
    vs.lastAckingHeight[ackedVID] = height
    hash = ackedValidator.blocks[height].Hash
    ok = true
    return
}

// validatorSetStatus is a state holder for all validators
// during generating blocks.
type validatorSetStatus struct {
    status       map[types.ValidatorID]*validatorStatus
    validatorIDs []types.ValidatorID
    randGen      *rand.Rand
    hashBlock    hashBlockFn
}

func newValidatorSetStatus(vIDs []types.ValidatorID, hashBlock hashBlockFn) *validatorSetStatus {
    status := make(map[types.ValidatorID]*validatorStatus)
    for _, vID := range vIDs {
        status[vID] = &validatorStatus{
            blocks:           []*types.Block{},
            lastAckingHeight: make(map[types.ValidatorID]uint64),
        }
    }
    return &validatorSetStatus{
        status:       status,
        validatorIDs: vIDs,
        randGen:      rand.New(rand.NewSource(time.Now().UnixNano())),
        hashBlock:    hashBlock,
    }
}

// findIncompleteValidators is a helper to check which validator
// doesn't generate enough blocks.
func (vs *validatorSetStatus) findIncompleteValidators(
    blockCount int) (vIDs []types.ValidatorID) {

    for vID, status := range vs.status {
        if len(status.blocks) < blockCount {
            vIDs = append(vIDs, vID)
        }
    }
    return
}

// prepareAcksForNewBlock collects acks for one block.
func (vs *validatorSetStatus) prepareAcksForNewBlock(
    proposerID types.ValidatorID, ackingCount int) (
    acks map[common.Hash]struct{}, err error) {

    acks = make(map[common.Hash]struct{})
    if len(vs.status[proposerID].blocks) == 0 {
        // The 'Acks' filed of genesis blocks would always be empty.
        return
    }
    // Pick validatorIDs to be acked.
    ackingVIDs := map[types.ValidatorID]struct{}{
        proposerID: struct{}{}, // Acking parent block is always required.
    }
    if ackingCount > 0 {
        ackingCount-- // We would always include ack to parent block.
    }
    for _, i := range vs.randGen.Perm(len(vs.validatorIDs))[:ackingCount] {
        ackingVIDs[vs.validatorIDs[i]] = struct{}{}
    }
    // Generate acks.
    for vID := range ackingVIDs {
        ack, ok := vs.status[proposerID].getAckedBlockHash(
            vID, vs.status[vID], vs.randGen)
        if !ok {
            if vID == proposerID {
                err = ErrParentNotAcked
            }
            continue
        }
        acks[ack] = struct{}{}
    }
    return
}

// proposeBlock propose new block and update validator status.
func (vs *validatorSetStatus) proposeBlock(
    proposerID types.ValidatorID,
    acks map[common.Hash]struct{}) (*types.Block, error) {

    status := vs.status[proposerID]
    parentHash := common.Hash{}
    if len(status.blocks) > 0 {
        parentHash = status.blocks[len(status.blocks)-1].Hash
    }

    ts := map[types.ValidatorID]time.Time{}
    for vid := range vs.status {
        ts[vid] = time.Time{}
    }
    newBlock := &types.Block{
        ProposerID: proposerID,
        ParentHash: parentHash,
        Height:     uint64(len(status.blocks)),
        Acks:       acks,
        Timestamps: ts,
        // TODO(mission.liao): Generate timestamp.
    }
    for i, vID := range vs.validatorIDs {
        if vID == proposerID {
            newBlock.ChainID = uint64(i)
        }
    }
    var err error
    newBlock.Hash, err = vs.hashBlock(newBlock)
    if err != nil {
        return nil, err
    }
    status.blocks = append(status.blocks, newBlock)
    return newBlock, nil
}

// normalAckingCountGenerator would randomly pick acking count
// by a normal distribution.
func normalAckingCountGenerator(
    validatorCount int, mean, deviation float64) func() int {

    return func() int {
        var expected float64
        for {
            expected = rand.NormFloat64()*deviation + mean
            if expected >= 0 && expected <= float64(validatorCount) {
                break
            }
        }
        return int(math.Ceil(expected))
    }
}

// MaxAckingCountGenerator return generator which returns
// fixed maximum acking count.
func MaxAckingCountGenerator(count int) func() int {
    return func() int { return count }
}

// generateValidatorPicker is a function generator, which would generate
// a function to randomly pick one validator ID from a slice of validator ID.
func generateValidatorPicker() func([]types.ValidatorID) types.ValidatorID {
    privateRand := rand.New(rand.NewSource(time.Now().UnixNano()))
    return func(vIDs []types.ValidatorID) types.ValidatorID {
        return vIDs[privateRand.Intn(len(vIDs))]
    }
}

// BlocksGenerator could generate blocks forming valid DAGs.
type BlocksGenerator struct {
    validatorPicker func([]types.ValidatorID) types.ValidatorID
    hashBlock       hashBlockFn
}

// NewBlocksGenerator constructs BlockGenerator.
func NewBlocksGenerator(validatorPicker func(
    []types.ValidatorID) types.ValidatorID,
    hashBlock hashBlockFn) *BlocksGenerator {

    if validatorPicker == nil {
        validatorPicker = generateValidatorPicker()
    }
    return &BlocksGenerator{
        validatorPicker: validatorPicker,
        hashBlock:       hashBlock,
    }
}

// Generate is the entry point to generate blocks. The caller is responsible
// to provide a function to generate count of acked block for each new block.
// The prototype of ackingCountGenerator is a function returning 'int'.
// For example, if you need to generate a group of blocks and each of them
// has maximum 2 acks.
//   func () int { return 2 }
// The default ackingCountGenerator would randomly pick a number based on
// the validatorCount you provided with a normal distribution.
func (gen *BlocksGenerator) Generate(
    validatorCount int,
    blockCount int,
    ackingCountGenerator func() int,
    writer blockdb.Writer) (
    validators types.ValidatorIDs, err error) {

    if ackingCountGenerator == nil {
        ackingCountGenerator = normalAckingCountGenerator(
            validatorCount,
            float64(validatorCount/2),
            float64(validatorCount/4+1))
    }
    validators = types.ValidatorIDs{}
    for i := 0; i < validatorCount; i++ {
        validators = append(
            validators, types.ValidatorID{Hash: common.NewRandomHash()})
    }
    status := newValidatorSetStatus(validators, gen.hashBlock)

    // We would record the smallest height of block that could be acked
    // from each validator's point-of-view.
    toAck := make(map[types.ValidatorID]map[types.ValidatorID]uint64)
    for _, vID := range validators {
        toAck[vID] = make(map[types.ValidatorID]uint64)
    }

    for {
        // Find validators that doesn't propose enough blocks and
        // pick one from them randomly.
        notYet := status.findIncompleteValidators(blockCount)
        if len(notYet) == 0 {
            break
        }

        // Propose a new block.
        var (
            proposerID = gen.validatorPicker(notYet)
            acks       map[common.Hash]struct{}
        )
        acks, err = status.prepareAcksForNewBlock(
            proposerID, ackingCountGenerator())
        if err != nil {
            return
        }
        var newBlock *types.Block
        newBlock, err = status.proposeBlock(proposerID, acks)
        if err != nil {
            return
        }

        // Persist block to db.
        err = writer.Put(*newBlock)
        if err != nil {
            return
        }
    }
    return
}