diff --git a/op-program/preimage/hints.go b/op-program/preimage/hints.go index bcbdab29edb7..3757ccf0976e 100644 --- a/op-program/preimage/hints.go +++ b/op-program/preimage/hints.go @@ -55,6 +55,8 @@ func (hr *HintReader) NextHint(router func(hint string) error) error { } } if err := router(string(payload)); err != nil { + // stream recovery + _, _ = hr.r.Read([]byte{0}) return fmt.Errorf("failed to handle hint: %w", err) } if _, err := hr.r.Read([]byte{0}); err != nil { diff --git a/op-program/preimage/hints_test.go b/op-program/preimage/hints_test.go index 1845b1906d92..7d9023c6db83 100644 --- a/op-program/preimage/hints_test.go +++ b/op-program/preimage/hints_test.go @@ -3,6 +3,7 @@ package preimage import ( "bytes" "crypto/rand" + "errors" "io" "testing" @@ -71,4 +72,21 @@ func TestHints(t *testing.T) { err := hr.NextHint(func(hint string) error { return nil }) require.ErrorIs(t, err, io.ErrUnexpectedEOF) }) + t.Run("cb error", func(t *testing.T) { + var buf bytes.Buffer + hw := NewHintWriter(&buf) + hw.Hint(rawHint("one")) + hw.Hint(rawHint("two")) + hr := NewHintReader(&buf) + cbErr := errors.New("fail") + err := hr.NextHint(func(hint string) error { return cbErr }) + require.ErrorIs(t, err, cbErr) + var readHint string + err = hr.NextHint(func(hint string) error { + readHint = hint + return nil + }) + require.NoError(t, err) + require.Equal(t, readHint, "two") + }) }