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
  • Loading branch information
vgonkivs committed Oct 18, 2023
1 parent b00ce70 commit 361a4a1
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

// headersThreshold specifies the maximum allowable height of the Proof
// relative to the network head to be verified.
headersThreshold 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.HeadFetcher[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.HeadFetcher[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()+headersThreshold < proof.Height() {
err = fmt.Errorf("received proof above the max threshhold."+
"maxHeight: %d, proofHeight: %d, proofType: %s",
head.Height()+headersThreshold,
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)

// HeadFetcher aliases a function that is used to get current network head.
type HeadFetcher[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 361a4a1

Please sign in to comment.