Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

perf: cache requests to the underlying fork provider #581

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Both `.provider()` and `.server()` take a single object which allows you to spec
* `"total_accounts"`: `number` - Number of accounts to generate at startup.
* `"fork"`: `string` or `object` - Fork from another currently running Ethereum client at a given block. When a `string`, input should be the HTTP location and port of the other client, e.g. `http://localhost:8545`. You can optionally specify the block to fork from using an `@` sign: `http://localhost:8545@1599200`. Can also be a `Web3 Provider` object, optionally used in conjunction with the `fork_block_number` option below.
* `"fork_block_number"`: `string` or `number` - Block number the provider should fork from, when the `fork` option is specified. If the `fork` option is specified as a string including the `@` sign and a block number, the block number in the `fork` parameter takes precedence.
- `"fork_cache_size"`: `number` - The maximum size of the cache for queries to the forked chain. Defaults to `10000`. You can set this to 0 to disable caching.
* `"network_id"`: Specify the network id ganache-core will use to identify itself (defaults to the current time or the network id of the forked blockchain if configured)
* `"time"`: `Date` - Date that the first block should start. Use this feature, along with the `evm_increaseTime` method to test time-dependent code.
* `"locked"`: `boolean` - whether or not accounts are locked by default.
Expand Down
27 changes: 27 additions & 0 deletions lib/utils/forkedblockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var Web3 = require("web3");
var to = require("./to.js");
var Transaction = require("./transaction");
var async = require("async");
var LRUCache = require("lru-cache");
const BN = utils.BN;

var inherits = require("util").inherits;
Expand Down Expand Up @@ -56,7 +57,33 @@ function ForkedBlockchain(options) {
} else {
this.fork = options.fork;
}

this.forkBlockNumber = options.fork_block_number;
this.forkCacheSize = parseInt(options.fork_cache_size == null ? 10000 : options.fork_cache_size);

if (this.forkCacheSize !== 0) {
const send = this.fork.send;
const cache = new LRUCache({ max: this.forkCacheSize });

// Patch the `send` method of the underlying fork provider. We can
// simply cache every non-error result because all requests to the
// fork should be deterministic.
this.fork.send = (payload, callback) => {
const key = JSON.stringify(Object.assign({}, payload, { id: null }));

if (cache.has(key)) {
callback(null, Object.assign({}, JSON.parse(cache.get(key)), { id: payload.id }));
} else {
send.call(this.fork, payload, (error, result) => {
if (!error) {
cache.set(key, JSON.stringify(Object.assign({}, result, { id: null })));
}

callback(error, result);
});
}
};
}

this.time = options.time;
this.storageTrieCache = {};
Expand Down
16 changes: 12 additions & 4 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"level-sublevel": "6.6.4",
"levelup": "3.1.1",
"lodash": "4.17.14",
"lru-cache": "^5.1.1",
"merkle-patricia-tree": "2.3.2",
"seedrandom": "3.0.1",
"source-map-support": "0.5.12",
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare module "ganache-core" {
default_balance_ether?: number;
fork?: string | object;
fork_block_number?: string | number;
fork_cache_size?: number;
gasLimit?: string | number;
gasPrice?: string;
hardfork?: "byzantium" | "constantinople" | "petersburg" | "istanbul" | "muirGlacier";
Expand Down