@@ -19,22 +19,22 @@ import (
1919 "errors"
2020 "fmt"
2121 "log"
22- "math/big"
2322 "time"
2423
2524 "github.com/coinbase/rosetta-sdk-go/fetcher"
2625 "github.com/coinbase/rosetta-sdk-go/storage"
2726 "github.com/coinbase/rosetta-sdk-go/syncer"
2827 "github.com/coinbase/rosetta-sdk-go/types"
28+ "github.com/coinbase/rosetta-sdk-go/utils"
2929)
3030
3131var _ syncer.Handler = (* StatefulSyncer )(nil )
3232var _ syncer.Helper = (* StatefulSyncer )(nil )
3333
3434const (
35- // pruneSleepTime is how long we sleep between
35+ // DefaultPruneSleepTime is how long we sleep between
3636 // pruning attempts.
37- pruneSleepTime = 10 * time .Second
37+ DefaultPruneSleepTime = 30 * time .Minute
3838
3939 // pruneBuffer is the cushion we apply to pastBlockLimit
4040 // when pruning.
@@ -54,9 +54,12 @@ type StatefulSyncer struct {
5454 counterStorage * storage.CounterStorage
5555 logger Logger
5656 workers []storage.BlockWorker
57- cacheSize int
58- maxConcurrency int64
59- pastBlockLimit int
57+
58+ cacheSize int
59+ maxConcurrency int64
60+ pastBlockLimit int
61+ adjustmentWindow int64
62+ pruneSleepTime time.Duration
6063}
6164
6265// Logger is used by the statefulsyncer to
@@ -87,22 +90,31 @@ func New(
8790 logger Logger ,
8891 cancel context.CancelFunc ,
8992 workers []storage.BlockWorker ,
90- cacheSize int ,
91- maxConcurrency int64 ,
92- pastBlockLimit int ,
93+ options ... Option ,
9394) * StatefulSyncer {
94- return & StatefulSyncer {
95+ s := & StatefulSyncer {
9596 network : network ,
9697 fetcher : fetcher ,
9798 cancel : cancel ,
9899 blockStorage : blockStorage ,
99100 counterStorage : counterStorage ,
100101 workers : workers ,
101102 logger : logger ,
102- cacheSize : cacheSize ,
103- maxConcurrency : maxConcurrency ,
104- pastBlockLimit : pastBlockLimit ,
103+
104+ // Optional args
105+ cacheSize : syncer .DefaultCacheSize ,
106+ maxConcurrency : syncer .DefaultMaxConcurrency ,
107+ pastBlockLimit : syncer .DefaultPastBlockLimit ,
108+ adjustmentWindow : syncer .DefaultAdjustmentWindow ,
109+ pruneSleepTime : DefaultPruneSleepTime ,
105110 }
111+
112+ // Override defaults with any provided options
113+ for _ , opt := range options {
114+ opt (s )
115+ }
116+
117+ return s
106118}
107119
108120// Sync starts a new sync run after properly initializing blockStorage.
@@ -135,6 +147,7 @@ func (s *StatefulSyncer) Sync(ctx context.Context, startIndex int64, endIndex in
135147 syncer .WithPastBlocks (pastBlocks ),
136148 syncer .WithCacheSize (s .cacheSize ),
137149 syncer .WithMaxConcurrency (s .maxConcurrency ),
150+ syncer .WithAdjustmentWindow (s .adjustmentWindow ),
138151 )
139152
140153 return syncer .Sync (ctx , startIndex , endIndex )
@@ -148,10 +161,16 @@ func (s *StatefulSyncer) Sync(ctx context.Context, startIndex int64, endIndex in
148161// pruning strategies during syncing.
149162func (s * StatefulSyncer ) Prune (ctx context.Context , helper PruneHelper ) error {
150163 for ctx .Err () == nil {
164+ // We don't use a timer pattern because s.pruneSleepTime is defined
165+ // as the time between pruning runs. Using a timer would only guarantee
166+ // that the difference between starts of each pruning run are s.pruneSleepTime.
167+ if err := utils .ContextSleep (ctx , s .pruneSleepTime ); err != nil {
168+ return err
169+ }
170+
151171 headBlock , err := s .blockStorage .GetHeadBlockIdentifier (ctx )
152172 if headBlock == nil && errors .Is (err , storage .ErrHeadBlockNotFound ) {
153173 // this will occur when we are waiting for the first block to be synced
154- time .Sleep (pruneSleepTime )
155174 continue
156175 }
157176 if err != nil {
@@ -161,7 +180,6 @@ func (s *StatefulSyncer) Prune(ctx context.Context, helper PruneHelper) error {
161180 oldestIndex , err := s .blockStorage .GetOldestBlockIndex (ctx )
162181 if oldestIndex == - 1 && errors .Is (err , storage .ErrOldestIndexMissing ) {
163182 // this will occur when we have yet to store the oldest index
164- time .Sleep (pruneSleepTime )
165183 continue
166184 }
167185 if err != nil {
@@ -174,7 +192,6 @@ func (s *StatefulSyncer) Prune(ctx context.Context, helper PruneHelper) error {
174192 }
175193
176194 if pruneableIndex < oldestIndex {
177- time .Sleep (pruneSleepTime )
178195 continue
179196 }
180197
@@ -196,8 +213,6 @@ func (s *StatefulSyncer) Prune(ctx context.Context, helper PruneHelper) error {
196213
197214 log .Println (pruneMessage )
198215 }
199-
200- time .Sleep (pruneSleepTime )
201216 }
202217
203218 return ctx .Err ()
@@ -215,23 +230,7 @@ func (s *StatefulSyncer) BlockAdded(ctx context.Context, block *types.Block) err
215230 )
216231 }
217232
218- if err := s .logger .AddBlockStream (ctx , block ); err != nil {
219- return nil
220- }
221-
222- // Update Counters
223- _ , _ = s .counterStorage .Update (ctx , storage .BlockCounter , big .NewInt (1 ))
224- _ , _ = s .counterStorage .Update (
225- ctx ,
226- storage .TransactionCounter ,
227- big .NewInt (int64 (len (block .Transactions ))),
228- )
229- opCount := int64 (0 )
230- for _ , txn := range block .Transactions {
231- opCount += int64 (len (txn .Operations ))
232- }
233- _ , _ = s .counterStorage .Update (ctx , storage .OperationCounter , big .NewInt (opCount ))
234-
233+ _ = s .logger .AddBlockStream (ctx , block )
235234 return nil
236235}
237236
@@ -250,14 +249,8 @@ func (s *StatefulSyncer) BlockRemoved(
250249 )
251250 }
252251
253- if err := s .logger .RemoveBlockStream (ctx , blockIdentifier ); err != nil {
254- return nil
255- }
256-
257- // Update Counters
258- _ , _ = s .counterStorage .Update (ctx , storage .OrphanCounter , big .NewInt (1 ))
259-
260- return err
252+ _ = s .logger .RemoveBlockStream (ctx , blockIdentifier )
253+ return nil
261254}
262255
263256// NetworkStatus is called by the syncer to get the current
0 commit comments