Skip to content

Commit 09cbe26

Browse files
authored
refactor(sync): privatise trustingPeriod in syncer params (#342)
Do not expose `trustingPeriod` to node operator - privatise in syncer params.
1 parent 030380e commit 09cbe26

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

sync/options.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ type Option func(*Parameters)
1414

1515
// Parameters is the set of parameters that must be configured for the syncer.
1616
type Parameters struct {
17-
// TrustingPeriod is period through which we can trust a header's validators set.
18-
//
19-
// Should be significantly less than the unbonding period (e.g. unbonding
20-
// period = 3 weeks, trusting period = 2 weeks).
21-
//
22-
// More specifically, trusting period + time needed to check headers + time
23-
// needed to report and punish misbehavior should be less than the unbonding
24-
// period.
25-
TrustingPeriod time.Duration
2617
// PruningWindow defines the duration within which headers are retained before being pruned.
2718
PruningWindow time.Duration
2819
// SyncFromHash is the hash of the header from which Syncer should start syncing.
@@ -41,6 +32,15 @@ type Parameters struct {
4132
//
4233
// SyncFromHeight has lower priority than SyncFromHash.
4334
SyncFromHeight uint64
35+
// trustingPeriod is period through which we can trust a header's validators set.
36+
//
37+
// Should be significantly less than the unbonding period (e.g. unbonding
38+
// period = 3 weeks, trusting period = 2 weeks).
39+
//
40+
// More specifically, trusting period + time needed to check headers + time
41+
// needed to report and punish misbehavior should be less than the unbonding
42+
// period.
43+
trustingPeriod time.Duration
4444
// blockTime provides a reference point for the Syncer to determine
4545
// whether its subjective head is outdated.
4646
// Keeping it private to disable serialization for it.
@@ -58,14 +58,14 @@ type Parameters struct {
5858
// DefaultParameters returns the default params to configure the syncer.
5959
func DefaultParameters() Parameters {
6060
return Parameters{
61-
TrustingPeriod: 336 * time.Hour, // tendermint's default trusting period
61+
trustingPeriod: 336 * time.Hour, // tendermint's default trusting period
6262
PruningWindow: 337 * time.Hour,
6363
}
6464
}
6565

6666
func (p *Parameters) Validate() error {
67-
if p.TrustingPeriod == 0 {
68-
return fmt.Errorf("invalid TrustingPeriod duration: %v", p.TrustingPeriod)
67+
if p.trustingPeriod == 0 {
68+
return fmt.Errorf("invalid trustingPeriod duration: %v", p.trustingPeriod)
6969
}
7070
if p.SyncFromHash == "" && p.PruningWindow == 0 && p.SyncFromHeight == 0 {
7171
return fmt.Errorf(
@@ -115,10 +115,10 @@ func WithRecencyThreshold(threshold time.Duration) Option {
115115
}
116116

117117
// WithTrustingPeriod is a functional option that configures the
118-
// `TrustingPeriod` parameter.
118+
// `trustingPeriod` parameter.
119119
func WithTrustingPeriod(duration time.Duration) Option {
120120
return func(p *Parameters) {
121-
p.TrustingPeriod = duration
121+
p.trustingPeriod = duration
122122
}
123123
}
124124

sync/syncer_head.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (s *Syncer[H]) networkHead(ctx context.Context) (H, bool, error) {
129129
func (s *Syncer[H]) subjectiveHead(ctx context.Context) (H, bool, error) {
130130
sbjHead, err := s.localHead(ctx)
131131

132-
switch expired, expiredFor := isExpired(sbjHead, s.Params.TrustingPeriod); {
132+
switch expired, expiredFor := isExpired(sbjHead, s.Params.trustingPeriod); {
133133
case expired:
134134
log.Infow(
135135
"subjective head expired, reinitializing...",
@@ -152,7 +152,7 @@ func (s *Syncer[H]) subjectiveHead(ctx context.Context) (H, bool, error) {
152152
return newHead, false, fmt.Errorf("exchange head: %w", err)
153153
}
154154
// still check if even the newly requested head is expired
155-
if expired, expiredFor := isExpired(newHead, s.Params.TrustingPeriod); expired {
155+
if expired, expiredFor := isExpired(newHead, s.Params.trustingPeriod); expired {
156156
// forbid initializing off an expired header
157157
err := fmt.Errorf(
158158
"subjective initialization with header(%d) expired for %s",

sync/syncer_tail.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (s *Syncer[H]) tailHeight(ctx context.Context, oldTail, head H) (uint64, er
200200
// estimateTailHeight estimates the tail header based on the current head.
201201
// It respects the trusting period, ensuring Syncer never initializes off an expired header.
202202
func (s *Syncer[H]) estimateTailHeight(head H) uint64 {
203-
headersToRetain := uint64(s.Params.TrustingPeriod / s.Params.blockTime) //nolint:gosec
203+
headersToRetain := uint64(s.Params.trustingPeriod / s.Params.blockTime) //nolint:gosec
204204
if headersToRetain >= head.Height() {
205205
// means chain is very young so we can keep all headers starting from genesis
206206
return 1

0 commit comments

Comments
 (0)