Skip to content

Commit

Permalink
Support secondary blocks source (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
GraDKh authored Jan 8, 2024
1 parent 632fd46 commit 5c38c4e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
28 changes: 28 additions & 0 deletions cmd/blockimporter/block_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,31 @@ func (decorator *retryBlockSourceDecorator) GetChainID() (int64, error) {

return 0, err
}

type secondaryBlocksSourceDecorator struct {
primarySource BlockSource
secondaryBlocksSource BlockSource
}

func WithSecondaryBlocksSource(primarySource BlockSource, secondaryBlocksSource BlockSource) BlockSource {
return &secondaryBlocksSourceDecorator{
primarySource: primarySource,
secondaryBlocksSource: secondaryBlocksSource,
}
}

func (decorator *secondaryBlocksSourceDecorator) PollBlocks(fromBlock uint64) ([]types.Block, error) {
if blocks, err := decorator.primarySource.PollBlocks(fromBlock); err == nil {
return blocks, err
}

return decorator.secondaryBlocksSource.PollBlocks(fromBlock)
}

func (decorator *secondaryBlocksSourceDecorator) GetInitialBalances() ([]BalanceEntry, error) {
return decorator.primarySource.GetInitialBalances()
}

func (decorator *secondaryBlocksSourceDecorator) GetChainID() (int64, error) {
return decorator.primarySource.GetChainID()
}
15 changes: 12 additions & 3 deletions cmd/blockimporter/import_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Settings struct {
PollInterval time.Duration
}

func RunImport(settings *Settings, blockSource BlockSource) error {
func RunImport(settings *Settings, blockSource BlockSource, secondaryBlocksSource BlockSource) error {
db, err := NewDB(settings.DBPath, settings.Logger)
if err != nil {
return err
Expand All @@ -39,7 +39,7 @@ func RunImport(settings *Settings, blockSource BlockSource) error {
}

blockNum := state.BlockNum()
blockSource = makeBlockSource(settings, blockSource)
blockSource = makeBlockSource(settings, blockSource, secondaryBlocksSource)
for {
select {
case <-settings.Terminated:
Expand All @@ -66,7 +66,16 @@ func RunImport(settings *Settings, blockSource BlockSource) error {
}
}

func makeBlockSource(settings *Settings, blockSource BlockSource) BlockSource {
func makeBlockSource(settings *Settings, blockSource BlockSource, secondaryBlockSource BlockSource) BlockSource {
primarySource := makeSingleBlockSource(settings, blockSource)
if secondaryBlockSource != nil {
return WithSecondaryBlocksSource(primarySource, makeSingleBlockSource(settings, secondaryBlockSource))
} else {
return primarySource
}
}

func makeSingleBlockSource(settings *Settings, blockSource BlockSource) BlockSource {
if settings.RetryCount > 0 {
blockSource = WithRetries(blockSource, settings.RetryCount, settings.RetryInterval, settings.Terminated)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/blockimporter/import_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ func TestImport(t *testing.T) {
time.Sleep(time.Second)
close(settings.Terminated)
}()
err := RunImport(&settings, NewFileBasedMockBlockSource())
err := RunImport(&settings, NewFileBasedMockBlockSource(), nil)
require.Empty(t, err)
}
12 changes: 9 additions & 3 deletions cmd/blockimporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
func main() {
// Parse commandline arguments
var (
dbPath = flag.String("db", "./db", "database path")
evmUrl = flag.String("evm", "http://127.0.0.1:8545", "EVM canister HTTP endpoint URL")
dbPath = flag.String("db", "./db", "database path")
evmUrl = flag.String("evm", "http://127.0.0.1:8545", "EVM canister HTTP endpoint URL")
secondaryBlockSourceUrl = flag.String("secondary-blocks-url", "", "URL of the secondary blocks source")
)
flag.Parse()

Expand All @@ -36,7 +37,12 @@ func main() {
}()

blockSource := NewHttpBlockSource(*evmUrl)
err := RunImport(&settings, &blockSource)
var secondaryBlockSource BlockSource
if *secondaryBlockSourceUrl != "" {
secondarySource := NewHttpBlockSource(*secondaryBlockSourceUrl)
secondaryBlockSource = &secondarySource
}
err := RunImport(&settings, &blockSource, secondaryBlockSource)

if err != nil {
logger.Error(err.Error())
Expand Down

0 comments on commit 5c38c4e

Please sign in to comment.