From aee88c2d98be5185fcfcc4f4c4fe1a60ab57a3fc Mon Sep 17 00:00:00 2001 From: Viacheslav Date: Thu, 9 Jun 2022 22:19:13 +0300 Subject: [PATCH] fraud: introduce fraud service interface (#808) * fraud: introduce fraud service interface --- docs/adr/adr-006-fraud-service.md | 37 ++++++++++++++------------ fraud/interface.go | 43 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 fraud/interface.go diff --git a/docs/adr/adr-006-fraud-service.md b/docs/adr/adr-006-fraud-service.md index 3e9273884d..86ab59611e 100644 --- a/docs/adr/adr-006-fraud-service.md +++ b/docs/adr/adr-006-fraud-service.md @@ -5,6 +5,11 @@ - 2022.03.03 - init commit - 2022.03.08 - added pub-sub - 2022.03.15 - added BEFP verification +- 2022.06.08 - + * updated rsmt2d error naming(as it was changed in implementation); + * changed from NamespaceShareWithProof to ShareWithProof; + * made ProofUnmarshaler public and extended return params; + * fixed typo issues; ## Authors @@ -13,7 +18,7 @@ ## Bad Encoding Fraud Proof (BEFP) ## Context -In the case where a Full Node receives `ErrByzantineRow`/`ErrByzantineCol` from the [rsmt2d](https://github.com/celestiaorg/rsmt2d) library, it generates a fraud-proof and broadcasts it to DA network such that the light nodes are notified that the corresponding block could be malicious. +In the case where a Full Node receives `ErrByzantineData` from the [rsmt2d](https://github.com/celestiaorg/rsmt2d) library, it generates a fraud-proof and broadcasts it to DA network such that the light nodes are notified that the corresponding block could be malicious. ## Decision @@ -37,7 +42,7 @@ Based on `ErrByzantineRow`/`ErrByzantineCol` internal fields, we should generate type ErrByzantine struct { // Shares contains all shares from row/col. // For non-nil shares MerkleProof is computed - Shares []*NamespacedShareWithProof + Shares []*ShareWithProof // Index represents the number of row/col where ErrByzantineRow/ErrByzantineColl occurred. Index uint8 isRow bool @@ -64,7 +69,7 @@ type BadEncodingProof struct { // Shares contains all shares from row/col // Shares that did not pass verification in rmst2d will be nil // For non-nil shares MerkleProofs are computed - Shares []*NamespacedShareWithProof + Shares []*ShareWithProof // Index represents the number of row/col where ErrByzantineRow/ErrByzantineColl occurred Index uint8 isRow bool @@ -95,7 +100,7 @@ message BadEnconding { } ``` -`das.Daser` imports a data structure that implements `proof.Broadcaster` interface that uses libp2p.pubsub under the hood: +`das.Daser` imports a data structure that implements `fraud.Broadcaster` interface that uses libp2p.pubsub under the hood: ```go // Broadcaster is a generic interface that sends a `Proof` to all nodes subscribed on the Broadcaster's topic. @@ -122,19 +127,19 @@ type Proof interface { 2a. From the other side, light nodes will, by default, subscribe to the BEFP topic and verify messages received on the topic: ```go -type proofUnmarshaller func([]byte) Proof +type ProofUnmarshaller func([]byte) (Proof,error) // Subscriber encompasses the behavior necessary to // subscribe/unsubscribe from new FraudProofs events from the // network. type Subscriber interface { - // Subscribe allows to subscribe on pub sub topic by it's type. + // Subscribe allows to subscribe on pub sub topic by its type. // Subscribe should register pub-sub validator on topic. Subscribe(ctx context.Context, proofType ProofType) (Subscription, error) - // RegisterUnmarshaller registers unmarshaller for the given ProofType. - // If there is no umarshaller for `ProofType`, then `Subscribe` returns an error. + // RegisterUnmarshaler registers unmarshaler for the given ProofType. + // If there is no unmarshaler for `ProofType`, then `Subscribe` returns an error. RegisterUnmarshaller(proofType ProofType, f proofUnmarshaller) error - // UnregisterUnmarshaller removes unmarshaller for the given ProofType. - // If there is no unmarshaller for `ProofType`, then it returns an error. + // UnregisterUnmarshaler removes unmarshaler for the given ProofType. + // If there is no unmarshaler for `ProofType`, then it returns an error. UnregisterUnmarshaller(proofType ProofType) error{} } ``` @@ -142,7 +147,7 @@ type Subscriber interface { ```go // Subscription returns a valid proof if one is received on the topic. type Subscription interface { - Proof() (Proof, error) + Proof(context.Context) (Proof, error) Cancel() error } ``` @@ -152,14 +157,14 @@ type Subscription interface { type FraudSub struct { pubsub *pubsub.PubSub topics map[ProofType]*pubsub.Topic - unmarshallers map[ProofType]proofUnmarshaller + unmarshallers map[ProofType]ProofUnmarshaller } -func(s *FraudSub) RegisterUnmarshaller(proofType ProofType, f proofUnmarshaller) error{} -func(s *FraudSub) UnregisterUnmarshaller(proofType ProofType) error{} +func(s *service) RegisterUnmarshaler(proofType ProofType, f ProofUnmarshaller) error{} +func(s *service) UnregisterUnmarshaler(proofType ProofType) error{} -func(s *FraudSub) Subscribe(ctx context.Context, proofType ProofType) (Subscription, error){} -func(s *FraudSub) Broadcast(ctx context.Context, p Proof) error{} +func(s *service) Subscribe(ctx context.Context, proofType ProofType) (Subscription, error){} +func(s *service) Broadcast(ctx context.Context, p Proof) error{} ``` ### BEFP verification Once a light node receives a `BadEncodingProof` fraud proof, it should: diff --git a/fraud/interface.go b/fraud/interface.go new file mode 100644 index 0000000000..51944de0ea --- /dev/null +++ b/fraud/interface.go @@ -0,0 +1,43 @@ +package fraud + +import ( + "context" +) + +// ProofUnmarshaler aliases a function that parses data to `Proof`. +type ProofUnmarshaler func([]byte) (Proof, error) + +// Service encompasses the behavior necessary to subscribe and broadcast +// Fraud Proofs within the network. +type Service interface { + Subscriber + Broadcaster +} + +// Broadcaster is a generic interface that sends a `Proof` to all nodes subscribed on the Broadcaster's topic. +type Broadcaster interface { + // Broadcast takes a fraud `Proof` data structure that implements standard BinaryMarshal + // interface and broadcasts it to all subscribed peers. + Broadcast(context.Context, Proof) error +} + +// Subscriber encompasses the behavior necessary to +// subscribe/unsubscribe from new FraudProof events from the +// network. +type Subscriber interface { + // Subscribe allows to subscribe on a Proof pub sub topic by its type. + Subscribe(ProofType) (Subscription, error) + // RegisterUnmarshaler registers unmarshaler for the given ProofType. + // If there is no unmarshaler for `ProofType`, then `Subscribe` returns an error. + RegisterUnmarshaler(ProofType, ProofUnmarshaler) error + // UnregisterUnmarshaler removes unmarshaler for the given ProofType. + // If there is no unmarshaler for `ProofType`, then it returns an error. + UnregisterUnmarshaler(ProofType) error +} + +// Subscription returns a valid proof if one is received on the topic. +type Subscription interface { + // Proof returns already verified valid proof. + Proof(context.Context) (Proof, error) + Cancel() +}