-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoch_check.go
45 lines (38 loc) · 1.12 KB
/
epoch_check.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
package epochcheck
import (
"errors"
"github.com/Deepchain-foundation/lachesis-base/inter/dag"
"github.com/Deepchain-foundation/lachesis-base/inter/idx"
"github.com/Deepchain-foundation/lachesis-base/inter/pos"
)
var (
// ErrNotRelevant indicates the event's epoch isn't equal to current epoch.
ErrNotRelevant = errors.New("event is too old or too new")
// ErrAuth indicates that event's creator isn't authorized to create events in current epoch.
ErrAuth = errors.New("event creator isn't a validator")
)
// Reader returns currents epoch and its validators group.
type Reader interface {
GetEpochValidators() (*pos.Validators, idx.Epoch)
}
// Checker which require only current epoch info
type Checker struct {
reader Reader
}
func New(reader Reader) *Checker {
return &Checker{
reader: reader,
}
}
// Validate event
func (v *Checker) Validate(e dag.Event) error {
// check epoch first, because validators group is returned only for the current epoch
validators, epoch := v.reader.GetEpochValidators()
if e.Epoch() != epoch {
return ErrNotRelevant
}
if !validators.Exists(e.Creator()) {
return ErrAuth
}
return nil
}