Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 5c5086d

Browse files
mpetrun5nulltea
andauthored
feat: add finality threshold check (#38)
Co-authored-by: Timofey Luin <[email protected]>
1 parent 531134c commit 5c5086d

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

chains/evm/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type EVMConfig struct {
2323
CommitteePeriodLength uint64 `default:"256" split_words:"true"`
2424
StartingPeriod uint64 `required:"true" split_words:"true"`
2525
ForcePeriod bool `default:"false" split_words:"true"`
26+
FinalityThreshold uint64 `default:"342" split_words:"true"`
2627
}
2728

2829
// LoadEVMConfig loads EVM config from the environment and validates the fields

chains/evm/config/config_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad_DefaultValues() {
6464
BeaconEndpoint: "endpoint",
6565
StartingPeriod: 500,
6666
ForcePeriod: false,
67+
FinalityThreshold: 342,
6768
})
6869
}
6970

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

8789
c, err := config.LoadEVMConfig(1)
8890

@@ -103,5 +105,6 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
103105
BeaconEndpoint: "endpoint",
104106
StartingPeriod: 500,
105107
ForcePeriod: true,
108+
FinalityThreshold: 382,
106109
})
107110
}

chains/evm/prover/prover.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,32 @@ type Prover struct {
5858
beaconClient BeaconClient
5959
proverClient ProverClient
6060

61-
spec Spec
61+
spec Spec
62+
finalityThreshold uint64
6263
}
6364

6465
func NewProver(
6566
proverClient ProverClient,
6667
beaconClient BeaconClient,
6768
lightClient LightClient,
6869
spec Spec,
70+
finalityTreshold uint64,
6971
) *Prover {
7072
return &Prover{
71-
proverClient: proverClient,
72-
spec: spec,
73-
beaconClient: beaconClient,
74-
lightClient: lightClient,
73+
proverClient: proverClient,
74+
spec: spec,
75+
beaconClient: beaconClient,
76+
lightClient: lightClient,
77+
finalityThreshold: finalityTreshold,
7578
}
7679
}
7780

7881
// StepProof generates the proof for the sync step
7982
func (p *Prover) StepProof(args *StepArgs) (*EvmProof[message.SyncStepInput], error) {
83+
participation := uint64(CountSetBits(args.Update.SyncAggregate.SyncCommiteeBits))
84+
if participation < p.finalityThreshold {
85+
return nil, fmt.Errorf("participation %d lower than finality treshold %d", participation, p.finalityThreshold)
86+
}
8087
updateSzz, err := args.Update.MarshalSSZ()
8188
if err != nil {
8289
return nil, err
@@ -113,7 +120,7 @@ func (p *Prover) StepProof(args *StepArgs) (*EvmProof[message.SyncStepInput], er
113120
Input: message.SyncStepInput{
114121
AttestedSlot: args.Update.AttestedHeader.Header.Slot,
115122
FinalizedSlot: args.Update.FinalizedHeader.Header.Slot,
116-
Participation: uint64(CountSetBits(args.Update.SyncAggregate.SyncCommiteeBits)),
123+
Participation: participation,
117124
FinalizedHeaderRoot: finalizedHeaderRoot,
118125
ExecutionPayloadRoot: executionRoot,
119126
},
@@ -154,6 +161,7 @@ func (p *Prover) StepArgs() (*StepArgs, error) {
154161
if err != nil {
155162
return nil, err
156163
}
164+
157165
blockRoot, err := p.beaconClient.BeaconBlockRoot(context.Background(), &api.BeaconBlockRootOpts{
158166
Block: fmt.Sprint(update.FinalizedHeader.Header.Slot),
159167
})

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func main() {
128128
}
129129

130130
lightClient := lightclient.NewLightClient(config.BeaconEndpoint)
131-
p := prover.NewProver(proverClient, beaconProvider, lightClient, prover.Spec(config.Spec))
131+
p := prover.NewProver(proverClient, beaconProvider, lightClient, prover.Spec(config.Spec), config.FinalityThreshold)
132132
routerAddress := common.HexToAddress(config.Router)
133133
stepHandler := handlers.NewStepEventHandler(msgChan, client, beaconProvider, p, routerAddress, id, domains)
134134
rotateHandler := handlers.NewRotateHandler(msgChan, periodStore, p, id, domains, config.CommitteePeriodLength, latestPeriod)

0 commit comments

Comments
 (0)