blob save: add better data checking for empty blob issues (#13647)

This commit is contained in:
Preston Van Loon 2024-02-21 15:57:43 -06:00 committed by GitHub
parent 9f67ad9496
commit daad29d0de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,7 +21,9 @@ import (
) )
var ( var (
errIndexOutOfBounds = errors.New("blob index in file name >= MaxBlobsPerBlock") errIndexOutOfBounds = errors.New("blob index in file name >= MaxBlobsPerBlock")
errEmptyBlobWritten = errors.New("zero bytes written to disk when saving blob sidecar")
errSidecarEmptySSZData = errors.New("sidecar marshalled to an empty ssz byte slice")
) )
const ( const (
@ -111,7 +113,10 @@ func (bs *BlobStorage) Save(sidecar blocks.VerifiedROBlob) error {
sidecarData, err := sidecar.MarshalSSZ() sidecarData, err := sidecar.MarshalSSZ()
if err != nil { if err != nil {
return errors.Wrap(err, "failed to serialize sidecar data") return errors.Wrap(err, "failed to serialize sidecar data")
} else if len(sidecarData) == 0 {
return errSidecarEmptySSZData
} }
if err := bs.fs.MkdirAll(fname.dir(), directoryPermissions); err != nil { if err := bs.fs.MkdirAll(fname.dir(), directoryPermissions); err != nil {
return err return err
} }
@ -138,7 +143,7 @@ func (bs *BlobStorage) Save(sidecar blocks.VerifiedROBlob) error {
return errors.Wrap(err, "failed to create partial file") return errors.Wrap(err, "failed to create partial file")
} }
_, err = partialFile.Write(sidecarData) n, err := partialFile.Write(sidecarData)
if err != nil { if err != nil {
closeErr := partialFile.Close() closeErr := partialFile.Close()
if closeErr != nil { if closeErr != nil {
@ -151,6 +156,14 @@ func (bs *BlobStorage) Save(sidecar blocks.VerifiedROBlob) error {
return err return err
} }
if n != len(sidecarData) {
return fmt.Errorf("failed to write the full bytes of sidecarData, wrote only %d of %d bytes", n, len(sidecarData))
}
if n == 0 {
return errEmptyBlobWritten
}
// Atomically rename the partial file to its final name. // Atomically rename the partial file to its final name.
err = bs.fs.Rename(partPath, sszPath) err = bs.fs.Rename(partPath, sszPath)
if err != nil { if err != nil {