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

fix: preemptive panic recovery #75

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion fraudserv/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,22 @@ func (f *ProofService[H]) processIncoming(
proofType fraud.ProofType,
from peer.ID,
msg *pubsub.Message,
) pubsub.ValidationResult {
) (res pubsub.ValidationResult) {
ctx, span := tracer.Start(ctx, "process_proof", trace.WithAttributes(
attribute.String("proof_type", string(proofType)),
))
defer span.End()

defer func() {
r := recover()
if r != nil {
err := fmt.Errorf("PANIC while processing a proof: %s", r)
log.Error(err)
span.RecordError(err)
res = pubsub.ValidationReject
}
}()

// unmarshal message to the Proof.
// Peer will be added to black list if unmarshalling fails.
proof, err := f.unmarshal.Unmarshal(proofType, msg.Data)
Expand Down
22 changes: 22 additions & 0 deletions fraudserv/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ import (
"github.com/celestiaorg/go-fraud/fraudtest"
)

func TestService_processIncomingRecovery(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)

serv := newTestService(ctx, t, false)
require.NoError(t, serv.Start(ctx))

fraud := fraudtest.NewPanickingProof[*headertest.DummyHeader]()
sub, err := serv.Subscribe(fraud.Type())
require.NoError(t, err)
defer sub.Cancel()

err = serv.Broadcast(ctx, fraud)
require.Error(t, err)

ctx2, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
t.Cleanup(cancel)

_, err = sub.Proof(ctx2)
require.Error(t, err)
}

func TestService_SubscribeBroadcastValid(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)
Expand Down
14 changes: 11 additions & 3 deletions fraudtest/dummy_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ import (
const DummyProofType fraud.ProofType = "DummyProof"

type DummyProof[H header.Header[H]] struct {
Valid bool
Valid bool
Panics bool
}

func NewValidProof[H header.Header[H]]() *DummyProof[H] {
return &DummyProof[H]{true}
return &DummyProof[H]{true, false}
}

func NewInvalidProof[H header.Header[H]]() *DummyProof[H] {
return &DummyProof[H]{false}
return &DummyProof[H]{false, false}
}

func NewPanickingProof[H header.Header[H]]() *DummyProof[H] {
return &DummyProof[H]{false, true}
}

func (m *DummyProof[H]) Type() fraud.ProofType {
Expand All @@ -35,6 +40,9 @@ func (m *DummyProof[H]) Height() uint64 {
}

func (m *DummyProof[H]) Validate(H) error {
if m.Panics {
panic("crippling anxiety panic attack")
}
if !m.Valid {
return errors.New("DummyProof: proof is not valid")
}
Expand Down
Loading