Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hooks to allow overriding Helper field in Syncer #423

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions statefulsyncer/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package statefulsyncer

import (
"time"

"github.com/coinbase/rosetta-sdk-go/syncer"
)

// Option is used to overwrite default values in
Expand Down Expand Up @@ -66,3 +68,9 @@ func WithSeenConcurrency(concurrency int64) Option {
s.seenSemaphoreSize = concurrency
}
}

func WithExtraSyncerOpts(os ...syncer.Option) Option {
return func(s *StatefulSyncer) {
s.extraSyncerOpts = os
}
}
Comment on lines +72 to +76
Copy link
Author

@bisgardo bisgardo Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more flexible solution would be to instead register a mapping function (similar to what is passed to WithCustomHelper below).

The usage of this from our fork would then be something like

statefulsyncer.WithCustomSyncerOpts(
	func(opts ...syncer.Option) []syncer.Option {
		return append(
			opts,
			syncer.WithCustomHelper(func(h syncer.Helper) syncer.Helper {
				return newConcordiumHelper(h)
			}))
	},
),

Might be a little too much complexity...

12 changes: 8 additions & 4 deletions statefulsyncer/stateful_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type StatefulSyncer struct {
// BlockSeen occur concurrently.
seenSemaphore *semaphore.Weighted
seenSemaphoreSize int64

extraSyncerOpts []syncer.Option
}

// Logger is used by the statefulsyncer to
Expand Down Expand Up @@ -162,10 +164,12 @@ func (s *StatefulSyncer) Sync(ctx context.Context, startIndex int64, endIndex in
s,
s,
s.cancel,
syncer.WithPastBlocks(pastBlocks),
syncer.WithCacheSize(s.cacheSize),
syncer.WithMaxConcurrency(s.maxConcurrency),
syncer.WithAdjustmentWindow(s.adjustmentWindow),
append([]syncer.Option{
syncer.WithPastBlocks(pastBlocks),
syncer.WithCacheSize(s.cacheSize),
syncer.WithMaxConcurrency(s.maxConcurrency),
syncer.WithAdjustmentWindow(s.adjustmentWindow),
}, s.extraSyncerOpts...)...,
)

return syncer.Sync(ctx, startIndex, endIndex)
Expand Down
6 changes: 6 additions & 0 deletions syncer/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ func WithAdjustmentWindow(adjustmentWindow int64) Option {
s.adjustmentWindow = adjustmentWindow
}
}

func WithCustomHelper(h func(Helper) Helper) Option {
return func(s *Syncer) {
s.helper = h(s.helper)
}
}