Skip to content

Commit

Permalink
blob save: add better data checking for empty blob issues (#13647)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon committed Feb 21, 2024
1 parent 9f67ad9 commit daad29d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions beacon-chain/db/filesystem/blob.go
Expand Up @@ -21,7 +21,9 @@ import (
)

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 (
Expand Down Expand Up @@ -111,7 +113,10 @@ func (bs *BlobStorage) Save(sidecar blocks.VerifiedROBlob) error {
sidecarData, err := sidecar.MarshalSSZ()
if err != nil {
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 {
return err
}
Expand All @@ -138,7 +143,7 @@ func (bs *BlobStorage) Save(sidecar blocks.VerifiedROBlob) error {
return errors.Wrap(err, "failed to create partial file")
}

_, err = partialFile.Write(sidecarData)
n, err := partialFile.Write(sidecarData)
if err != nil {
closeErr := partialFile.Close()
if closeErr != nil {
Expand All @@ -151,6 +156,14 @@ func (bs *BlobStorage) Save(sidecar blocks.VerifiedROBlob) error {
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.
err = bs.fs.Rename(partPath, sszPath)
if err != nil {
Expand Down

0 comments on commit daad29d

Please sign in to comment.