forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from GrapeBaBa/lc_api
feat:add light client types
- Loading branch information
Showing
4 changed files
with
179 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,39 @@ | ||
package api | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/beacon/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/portalnetwork/beacon" | ||
"github.com/protolambda/zrnt/eth2/beacon/capella" | ||
zrntcommon "github.com/protolambda/zrnt/eth2/beacon/common" | ||
"github.com/protolambda/ztyp/tree" | ||
) | ||
|
||
type PortalLightApi struct { | ||
bn *beacon.BeaconNetwork | ||
} | ||
|
||
func NewPortalLightApi() *PortalLightApi { | ||
return &PortalLightApi{} | ||
} | ||
|
||
func (api *PortalLightApi) GetBestUpdatesAndCommittees(firstPeriod, count uint64) ([]*types.LightClientUpdate, []*types.SerializedSyncCommittee, error) { | ||
//contentKey := &beacon.LightClientUpdateKey{ | ||
// StartPeriod: firstPeriod, | ||
// Count: count, | ||
//} | ||
|
||
//resp, err := api.httpGetf("/eth/v1/beacon/light_client/updates?start_period=%d&count=%d", firstPeriod, count) | ||
//if err != nil { | ||
// return nil, nil, err | ||
//} | ||
// | ||
//var data []CommitteeUpdate | ||
//if err := json.Unmarshal(resp, &data); err != nil { | ||
// return nil, nil, err | ||
//} | ||
//if len(data) != int(count) { | ||
// return nil, nil, errors.New("invalid number of committee updates") | ||
//} | ||
//updates := make([]*types.LightClientUpdate, int(count)) | ||
//committees := make([]*types.SerializedSyncCommittee, int(count)) | ||
//for i, d := range data { | ||
// if d.Update.AttestedHeader.Header.SyncPeriod() != firstPeriod+uint64(i) { | ||
// return nil, nil, errors.New("wrong committee update header period") | ||
// } | ||
// if err := d.Update.Validate(); err != nil { | ||
// return nil, nil, err | ||
// } | ||
// if d.NextSyncCommittee.Root() != d.Update.NextSyncCommitteeRoot { | ||
// return nil, nil, errors.New("wrong sync committee root") | ||
// } | ||
// updates[i], committees[i] = new(types.LightClientUpdate), new(types.SerializedSyncCommittee) | ||
// *updates[i], *committees[i] = d.Update, d.NextSyncCommittee | ||
//} | ||
//return updates, committees, nil | ||
|
||
return nil, nil, errors.New("not implemented") | ||
func (api *PortalLightApi) GetUpdates(firstPeriod, count uint64) (beacon.LightClientUpdateRange, error) { | ||
return api.bn.GetUpdates(firstPeriod, count) | ||
} | ||
|
||
func (api *PortalLightApi) GetCheckpointData(checkpointHash common.Hash) (*types.BootstrapData, error) { | ||
return nil, errors.New("not implemented") | ||
func (api *PortalLightApi) GetCheckpointData(checkpointHash tree.Root) (*capella.LightClientBootstrap, error) { | ||
return api.bn.GetCheckpointData(checkpointHash) | ||
} | ||
|
||
func (api *PortalLightApi) GetFinalityData() (*capella.LightClientFinalityUpdate, error) { | ||
expectedCurrentSlot := api.bn.Spec.TimeToSlot(zrntcommon.Timestamp(time.Now().Unix()), zrntcommon.Timestamp(beacon.BeaconGenesisTime)) | ||
recentEpochStart := expectedCurrentSlot - (expectedCurrentSlot % api.bn.Spec.SLOTS_PER_EPOCH) + 1 | ||
|
||
return api.bn.GetFinalityUpdate(uint64(recentEpochStart)) | ||
} | ||
|
||
func (api *PortalLightApi) GetOptimisticData() (*capella.LightClientOptimisticUpdate, error) { | ||
expectedCurrentSlot := api.bn.Spec.TimeToSlot(zrntcommon.Timestamp(time.Now().Unix()), zrntcommon.Timestamp(beacon.BeaconGenesisTime)) | ||
|
||
return api.bn.GetOptimisticUpdate(uint64(expectedCurrentSlot)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package beacon | ||
|
||
import ( | ||
"github.com/protolambda/zrnt/eth2/beacon/capella" | ||
"github.com/protolambda/zrnt/eth2/beacon/common" | ||
"github.com/protolambda/ztyp/view" | ||
) | ||
|
||
type ConsensusAPI interface { | ||
GetUpdates(firstPeriod, count uint64) (LightClientUpdateRange, error) | ||
GetCheckpointData(checkpointHash common.Root) (*capella.LightClientBootstrap, error) | ||
GetFinalityData() (*capella.LightClientFinalityUpdate, error) | ||
GetOptimisticData() (*capella.LightClientOptimisticUpdate, error) | ||
ChainID() uint64 | ||
Name() string | ||
} | ||
|
||
type LightClientStore struct { | ||
FinalizedHeader common.BeaconBlockHeader | ||
CurrentSyncCommittee common.SyncCommittee | ||
NextSyncCommittee common.SyncCommittee | ||
OptimisticHeader common.BeaconBlockHeader | ||
PreviousMaxActiveParticipants view.Uint64View | ||
CurrentMaxActiveParticipants view.Uint64View | ||
} | ||
|
||
type ConsensusLightClient struct { | ||
Store LightClientStore | ||
API ConsensusAPI | ||
InitialCheckpoint common.Root | ||
LastCheckpoint common.Root | ||
Config Config | ||
} | ||
|
||
type Config struct { | ||
ConsensusAPI string | ||
Port uint64 | ||
DefaultCheckpoint common.Root | ||
Checkpoint common.Root | ||
DataDir string | ||
ChainConfig ChainConfig | ||
Spec *common.Spec | ||
MaxCheckpointAge uint64 | ||
Fallback string | ||
LoadExternalFallback bool | ||
StrictCheckpointAge bool | ||
} | ||
|
||
type ChainConfig struct { | ||
ChainID uint64 | ||
GenesisTime uint64 | ||
GenesisRoot common.Root | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package beacon | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/protolambda/zrnt/eth2/beacon/capella" | ||
zrntcommon "github.com/protolambda/zrnt/eth2/beacon/common" | ||
"github.com/protolambda/ztyp/tree" | ||
) | ||
|
||
var _ ConsensusAPI = &PortalLightApi{} | ||
|
||
type PortalLightApi struct { | ||
bn *BeaconNetwork | ||
} | ||
|
||
func NewPortalLightApi() *PortalLightApi { | ||
return &PortalLightApi{} | ||
} | ||
|
||
func (api *PortalLightApi) GetUpdates(firstPeriod, count uint64) (LightClientUpdateRange, error) { | ||
return api.bn.GetUpdates(firstPeriod, count) | ||
} | ||
|
||
func (api *PortalLightApi) GetCheckpointData(checkpointHash tree.Root) (*capella.LightClientBootstrap, error) { | ||
return api.bn.GetCheckpointData(checkpointHash) | ||
} | ||
|
||
func (api *PortalLightApi) GetFinalityData() (*capella.LightClientFinalityUpdate, error) { | ||
expectedCurrentSlot := api.bn.Spec.TimeToSlot(zrntcommon.Timestamp(time.Now().Unix()), zrntcommon.Timestamp(BeaconGenesisTime)) | ||
recentEpochStart := expectedCurrentSlot - (expectedCurrentSlot % api.bn.Spec.SLOTS_PER_EPOCH) + 1 | ||
|
||
return api.bn.GetFinalityUpdate(uint64(recentEpochStart)) | ||
} | ||
|
||
func (api *PortalLightApi) GetOptimisticData() (*capella.LightClientOptimisticUpdate, error) { | ||
expectedCurrentSlot := api.bn.Spec.TimeToSlot(zrntcommon.Timestamp(time.Now().Unix()), zrntcommon.Timestamp(BeaconGenesisTime)) | ||
|
||
return api.bn.GetOptimisticUpdate(uint64(expectedCurrentSlot)) | ||
} | ||
|
||
func (api *PortalLightApi) ChainID() uint64 { | ||
return 1 | ||
} | ||
|
||
func (api *PortalLightApi) Name() string { | ||
return "portal" | ||
} |