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

Commit 8c4efb1

Browse files
authored
fix: starting period 1 when period does not exist (#27)
1 parent 19a54c0 commit 8c4efb1

File tree

5 files changed

+41
-25
lines changed

5 files changed

+41
-25
lines changed

chains/evm/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type EVMConfig struct {
2222
GasIncreasePercentage int64 `default:"15" split_words:"true"`
2323
RetryInterval uint64 `default:"12" split_words:"true"`
2424
CommitteePeriodLength uint64 `default:"256" split_words:"true"`
25+
StartingPeriod uint64 `required:"true" split_words:"true"`
2526
}
2627

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

chains/evm/config/config_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad_DefaultValues() {
4242
os.Setenv("SPECTRE_DOMAINS_1_ROUTER", "router")
4343
os.Setenv("SPECTRE_DOMAINS_1_BEACON_ENDPOINT", "endpoint")
4444
os.Setenv("SPECTRE_DOMAINS_2_ROUTER", "invalid")
45+
os.Setenv("SPECTRE_DOMAINS_1_STARTING_PERIOD", "500")
46+
os.Setenv("SPECTRE_DOMAINS_2_STARTING_PERIOD", "500")
4547

4648
c, err := config.LoadEVMConfig(1)
4749

@@ -61,6 +63,7 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad_DefaultValues() {
6163
RetryInterval: 12,
6264
CommitteePeriodLength: 256,
6365
BeaconEndpoint: "endpoint",
66+
StartingPeriod: 500,
6467
})
6568
}
6669

@@ -78,6 +81,7 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
7881
os.Setenv("SPECTRE_DOMAINS_1_RETRY_INTERVAL", "30")
7982
os.Setenv("SPECTRE_DOMAINS_1_COMMITTEE_PERIOD_LENGTH", "128")
8083
os.Setenv("SPECTRE_DOMAINS_2_ROUTER", "invalid")
84+
os.Setenv("SPECTRE_DOMAINS_1_STARTING_PERIOD", "500")
8185

8286
c, err := config.LoadEVMConfig(1)
8387

@@ -97,5 +101,6 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
97101
RetryInterval: 30,
98102
CommitteePeriodLength: 128,
99103
BeaconEndpoint: "endpoint",
104+
StartingPeriod: 500,
100105
})
101106
}

chains/evm/listener/handlers/rotate.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,51 @@ type RotateHandler struct {
3232

3333
prover Prover
3434
periodStorer PeriodStorer
35+
latestPeriod *big.Int
3536

3637
committeePeriodLength uint64
3738
}
3839

39-
func NewRotateHandler(msgChan chan []*message.Message, periodStorer PeriodStorer, prover Prover, domainID uint8, domains []uint8, committeePeriodLenght uint64) *RotateHandler {
40+
func NewRotateHandler(
41+
msgChan chan []*message.Message,
42+
periodStorer PeriodStorer,
43+
prover Prover,
44+
domainID uint8,
45+
domains []uint8,
46+
committeePeriodLenght uint64,
47+
startingPeriod uint64,
48+
) (*RotateHandler, error) {
49+
storedPeriod, err := periodStorer.Period(domainID)
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
var latestPeriod *big.Int
55+
if storedPeriod.Uint64() >= startingPeriod {
56+
latestPeriod = storedPeriod
57+
} else {
58+
latestPeriod = big.NewInt(int64(startingPeriod))
59+
}
4060
return &RotateHandler{
4161
prover: prover,
4262
periodStorer: periodStorer,
4363
domainID: domainID,
4464
domains: domains,
4565
msgChan: msgChan,
4666
committeePeriodLength: committeePeriodLenght,
47-
}
67+
latestPeriod: latestPeriod,
68+
}, err
4869
}
4970

5071
// HandleEvents checks if the current period is newer than the last stored
5172
// period and rotates the committee if it is
5273
func (h *RotateHandler) HandleEvents(checkpoint *apiv1.Finality) error {
53-
latestPeriod, err := h.periodStorer.Period(h.domainID)
54-
if err != nil {
55-
return err
56-
}
5774
currentPeriod := uint64(checkpoint.Finalized.Epoch) / h.committeePeriodLength
58-
if currentPeriod <= latestPeriod.Uint64() {
75+
if currentPeriod <= h.latestPeriod.Uint64() {
5976
return nil
6077
}
6178

62-
targetPeriod := latestPeriod.Add(latestPeriod, big.NewInt(1))
79+
targetPeriod := new(big.Int).Add(h.latestPeriod, big.NewInt(1))
6380
args, err := h.prover.RotateArgs(targetPeriod.Uint64())
6481
if err != nil {
6582
return err
@@ -104,5 +121,6 @@ func (h *RotateHandler) HandleEvents(checkpoint *apiv1.Finality) error {
104121
}
105122
}
106123

124+
h.latestPeriod = targetPeriod
107125
return h.periodStorer.StorePeriod(h.domainID, targetPeriod)
108126
}

chains/evm/listener/handlers/rotate_test.go

+5-16
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,19 @@ func (s *RotateHandlerTestSuite) SetupTest() {
4848
s.mockProver = mock.NewMockProver(ctrl)
4949
s.mockPeriodStorer = mock.NewMockPeriodStorer(ctrl)
5050
s.msgChan = make(chan []*message.Message, 2)
51-
s.handler = handlers.NewRotateHandler(
51+
s.mockPeriodStorer.EXPECT().Period(uint8(1)).Return(big.NewInt(2), nil)
52+
s.handler, _ = handlers.NewRotateHandler(
5253
s.msgChan,
5354
s.mockPeriodStorer,
5455
s.mockProver,
5556
1,
5657
[]uint8{2, 3},
5758
256,
59+
3,
5860
)
5961
}
6062

61-
func (s *RotateHandlerTestSuite) Test_HandleEvents_PeriodFetchingFails() {
62-
s.mockPeriodStorer.EXPECT().Period(uint8(1)).Return(nil, fmt.Errorf("error"))
63-
64-
err := s.handler.HandleEvents(&apiv1.Finality{})
65-
s.NotNil(err)
66-
67-
_, err = readFromChannel(s.msgChan)
68-
s.NotNil(err)
69-
}
70-
7163
func (s *RotateHandlerTestSuite) Test_HandleEvents_CurrentPeriodOlderThanLatest() {
72-
s.mockPeriodStorer.EXPECT().Period(uint8(1)).Return(big.NewInt(2), nil)
73-
7464
err := s.handler.HandleEvents(&apiv1.Finality{
7565
Finalized: &phase0.Checkpoint{
7666
Epoch: phase0.Epoch(300),
@@ -83,9 +73,8 @@ func (s *RotateHandlerTestSuite) Test_HandleEvents_CurrentPeriodOlderThanLatest(
8373
}
8474

8575
func (s *RotateHandlerTestSuite) Test_HandleEvents_ValidPeriod() {
86-
s.mockPeriodStorer.EXPECT().Period(uint8(1)).Return(big.NewInt(2), nil)
87-
s.mockPeriodStorer.EXPECT().StorePeriod(uint8(1), big.NewInt(3)).Return(nil)
88-
s.mockProver.EXPECT().RotateArgs(uint64(3)).Return(&prover.RotateArgs{
76+
s.mockPeriodStorer.EXPECT().StorePeriod(uint8(1), big.NewInt(4)).Return(nil)
77+
s.mockProver.EXPECT().RotateArgs(uint64(4)).Return(&prover.RotateArgs{
8978
Update: &consensus.LightClientUpdateCapella{},
9079
Domain: phase0.Domain{},
9180
Spec: "mainnet",

main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ func main() {
113113
p := prover.NewProver(proverClient, beaconProvider, lightClient, prover.Spec(config.Spec))
114114
routerAddress := common.HexToAddress(config.Router)
115115
stepHandler := handlers.NewStepEventHandler(msgChan, client, beaconProvider, p, routerAddress, id, domains, config.BlockInterval)
116-
rotateHandler := handlers.NewRotateHandler(msgChan, periodStore, p, id, domains, config.CommitteePeriodLength)
116+
rotateHandler, err := handlers.NewRotateHandler(msgChan, periodStore, p, id, domains, config.CommitteePeriodLength, config.StartingPeriod)
117+
if err != nil {
118+
panic(err)
119+
}
117120
listener := listener.NewEVMListener(beaconProvider, []listener.EventHandler{rotateHandler, stepHandler}, id, time.Duration(config.RetryInterval)*time.Second)
118121

119122
messageHandler := message.NewMessageHandler()

0 commit comments

Comments
 (0)