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

consider updating persistent cache to get blocks from fork network by tag #3773

Open
MicaiahReid opened this issue Oct 6, 2022 · 1 comment

Comments

@MicaiahReid
Copy link
Contributor

MicaiahReid commented Oct 6, 2022

The persistent cache's get function doesn't currently allow passing the "earliest" tag to cache that request:

async get(method: string, params: any[], key: string) {
const blockNumber = getBlockNumberFromParams(method, params);
if (blockNumber == null) return;
const height = Quantity.from(blockNumber);
const bufKey = Buffer.from(key);
const start = lexico.encode([height.toBuffer(), bufKey]);
const end = Buffer.concat([start, maxValueByteBuffer]);
const readStream = this.cacheDb.createReadStream({
gt: start,
lt: end,
keys: true,
values: true
});
for await (const data of readStream) {
const { key: k, value } = data as any as { key: Buffer; value: Buffer };
const [_height, _key, blockHash] = lexico.decode(k);
// if our key no longer matches make sure we don't keep searching
if (!_key.equals(bufKey)) return;
if (
this.hashBuffer.equals(blockHash) ||
(await this.ancestry.has(blockHash))
) {
return value;
}
}
}

This is kind of tricky to work around because of the way we lexicographically encode the requests to store/retrieve them efficiently in the database.

Because the persistent cache always needs to fetch the earliest block (to validate ancestry of the fork), we should cache this block in memory.

When the block manager is started, we are currently disabling the cache, so we should fix this once this issue is resolved.
https://github.com/trufflesuite/ganache/pull/3755/files#r989390078

@MicaiahReid
Copy link
Contributor Author

MicaiahReid commented Oct 6, 2022

Related: #3755

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

1 participant