Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding verifiers to ProofService #5

Merged
merged 6 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions fraudserv/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ type ProofService struct {
storesLk sync.RWMutex
stores map[fraud.ProofType]datastore.Datastore

pubsub *pubsub.PubSub
host host.Host
getter fraud.HeaderFetcher
ds datastore.Datastore
pubsub *pubsub.PubSub
host host.Host
getter fraud.HeaderFetcher
verifiers map[fraud.ProofType]fraud.Verifier
ds datastore.Datastore

syncerEnabled bool
}
Expand All @@ -64,6 +65,7 @@ func NewProofService(
pubsub: p,
host: host,
getter: getter,
verifiers: make(map[fraud.ProofType]fraud.Verifier),
topics: make(map[fraud.ProofType]*pubsub.Topic),
stores: make(map[fraud.ProofType]datastore.Datastore),
ds: ds,
Expand Down Expand Up @@ -138,6 +140,14 @@ func (f *ProofService) Broadcast(ctx context.Context, p fraud.Proof) error {
return t.Publish(ctx, bin)
}

func (f *ProofService) AddVerifier(proofType fraud.ProofType, verifier fraud.Verifier) error {
gupadhyaya marked this conversation as resolved.
Show resolved Hide resolved
if _, ok := f.verifiers[proofType]; ok {
return fmt.Errorf("verifier for proof type %s already exist", proofType)
gupadhyaya marked this conversation as resolved.
Show resolved Hide resolved
gupadhyaya marked this conversation as resolved.
Show resolved Hide resolved
}
f.verifiers[proofType] = verifier
return nil
}

// processIncoming encompasses the logic for validating fraud proofs.
func (f *ProofService) processIncoming(
ctx context.Context,
Expand Down Expand Up @@ -181,6 +191,20 @@ func (f *ProofService) processIncoming(
"err", err, "proofType", proof.Type(), "height", proof.Height())
return pubsub.ValidationIgnore
}

// execute the verifier for proof type if exists
if verifier, ok := f.verifiers[proofType]; ok {
status, err := verifier(proof)
if err != nil {
log.Errorw("failed to run the verifier, err:", err, "proofType", proof.Type())
gupadhyaya marked this conversation as resolved.
Show resolved Hide resolved
return pubsub.ValidationReject
gupadhyaya marked this conversation as resolved.
Show resolved Hide resolved
}
if !status {
log.Errorw("failed to verify fraud proof for proofType", proof.Type())
gupadhyaya marked this conversation as resolved.
Show resolved Hide resolved
return pubsub.ValidationReject
gupadhyaya marked this conversation as resolved.
Show resolved Hide resolved
}
}

// validate the fraud proof.
// Peer will be added to black list if the validation fails.
err = proof.Validate(extHeader)
Expand Down
4 changes: 4 additions & 0 deletions fraudserv/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-header/headertest"

gofraud "github.com/celestiaorg/go-fraud"
"github.com/celestiaorg/go-fraud/fraudtest"
)

Expand All @@ -26,6 +27,9 @@ func TestService_SubscribeBroadcastValid(t *testing.T) {
require.NoError(t, serv.Start(ctx))

fraud := fraudtest.NewValidProof()
serv.AddVerifier(fraud.Type(), func(fraudProof gofraud.Proof) (bool, error) {
return true, nil
})
gupadhyaya marked this conversation as resolved.
Show resolved Hide resolved
sub, err := serv.Subscribe(fraud.Type())
require.NoError(t, err)
defer sub.Cancel()
Expand Down
3 changes: 3 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
// HeaderFetcher aliases a function that is used to fetch an ExtendedHeader from store by height.
type HeaderFetcher func(context.Context, uint64) (header.Header, error)

// Verifier is a function that is executed as part of processing the incoming fraud proof
type Verifier func(fraud Proof) (bool, error)

// ProofUnmarshaler aliases a function that parses data to `Proof`.
type ProofUnmarshaler func([]byte) (Proof, error)

Expand Down