internal/era: elevate anonymous func to named

This commit is contained in:
lightclient@protonmail.com 2023-05-22 16:30:56 +02:00 committed by lightclient
parent 545f0b1aa7
commit d58c9137a3
No known key found for this signature in database
GPG key ID: 75C916AFEE20183E

View file

@ -130,38 +130,17 @@ func (b *Builder) AddRLP(header, body, receipts []byte, number uint64, hash comm
b.hashes = append(b.hashes, hash) b.hashes = append(b.hashes, hash)
b.tds = append(b.tds, td) b.tds = append(b.tds, td)
// Small helper to take care snappy encoding and writing e2store entry. // Write block data.
snappyWrite := func(typ uint16, in []byte) error { if err := b.snappyWrite(TypeCompressedHeader, header); err != nil {
var ( return err
buf = b.buf
s = b.snappy
)
buf.Reset()
s.Reset(buf)
if _, err := b.snappy.Write(in); err != nil {
return fmt.Errorf("error snappy encoding: %w", err)
} }
if err := s.Flush(); err != nil { if err := b.snappyWrite(TypeCompressedBody, body); err != nil {
return fmt.Errorf("error flushing snappy encoding: %w", err) return err
} }
n, err := b.w.Write(typ, b.buf.Bytes()) if err := b.snappyWrite(TypeCompressedReceipts, receipts); err != nil {
b.written += n return err
if err != nil {
return fmt.Errorf("error writing e2store entry: %w", err)
}
return nil
} }
// Write block data.
if err := snappyWrite(TypeCompressedHeader, header); err != nil {
return err
}
if err := snappyWrite(TypeCompressedBody, body); err != nil {
return err
}
if err := snappyWrite(TypeCompressedReceipts, receipts); err != nil {
return err
}
// Also write total difficulty, but don't snappy encode. // Also write total difficulty, but don't snappy encode.
btd := bigToBytes32(td) btd := bigToBytes32(td)
n, err := b.w.Write(TypeTotalDifficulty, btd[:]) n, err := b.w.Write(TypeTotalDifficulty, btd[:])
@ -220,6 +199,28 @@ func (b *Builder) Finalize() (common.Hash, error) {
return root, nil return root, nil
} }
// snappyWrite is a small helper to take care snappy encoding and writing an e2store entry.
func (b *Builder) snappyWrite(typ uint16, in []byte) error {
var (
buf = b.buf
s = b.snappy
)
buf.Reset()
s.Reset(buf)
if _, err := b.snappy.Write(in); err != nil {
return fmt.Errorf("error snappy encoding: %w", err)
}
if err := s.Flush(); err != nil {
return fmt.Errorf("error flushing snappy encoding: %w", err)
}
n, err := b.w.Write(typ, b.buf.Bytes())
b.written += n
if err != nil {
return fmt.Errorf("error writing e2store entry: %w", err)
}
return nil
}
// writeVersion writes a version entry to e2store. // writeVersion writes a version entry to e2store.
func writeVersion(w *e2store.Writer) error { func writeVersion(w *e2store.Writer) error {
_, err := w.Write(TypeVersion, nil) _, err := w.Write(TypeVersion, nil)