1
1
package sync
2
2
3
3
import (
4
+ "encoding/hex"
4
5
"fmt"
5
6
"time"
6
7
@@ -24,14 +25,16 @@ type Parameters struct {
24
25
TrustingPeriod time.Duration
25
26
// PruningWindow defines the duration within which headers are retained before being pruned.
26
27
PruningWindow time.Duration
27
- // SyncFromHash is the hash of the header from which the syncer should start syncing.
28
+ // SyncFromHash is the hash of the header from which Syncer should start syncing.
29
+ // Zero value to disable. Value updates up and down the chain are gracefully handled by Syncer.
28
30
//
29
31
// By default, Syncer maintains PruningWindow number of headers. SyncFromHash overrides this default,
30
32
// allowing any user to specify a custom starting point.
31
33
//
32
34
// SyncFromHash has higher priority than SyncFromHeight.
33
- SyncFromHash header.Hash
34
- // SyncFromHeight is the height of the header from which the syncer should start syncing.
35
+ SyncFromHash string
36
+ // SyncFromHeight is the height of the header from which Syncer should start syncing.
37
+ // Zero value to disable. Value updates up and down the chain are gracefully handled by Syncer.
35
38
//
36
39
// By default, Syncer maintains PruningWindow number of headers. SyncFromHeight overrides this default,
37
40
// allowing any user to specify a custom starting point.
@@ -48,6 +51,8 @@ type Parameters struct {
48
51
recencyThreshold time.Duration
49
52
// metrics is a flag that enables metrics collection.
50
53
metrics bool
54
+ // hash cache
55
+ hash header.Hash
51
56
}
52
57
53
58
// DefaultParameters returns the default params to configure the syncer.
@@ -60,11 +65,32 @@ func DefaultParameters() Parameters {
60
65
61
66
func (p * Parameters ) Validate () error {
62
67
if p .TrustingPeriod == 0 {
63
- return fmt .Errorf ("invalid trusting period duration: %v" , p .TrustingPeriod )
68
+ return fmt .Errorf ("invalid TrustingPeriod duration: %v" , p .TrustingPeriod )
69
+ }
70
+ if (p .SyncFromHeight == 0 || p .SyncFromHash == "" ) && p .PruningWindow == 0 {
71
+ return fmt .Errorf (
72
+ "PruningWindow duration can't be zero when either of SyncFromHeight or SyncFromHash are not set" ,
73
+ )
74
+ }
75
+ _ , err := p .Hash ()
76
+ if err != nil {
77
+ return err
64
78
}
65
79
return nil
66
80
}
67
81
82
+ func (p * Parameters ) Hash () (header.Hash , error ) {
83
+ if p .hash == nil && len (p .SyncFromHash ) > 0 {
84
+ hash , err := hex .DecodeString (p .SyncFromHash )
85
+ if err != nil {
86
+ return nil , fmt .Errorf ("invalid SyncFromHash: %w" , err )
87
+ }
88
+
89
+ p .hash = hash
90
+ }
91
+ return p .hash , nil
92
+ }
93
+
68
94
// WithMetrics enables Metrics on Syncer.
69
95
func WithMetrics () Option {
70
96
return func (p * Parameters ) {
@@ -105,7 +131,7 @@ func WithParams(params Parameters) Option {
105
131
106
132
// WithSyncFromHash sets given header hash a starting point for syncing.
107
133
// See [Parameters.SyncFromHash] for details.
108
- func WithSyncFromHash (hash header. Hash ) Option {
134
+ func WithSyncFromHash (hash string ) Option {
109
135
return func (p * Parameters ) {
110
136
p .SyncFromHash = hash
111
137
}
0 commit comments