diff --git a/README.md b/README.md index 932ce86..d5dc8fc 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,8 @@ ethereumjs.run({ network: 'rinkeby', syncmode: 'light', bootnodes: '/ip4/127.0.0 That's it! Now, you should start seeing headers being downloaded to the local storage of your browser. Since IndexDB is being used, even if you close and re-open the browser window, the headers you'll already downloaded will be saved. +![EthereumJS Client Libp2p Browser Syncing](./browser_sync.png?raw=true) + ## Design **Goals** diff --git a/browser_sync.png b/browser_sync.png new file mode 100644 index 0000000..e782229 Binary files /dev/null and b/browser_sync.png differ diff --git a/lib/service/ethereumservice.js b/lib/service/ethereumservice.js index d9c8eaa..7d9129a 100644 --- a/lib/service/ethereumservice.js +++ b/lib/service/ethereumservice.js @@ -37,6 +37,7 @@ class EthereumService extends Service { this.flow = new FlowControl(options) this.chain = options.chain || new Chain(options) + this.common = options.common this.minPeers = options.minPeers this.interval = options.interval this.timeout = options.timeout diff --git a/lib/service/fastethereumservice.js b/lib/service/fastethereumservice.js index 0051cab..aa907da 100644 --- a/lib/service/fastethereumservice.js +++ b/lib/service/fastethereumservice.js @@ -39,6 +39,7 @@ class FastEthereumService extends EthereumService { logger: this.logger, pool: this.pool, chain: this.chain, + common: this.common, minPeers: this.minPeers, interval: this.interval }) diff --git a/lib/service/lightethereumservice.js b/lib/service/lightethereumservice.js index 9c3c6c6..4baca1b 100644 --- a/lib/service/lightethereumservice.js +++ b/lib/service/lightethereumservice.js @@ -31,6 +31,7 @@ class LightEthereumService extends EthereumService { logger: this.logger, pool: this.pool, chain: this.chain, + common: this.common, minPeers: this.minPeers, flow: this.flow, interval: this.interval diff --git a/lib/sync/fastsync.js b/lib/sync/fastsync.js index 4c5346e..e34577b 100644 --- a/lib/sync/fastsync.js +++ b/lib/sync/fastsync.js @@ -75,6 +75,7 @@ class FastSynchronizer extends Synchronizer { this.blockFetcher = new BlockFetcher({ pool: this.pool, chain: this.chain, + common: this.common, logger: this.logger, interval: this.interval, first, diff --git a/lib/sync/fetcher/blockfetcher.js b/lib/sync/fetcher/blockfetcher.js index c26fa4f..afbc708 100644 --- a/lib/sync/fetcher/blockfetcher.js +++ b/lib/sync/fetcher/blockfetcher.js @@ -64,7 +64,7 @@ class BlockFetcher extends Fetcher { let { first, count } = task const headers = await peer.eth.getBlockHeaders({ block: first, max: count }) const bodies = await peer.eth.getBlockBodies(headers.map(h => h.hash())) - const blocks = bodies.map((body, i) => new Block([headers[i]].concat(body))) + const blocks = bodies.map((body, i) => new Block([headers[i]].concat(body), { common: this.common })) return { blocks } } diff --git a/lib/sync/fetcher/fetcher.js b/lib/sync/fetcher/fetcher.js index d9683db..09f1116 100644 --- a/lib/sync/fetcher/fetcher.js +++ b/lib/sync/fetcher/fetcher.js @@ -2,9 +2,11 @@ const { Readable, Writable } = require('stream') const Heap = require('qheap') +const Common = require('ethereumjs-common').default const { defaultLogger } = require('../../logging') const defaultOptions = { + common: new Common('mainnet', 'chainstart'), logger: defaultLogger, timeout: 5000, interval: 1000, @@ -24,6 +26,7 @@ class Fetcher extends Readable { /** * Create new fetcher * @param {Object} options constructor parameters + * @param {Common} options.common common chain config * @param {PeerPool} options.pool peer pool * @param {number} [options.timeout] fetch task timeout * @param {number} [options.banTime] how long to ban misbehaving peers @@ -36,6 +39,7 @@ class Fetcher extends Readable { super({ ...options, objectMode: true }) options = { ...defaultOptions, ...options } + this.common = options.common this.pool = options.pool this.logger = options.logger this.timeout = options.timeout diff --git a/lib/sync/lightsync.js b/lib/sync/lightsync.js index b53d86c..506bbe7 100644 --- a/lib/sync/lightsync.js +++ b/lib/sync/lightsync.js @@ -62,6 +62,7 @@ class LightSynchronizer extends Synchronizer { this.headerFetcher = new HeaderFetcher({ pool: this.pool, chain: this.chain, + common: this.common, flow: this.flow, logger: this.logger, interval: this.interval, diff --git a/lib/sync/sync.js b/lib/sync/sync.js index 8df89b2..54ea3da 100644 --- a/lib/sync/sync.js +++ b/lib/sync/sync.js @@ -1,9 +1,11 @@ 'use strict' +const Common = require('ethereumjs-common').default const EventEmitter = require('events') const { defaultLogger } = require('../logging') const defaultOptions = { + common: new Common('mainnet', 'chainstart'), logger: defaultLogger, interval: 1000, minPeers: 3 @@ -19,6 +21,7 @@ class Synchronizer extends EventEmitter { * @param {Object} options constructor parameters * @param {PeerPool} options.pool peer pool * @param {Chain} options.chain blockchain + * @param {Common} options.common common chain config * @param {FlowControl} options.flow flow control manager * @param {number} [options.minPeers=3] number of peers needed before syncing * @param {number} [options.interval] refresh interval @@ -31,6 +34,7 @@ class Synchronizer extends EventEmitter { this.logger = options.logger this.pool = options.pool this.chain = options.chain + this.common = options.common this.flow = options.flow this.minPeers = options.minPeers this.interval = options.interval