Skip to content

Commit

Permalink
feat:add lc bootstrap and update range
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 f82ef2e commit 6edb2a2
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 19 deletions.
5 changes: 5 additions & 0 deletions beacon/light/api/portal_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"

"github.com/ethereum/go-ethereum/beacon/types"
"github.com/ethereum/go-ethereum/common"
)

type PortalLightApi struct {
Expand Down Expand Up @@ -50,3 +51,7 @@ func (api *PortalLightApi) GetBestUpdatesAndCommittees(firstPeriod, count uint64

return nil, nil, errors.New("not implemented")
}

func (api *PortalLightApi) GetCheckpointData(checkpointHash common.Hash) (*types.BootstrapData, error) {
return nil, errors.New("not implemented")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,4 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/protolambda/zrnt v0.32.2 => github.com/optimism-java/zrnt v0.32.4-0.20240402113914-188558bfad88
replace github.com/protolambda/zrnt v0.32.2 => github.com/optimism-java/zrnt v0.32.4-0.20240403132616-04d1d446b0da
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsq
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/optimism-java/utp-go v0.0.0-20240309041853-b6b3a0dea581 h1:ZxgrtI0xIw+clB32iDDDWaiTcCizTeN7rNyzH9YorPI=
github.com/optimism-java/utp-go v0.0.0-20240309041853-b6b3a0dea581/go.mod h1:DZ0jYzLzt4ZsCmhI/iqYgGFoNx45OfpEoKzXB8HVALQ=
github.com/optimism-java/zrnt v0.32.4-0.20240402113914-188558bfad88 h1:hYCtTSAgXOtapscLmr6OgCQJulRjF3K7wZlvh9Zzrwk=
github.com/optimism-java/zrnt v0.32.4-0.20240402113914-188558bfad88/go.mod h1:A0fezkp9Tt3GBLATSPIbuY4ywYESyAuc/FFmPKg8Lqs=
github.com/optimism-java/zrnt v0.32.4-0.20240403132616-04d1d446b0da h1:s8V7G1gRW5EeyHYCDVW3O01gEm2BVAvV1cLPZ+WIB1E=
github.com/optimism-java/zrnt v0.32.4-0.20240403132616-04d1d446b0da/go.mod h1:A0fezkp9Tt3GBLATSPIbuY4ywYESyAuc/FFmPKg8Lqs=
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 h1:oYW+YCJ1pachXTQmzR3rNLYGGz4g/UgFcjb28p/viDM=
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0=
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
Expand Down
3 changes: 2 additions & 1 deletion p2p/discover/portal_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"math/big"
"math/rand"
"net"
"slices"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -1223,7 +1224,7 @@ func (p *PortalProtocol) verifyResponseNode(sender *enode.Node, r *enr.Record, d
}
if distances != nil {
nd := enode.LogDist(sender.ID(), n.ID())
if !containsUint(uint(nd), distances) {
if !slices.Contains(distances, uint(nd)) {
return nil, errors.New("does not match any requested distance")
}
}
Expand Down
42 changes: 37 additions & 5 deletions portalnetwork/beacon/beacon_network.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package beacon

import (
"bytes"
"errors"

"github.com/ethereum/go-ethereum/beacon/types"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/portalnetwork/storage"
ssz "github.com/ferranbt/fastssz"
"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/ztyp/codec"
"github.com/protolambda/ztyp/tree"
)

const (
Expand All @@ -21,18 +24,47 @@ type BeaconNetwork struct {
portalProtocol *discover.PortalProtocol
}

func (bn *BeaconNetwork) GetBestUpdatesAndCommittees(firstPeriod, count uint64) ([]*types.LightClientUpdate, []*types.SerializedSyncCommittee, error) {
func (bn *BeaconNetwork) GetBestUpdatesAndCommittees(firstPeriod, count uint64) (LightClientUpdateRange, error) {
lightClientUpdateKey := &LightClientUpdateKey{
StartPeriod: firstPeriod,
Count: count,
}

_, err := bn.getContent(LightClientUpdate, lightClientUpdateKey)
lightClientUpdateRangeContent, err := bn.getContent(LightClientUpdate, lightClientUpdateKey)
if err != nil {
return nil, nil, err
return nil, err
}

var lightClientUpdateRange LightClientUpdateRange = make([]ForkedLightClientUpdate, 0)
err = lightClientUpdateRange.Deserialize(codec.NewDecodingReader(bytes.NewReader(lightClientUpdateRangeContent), uint64(len(lightClientUpdateRangeContent))))
if err != nil {
return nil, err
}

return lightClientUpdateRange, nil
}

func (bn *BeaconNetwork) GetCheckpointData(checkpointHash tree.Root) (*capella.LightClientBootstrap, error) {
bootstrapKey := &LightClientBootstrapKey{
BlockHash: checkpointHash[:],
}

bootstrapValue, err := bn.getContent(LightClientBootstrap, bootstrapKey)
if err != nil {
return nil, err
}

var forkedLightClientBootstrap ForkedLightClientBootstrap
err = forkedLightClientBootstrap.Deserialize(codec.NewDecodingReader(bytes.NewReader(bootstrapValue), uint64(len(bootstrapValue))))
if err != nil {
return nil, err
}

if forkedLightClientBootstrap.ForkDigest != Capella {
return nil, errors.New("unknown fork digest")
}

return nil, nil, err
return forkedLightClientBootstrap.Bootstrap.(*capella.LightClientBootstrap), nil
}

func (bn *BeaconNetwork) getContent(contentType storage.ContentType, beaconContentKey ssz.Marshaler) ([]byte, error) {
Expand Down
117 changes: 108 additions & 9 deletions portalnetwork/beacon/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/zrnt/eth2/configs"
"github.com/protolambda/ztyp/codec"
tree "github.com/protolambda/ztyp/tree"
)

var (
Bellatrix common.ForkDigest = [4]byte{0x0, 0x0, 0x0, 0x0}
Capella common.ForkDigest = [4]byte{0xbb, 0xa4, 0xda, 0x96}
)

//go:generate sszgen --path types.go
//go:generate sszgen --path types.go --exclude-objs ForkedLightClientBootstrap,ForkedLightClientUpdate,LightClientUpdateRange

type LightClientUpdateKey struct {
StartPeriod uint64
Expand All @@ -39,28 +40,126 @@ type ForkedLightClientBootstrap struct {
Bootstrap common.SpecObj
}

func (flb *ForkedLightClientBootstrap) Deserialize(dr *codec.DecodingReader) error {
_, err := dr.Read(flb.ForkDigest[:])
func (flcb *ForkedLightClientBootstrap) Deserialize(dr *codec.DecodingReader) error {
_, err := dr.Read(flcb.ForkDigest[:])
if err != nil {
return err
}

if flb.ForkDigest == Bellatrix {
flb.Bootstrap = &altair.LightClientBootstrap{}
} else if flb.ForkDigest == Capella {
flb.Bootstrap = &capella.LightClientBootstrap{}
if flcb.ForkDigest == Bellatrix {
flcb.Bootstrap = &altair.LightClientBootstrap{}
} else if flcb.ForkDigest == Capella {
flcb.Bootstrap = &capella.LightClientBootstrap{}
} else {
return errors.New("unknown fork digest")
}

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

return nil
}

func (flb *ForkedLightClientBootstrap) FixedLength() uint64 {
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) FixedLength() uint64 {
return 0
}

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

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

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

func (flcu *ForkedLightClientUpdate) Deserialize(dr *codec.DecodingReader) error {
_, err := dr.Read(flcu.ForkDigest[:])
if err != nil {
return err
}

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

err = flcu.LightClientUpdate.Deserialize(configs.Mainnet, 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) FixedLength() uint64 {
return 0
}

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

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

type LightClientUpdateRange []ForkedLightClientUpdate

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

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

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

func (r *LightClientUpdateRange) FixedLength() uint64 {
return 0
}

func (r LightClientUpdateRange) HashTreeRoot(hFn tree.HashFn) common.Root {
length := uint64(len(r))
return hFn.ComplexListHTR(func(i uint64) tree.HTR {
if i < length {
return &r[i]
}
return nil
}, length, 128)
}
2 changes: 1 addition & 1 deletion portalnetwork/beacon/types_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions portalnetwork/beacon/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,35 @@ func TestForkedLightClientBootstrap(t *testing.T) {
err := f.Deserialize(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))
assert.NoError(t, err)
assert.Equal(t, b, buf.Bytes())
}
}

func TestLightClientUpdateRange(t *testing.T) {
filePath := "testdata/light_client_updates_by_range.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 LightClientUpdateRange = make([]ForkedLightClientUpdate, 0)
err := f.Deserialize(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))
assert.NoError(t, err)
assert.Equal(t, b, buf.Bytes())
}
}

0 comments on commit 6edb2a2

Please sign in to comment.