-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1023 from ethereumjs/client-typesafe
Client: make fetcher more typesafe
- Loading branch information
Showing
20 changed files
with
11,410 additions
and
947 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Fetcher, FetcherOptions } from './fetcher' | ||
import { BN } from 'ethereumjs-util' | ||
import { Chain } from '../../blockchain' | ||
|
||
export interface BlockFetcherOptions extends FetcherOptions { | ||
/* Blockchain */ | ||
chain: Chain | ||
|
||
/* Block number to start fetching from */ | ||
first: BN | ||
|
||
/* How many blocks to fetch */ | ||
count: BN | ||
} | ||
|
||
export type JobTask = { | ||
first: BN | ||
count: number | ||
} | ||
|
||
export abstract class BlockFetcherBase<JobResult, StorageItem> extends Fetcher< | ||
JobTask, | ||
JobResult, | ||
StorageItem | ||
> { | ||
protected chain: Chain | ||
protected first: BN | ||
protected count: BN | ||
|
||
/** | ||
* Create new block fetcher | ||
* @param {BlockFetcherOptions} | ||
*/ | ||
constructor(options: BlockFetcherOptions) { | ||
super(options) | ||
|
||
this.chain = options.chain | ||
this.maxPerRequest = options.maxPerRequest ?? 128 | ||
this.first = options.first | ||
this.count = options.count | ||
} | ||
|
||
/** | ||
* Generate list of tasks to fetch | ||
* @return {Object[]} tasks | ||
*/ | ||
tasks(): JobTask[] { | ||
const { first, count } = this | ||
const max = this.maxPerRequest | ||
const tasks: JobTask[] = [] | ||
while (count.gten(max)) { | ||
tasks.push({ first: first.clone(), count: max }) | ||
first.iaddn(max) | ||
count.isubn(max) | ||
} | ||
if (count.gtn(0)) { | ||
tasks.push({ first: first.clone(), count: count.toNumber() }) | ||
} | ||
return tasks | ||
} | ||
} |
Oops, something went wrong.