Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add remain light client content type #74

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions portalnetwork/beacon/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,147 @@ func (r LightClientUpdateRange) HashTreeRoot(spec *common.Spec, hFn tree.HashFn)
return nil
}, length, 128)
}

type ForkedLightClientOptimisticUpdate struct {
ForkDigest common.ForkDigest
LightClientOptimisticUpdate common.SpecObj
}

func (flcou *ForkedLightClientOptimisticUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
_, err := dr.Read(flcou.ForkDigest[:])
if err != nil {
return err
}

if flcou.ForkDigest == Bellatrix {
flcou.LightClientOptimisticUpdate = &altair.LightClientOptimisticUpdate{}
} else if flcou.ForkDigest == Capella {
flcou.LightClientOptimisticUpdate = &capella.LightClientOptimisticUpdate{}
} else {
return errors.New("unknown fork digest")
}

err = flcou.LightClientOptimisticUpdate.Deserialize(spec, dr)
if err != nil {
return err
}

return nil
}

func (flcou *ForkedLightClientOptimisticUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.Container(flcou.ForkDigest, spec.Wrap(flcou.LightClientOptimisticUpdate))
}

func (flcou *ForkedLightClientOptimisticUpdate) FixedLength(_ *common.Spec) uint64 {
return 0
}

func (flcou *ForkedLightClientOptimisticUpdate) ByteLength(spec *common.Spec) uint64 {
return 4 + flcou.LightClientOptimisticUpdate.ByteLength(spec)
}

func (flcou *ForkedLightClientOptimisticUpdate) HashTreeRoot(spec *common.Spec, h tree.HashFn) common.Root {
return h.HashTreeRoot(flcou.ForkDigest, spec.Wrap(flcou.LightClientOptimisticUpdate))
}

type ForkedLightClientFinalityUpdate struct {
ForkDigest common.ForkDigest
LightClientFinalityUpdate common.SpecObj
}

func (flcfu *ForkedLightClientFinalityUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
_, err := dr.Read(flcfu.ForkDigest[:])
if err != nil {
return err
}

if flcfu.ForkDigest == Bellatrix {
flcfu.LightClientFinalityUpdate = &altair.LightClientFinalityUpdate{}
} else if flcfu.ForkDigest == Capella {
flcfu.LightClientFinalityUpdate = &capella.LightClientFinalityUpdate{}
} else {
return errors.New("unknown fork digest")
}

err = flcfu.LightClientFinalityUpdate.Deserialize(spec, dr)
if err != nil {
return err
}

return nil
}

func (flcfu *ForkedLightClientFinalityUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.Container(flcfu.ForkDigest, spec.Wrap(flcfu.LightClientFinalityUpdate))
}

func (flcfu *ForkedLightClientFinalityUpdate) FixedLength(_ *common.Spec) uint64 {
return 0
}

func (flcfu *ForkedLightClientFinalityUpdate) ByteLength(spec *common.Spec) uint64 {
return 4 + flcfu.LightClientFinalityUpdate.ByteLength(spec)
}

func (flcfu *ForkedLightClientFinalityUpdate) HashTreeRoot(spec *common.Spec, h tree.HashFn) common.Root {
return h.HashTreeRoot(flcfu.ForkDigest, spec.Wrap(flcfu.LightClientFinalityUpdate))
}

type HistoricalSummariesProof struct {
Proof [5]common.Bytes32
}

func (hsp *HistoricalSummariesProof) Deserialize(dr *codec.DecodingReader) error {
roots := hsp.Proof[:]
return tree.ReadRoots(dr, &roots, 5)
}

func (hsp *HistoricalSummariesProof) Serialize(w *codec.EncodingWriter) error {
return tree.WriteRoots(w, hsp.Proof[:])
}

func (hsp *HistoricalSummariesProof) ByteLength() uint64 {
return 32 * 5
}

func (hsp *HistoricalSummariesProof) FixedLength() uint64 {
return 32 * 5
}

func (hsp *HistoricalSummariesProof) HashTreeRoot(hFn tree.HashFn) common.Root {
return hFn.ComplexVectorHTR(func(i uint64) tree.HTR {
if i < 5 {
return &hsp.Proof[i]
}
return nil
}, 5)
}

// TODO: Add tests for HistoricalSummariesWithProof

type HistoricalSummariesWithProof struct {
EPOCH common.Epoch
HistoricalSummaries capella.HistoricalSummaries
Proof *HistoricalSummariesProof
}

func (hswp *HistoricalSummariesWithProof) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
return dr.Container(&hswp.EPOCH, spec.Wrap(&hswp.HistoricalSummaries), hswp.Proof)
}

func (hswp *HistoricalSummariesWithProof) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.Container(hswp.EPOCH, spec.Wrap(&hswp.HistoricalSummaries), hswp.Proof)
}

func (hswp *HistoricalSummariesWithProof) ByteLength(spec *common.Spec) uint64 {
return codec.ContainerLength(hswp.EPOCH, spec.Wrap(&hswp.HistoricalSummaries), hswp.Proof)
}

func (hswp *HistoricalSummariesWithProof) FixedLength(_ *common.Spec) uint64 {
return 0
}

func (hswp *HistoricalSummariesWithProof) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(hswp.EPOCH, spec.Wrap(&hswp.HistoricalSummaries), hswp.Proof)
}
48 changes: 48 additions & 0 deletions portalnetwork/beacon/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,51 @@ func TestLightClientUpdateRange(t *testing.T) {
assert.Equal(t, b, buf.Bytes())
}
}

func TestForkedLightClientOptimisticUpdate(t *testing.T) {
filePath := "testdata/light_client_optimistic_update.json"

f, _ := os.Open(filePath)
jsonStr, _ := io.ReadAll(f)

var result map[string]interface{}
_ = json.Unmarshal(jsonStr, &result)

for k, v := range result {
b, _ := hexutil.Decode(v.(map[string]interface{})["content_value"].(string))
dec := codec.NewDecodingReader(bytes.NewReader(b), uint64(len(b)))
var f ForkedLightClientOptimisticUpdate
err := f.Deserialize(configs.Mainnet, dec)
assert.NoError(t, err)
assert.Equal(t, k, f.LightClientOptimisticUpdate.(*capella.LightClientOptimisticUpdate).AttestedHeader.Beacon.Slot.String())

var buf bytes.Buffer
err = f.Serialize(configs.Mainnet, codec.NewEncodingWriter(&buf))
assert.NoError(t, err)
assert.Equal(t, b, buf.Bytes())
}
}

func TestForkedLightClientFinalityUpdate(t *testing.T) {
filePath := "testdata/light_client_finality_update.json"

f, _ := os.Open(filePath)
jsonStr, _ := io.ReadAll(f)

var result map[string]interface{}
_ = json.Unmarshal(jsonStr, &result)

for k, v := range result {
b, _ := hexutil.Decode(v.(map[string]interface{})["content_value"].(string))
dec := codec.NewDecodingReader(bytes.NewReader(b), uint64(len(b)))
var f ForkedLightClientFinalityUpdate
err := f.Deserialize(configs.Mainnet, dec)
assert.NoError(t, err)
assert.Equal(t, k, f.LightClientFinalityUpdate.(*capella.LightClientFinalityUpdate).AttestedHeader.Beacon.Slot.String())

var buf bytes.Buffer
err = f.Serialize(configs.Mainnet, codec.NewEncodingWriter(&buf))
assert.NoError(t, err)
assert.Equal(t, b, buf.Bytes())
}
}