-
Notifications
You must be signed in to change notification settings - Fork 47
/
sequencer.go
102 lines (83 loc) · 2.35 KB
/
sequencer.go
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
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this
// file, you can obtain one at https://opensource.org/licenses/MIT.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
package chain
import (
"encoding/hex"
"errors"
"sync"
"github.com/dusk-network/dusk-blockchain/pkg/core/data/block"
"github.com/sirupsen/logrus"
)
// The sequencer is used to order incoming blocks and provide them
// in the correct order to the Chain when synchronizing.
// NOTE: the sequencer is not synchronized, as it is used by the Chain
// directly during the acceptance procedure. The mutex in this procedure
// should be sufficient to guard this map.
type sequencer struct {
lock sync.RWMutex
blockPool map[uint64]block.Block
}
func newSequencer() *sequencer {
return &sequencer{blockPool: make(map[uint64]block.Block)}
}
func (s *sequencer) add(blk block.Block) {
s.lock.Lock()
defer s.lock.Unlock()
s.blockPool[blk.Header.Height] = blk
}
func (s *sequencer) get(height uint64) (block.Block, error) {
s.lock.RLock()
defer s.lock.RUnlock()
blk, ok := s.blockPool[height]
if !ok {
return block.Block{}, errors.New("block not found")
}
return blk, nil
}
func (s *sequencer) remove(height uint64) {
s.lock.Lock()
defer s.lock.Unlock()
delete(s.blockPool, height)
}
// cleanup removes all blocks that are lower than currentHeight.
func (s *sequencer) cleanup(currentHeight uint64) {
s.lock.Lock()
defer s.lock.Unlock()
for height := range s.blockPool {
if height < currentHeight {
delete(s.blockPool, height)
}
}
}
// Provide successive blocks to the given height. Once a gap is detected, the loop
// quits and returns a set of blocks.
func (s *sequencer) provideSuccessors(blk block.Block) []block.Block {
blks := []block.Block{blk}
for i := blk.Header.Height + 1; ; i++ {
blk, err := s.get(i)
if err != nil {
return blks
}
blks = append(blks, blk)
s.remove(i)
}
}
// dump report a log entry with current state of sequencer.
func (s *sequencer) dump() {
if logrus.GetLevel() != logrus.TraceLevel {
return
}
s.lock.RLock()
defer s.lock.RUnlock()
for height := range s.blockPool {
blk, err := s.get(height)
if err == nil {
log.WithField("hash", hex.EncodeToString(blk.Header.Hash)).
WithField("height", blk.Header.Height).
Trace("sequencer item")
}
}
}