Skip to content

Commit

Permalink
feat: beacon-storage-part2
Browse files Browse the repository at this point in the history
impl get and put for LightClientFinalityUpdate and LightClientOptimisticUpdate
  • Loading branch information
fearlessfe authored and GrapeBaBa committed Apr 8, 2024
1 parent 74e0424 commit eb46343
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 48 deletions.
19 changes: 19 additions & 0 deletions portalnetwork/beacon/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type BeaconStorage struct {
db *sql.DB
log log.Logger
spec *common.Spec
cache *beaconStorageCache
}

type beaconStorageCache struct {
OptimisticUpdate []byte
FinalityUpdate []byte
}

var _ storage.ContentStorage = &BeaconStorage{}
Expand All @@ -29,6 +35,7 @@ func NewBeaconStorage(config storage.PortalStorageConfig) (storage.ContentStorag
db: config.DB,
log: log.New("beacon_storage"),
spec: config.Spec,
cache: &beaconStorageCache{},
}
if err := bs.setup(); err != nil {
return nil, err
Expand Down Expand Up @@ -58,7 +65,15 @@ func (bs *BeaconStorage) Get(contentKey []byte, contentId []byte) ([]byte, error
}
return bs.getLcUpdateValueByRange(lightClientUpdateKey.StartPeriod, lightClientUpdateKey.StartPeriod+lightClientUpdateKey.Count)
case LightClientFinalityUpdate:
if bs.cache.FinalityUpdate == nil {
return nil, storage.ErrContentNotFound
}
return bs.cache.FinalityUpdate, nil
case LightClientOptimisticUpdate:
if bs.cache.OptimisticUpdate == nil {
return nil, storage.ErrContentNotFound
}
return bs.cache.OptimisticUpdate, nil
}
return nil, nil
}
Expand Down Expand Up @@ -94,7 +109,11 @@ func (bs *BeaconStorage) Put(contentKey []byte, contentId []byte, content []byte
}
return nil
case LightClientFinalityUpdate:
bs.cache.FinalityUpdate = content
return nil
case LightClientOptimisticUpdate:
bs.cache.OptimisticUpdate = content
return nil
}
return nil
}
Expand Down
90 changes: 42 additions & 48 deletions portalnetwork/beacon/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,25 @@ func TestGetAndPut(t *testing.T) {
beaconStorage, err := genStorage(testDir)
require.NoError(t, err)
defer clearNodeData(testDir)
key, value, err := getClientBootstrap()
require.NoError(t, err)

contentId := defaultContentIdFunc(key)
_, err = beaconStorage.Get(key, contentId)
require.Equal(t, storage.ErrContentNotFound, err)

err = beaconStorage.Put(key, contentId, value)
testData, err := getTestData()
require.NoError(t, err)

res, err := beaconStorage.Get(key, contentId)
require.NoError(t, err)
require.Equal(t, value, res)
for _, entry := range testData {
key := entry.key
value := entry.value

key, value, err = getClientUpdatesByRange()
require.NoError(t, err)
contentId := defaultContentIdFunc(key)
_, err = beaconStorage.Get(key, contentId)
require.Equal(t, storage.ErrContentNotFound, err)

contentId = defaultContentIdFunc(key)
_, err = beaconStorage.Get(key, contentId)
require.Equal(t, storage.ErrContentNotFound, err)
err = beaconStorage.Put(key, contentId, value)
require.NoError(t, err)

err = beaconStorage.Put(key, contentId, value)
require.NoError(t, err)

res, err = beaconStorage.Get(key, contentId)
require.NoError(t, err)
require.Equal(t, value, res)
res, err := beaconStorage.Get(key, contentId)
require.NoError(t, err)
require.Equal(t, value, res)
}
}

func genStorage(testDir string) (storage.ContentStorage, error) {
Expand All @@ -77,38 +69,40 @@ func genStorage(testDir string) (storage.ContentStorage, error) {
return NewBeaconStorage(*config)
}

func getClientBootstrap() ([]byte, []byte, error) {
filePath := "testdata/light_client_bootstrap.json"

f, err := os.ReadFile(filePath)
if err != nil {
return nil, nil, err
}
var result map[string]map[string]string
err = json.Unmarshal(f, &result)
if err != nil {
return nil, nil, err
}
contentKey := hexutil.MustDecode(result["6718368"]["content_key"])
contentValue := hexutil.MustDecode(result["6718368"]["content_value"])
return contentKey, contentValue, nil
type entry struct {
key []byte
value []byte
}

func getClientUpdatesByRange() ([]byte, []byte, error) {
filePath := "testdata/light_client_updates_by_range.json"

f, err := os.ReadFile(filePath)
func getTestData() ([]entry, error) {
baseDir := "./testdata"
items, err := os.ReadDir(baseDir)
if err != nil {
return nil, nil, err
return nil, err
}
var result map[string]map[string]string
err = json.Unmarshal(f, &result)
if err != nil {
return nil, nil, err

entries := make([]entry, 0)

for _, item := range items {
if !item.IsDir() {
f, err := os.ReadFile(fmt.Sprintf("%s/%s", baseDir, item.Name()))
if err != nil {
return nil, err
}
var result map[string]map[string]string
err = json.Unmarshal(f, &result)
if err != nil {
return nil, err
}
for _, v := range result {
entries = append(entries, entry{
key: hexutil.MustDecode(v["content_key"]),
value: hexutil.MustDecode(v["content_value"]),
})
}
}
}
contentKey := hexutil.MustDecode(result["6684738"]["content_key"])
contentValue := hexutil.MustDecode(result["6684738"]["content_value"])
return contentKey, contentValue, nil
return entries, nil
}

func clearNodeData(nodeDataDir string) {
Expand Down

0 comments on commit eb46343

Please sign in to comment.