Skip to content

Commit

Permalink
feat:make spec arg
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Apr 7, 2024
1 parent 6edb2a2 commit bf6dd12
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
6 changes: 4 additions & 2 deletions portalnetwork/beacon/beacon_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/portalnetwork/storage"
ssz "github.com/ferranbt/fastssz"
"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/ztyp/codec"
"github.com/protolambda/ztyp/tree"
)
Expand All @@ -22,6 +23,7 @@ const (

type BeaconNetwork struct {
portalProtocol *discover.PortalProtocol
spec *common.Spec
}

func (bn *BeaconNetwork) GetBestUpdatesAndCommittees(firstPeriod, count uint64) (LightClientUpdateRange, error) {
Expand All @@ -36,7 +38,7 @@ func (bn *BeaconNetwork) GetBestUpdatesAndCommittees(firstPeriod, count uint64)
}

var lightClientUpdateRange LightClientUpdateRange = make([]ForkedLightClientUpdate, 0)
err = lightClientUpdateRange.Deserialize(codec.NewDecodingReader(bytes.NewReader(lightClientUpdateRangeContent), uint64(len(lightClientUpdateRangeContent))))
err = lightClientUpdateRange.Deserialize(bn.spec, codec.NewDecodingReader(bytes.NewReader(lightClientUpdateRangeContent), uint64(len(lightClientUpdateRangeContent))))
if err != nil {
return nil, err
}
Expand All @@ -55,7 +57,7 @@ func (bn *BeaconNetwork) GetCheckpointData(checkpointHash tree.Root) (*capella.L
}

var forkedLightClientBootstrap ForkedLightClientBootstrap
err = forkedLightClientBootstrap.Deserialize(codec.NewDecodingReader(bytes.NewReader(bootstrapValue), uint64(len(bootstrapValue))))
err = forkedLightClientBootstrap.Deserialize(bn.spec, codec.NewDecodingReader(bytes.NewReader(bootstrapValue), uint64(len(bootstrapValue))))
if err != nil {
return nil, err
}
Expand Down
61 changes: 27 additions & 34 deletions portalnetwork/beacon/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/protolambda/zrnt/eth2/beacon/altair"
"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/zrnt/eth2/configs"
"github.com/protolambda/ztyp/codec"
tree "github.com/protolambda/ztyp/tree"
)
Expand Down Expand Up @@ -40,7 +39,7 @@ type ForkedLightClientBootstrap struct {
Bootstrap common.SpecObj
}

func (flcb *ForkedLightClientBootstrap) Deserialize(dr *codec.DecodingReader) error {
func (flcb *ForkedLightClientBootstrap) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
_, err := dr.Read(flcb.ForkDigest[:])
if err != nil {
return err
Expand All @@ -54,39 +53,36 @@ func (flcb *ForkedLightClientBootstrap) Deserialize(dr *codec.DecodingReader) er
return errors.New("unknown fork digest")
}

err = flcb.Bootstrap.Deserialize(configs.Mainnet, dr)
err = flcb.Bootstrap.Deserialize(spec, dr)
if err != nil {
return err
}

return nil
}

func (flcb *ForkedLightClientBootstrap) Serialize(w *codec.EncodingWriter) error {
if err := w.Write(flcb.ForkDigest[:]); err != nil {
return err
}
return flcb.Bootstrap.Serialize(configs.Mainnet, w)
func (flcb *ForkedLightClientBootstrap) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.Container(flcb.ForkDigest, spec.Wrap(flcb.Bootstrap))
}

func (flcb *ForkedLightClientBootstrap) FixedLength() uint64 {
func (flcb *ForkedLightClientBootstrap) FixedLength(_ *common.Spec) uint64 {
return 0
}

func (flcb *ForkedLightClientBootstrap) ByteLength() uint64 {
return 4 + flcb.Bootstrap.ByteLength(configs.Mainnet)
func (flcb *ForkedLightClientBootstrap) ByteLength(spec *common.Spec) uint64 {
return 4 + flcb.Bootstrap.ByteLength(spec)
}

func (flcb *ForkedLightClientBootstrap) HashTreeRoot(h tree.HashFn) common.Root {
return h.HashTreeRoot(flcb.ForkDigest, configs.Mainnet.Wrap(flcb.Bootstrap))
func (flcb *ForkedLightClientBootstrap) HashTreeRoot(spec *common.Spec, h tree.HashFn) common.Root {
return h.HashTreeRoot(flcb.ForkDigest, spec.Wrap(flcb.Bootstrap))
}

type ForkedLightClientUpdate struct {
ForkDigest common.ForkDigest
LightClientUpdate common.SpecObj
}

func (flcu *ForkedLightClientUpdate) Deserialize(dr *codec.DecodingReader) error {
func (flcu *ForkedLightClientUpdate) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
_, err := dr.Read(flcu.ForkDigest[:])
if err != nil {
return err
Expand All @@ -100,65 +96,62 @@ func (flcu *ForkedLightClientUpdate) Deserialize(dr *codec.DecodingReader) error
return errors.New("unknown fork digest")
}

err = flcu.LightClientUpdate.Deserialize(configs.Mainnet, dr)
err = flcu.LightClientUpdate.Deserialize(spec, dr)
if err != nil {
return err
}

return nil
}

func (flcu *ForkedLightClientUpdate) Serialize(w *codec.EncodingWriter) error {
if err := w.Write(flcu.ForkDigest[:]); err != nil {
return err
}
return flcu.LightClientUpdate.Serialize(configs.Mainnet, w)
func (flcu *ForkedLightClientUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.Container(flcu.ForkDigest, spec.Wrap(flcu.LightClientUpdate))
}

func (flcu *ForkedLightClientUpdate) FixedLength() uint64 {
func (flcu *ForkedLightClientUpdate) FixedLength(_ *common.Spec) uint64 {
return 0
}

func (flcu *ForkedLightClientUpdate) ByteLength() uint64 {
return 4 + flcu.LightClientUpdate.ByteLength(configs.Mainnet)
func (flcu *ForkedLightClientUpdate) ByteLength(spec *common.Spec) uint64 {
return 4 + flcu.LightClientUpdate.ByteLength(spec)
}

func (flcu *ForkedLightClientUpdate) HashTreeRoot(h tree.HashFn) common.Root {
return h.HashTreeRoot(flcu.ForkDigest, configs.Mainnet.Wrap(flcu.LightClientUpdate))
func (flcu *ForkedLightClientUpdate) HashTreeRoot(spec *common.Spec, h tree.HashFn) common.Root {
return h.HashTreeRoot(flcu.ForkDigest, spec.Wrap(flcu.LightClientUpdate))
}

type LightClientUpdateRange []ForkedLightClientUpdate

func (r *LightClientUpdateRange) Deserialize(dr *codec.DecodingReader) error {
func (r *LightClientUpdateRange) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
return dr.List(func() codec.Deserializable {
i := len(*r)
*r = append(*r, ForkedLightClientUpdate{})
return &((*r)[i])
return spec.Wrap(&((*r)[i]))
}, 0, 128)
}

func (r LightClientUpdateRange) Serialize(w *codec.EncodingWriter) error {
func (r LightClientUpdateRange) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
return w.List(func(i uint64) codec.Serializable {
return &r[i]
return spec.Wrap(&r[i])
}, 0, uint64(len(r)))
}

func (r LightClientUpdateRange) ByteLength() (out uint64) {
func (r LightClientUpdateRange) ByteLength(spec *common.Spec) (out uint64) {
for _, v := range r {
out += v.ByteLength() + codec.OFFSET_SIZE
out += v.ByteLength(spec) + codec.OFFSET_SIZE
}
return
}

func (r *LightClientUpdateRange) FixedLength() uint64 {
func (r *LightClientUpdateRange) FixedLength(_ *common.Spec) uint64 {
return 0
}

func (r LightClientUpdateRange) HashTreeRoot(hFn tree.HashFn) common.Root {
func (r LightClientUpdateRange) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
length := uint64(len(r))
return hFn.ComplexListHTR(func(i uint64) tree.HTR {
if i < length {
return &r[i]
return spec.Wrap(&r[i])
}
return nil
}, length, 128)
Expand Down
9 changes: 5 additions & 4 deletions portalnetwork/beacon/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/configs"
"github.com/protolambda/ztyp/codec"
"github.com/stretchr/testify/assert"
)
Expand All @@ -26,12 +27,12 @@ func TestForkedLightClientBootstrap(t *testing.T) {
b, _ := hexutil.Decode(v.(map[string]interface{})["content_value"].(string))
dec := codec.NewDecodingReader(bytes.NewReader(b), uint64(len(b)))
var f ForkedLightClientBootstrap
err := f.Deserialize(dec)
err := f.Deserialize(configs.Mainnet, dec)
assert.NoError(t, err)
assert.Equal(t, k, f.Bootstrap.(*capella.LightClientBootstrap).Header.Beacon.Slot.String())

var buf bytes.Buffer
err = f.Serialize(codec.NewEncodingWriter(&buf))
err = f.Serialize(configs.Mainnet, codec.NewEncodingWriter(&buf))
assert.NoError(t, err)
assert.Equal(t, b, buf.Bytes())
}
Expand All @@ -50,13 +51,13 @@ func TestLightClientUpdateRange(t *testing.T) {
b, _ := hexutil.Decode(v.(map[string]interface{})["content_value"].(string))
dec := codec.NewDecodingReader(bytes.NewReader(b), uint64(len(b)))
var f LightClientUpdateRange = make([]ForkedLightClientUpdate, 0)
err := f.Deserialize(dec)
err := f.Deserialize(configs.Mainnet, dec)
assert.NoError(t, err)
assert.Equal(t, k, f[0].LightClientUpdate.(*capella.LightClientUpdate).AttestedHeader.Beacon.Slot.String())
assert.Equal(t, 4, len(f))

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

0 comments on commit bf6dd12

Please sign in to comment.