Skip to content

Commit

Permalink
feat: add finality threshold check
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Mar 12, 2024
1 parent ab60f7c commit 7edf00a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions chains/evm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type EVMConfig struct {
CommitteePeriodLength uint64 `default:"256" split_words:"true"`
StartingPeriod uint64 `required:"true" split_words:"true"`
ForcePeriod bool `default:"false" split_words:"true"`
FinalityThreshold uint64 `default:"342" split_words:"true"`
}

// LoadEVMConfig loads EVM config from the environment and validates the fields
Expand Down
3 changes: 3 additions & 0 deletions chains/evm/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad_DefaultValues() {
BeaconEndpoint: "endpoint",
StartingPeriod: 500,
ForcePeriod: false,
FinalityThreshold: 342,
})
}

Expand All @@ -83,6 +84,7 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
os.Setenv("SPECTRE_DOMAINS_2_ROUTER", "invalid")
os.Setenv("SPECTRE_DOMAINS_1_STARTING_PERIOD", "500")
os.Setenv("SPECTRE_DOMAINS_1_FORCE_PERIOD", "true")
os.Setenv("SPECTRE_DOMAINS_1_FINALITY_THRESHOLD", "382")

c, err := config.LoadEVMConfig(1)

Expand All @@ -103,5 +105,6 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
BeaconEndpoint: "endpoint",
StartingPeriod: 500,
ForcePeriod: true,
FinalityThreshold: 382,
})
}
20 changes: 14 additions & 6 deletions chains/evm/prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,32 @@ type Prover struct {
beaconClient BeaconClient
proverClient ProverClient

spec Spec
spec Spec
finalityThreshold uint64
}

func NewProver(
proverClient ProverClient,
beaconClient BeaconClient,
lightClient LightClient,
spec Spec,
finalityTreshold uint64,
) *Prover {
return &Prover{
proverClient: proverClient,
spec: spec,
beaconClient: beaconClient,
lightClient: lightClient,
proverClient: proverClient,
spec: spec,
beaconClient: beaconClient,
lightClient: lightClient,
finalityThreshold: finalityTreshold,
}
}

// StepProof generates the proof for the sync step
func (p *Prover) StepProof(args *StepArgs) (*EvmProof[message.SyncStepInput], error) {
participation := uint64(CountSetBits(args.Update.SyncAggregate.SyncCommiteeBits))
if participation < p.finalityThreshold {
return nil, fmt.Errorf("participation %d lower than finality treshold %d", participation, p.finalityThreshold)
}
updateSzz, err := args.Update.MarshalSSZ()
if err != nil {
return nil, err
Expand Down Expand Up @@ -113,7 +120,7 @@ func (p *Prover) StepProof(args *StepArgs) (*EvmProof[message.SyncStepInput], er
Input: message.SyncStepInput{
AttestedSlot: args.Update.AttestedHeader.Header.Slot,
FinalizedSlot: args.Update.FinalizedHeader.Header.Slot,
Participation: uint64(CountSetBits(args.Update.SyncAggregate.SyncCommiteeBits)),
Participation: participation,
FinalizedHeaderRoot: finalizedHeaderRoot,
ExecutionPayloadRoot: executionRoot,
},
Expand Down Expand Up @@ -154,6 +161,7 @@ func (p *Prover) StepArgs() (*StepArgs, error) {
if err != nil {
return nil, err
}

blockRoot, err := p.beaconClient.BeaconBlockRoot(context.Background(), &api.BeaconBlockRootOpts{
Block: fmt.Sprint(update.FinalizedHeader.Header.Slot),
})
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func main() {
}

lightClient := lightclient.NewLightClient(config.BeaconEndpoint)
p := prover.NewProver(proverClient, beaconProvider, lightClient, prover.Spec(config.Spec))
p := prover.NewProver(proverClient, beaconProvider, lightClient, prover.Spec(config.Spec), config.FinalityThreshold)
routerAddress := common.HexToAddress(config.Router)
stepHandler := handlers.NewStepEventHandler(msgChan, client, beaconProvider, p, routerAddress, id, domains)
rotateHandler := handlers.NewRotateHandler(msgChan, periodStore, p, id, domains, config.CommitteePeriodLength, latestPeriod)
Expand Down

0 comments on commit 7edf00a

Please sign in to comment.