forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.go
58 lines (47 loc) · 1.26 KB
/
subscription.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
package fraud
import (
"context"
pubsub "github.com/libp2p/go-libp2p-pubsub"
)
// subscription handles Fraud Proof from the pubsub topic.
type subscription struct {
subscription *pubsub.Subscription
unmarshaler ProofUnmarshaler
}
func newSubscription(t *topic) (*subscription, error) {
sub, err := t.topic.Subscribe()
if err != nil {
return nil, err
}
return &subscription{sub, t.unmarshal}, nil
}
func (s *subscription) Proof(ctx context.Context) (Proof, error) {
data, err := s.subscription.Next(ctx)
if err != nil {
return nil, err
}
return s.unmarshaler(data.Data)
}
func (s *subscription) Cancel() {
s.subscription.Cancel()
}
// OnProof subscribes on a single Fraud Proof.
// In case a Fraud Proof is received, then the given handle function will be invoked.
func OnProof(ctx context.Context, subscriber Subscriber, p ProofType, handle func(proof Proof)) {
subscription, err := subscriber.Subscribe(p)
if err != nil {
log.Error(err)
return
}
defer subscription.Cancel()
// At this point we receive already verified fraud proof,
// so there are no needs to call Validate.
proof, err := subscription.Proof(ctx)
if err != nil {
if err != context.Canceled {
log.Errorw("reading next proof failed", "err", err)
}
return
}
handle(proof)
}