1
1
package observer
2
2
3
3
import (
4
- "bytes"
5
4
"context"
6
5
"database/sql"
7
6
_ "embed"
@@ -14,7 +13,9 @@ import (
14
13
"sync"
15
14
"time"
16
15
16
+ "github.com/filecoin-project/go-f3"
17
17
"github.com/filecoin-project/go-f3/gpbft"
18
+ "github.com/filecoin-project/go-f3/internal/encoding"
18
19
"github.com/filecoin-project/go-f3/internal/psutil"
19
20
"github.com/filecoin-project/go-f3/manifest"
20
21
"github.com/ipfs/go-log/v2"
@@ -41,16 +42,22 @@ type Observer struct {
41
42
42
43
messageObserved chan * message
43
44
networkChanged <- chan gpbft.NetworkName
45
+ msgEncoding * encoding.ZSTD [* f3.PartialGMessage ]
44
46
}
45
47
46
48
func New (o ... Option ) (* Observer , error ) {
47
49
opts , err := newOptions (o ... )
48
50
if err != nil {
49
51
return nil , err
50
52
}
53
+ msgEncoding , err := encoding .NewZSTD [* f3.PartialGMessage ]()
54
+ if err != nil {
55
+ return nil , err
56
+ }
51
57
return & Observer {
52
58
options : opts ,
53
59
messageObserved : make (chan * message , opts .messageBufferSize ),
60
+ msgEncoding : msgEncoding ,
54
61
}, nil
55
62
}
56
63
@@ -189,7 +196,7 @@ func (o *Observer) observe(ctx context.Context) error {
189
196
}
190
197
191
198
func (o * Observer ) storeMessage (ctx context.Context , om * message ) error {
192
- const insertMessage = `INSERT INTO latest_messages VALUES(?,?,?,?::json,?,?,?::json);`
199
+ const insertMessage = `INSERT INTO latest_messages VALUES(?,?,?,?::json,?,?,?::json,? );`
193
200
voteMarshaled , err := json .Marshal (om .Vote )
194
201
if err != nil {
195
202
return fmt .Errorf ("failed to marshal vote: %w" , err )
@@ -210,6 +217,7 @@ func (o *Observer) storeMessage(ctx context.Context, om *message) error {
210
217
om .Signature ,
211
218
om .Ticket ,
212
219
justificationMarshaled ,
220
+ om .VoteValueKey ,
213
221
); err != nil {
214
222
return fmt .Errorf ("failed to execute query: %w" , err )
215
223
}
@@ -317,7 +325,7 @@ func (o *Observer) startObserverFor(ctx context.Context, networkName gpbft.Netwo
317
325
}
318
326
}
319
327
}()
320
- if err := o .pubSub .RegisterTopicValidator (topicName , validatePubSubMessage ); err != nil {
328
+ if err := o .pubSub .RegisterTopicValidator (topicName , o . validatePubSubMessage ); err != nil {
321
329
return nil , fmt .Errorf ("failed to register topic validator: %w" , err )
322
330
}
323
331
topic , err = o .pubSub .Join (topicName , pubsub .WithTopicMessageIdFn (psutil .GPBFTMessageIdFn ))
@@ -353,7 +361,7 @@ func (o *Observer) startObserverFor(ctx context.Context, networkName gpbft.Netwo
353
361
continue
354
362
}
355
363
356
- om , err := newMessage (time .Now ().UTC (), string (networkName ), msg .ValidatorData .(gpbft. GMessage ))
364
+ om , err := newMessage (time .Now ().UTC (), string (networkName ), msg .ValidatorData .(f3. PartialGMessage ))
357
365
if err != nil {
358
366
logger .Errorw ("Failed to instantiate observation message" , "err" , err )
359
367
continue
@@ -394,11 +402,11 @@ func (o *Observer) Stop(ctx context.Context) error {
394
402
return err
395
403
}
396
404
397
- func validatePubSubMessage (_ context.Context , _ peer.ID , msg * pubsub.Message ) pubsub.ValidationResult {
398
- var gmsg gpbft. GMessage
399
- if err := gmsg . UnmarshalCBOR ( bytes . NewReader (msg .Data ) ); err != nil {
405
+ func ( o * Observer ) validatePubSubMessage (_ context.Context , _ peer.ID , msg * pubsub.Message ) pubsub.ValidationResult {
406
+ var pgmsg f3. PartialGMessage
407
+ if err := o . msgEncoding . Decode (msg .Data , & pgmsg ); err != nil {
400
408
return pubsub .ValidationReject
401
409
}
402
- msg .ValidatorData = gmsg
410
+ msg .ValidatorData = pgmsg
403
411
return pubsub .ValidationAccept
404
412
}
0 commit comments