-
Notifications
You must be signed in to change notification settings - Fork 107
/
validator.go
68 lines (60 loc) 路 1.98 KB
/
validator.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
package client
import (
"bytes"
"context"
"time"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/peer"
"google.golang.org/protobuf/proto"
"github.com/drand/drand/chain"
"github.com/drand/drand/client"
"github.com/drand/drand/crypto"
"github.com/drand/drand/protobuf/drand"
)
func randomnessValidator(info *chain.Info, cache client.Cache, c *Client) pubsub.ValidatorEx {
return func(ctx context.Context, p peer.ID, m *pubsub.Message) pubsub.ValidationResult {
rand := &drand.PublicRandResponse{}
err := proto.Unmarshal(m.Data, rand)
if err != nil {
return pubsub.ValidationReject
}
if info == nil {
c.log.Warnw("", "gossip validator", "Not validating received randomness due to lack of trust root.")
return pubsub.ValidationAccept
}
// Unwilling to relay beacons in the future.
if time.Unix(chain.TimeOfRound(info.Period, info.GenesisTime, rand.GetRound()), 0).After(time.Now()) {
return pubsub.ValidationReject
}
if cache != nil {
if current := cache.TryGet(rand.GetRound()); current != nil {
currentFull, ok := current.(*client.RandomData)
if !ok {
// Note: this shouldn't happen in practice, but if we have a
// degraded cache entry we can't validate the full byte
// sequence.
if bytes.Equal(rand.GetSignature(), current.Signature()) {
return pubsub.ValidationIgnore
}
return pubsub.ValidationReject
}
if current.Round() == rand.GetRound() &&
bytes.Equal(current.Randomness(), rand.GetRandomness()) &&
bytes.Equal(current.Signature(), rand.GetSignature()) &&
bytes.Equal(currentFull.PreviousSignature, rand.GetPreviousSignature()) {
return pubsub.ValidationIgnore
}
return pubsub.ValidationReject
}
}
scheme, err := crypto.SchemeFromName(info.Scheme)
if err != nil {
return pubsub.ValidationReject
}
err = scheme.VerifyBeacon(rand, info.PublicKey)
if err != nil {
return pubsub.ValidationReject
}
return pubsub.ValidationAccept
}
}