-
Notifications
You must be signed in to change notification settings - Fork 3
/
module.go
150 lines (125 loc) · 3.8 KB
/
module.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package f3
import (
"context"
"github.com/filecoin-project/go-f3/certs"
"github.com/filecoin-project/go-f3/gpbft"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/host"
peer "github.com/libp2p/go-libp2p/core/peer"
"go.uber.org/multierr"
"golang.org/x/xerrors"
)
type Module struct {
NetworkName gpbft.NetworkName
CertStore *certs.Store
ds datastore.Datastore
host host.Host
pubsub *pubsub.PubSub
verif gpbft.Verifier
sigs gpbft.Signer
ec ECBackend
log Logger
// topic is populated after Run is called
topic *pubsub.Topic
}
// NewModule creates and setups new libp2p f3 module
// The context is used for initialization not runtime.
func NewModule(ctx context.Context, nn gpbft.NetworkName, ds datastore.Datastore, h host.Host,
ps *pubsub.PubSub, sigs gpbft.Signer, verif gpbft.Verifier, ec ECBackend, log Logger) (*Module, error) {
ds = namespace.Wrap(ds, nn.DatastorePrefix())
cs, err := certs.NewStore(ctx, ds)
if err != nil {
return nil, xerrors.Errorf("creating CertStore: %w", err)
}
m := Module{
NetworkName: nn,
CertStore: cs,
ds: ds,
host: h,
pubsub: ps,
verif: verif,
sigs: sigs,
ec: ec,
log: log,
}
return &m, nil
}
func (m *Module) setupPubsub() error {
// pubsub will probably move to separate file/struct
pubsubTopicName := m.NetworkName.PubSubTopic()
err := m.pubsub.RegisterTopicValidator(pubsubTopicName, m.pubsubTopicValidator)
if err != nil {
return xerrors.Errorf("registering topic validator: %w", err)
}
topic, err := m.pubsub.Join(pubsubTopicName)
if err != nil {
return xerrors.Errorf("could not join on pubsub topic: %s: %w", pubsubTopicName, err)
}
m.topic = topic
return nil
}
func (m *Module) teardownPubsub() error {
return multierr.Combine(
m.pubsub.UnregisterTopicValidator(m.NetworkName.PubSubTopic()),
m.topic.Close(),
)
}
// Run start the module. It will exit when context is cancelled.
func (m *Module) Run(ctx context.Context) error {
if err := m.setupPubsub(); err != nil {
return xerrors.Errorf("setting up pubsub: %w", err)
}
sub, err := m.topic.Subscribe()
if err != nil {
return xerrors.Errorf("subscribing to topic: %w", err)
}
for {
msg, err := sub.Next(ctx)
if err != nil {
m.log.Errorf("pubsub subscription.Next() returned an error: %+v", err)
break
}
_ = msg
// TODO: pubsub integration
}
sub.Cancel()
if err := m.teardownPubsub(); err != nil {
return xerrors.Errorf("shutting down pubsub: %w", err)
}
return ctx.Err()
}
var _ pubsub.ValidatorEx = (*Module)(nil).pubsubTopicValidator
// validator for the pubsub
func (m *Module) pubsubTopicValidator(ctx context.Context, pID peer.ID, msg *pubsub.Message) pubsub.ValidationResult {
return pubsub.ValidationAccept
}
type ECBackend interface{}
type TODOVerifier struct{}
// Verifies a signature for the given sender ID.
func (v *TODOVerifier) Verify(pubKey gpbft.PubKey, msg []byte, sig []byte) bool {
panic("not implemented")
}
// Aggregates signatures from a participant to an existing signature.
func (v *TODOVerifier) Aggregate(sig [][]byte, aggSignature []byte) []byte {
panic("not implemented")
}
// VerifyAggregate verifies an aggregate signature.
func (v *TODOVerifier) VerifyAggregate(payload []byte, aggSig []byte, signers []gpbft.PubKey) bool {
panic("not implemented")
}
type Logger interface {
Debug(args ...interface{})
Debugf(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
Panic(args ...interface{})
Panicf(format string, args ...interface{})
Warn(args ...interface{})
Warnf(format string, args ...interface{})
}