Skip to content

Commit

Permalink
Remove utils from consensus and move to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkLord017 committed Sep 21, 2024
1 parent 0e1b177 commit 363da70
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 260 deletions.
47 changes: 24 additions & 23 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/BlocSoc-iitr/selene/config/checkpoints"
"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"github.com/BlocSoc-iitr/selene/consensus/rpc"
"github.com/BlocSoc-iitr/selene/utils"
"github.com/BlocSoc-iitr/selene/utils/bls"
geth "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -254,7 +255,7 @@ func (in *Inner) get_execution_payload(ctx context.Context, slot *uint64) (*cons
return nil, err
}

blockHash, err := TreeHashRoot(block.Body.ToBytes())
blockHash, err := utils.TreeHashRoot(block.Body.ToBytes())

Check failure on line 258 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: utils.TreeHashRoot

Check failure on line 258 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils.TreeHashRoot

Check failure on line 258 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: utils.TreeHashRoot
if err != nil {
return nil, err
}
Expand All @@ -265,12 +266,12 @@ func (in *Inner) get_execution_payload(ctx context.Context, slot *uint64) (*cons
var errGettingBlockHash error

if *slot == latestSlot {
verifiedBlockHash, errGettingBlockHash = TreeHashRoot(in.Store.OptimisticHeader.ToBytes())
verifiedBlockHash, errGettingBlockHash = utils.TreeHashRoot(in.Store.OptimisticHeader.ToBytes())

Check failure on line 269 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: utils.TreeHashRoot

Check failure on line 269 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils.TreeHashRoot

Check failure on line 269 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: utils.TreeHashRoot
if errGettingBlockHash != nil {
return nil, ErrPayloadNotFound
}
} else if *slot == finalizedSlot {
verifiedBlockHash, errGettingBlockHash = TreeHashRoot(in.Store.FinalizedHeader.ToBytes())
verifiedBlockHash, errGettingBlockHash = utils.TreeHashRoot(in.Store.FinalizedHeader.ToBytes())

Check failure on line 274 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: utils.TreeHashRoot

Check failure on line 274 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils.TreeHashRoot

Check failure on line 274 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: utils.TreeHashRoot
if errGettingBlockHash != nil {
return nil, ErrPayloadNotFound
}
Expand Down Expand Up @@ -368,7 +369,7 @@ func (in *Inner) advance() error {
if in.Store.NextSyncCommitee == nil {
log.Printf("checking for sync committee update")

currentPeriod := CalcSyncPeriod(in.Store.FinalizedHeader.Slot)
currentPeriod := utils.CalcSyncPeriod(in.Store.FinalizedHeader.Slot)

Check failure on line 372 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: utils.CalcSyncPeriod

Check failure on line 372 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils.CalcSyncPeriod

Check failure on line 372 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: utils.CalcSyncPeriod
updates, err := in.RPC.GetUpdates(currentPeriod, 1)
if err != nil {
return err
Expand All @@ -394,7 +395,7 @@ func (in *Inner) sync(checkpoint [32]byte) error {
in.bootstrap(checkpoint)

// Calculate the current sync period
currentPeriod := CalcSyncPeriod(in.Store.FinalizedHeader.Slot)
currentPeriod := utils.CalcSyncPeriod(in.Store.FinalizedHeader.Slot)

Check failure on line 398 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: utils.CalcSyncPeriod

Check failure on line 398 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils.CalcSyncPeriod

Check failure on line 398 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: utils.CalcSyncPeriod

// Fetch updates
updates, err := in.RPC.GetUpdates(currentPeriod, MAX_REQUEST_LIGHT_CLIENT_UPDATES)
Expand Down Expand Up @@ -520,7 +521,7 @@ func verify_bootstrap(checkpoint [32]byte, bootstrap consensus_core.Bootstrap) {
return
}

headerHash, err := TreeHashRoot(bootstrap.Header.ToBytes())
headerHash, err := utils.TreeHashRoot(bootstrap.Header.ToBytes())

Check failure on line 524 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: utils.TreeHashRoot

Check failure on line 524 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils.TreeHashRoot

Check failure on line 524 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: utils.TreeHashRoot
if err != nil {
log.Println("failed to hash header")
return
Expand Down Expand Up @@ -560,8 +561,8 @@ func (in *Inner) verify_generic_update(update *GenericUpdate, expectedCurrentSlo
return ErrInvalidTimestamp
}

storePeriod := CalcSyncPeriod(store.FinalizedHeader.Slot)
updateSigPeriod := CalcSyncPeriod(update.SignatureSlot)
storePeriod := utils.CalcSyncPeriod(store.FinalizedHeader.Slot)

Check failure on line 564 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: utils.CalcSyncPeriod

Check failure on line 564 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils.CalcSyncPeriod

Check failure on line 564 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: utils.CalcSyncPeriod
updateSigPeriod := utils.CalcSyncPeriod(update.SignatureSlot)

Check failure on line 565 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: utils.CalcSyncPeriod

Check failure on line 565 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils.CalcSyncPeriod

Check failure on line 565 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: utils.CalcSyncPeriod

var validPeriod bool
if store.NextSyncCommitee != nil {
Expand All @@ -574,7 +575,7 @@ func (in *Inner) verify_generic_update(update *GenericUpdate, expectedCurrentSlo
return ErrInvalidPeriod
}

updateAttestedPeriod := CalcSyncPeriod(update.AttestedHeader.Slot)
updateAttestedPeriod := utils.CalcSyncPeriod(update.AttestedHeader.Slot)

Check failure on line 578 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: utils.CalcSyncPeriod

Check failure on line 578 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils.CalcSyncPeriod

Check failure on line 578 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: utils.CalcSyncPeriod
updateHasNextCommittee := store.NextSyncCommitee == nil && update.NextSyncCommittee != nil && updateAttestedPeriod == storePeriod

if update.AttestedHeader.Slot <= store.FinalizedHeader.Slot && !updateHasNextCommittee {
Expand Down Expand Up @@ -606,13 +607,13 @@ func (in *Inner) verify_generic_update(update *GenericUpdate, expectedCurrentSlo
} else {
syncCommittee = in.Store.NextSyncCommitee
}
pks, err := GetParticipatingKeys(syncCommittee, update.SyncAggregate.SyncCommitteeBits)
pks, err := utils.GetParticipatingKeys(syncCommittee, update.SyncAggregate.SyncCommitteeBits)

Check failure on line 610 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

undefined: utils.GetParticipatingKeys

Check failure on line 610 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils.GetParticipatingKeys

Check failure on line 610 in consensus/consensus.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: utils.GetParticipatingKeys
if err != nil {
return fmt.Errorf("failed to get participating keys: %w", err)
}

forkVersion := CalculateForkVersion(&forks, update.SignatureSlot)
forkDataRoot := ComputeForkDataRoot(forkVersion, consensus_core.Bytes32(in.Config.Chain.GenesisRoot))
forkVersion := utils.CalculateForkVersion(&forks, update.SignatureSlot)
forkDataRoot := utils.ComputeForkDataRoot(forkVersion, consensus_core.Bytes32(in.Config.Chain.GenesisRoot))

if !verifySyncCommitteeSignature(pks, &update.AttestedHeader, &update.SyncAggregate.SyncCommitteeSignature, forkDataRoot) {
return ErrInvalidSignature
Expand Down Expand Up @@ -667,13 +668,13 @@ func (in *Inner) apply_generic_update(store *LightClientStore, update *GenericUp
store.OptimisticHeader = update.AttestedHeader
}

updateAttestedPeriod := CalcSyncPeriod(update.AttestedHeader.Slot)
updateAttestedPeriod := utils.CalcSyncPeriod(update.AttestedHeader.Slot)

updateFinalizedSlot := uint64(0)
if update.FinalizedHeader != (consensus_core.Header{}) {
updateFinalizedSlot = update.FinalizedHeader.Slot
}
updateFinalizedPeriod := CalcSyncPeriod(updateFinalizedSlot)
updateFinalizedPeriod := utils.CalcSyncPeriod(updateFinalizedSlot)

updateHasFinalizedNextCommittee := in.Store.NextSyncCommitee == nil &&
in.has_sync_update(update) && in.has_finality_update(update) &&
Expand All @@ -692,7 +693,7 @@ func (in *Inner) apply_generic_update(store *LightClientStore, update *GenericUp

// Apply the update if conditions are met
if shouldApplyUpdate {
storePeriod := CalcSyncPeriod(store.FinalizedHeader.Slot)
storePeriod := utils.CalcSyncPeriod(store.FinalizedHeader.Slot)

// Sync committee update logic
if store.NextSyncCommitee == nil {
Expand All @@ -714,7 +715,7 @@ func (in *Inner) apply_generic_update(store *LightClientStore, update *GenericUp
}

if store.FinalizedHeader.Slot%32 == 0 {
checkpoint, err := TreeHashRoot(store.FinalizedHeader.ToBytes())
checkpoint, err := utils.TreeHashRoot(store.FinalizedHeader.ToBytes())
if err != nil {
return nil
}
Expand Down Expand Up @@ -824,7 +825,7 @@ func verifySyncCommitteeSignature(
}

// Compute headerRoot
headerRoot, err := TreeHashRoot(attestedHeader.ToBytes())
headerRoot, err := utils.TreeHashRoot(attestedHeader.ToBytes())
if err != nil {
return false
}
Expand All @@ -842,18 +843,18 @@ func verifySyncCommitteeSignature(
}
}

return isAggregateValid(*signature, signingRoot, g2Points)
return utils.IsAggregateValid(*signature, signingRoot, g2Points)
}

func ComputeCommitteeSignRoot(header consensus_core.Bytes32, fork consensus_core.Bytes32) consensus_core.Bytes32 {
// Domain type for the sync committee
domainType := [4]byte{7, 0, 0, 0}

// Compute the domain
domain := ComputeDomain(domainType, fork)
domain := utils.ComputeDomain(domainType, fork)

// Compute and return the signing root
return ComputeSigningRoot(header, domain)
return utils.ComputeSigningRoot(header, domain)
}
func (in *Inner) Age(slot uint64) time.Duration {
expectedTime := slot*12 + in.Config.Chain.GenesisTime
Expand Down Expand Up @@ -883,23 +884,23 @@ func isFinalityProofValid(attestedHeader *consensus_core.Header, finalizedHeader
if err != nil {
return false
}
return isProofValid(attestedHeader, finalizedHeader.ToBytes(), finalityBranchForProof, 6, 41)
return utils.IsProofValid(attestedHeader, finalizedHeader.ToBytes(), finalityBranchForProof, 6, 41)
}

func isCurrentCommitteeProofValid(attestedHeader *consensus_core.Header, currentCommittee *consensus_core.SyncCommittee, currentCommitteeBranch []consensus_core.Bytes32) bool {
CurrentCommitteeForProof, err := branchToNodes(currentCommitteeBranch)
if err != nil {
return false
}
return isProofValid(attestedHeader, currentCommittee.ToBytes(), CurrentCommitteeForProof, 5, 22)
return utils.IsProofValid(attestedHeader, currentCommittee.ToBytes(), CurrentCommitteeForProof, 5, 22)
}

func isNextCommitteeProofValid(attestedHeader *consensus_core.Header, currentCommittee *consensus_core.SyncCommittee, currentCommitteeBranch []consensus_core.Bytes32) bool {
currentCommitteeBranchForProof, err := branchToNodes(currentCommitteeBranch)
if err != nil {
return false
}
return isProofValid(attestedHeader, currentCommittee.ToBytes(), currentCommitteeBranchForProof, 5, 23)
return utils.IsProofValid(attestedHeader, currentCommittee.ToBytes(), currentCommitteeBranchForProof, 5, 23)
}

func PayloadToBlock(value *consensus_core.ExecutionPayload) (*common.Block, error) {
Expand Down
Loading

0 comments on commit 363da70

Please sign in to comment.