Skip to content

Commit

Permalink
fraud: introduce fraud service interface (#808)
Browse files Browse the repository at this point in the history
* fraud: introduce fraud service interface
  • Loading branch information
vgonkivs authored Jun 9, 2022
1 parent 87beabf commit aee88c2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
37 changes: 21 additions & 16 deletions docs/adr/adr-006-fraud-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -122,27 +127,27 @@ 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{}
}
```

```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
}
```
Expand All @@ -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:
Expand Down
43 changes: 43 additions & 0 deletions fraud/interface.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit aee88c2

Please sign in to comment.