Skip to content

Commit

Permalink
Merge pull request #99 from fearlessfe/portal-api
Browse files Browse the repository at this point in the history
feat: change portal api
  • Loading branch information
fearlessfe authored Apr 20, 2024
2 parents c6b57fd + 73612bf commit 1ab87da
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 122 deletions.
2 changes: 1 addition & 1 deletion cmd/shisui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func initDiscV5(config Config, conn discover.UDPConn) (*discover.UDPv5, *enode.L
PrivateKey: config.PrivateKey,
NetRestrict: config.Protocol.NetRestrict,
Bootnodes: config.Protocol.BootstrapNodes,
Log: log.New("discV5"),
Log: log.New("protocol", "discV5"),
}

nodeDB, err := enode.OpenDB(config.Protocol.NodeDBPath)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,6 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

// replace github.com/protolambda/zrnt v0.32.2 => ../zrnt-fork

replace github.com/protolambda/zrnt v0.32.2 => github.com/optimism-java/zrnt v0.32.4-0.20240415084906-d9dbf06b32f7
63 changes: 22 additions & 41 deletions portalnetwork/beacon/beacon_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"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/zrnt/eth2/beacon/common"
"github.com/protolambda/zrnt/eth2/configs"
"github.com/protolambda/ztyp/codec"
Expand Down Expand Up @@ -62,100 +61,82 @@ func (bn *BeaconNetwork) Stop() {
bn.portalProtocol.Stop()
}

func (bn *BeaconNetwork) GetUpdates(firstPeriod, count uint64) ([]*capella.LightClientUpdate, error) {
func (bn *BeaconNetwork) GetUpdates(firstPeriod, count uint64) ([]common.SpecObj, error) {
lightClientUpdateKey := &LightClientUpdateKey{
StartPeriod: firstPeriod,
Count: count,
}

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

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

updates := make([]*capella.LightClientUpdate, len(lightClientUpdateRange))
for i, update := range lightClientUpdateRange {
if update.ForkDigest != Capella {
return nil, errors.New("unknown fork digest")
}
updates[i] = update.LightClientUpdate.(*capella.LightClientUpdate)
for i, item := range lightClientUpdateRange {
res[i] = item.LightClientUpdate
}
return updates, nil
return res, nil
}

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

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

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

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

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

func (bn *BeaconNetwork) GetFinalityUpdate(finalizedSlot uint64) (*capella.LightClientFinalityUpdate, error) {
func (bn *BeaconNetwork) GetFinalityUpdate(finalizedSlot uint64) (common.SpecObj, error) {
finalityUpdateKey := &LightClientFinalityUpdateKey{
FinalizedSlot: finalizedSlot,
}

finalityUpdateValue, err := bn.getContent(LightClientFinalityUpdate, finalityUpdateKey)
data, err := bn.getContent(LightClientFinalityUpdate, finalityUpdateKey)
if err != nil {
return nil, err
}

var forkedLightClientFinalityUpdate ForkedLightClientFinalityUpdate
err = forkedLightClientFinalityUpdate.Deserialize(bn.spec, codec.NewDecodingReader(bytes.NewReader(finalityUpdateValue), uint64(len(finalityUpdateValue))))
var forkedLightClientFinalityUpdate *ForkedLightClientFinalityUpdate
err = forkedLightClientFinalityUpdate.Deserialize(bn.spec, codec.NewDecodingReader(bytes.NewReader(data), uint64(len(data))))
if err != nil {
return nil, err
}

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

return forkedLightClientFinalityUpdate.LightClientFinalityUpdate.(*capella.LightClientFinalityUpdate), nil
return forkedLightClientFinalityUpdate.LightClientFinalityUpdate, nil
}

func (bn *BeaconNetwork) GetOptimisticUpdate(optimisticSlot uint64) (*capella.LightClientOptimisticUpdate, error) {
func (bn *BeaconNetwork) GetOptimisticUpdate(optimisticSlot uint64) (common.SpecObj, error) {
optimisticUpdateKey := &LightClientOptimisticUpdateKey{
OptimisticSlot: optimisticSlot,
}

optimisticUpdateValue, err := bn.getContent(LightClientOptimisticUpdate, optimisticUpdateKey)
data, err := bn.getContent(LightClientOptimisticUpdate, optimisticUpdateKey)
if err != nil {
return nil, err
}

var forkedLightClientOptimisticUpdate ForkedLightClientOptimisticUpdate
err = forkedLightClientOptimisticUpdate.Deserialize(bn.spec, codec.NewDecodingReader(bytes.NewReader(optimisticUpdateValue), uint64(len(optimisticUpdateValue))))
var forkedLightClientOptimisticUpdate *ForkedLightClientOptimisticUpdate
err = forkedLightClientOptimisticUpdate.Deserialize(bn.spec, codec.NewDecodingReader(bytes.NewReader(data), uint64(len(data))))
if err != nil {
return nil, err
}

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

return forkedLightClientOptimisticUpdate.LightClientOptimisticUpdate.(*capella.LightClientOptimisticUpdate), nil
return forkedLightClientOptimisticUpdate.LightClientOptimisticUpdate, nil
}

func (bn *BeaconNetwork) getContent(contentType storage.ContentType, beaconContentKey ssz.Marshaler) ([]byte, error) {
Expand Down
Loading

0 comments on commit 1ab87da

Please sign in to comment.