Commit 345172f7 authored by inphi's avatar inphi

op-program: Hint reader recovery

Attempt to recover the hint read stream when the NextHint callback fails
parent e6f1f61c
......@@ -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 {
......
......@@ -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")
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment