Skip to content

Commit

Permalink
Dump the last 1024 bytes read from core
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Bellamy committed Dec 7, 2021
1 parent f5cd844 commit 197f388
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion ingest/ledgerbackend/buffered_meta_pipe_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ledgerbackend

import (
"bufio"
"encoding/hex"
"io"
"time"

Expand Down Expand Up @@ -67,16 +68,43 @@ type bufferedLedgerMetaReader struct {
r *bufio.Reader
c chan metaResult
decoder *xdr3.Decoder
buffer *windowBuffer
}

type windowBuffer struct {
b []byte
length int
}

func (w *windowBuffer) Write(p []byte) (n int, err error) {
lenP := len(p)
capacity := len(w.b)
if lenP >= capacity {
n = copy(w.b, p[lenP-capacity:])
w.length = capacity
return n, nil
}

w.length = (w.length + lenP) % capacity
copy(w.b, w.b[capacity-lenP:])
n = copy(w.b[capacity-lenP:], p)
return n, nil
}

func (w *windowBuffer) Bytes() []byte {
return w.b[:w.length]
}

// newBufferedLedgerMetaReader creates a new meta reader that will shutdown
// when stellar-core terminates.
func newBufferedLedgerMetaReader(reader io.Reader) *bufferedLedgerMetaReader {
r := bufio.NewReaderSize(reader, metaPipeBufferSize)
buffer := &windowBuffer{b: make([]byte, 1024, 1024)}
r := bufio.NewReaderSize(io.TeeReader(reader, buffer), metaPipeBufferSize)
return &bufferedLedgerMetaReader{
c: make(chan metaResult, ledgerReadAheadBufferSize),
r: r,
decoder: xdr3.NewDecoder(r),
buffer: buffer,
}
}

Expand All @@ -88,6 +116,7 @@ func newBufferedLedgerMetaReader(reader io.Reader) *bufferedLedgerMetaReader {
func (b *bufferedLedgerMetaReader) readLedgerMetaFromPipe() (*xdr.LedgerCloseMeta, error) {
frameLength, err := xdr.ReadFrameLength(b.decoder)
if err != nil {
log.Debug("Last read bytes:\n", hex.Dump(b.buffer.Bytes()))
return nil, errors.Wrap(err, "error reading frame length")
}

Expand Down

0 comments on commit 197f388

Please sign in to comment.