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
|
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus 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 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 library. If not, see
// <http://www.gnu.org/licenses/>.
package core
import (
"fmt"
"sync"
"time"
"github.com/dexon-foundation/dexon-consensus/common"
"github.com/dexon-foundation/dexon-consensus/core/crypto"
"github.com/dexon-foundation/dexon-consensus/core/types"
"github.com/dexon-foundation/dexon-consensus/core/utils"
)
// Errors for compaction chain module.
var (
ErrBlockNotRegistered = fmt.Errorf(
"block not registered")
ErrNotInitiazlied = fmt.Errorf(
"not initialized")
ErrTSigNotReady = fmt.Errorf(
"tsig not ready")
ErrIncorrectBlockRandomnessResult = fmt.Errorf(
"incorrect block randomness result")
)
const maxPendingPeriod = 3 * time.Second
type pendingRandomnessResult struct {
receivedTime time.Time
randResult *types.BlockRandomnessResult
}
type finalizedBlockHeap = types.ByFinalizationHeight
type compactionChain struct {
gov Governance
chainUnsynced uint32
tsigVerifier *TSigVerifierCache
blocks map[common.Hash]*types.Block
blockRandomness map[common.Hash][]byte
pendingRandomness map[common.Hash]pendingRandomnessResult
pendingBlocks []*types.Block
lock sync.RWMutex
prevBlock *types.Block
}
func newCompactionChain(gov Governance) *compactionChain {
return &compactionChain{
gov: gov,
tsigVerifier: NewTSigVerifierCache(gov, 7),
blocks: make(map[common.Hash]*types.Block),
blockRandomness: make(map[common.Hash][]byte),
pendingRandomness: make(map[common.Hash]pendingRandomnessResult),
}
}
// init the compaction chain module with a finalized block, or just an empty
// block for bootstrap case.
func (cc *compactionChain) init(initBlock *types.Block) {
cc.lock.Lock()
defer cc.lock.Unlock()
cc.prevBlock = initBlock
cc.pendingBlocks = []*types.Block{}
// It's the bootstrap case, compactionChain would only deliver blocks until
// tips of all chains are received.
if initBlock.Finalization.Height == 0 {
cc.chainUnsynced = utils.GetConfigWithPanic(cc.gov, 0, nil).NumChains
}
}
func (cc *compactionChain) registerBlock(block *types.Block) {
if cc.blockRegistered(block.Hash) {
return
}
cc.lock.Lock()
defer cc.lock.Unlock()
cc.blocks[block.Hash] = block
if rand, exist := cc.pendingRandomness[block.Hash]; exist {
cc.blockRandomness[rand.randResult.BlockHash] = rand.randResult.Randomness
delete(cc.pendingRandomness, block.Hash)
}
}
func (cc *compactionChain) blockRegistered(hash common.Hash) bool {
cc.lock.RLock()
defer cc.lock.RUnlock()
return cc.blockRegisteredNoLock(hash)
}
func (cc *compactionChain) blockRegisteredNoLock(
hash common.Hash) (exist bool) {
_, exist = cc.blocks[hash]
return
}
func (cc *compactionChain) processBlock(block *types.Block) error {
prevBlock := cc.lastDeliveredBlock()
if prevBlock == nil {
return ErrNotInitiazlied
}
cc.lock.Lock()
defer cc.lock.Unlock()
if prevBlock.Finalization.Height == 0 && block.Position.Height == 0 {
cc.chainUnsynced--
}
cc.pendingBlocks = append(cc.pendingBlocks, block)
return nil
}
func (cc *compactionChain) extractBlocks() []*types.Block {
prevBlock := cc.lastDeliveredBlock()
// Check if we're synced.
if !func() bool {
cc.lock.RLock()
defer cc.lock.RUnlock()
if len(cc.pendingBlocks) == 0 {
return false
}
// Finalization.Height == 0 is syncing from bootstrap.
if prevBlock.Finalization.Height == 0 {
return cc.chainUnsynced == 0
}
return true
}() {
return []*types.Block{}
}
deliveringBlocks := make([]*types.Block, 0)
cc.lock.Lock()
defer cc.lock.Unlock()
var block *types.Block
for len(cc.pendingBlocks) > 0 &&
(len(cc.blockRandomness[cc.pendingBlocks[0].Hash]) != 0 ||
cc.pendingBlocks[0].Position.Round == 0) {
delete(cc.blocks, cc.pendingBlocks[0].Hash)
block, cc.pendingBlocks = cc.pendingBlocks[0], cc.pendingBlocks[1:]
block.Finalization.ParentHash = prevBlock.Hash
block.Finalization.Height = prevBlock.Finalization.Height + 1
if block.Position.Round != 0 {
block.Finalization.Randomness = cc.blockRandomness[block.Hash]
delete(cc.blockRandomness, block.Hash)
}
deliveringBlocks = append(deliveringBlocks, block)
prevBlock = block
}
cc.prevBlock = prevBlock
return deliveringBlocks
}
func (cc *compactionChain) verifyRandomness(
blockHash common.Hash, round uint64, randomness []byte) (bool, error) {
if round == 0 {
return len(randomness) == 0, nil
}
// Randomness is not available at round 0.
v, ok, err := cc.tsigVerifier.UpdateAndGet(round)
if err != nil {
return false, err
}
if !ok {
return false, ErrTSigNotReady
}
return v.VerifySignature(blockHash, crypto.Signature{
Type: "bls",
Signature: randomness}), nil
}
func (cc *compactionChain) processFinalizedBlock(block *types.Block) error {
if block.Finalization.Height <= cc.lastDeliveredBlock().Finalization.Height {
return nil
}
// Block of round 0 should not have randomness.
if block.Position.Round == 0 && len(block.Finalization.Randomness) != 0 {
return nil
}
cc.lock.Lock()
defer cc.lock.Unlock()
// The randomness result is missed previously.
if cc.blockRegisteredNoLock(block.Hash) {
ok, err := cc.verifyRandomness(
block.Hash, block.Position.Round, block.Finalization.Randomness)
if err != nil {
return err
}
if ok {
cc.blockRandomness[block.Hash] = block.Finalization.Randomness
}
}
return nil
}
func (cc *compactionChain) processBlockRandomnessResult(
rand *types.BlockRandomnessResult) error {
ok, err := cc.verifyRandomness(
rand.BlockHash, rand.Position.Round, rand.Randomness)
if err != nil {
return err
}
if !ok {
return ErrIncorrectBlockRandomnessResult
}
cc.lock.Lock()
defer cc.lock.Unlock()
if !cc.blockRegisteredNoLock(rand.BlockHash) {
cc.purgePending()
cc.pendingRandomness[rand.BlockHash] = pendingRandomnessResult{
receivedTime: time.Now(),
randResult: rand,
}
return ErrBlockNotRegistered
}
cc.blockRandomness[rand.BlockHash] = rand.Randomness
return nil
}
func (cc *compactionChain) purgePending() {
now := time.Now()
for key, rand := range cc.pendingRandomness {
if now.After(rand.receivedTime.Add(maxPendingPeriod)) {
delete(cc.pendingRandomness, key)
}
}
}
// lastDeliveredBlock returns the last delivered block, or the one used to
// initialize this module.
func (cc *compactionChain) lastDeliveredBlock() *types.Block {
cc.lock.RLock()
defer cc.lock.RUnlock()
return cc.prevBlock
}
// lastPendingBlock returns the last pending block.
func (cc *compactionChain) lastPendingBlock() *types.Block {
cc.lock.RLock()
defer cc.lock.RUnlock()
if len(cc.pendingBlocks) > 0 {
return cc.pendingBlocks[0]
}
return nil
}
|