Skip to content

Commit

Permalink
feat(proof/service)!: check that proof height is less than available …
Browse files Browse the repository at this point in the history
…threshold (#36)

## Overview

Fixes #35
  • Loading branch information
vgonkivs authored Oct 19, 2023
1 parent 17c7098 commit 642bb01
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
42 changes: 35 additions & 7 deletions fraudserv/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ var (
tracer = otel.Tracer("fraudserv")
)

// fraudRequests is the amount of external requests that will be tried to get fraud proofs from
// other peers.
const fraudRequests = 5
const (
// fraudRequests is the amount of external requests that will be tried to get fraud proofs from
// other peers.
fraudRequests = 5

// headThreshold specifies the maximum allowable height of the Proof
// relative to the network head to be verified.
headThreshold uint64 = 20
)

// ProofService is responsible for validating and propagating Fraud Proofs.
// It implements the Service interface.
Expand All @@ -51,7 +57,8 @@ type ProofService[H header.Header[H]] struct {

pubsub *pubsub.PubSub
host host.Host
getter fraud.HeaderFetcher[H]
headerGetter fraud.HeaderFetcher[H]
headGetter fraud.HeadGetter[H]
unmarshal fraud.ProofUnmarshaler[H]
ds datastore.Datastore
syncerEnabled bool
Expand All @@ -60,7 +67,8 @@ type ProofService[H header.Header[H]] struct {
func NewProofService[H header.Header[H]](
p *pubsub.PubSub,
host host.Host,
getter fraud.HeaderFetcher[H],
headerGetter fraud.HeaderFetcher[H],
headGetter fraud.HeadGetter[H],
unmarshal fraud.ProofUnmarshaler[H],
ds datastore.Datastore,
syncerEnabled bool,
Expand All @@ -69,7 +77,8 @@ func NewProofService[H header.Header[H]](
return &ProofService[H]{
pubsub: p,
host: host,
getter: getter,
headerGetter: headerGetter,
headGetter: headGetter,
unmarshal: unmarshal,
verifiers: make(map[fraud.ProofType]fraud.Verifier[H]),
topics: make(map[fraud.ProofType]*pubsub.Topic),
Expand Down Expand Up @@ -196,10 +205,29 @@ func (f *ProofService[H]) processIncoming(
return pubsub.ValidationIgnore
}

head, err := f.headGetter(ctx)
if err != nil {
log.Errorw("failed to fetch current network head to verify a fraud proof",
"err", err, "proofType", proof.Type(), "height", proof.Height())
return pubsub.ValidationIgnore
}

if head.Height()+headThreshold < proof.Height() {
err = fmt.Errorf("received proof above the max threshold."+
"maxHeight: %d, proofHeight: %d, proofType: %s",
head.Height()+headThreshold,
proof.Height(),
proof.Type(),
)
log.Error(err)
span.RecordError(err)
return pubsub.ValidationReject
}

msg.ValidatorData = proof

// fetch extended header in order to verify the fraud proof.
extHeader, err := f.getter(ctx, proof.Height())
extHeader, err := f.headerGetter(ctx, proof.Height())
if err != nil {
log.Errorw("failed to fetch header to verify a fraud proof",
"err", err, "proofType", proof.Type(), "height", proof.Height())
Expand Down
3 changes: 3 additions & 0 deletions fraudserv/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ func newTestServiceWithHost(
func(ctx context.Context, u uint64) (*headertest.DummyHeader, error) {
return store.GetByHeight(ctx, u)
},
func(ctx context.Context) (*headertest.DummyHeader, error) {
return store.Head(ctx)
},
unmarshaler,
sync.MutexWrap(datastore.NewMapDatastore()),
enabledSyncer,
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[H header.Header[H]] func(context.Context, uint64) (H, error)

// HeadGetter aliases a function that is used to get current network head.
type HeadGetter[H header.Header[H]] func(ctx context.Context) (H, error)

// Verifier is a function that is executed as part of processing the incoming fraud proof
type Verifier[H header.Header[H]] func(fraud Proof[H]) (bool, error)

Expand Down

0 comments on commit 642bb01

Please sign in to comment.