-
Notifications
You must be signed in to change notification settings - Fork 211
/
layer_patrol.go
51 lines (42 loc) · 1.3 KB
/
layer_patrol.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
// Package layerpatrol keeps parties informed about the general progress of each layer.
package layerpatrol
import (
"sync"
"github.com/spacemeshos/go-spacemesh/common/types"
)
const bufferSize = uint32(100)
// LayerPatrol keeps progress of each layer.
type LayerPatrol struct {
mu sync.Mutex
oldestLayer types.LayerID
runByHare map[types.LayerID]struct{}
}
// New returns an instance of LayerPatrol.
func New() *LayerPatrol {
return &LayerPatrol{
runByHare: make(map[types.LayerID]struct{}),
}
}
// SetHareInCharge sets the layer's validation to be triggered by hare (as opposed to syncer).
func (lp *LayerPatrol) SetHareInCharge(layerID types.LayerID) {
lp.mu.Lock()
defer lp.mu.Unlock()
if layerID.Uint32() > bufferSize {
lp.oldestLayer = layerID.Sub(bufferSize)
}
delete(lp.runByHare, lp.oldestLayer)
lp.runByHare[layerID] = struct{}{}
}
// IsHareInCharge returns true if the hare is set to handle the validation of the specified layer.
func (lp *LayerPatrol) IsHareInCharge(layerID types.LayerID) bool {
lp.mu.Lock()
defer lp.mu.Unlock()
_, ok := lp.runByHare[layerID]
return ok
}
// CompleteHare is called by hare instance that completed this layer.
func (lp *LayerPatrol) CompleteHare(layerID types.LayerID) {
lp.mu.Lock()
defer lp.mu.Unlock()
delete(lp.runByHare, layerID)
}