Skip to content

Commit

Permalink
Merge pull request #82 from GrapeBaBa/portal1
Browse files Browse the repository at this point in the history
feat:light client init bootstrap
  • Loading branch information
fearlessfe authored Apr 10, 2024
2 parents c203f9a + 3cb15c2 commit d02baa2
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 39 deletions.
39 changes: 0 additions & 39 deletions beacon/light/api/portal_api.go

This file was deleted.

84 changes: 84 additions & 0 deletions portalnetwork/beacon/light_client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package beacon

import (
"errors"
"fmt"
"time"

"github.com/ethereum/go-ethereum/log"
"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/util/merkle"
"github.com/protolambda/ztyp/tree"
"github.com/protolambda/ztyp/view"
)

Expand Down Expand Up @@ -30,6 +38,7 @@ type ConsensusLightClient struct {
InitialCheckpoint common.Root
LastCheckpoint common.Root
Config Config
Logger log.Logger
}

type Config struct {
Expand All @@ -51,3 +60,78 @@ type ChainConfig struct {
GenesisTime uint64
GenesisRoot common.Root
}

//lint:ignore U1000 placeholder function
func (c *ConsensusLightClient) bootstrap() error {
bootstrap, err := c.API.GetCheckpointData(c.InitialCheckpoint)
if err != nil {
return err
}

isValid := c.isValidCheckpoint(bootstrap.Header.Beacon.Slot)
if !isValid {
if c.Config.StrictCheckpointAge {
return errors.New("checkpoint is too old")
} else {
c.Logger.Warn("checkpoint is too old")
}
}

committeeValid := c.isCurrentCommitteeProofValid(bootstrap.Header.Beacon, bootstrap.CurrentSyncCommittee, bootstrap.CurrentSyncCommitteeBranch)

headerHash := bootstrap.Header.Beacon.HashTreeRoot(tree.GetHashFn()).String()
expectedHash := c.InitialCheckpoint.String()

headerValid := headerHash == expectedHash

if !headerValid {
return fmt.Errorf("header hash %s does not match expected hash %s", headerHash, expectedHash)
}

if !committeeValid {
return errors.New("committee proof is invalid")
}

c.Store = LightClientStore{
FinalizedHeader: bootstrap.Header.Beacon,
CurrentSyncCommittee: bootstrap.CurrentSyncCommittee,
OptimisticHeader: bootstrap.Header.Beacon,
PreviousMaxActiveParticipants: view.Uint64View(0),
CurrentMaxActiveParticipants: view.Uint64View(0),
}

return nil
}

func (c *ConsensusLightClient) isValidCheckpoint(blockHashSlot common.Slot) bool {
currentSlot := c.expectedCurrentSlot()
currentSlotTimestamp, err := c.slotTimestamp(currentSlot)
if err != nil {
return false
}
blockHashSlotTimestamp, err := c.slotTimestamp(blockHashSlot)
if err != nil {
return false
}

slotAge := currentSlotTimestamp - blockHashSlotTimestamp

return uint64(slotAge) < c.Config.MaxCheckpointAge
}

func (c *ConsensusLightClient) expectedCurrentSlot() common.Slot {
return c.Config.Spec.TimeToSlot(common.Timestamp(time.Now().Unix()), common.Timestamp(c.Config.ChainConfig.GenesisTime))
}

func (c *ConsensusLightClient) slotTimestamp(slot common.Slot) (common.Timestamp, error) {
atSlot, err := c.Config.Spec.TimeAtSlot(slot, common.Timestamp(c.Config.ChainConfig.GenesisTime))
if err != nil {
return 0, err
}

return atSlot, nil
}

func (c *ConsensusLightClient) isCurrentCommitteeProofValid(attestedHeader common.BeaconBlockHeader, currentCommittee common.SyncCommittee, currentCommitteeBranch altair.SyncCommitteeProofBranch) bool {
return merkle.VerifyMerkleBranch(currentCommittee.HashTreeRoot(c.Config.Spec, tree.GetHashFn()), currentCommitteeBranch[:], 5, 22, attestedHeader.StateRoot)
}

0 comments on commit d02baa2

Please sign in to comment.