mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-13 15:33:47 +00:00
feat:make spec arg
Signed-off-by: Chen Kai <281165273grape@gmail.com>
This commit is contained in:
parent
6edb2a29b3
commit
bf6dd12bad
3 changed files with 36 additions and 40 deletions
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/portalnetwork/storage"
|
"github.com/ethereum/go-ethereum/portalnetwork/storage"
|
||||||
ssz "github.com/ferranbt/fastssz"
|
ssz "github.com/ferranbt/fastssz"
|
||||||
"github.com/protolambda/zrnt/eth2/beacon/capella"
|
"github.com/protolambda/zrnt/eth2/beacon/capella"
|
||||||
|
"github.com/protolambda/zrnt/eth2/beacon/common"
|
||||||
"github.com/protolambda/ztyp/codec"
|
"github.com/protolambda/ztyp/codec"
|
||||||
"github.com/protolambda/ztyp/tree"
|
"github.com/protolambda/ztyp/tree"
|
||||||
)
|
)
|
||||||
|
|
@ -22,6 +23,7 @@ const (
|
||||||
|
|
||||||
type BeaconNetwork struct {
|
type BeaconNetwork struct {
|
||||||
portalProtocol *discover.PortalProtocol
|
portalProtocol *discover.PortalProtocol
|
||||||
|
spec *common.Spec
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bn *BeaconNetwork) GetBestUpdatesAndCommittees(firstPeriod, count uint64) (LightClientUpdateRange, error) {
|
func (bn *BeaconNetwork) GetBestUpdatesAndCommittees(firstPeriod, count uint64) (LightClientUpdateRange, error) {
|
||||||
|
|
@ -36,7 +38,7 @@ func (bn *BeaconNetwork) GetBestUpdatesAndCommittees(firstPeriod, count uint64)
|
||||||
}
|
}
|
||||||
|
|
||||||
var lightClientUpdateRange LightClientUpdateRange = make([]ForkedLightClientUpdate, 0)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -55,7 +57,7 @@ func (bn *BeaconNetwork) GetCheckpointData(checkpointHash tree.Root) (*capella.L
|
||||||
}
|
}
|
||||||
|
|
||||||
var forkedLightClientBootstrap ForkedLightClientBootstrap
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"github.com/protolambda/zrnt/eth2/beacon/altair"
|
"github.com/protolambda/zrnt/eth2/beacon/altair"
|
||||||
"github.com/protolambda/zrnt/eth2/beacon/capella"
|
"github.com/protolambda/zrnt/eth2/beacon/capella"
|
||||||
"github.com/protolambda/zrnt/eth2/beacon/common"
|
"github.com/protolambda/zrnt/eth2/beacon/common"
|
||||||
"github.com/protolambda/zrnt/eth2/configs"
|
|
||||||
"github.com/protolambda/ztyp/codec"
|
"github.com/protolambda/ztyp/codec"
|
||||||
tree "github.com/protolambda/ztyp/tree"
|
tree "github.com/protolambda/ztyp/tree"
|
||||||
)
|
)
|
||||||
|
|
@ -40,7 +39,7 @@ type ForkedLightClientBootstrap struct {
|
||||||
Bootstrap common.SpecObj
|
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[:])
|
_, err := dr.Read(flcb.ForkDigest[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -54,7 +53,7 @@ func (flcb *ForkedLightClientBootstrap) Deserialize(dr *codec.DecodingReader) er
|
||||||
return errors.New("unknown fork digest")
|
return errors.New("unknown fork digest")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = flcb.Bootstrap.Deserialize(configs.Mainnet, dr)
|
err = flcb.Bootstrap.Deserialize(spec, dr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -62,23 +61,20 @@ func (flcb *ForkedLightClientBootstrap) Deserialize(dr *codec.DecodingReader) er
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (flcb *ForkedLightClientBootstrap) Serialize(w *codec.EncodingWriter) error {
|
func (flcb *ForkedLightClientBootstrap) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
|
||||||
if err := w.Write(flcb.ForkDigest[:]); err != nil {
|
return w.Container(flcb.ForkDigest, spec.Wrap(flcb.Bootstrap))
|
||||||
return err
|
|
||||||
}
|
|
||||||
return flcb.Bootstrap.Serialize(configs.Mainnet, w)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (flcb *ForkedLightClientBootstrap) FixedLength() uint64 {
|
func (flcb *ForkedLightClientBootstrap) FixedLength(_ *common.Spec) uint64 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (flcb *ForkedLightClientBootstrap) ByteLength() uint64 {
|
func (flcb *ForkedLightClientBootstrap) ByteLength(spec *common.Spec) uint64 {
|
||||||
return 4 + flcb.Bootstrap.ByteLength(configs.Mainnet)
|
return 4 + flcb.Bootstrap.ByteLength(spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (flcb *ForkedLightClientBootstrap) HashTreeRoot(h tree.HashFn) common.Root {
|
func (flcb *ForkedLightClientBootstrap) HashTreeRoot(spec *common.Spec, h tree.HashFn) common.Root {
|
||||||
return h.HashTreeRoot(flcb.ForkDigest, configs.Mainnet.Wrap(flcb.Bootstrap))
|
return h.HashTreeRoot(flcb.ForkDigest, spec.Wrap(flcb.Bootstrap))
|
||||||
}
|
}
|
||||||
|
|
||||||
type ForkedLightClientUpdate struct {
|
type ForkedLightClientUpdate struct {
|
||||||
|
|
@ -86,7 +82,7 @@ type ForkedLightClientUpdate struct {
|
||||||
LightClientUpdate common.SpecObj
|
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[:])
|
_, err := dr.Read(flcu.ForkDigest[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -100,7 +96,7 @@ func (flcu *ForkedLightClientUpdate) Deserialize(dr *codec.DecodingReader) error
|
||||||
return errors.New("unknown fork digest")
|
return errors.New("unknown fork digest")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = flcu.LightClientUpdate.Deserialize(configs.Mainnet, dr)
|
err = flcu.LightClientUpdate.Deserialize(spec, dr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -108,57 +104,54 @@ func (flcu *ForkedLightClientUpdate) Deserialize(dr *codec.DecodingReader) error
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (flcu *ForkedLightClientUpdate) Serialize(w *codec.EncodingWriter) error {
|
func (flcu *ForkedLightClientUpdate) Serialize(spec *common.Spec, w *codec.EncodingWriter) error {
|
||||||
if err := w.Write(flcu.ForkDigest[:]); err != nil {
|
return w.Container(flcu.ForkDigest, spec.Wrap(flcu.LightClientUpdate))
|
||||||
return err
|
|
||||||
}
|
|
||||||
return flcu.LightClientUpdate.Serialize(configs.Mainnet, w)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (flcu *ForkedLightClientUpdate) FixedLength() uint64 {
|
func (flcu *ForkedLightClientUpdate) FixedLength(_ *common.Spec) uint64 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (flcu *ForkedLightClientUpdate) ByteLength() uint64 {
|
func (flcu *ForkedLightClientUpdate) ByteLength(spec *common.Spec) uint64 {
|
||||||
return 4 + flcu.LightClientUpdate.ByteLength(configs.Mainnet)
|
return 4 + flcu.LightClientUpdate.ByteLength(spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (flcu *ForkedLightClientUpdate) HashTreeRoot(h tree.HashFn) common.Root {
|
func (flcu *ForkedLightClientUpdate) HashTreeRoot(spec *common.Spec, h tree.HashFn) common.Root {
|
||||||
return h.HashTreeRoot(flcu.ForkDigest, configs.Mainnet.Wrap(flcu.LightClientUpdate))
|
return h.HashTreeRoot(flcu.ForkDigest, spec.Wrap(flcu.LightClientUpdate))
|
||||||
}
|
}
|
||||||
|
|
||||||
type LightClientUpdateRange []ForkedLightClientUpdate
|
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 {
|
return dr.List(func() codec.Deserializable {
|
||||||
i := len(*r)
|
i := len(*r)
|
||||||
*r = append(*r, ForkedLightClientUpdate{})
|
*r = append(*r, ForkedLightClientUpdate{})
|
||||||
return &((*r)[i])
|
return spec.Wrap(&((*r)[i]))
|
||||||
}, 0, 128)
|
}, 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 w.List(func(i uint64) codec.Serializable {
|
||||||
return &r[i]
|
return spec.Wrap(&r[i])
|
||||||
}, 0, uint64(len(r)))
|
}, 0, uint64(len(r)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r LightClientUpdateRange) ByteLength() (out uint64) {
|
func (r LightClientUpdateRange) ByteLength(spec *common.Spec) (out uint64) {
|
||||||
for _, v := range r {
|
for _, v := range r {
|
||||||
out += v.ByteLength() + codec.OFFSET_SIZE
|
out += v.ByteLength(spec) + codec.OFFSET_SIZE
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LightClientUpdateRange) FixedLength() uint64 {
|
func (r *LightClientUpdateRange) FixedLength(_ *common.Spec) uint64 {
|
||||||
return 0
|
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))
|
length := uint64(len(r))
|
||||||
return hFn.ComplexListHTR(func(i uint64) tree.HTR {
|
return hFn.ComplexListHTR(func(i uint64) tree.HTR {
|
||||||
if i < length {
|
if i < length {
|
||||||
return &r[i]
|
return spec.Wrap(&r[i])
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}, length, 128)
|
}, length, 128)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/protolambda/zrnt/eth2/beacon/capella"
|
"github.com/protolambda/zrnt/eth2/beacon/capella"
|
||||||
|
"github.com/protolambda/zrnt/eth2/configs"
|
||||||
"github.com/protolambda/ztyp/codec"
|
"github.com/protolambda/ztyp/codec"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
@ -26,12 +27,12 @@ func TestForkedLightClientBootstrap(t *testing.T) {
|
||||||
b, _ := hexutil.Decode(v.(map[string]interface{})["content_value"].(string))
|
b, _ := hexutil.Decode(v.(map[string]interface{})["content_value"].(string))
|
||||||
dec := codec.NewDecodingReader(bytes.NewReader(b), uint64(len(b)))
|
dec := codec.NewDecodingReader(bytes.NewReader(b), uint64(len(b)))
|
||||||
var f ForkedLightClientBootstrap
|
var f ForkedLightClientBootstrap
|
||||||
err := f.Deserialize(dec)
|
err := f.Deserialize(configs.Mainnet, dec)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, k, f.Bootstrap.(*capella.LightClientBootstrap).Header.Beacon.Slot.String())
|
assert.Equal(t, k, f.Bootstrap.(*capella.LightClientBootstrap).Header.Beacon.Slot.String())
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err = f.Serialize(codec.NewEncodingWriter(&buf))
|
err = f.Serialize(configs.Mainnet, codec.NewEncodingWriter(&buf))
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, b, buf.Bytes())
|
assert.Equal(t, b, buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
@ -50,13 +51,13 @@ func TestLightClientUpdateRange(t *testing.T) {
|
||||||
b, _ := hexutil.Decode(v.(map[string]interface{})["content_value"].(string))
|
b, _ := hexutil.Decode(v.(map[string]interface{})["content_value"].(string))
|
||||||
dec := codec.NewDecodingReader(bytes.NewReader(b), uint64(len(b)))
|
dec := codec.NewDecodingReader(bytes.NewReader(b), uint64(len(b)))
|
||||||
var f LightClientUpdateRange = make([]ForkedLightClientUpdate, 0)
|
var f LightClientUpdateRange = make([]ForkedLightClientUpdate, 0)
|
||||||
err := f.Deserialize(dec)
|
err := f.Deserialize(configs.Mainnet, dec)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, k, f[0].LightClientUpdate.(*capella.LightClientUpdate).AttestedHeader.Beacon.Slot.String())
|
assert.Equal(t, k, f[0].LightClientUpdate.(*capella.LightClientUpdate).AttestedHeader.Beacon.Slot.String())
|
||||||
assert.Equal(t, 4, len(f))
|
assert.Equal(t, 4, len(f))
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err = f.Serialize(codec.NewEncodingWriter(&buf))
|
err = f.Serialize(configs.Mainnet, codec.NewEncodingWriter(&buf))
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, b, buf.Bytes())
|
assert.Equal(t, b, buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue