-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/serve-blob-sidecars' into release/serving-bundle-fix
- Loading branch information
Showing
10 changed files
with
326 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package store | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/attestantio/go-eth2-client/spec/deneb" | ||
"github.com/attestantio/go-eth2-client/spec/phase0" | ||
"github.com/ethpandaops/checkpointz/pkg/cache" | ||
"github.com/ethpandaops/checkpointz/pkg/eth" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type BlobSidecar struct { | ||
store *cache.TTLMap | ||
log logrus.FieldLogger | ||
} | ||
|
||
func NewBlobSidecar(log logrus.FieldLogger, config Config, namespace string) *BlobSidecar { | ||
d := &BlobSidecar{ | ||
log: log.WithField("component", "beacon/store/blob_sidecar"), | ||
store: cache.NewTTLMap(config.MaxItems, "blob_sidecar", namespace), | ||
} | ||
|
||
d.store.OnItemDeleted(func(key string, value interface{}, expiredAt time.Time) { | ||
d.log.WithField("key", key).WithField("expired_at", expiredAt.String()).Debug("Blob sidecar was deleted from the cache") | ||
}) | ||
|
||
d.store.EnableMetrics(namespace) | ||
|
||
return d | ||
} | ||
|
||
func (d *BlobSidecar) Add(slot phase0.Slot, sidecars []*deneb.BlobSidecar, expiresAt time.Time) error { | ||
d.store.Add(eth.SlotAsString(slot), sidecars, expiresAt, false) | ||
|
||
d.log.WithFields( | ||
logrus.Fields{ | ||
"slot": eth.SlotAsString(slot), | ||
"expires_at": expiresAt.String(), | ||
}, | ||
).Debug("Added blob sidecar") | ||
|
||
return nil | ||
} | ||
|
||
func (d *BlobSidecar) GetBySlot(slot phase0.Slot) ([]*deneb.BlobSidecar, error) { | ||
data, _, err := d.store.Get(eth.SlotAsString(slot)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return d.parseSidecar(data) | ||
} | ||
|
||
func (d *BlobSidecar) parseSidecar(data interface{}) ([]*deneb.BlobSidecar, error) { | ||
sidecar, ok := data.([]*deneb.BlobSidecar) | ||
if !ok { | ||
return nil, errors.New("invalid blob sidecar type") | ||
} | ||
|
||
return sidecar, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package store | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/attestantio/go-eth2-client/spec/deneb" | ||
"github.com/attestantio/go-eth2-client/spec/phase0" | ||
"github.com/sirupsen/logrus/hooks/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBlobSidecarAddAndGet(t *testing.T) { | ||
logger, _ := test.NewNullLogger() | ||
config := Config{MaxItems: 10} | ||
namespace := "test" | ||
blobSidecarStore := NewBlobSidecar(logger, config, namespace) | ||
|
||
slot := phase0.Slot(100) | ||
expiresAt := time.Now().Add(10 * time.Minute) | ||
sidecars := []*deneb.BlobSidecar{ | ||
{ | ||
Blob: deneb.Blob{}, | ||
}, | ||
} | ||
|
||
err := blobSidecarStore.Add(slot, sidecars, expiresAt) | ||
assert.NoError(t, err) | ||
|
||
retrievedSidecars, err := blobSidecarStore.GetBySlot(slot) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, retrievedSidecars) | ||
assert.Equal(t, sidecars, retrievedSidecars) | ||
} | ||
|
||
func TestBlobSidecarGetBySlotNotFound(t *testing.T) { | ||
logger, _ := test.NewNullLogger() | ||
config := Config{MaxItems: 10} | ||
namespace := "test" | ||
blobSidecarStore := NewBlobSidecar(logger, config, namespace) | ||
|
||
slot := phase0.Slot(200) | ||
|
||
retrievedSidecars, err := blobSidecarStore.GetBySlot(slot) | ||
assert.Error(t, err) | ||
assert.Nil(t, retrievedSidecars) | ||
} |
Oops, something went wrong.