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

feat: get logs from a fork network directly #3692

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
69 changes: 44 additions & 25 deletions src/chains/ethereum/ethereum/src/data-managers/block-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,37 +177,56 @@ export default class BlockManager extends Manager<Block> {
}

async getNumberFromHash(hash: string | Buffer | Tag) {
return this.#blockIndexes.get(Data.toBuffer(hash)).catch(e => {
if (e.status === NOTFOUND) return null;
throw e;
}) as Promise<Buffer | null>;
const number = await this.#blockIndexes
.get(Data.toBuffer(hash))
.catch(e => {
if (e.status === NOTFOUND) return null;
throw e;
});
if (number !== null) {
return Quantity.from(number);
}
const fallback = this.#blockchain.fallback;
if (fallback) {
const json = await fallback.request<any>("eth_getBlockByHash", [
Data.from(hash),
true
]);
if (json) {
return Quantity.from(json.number);
}
}
return null;
}

async getByHash(hash: string | Buffer | Tag) {
const number = await this.getNumberFromHash(hash);
if (number === null) {
const fallback = this.#blockchain.fallback;
if (fallback) {
const json = await fallback.request<any>("eth_getBlockByHash", [
Data.from(hash),
true
]);
if (json) {
const blockNumber = BigInt(json.number);
if (blockNumber <= fallback.blockNumber.toBigInt()) {
const common = fallback.getCommonForBlockNumber(
this.#common,
blockNumber
);
return new Block(BlockManager.rawFromJSON(json, common), common);
}
const number = await this.#blockIndexes
.get(Data.toBuffer(hash))
.catch(e => {
if (e.status === NOTFOUND) return null;
throw e;
});
if (number !== null) {
return this.get(number);
}
const fallback = this.#blockchain.fallback;
if (fallback) {
const json = await fallback.request<any>("eth_getBlockByHash", [
Data.from(hash),
true
]);
if (json) {
const blockNumber = BigInt(json.number);
if (blockNumber <= fallback.blockNumber.toBigInt()) {
const common = fallback.getCommonForBlockNumber(
this.#common,
blockNumber
);
return new Block(BlockManager.rawFromJSON(json, common), common);
}
}

return null;
} else {
return this.get(number);
}
return null;
}

async getRawByBlockNumber(blockNumber: Quantity): Promise<Buffer> {
Expand Down
127 changes: 91 additions & 36 deletions src/chains/ethereum/ethereum/src/data-managers/blocklog-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GanacheLevelUp } from "../database";

export default class BlockLogManager extends Manager<BlockLogs> {
#blockchain: Blockchain;

constructor(base: GanacheLevelUp, blockchain: Blockchain) {
super(base, BlockLogs);
this.#blockchain = blockchain;
Expand All @@ -19,11 +20,13 @@ export default class BlockLogManager extends Manager<BlockLogs> {
log.blockNumber = Quantity.from(key);
} else if (this.#blockchain.fallback) {
const block = Quantity.from(key);
const res = await this.#blockchain.fallback.request<any[] | null>(
"eth_getLogs",
[{ fromBlock: block, toBlock: block }]
);
return BlockLogs.fromJSON(res);
if (this.#blockchain.fallback.isValidForkBlockNumber(block)) {
const res = await this.#blockchain.fallback.request<any[] | null>(
"eth_getLogs",
[{ fromBlock: block, toBlock: block }]
);
return BlockLogs.fromJSON(res);
}
}
return log;
}
Expand All @@ -35,42 +38,94 @@ export default class BlockLogManager extends Manager<BlockLogs> {
const blockNumber = await blockchain.blocks.getNumberFromHash(
filter.blockHash
);
if (!blockNumber) return [];

const logs = await this.get(blockNumber);
if (!blockNumber) {
return [];
}
const logs = await this.get(blockNumber.toBuffer());
return logs ? [...logs.filter(addresses, topics)] : [];
} else {
const { addresses, topics, fromBlock, toBlockNumber } = parseFilter(
filter,
blockchain
}
const { fromBlock, toBlock } = parseFilter(filter, blockchain);
if (fromBlock.toBigInt() > toBlock.toBigInt()) {
throw new Error(
"One of the blocks specified in filter (fromBlock, toBlock or blockHash) cannot be found."
);
}

const pendingLogsPromises: Promise<BlockLogs>[] = [
this.get(fromBlock.toBuffer())
];

const fromBlockNumber = fromBlock.toNumber();
// if we have a range of blocks to search, do that here:
if (fromBlockNumber !== toBlockNumber) {
// fetch all the blockLogs in-between `fromBlock` and `toBlock` (excluding
// from, because we already started fetching that one)
for (let i = fromBlockNumber + 1, l = toBlockNumber + 1; i < l; i++) {
pendingLogsPromises.push(this.get(Quantity.toBuffer(i)));
}
}
const fork = this.#blockchain.fallback;
if (!fork) {
return await this.getLocal(
fromBlock.toNumber(),
toBlock.toNumber(),
filter
);
}
const from = Quantity.min(fromBlock, toBlock);
const ret: Ethereum.Logs = [];
if (fork.isValidForkBlockNumber(from)) {
ret.push(
...(await this.getFromFork(
from,
Quantity.min(toBlock, fork.blockNumber),
filter
))
);
}
if (!fork.isValidForkBlockNumber(toBlock)) {
ret.push(
...(await this.getLocal(
Math.max(from.toNumber(), fork.blockNumber.toNumber() + 1),
toBlock.toNumber(),
filter
))
);
}
return ret;
}

// now filter and compute all the blocks' blockLogs (in block order)
return Promise.all(pendingLogsPromises).then(blockLogsRange => {
const filteredBlockLogs: Ethereum.Logs = [];
blockLogsRange.forEach(blockLogs => {
// TODO(perf): this loops over all addresses for every block.
// Maybe make it loop only once?
// Issue: https://github.com/trufflesuite/ganache/issues/3482
if (blockLogs)
filteredBlockLogs.push(...blockLogs.filter(addresses, topics));
});
return filteredBlockLogs;
getLocal(
from: number,
to: number,
filter: FilterArgs
): Promise<Ethereum.Logs> {
const { addresses, topics } = parseFilterDetails(filter);
const pendingLogsPromises: Promise<BlockLogs>[] = [];
for (let i = from; i <= to; i++) {
pendingLogsPromises.push(this.get(Quantity.toBuffer(i)));
}
return Promise.all(pendingLogsPromises).then(blockLogsRange => {
const filteredBlockLogs: Ethereum.Logs = [];
blockLogsRange.forEach(blockLogs => {
// TODO(perf): this loops over all addresses for every block.
// Maybe make it loop only once?
// Issue: https://github.com/trufflesuite/ganache/issues/3482
if (blockLogs)
filteredBlockLogs.push(...blockLogs.filter(addresses, topics));
});
return filteredBlockLogs;
});
}

async getFromFork(
from: Quantity,
to: Quantity,
filter: FilterArgs
): Promise<Ethereum.Logs> {
const { topics } = parseFilterDetails(filter);
const f = this.#blockchain.fallback;
if (!f || !f.isValidForkBlockNumber(from)) {
return [];
}
return await f.request<Ethereum.Logs | null>("eth_getLogs", [
{
fromBlock: from,
toBlock: f.selectValidForkBlockNumber(to),
address: filter.address
? Array.isArray(filter.address)
? filter.address
: [filter.address]
: [],
topics
}
]);
}
}
77 changes: 77 additions & 0 deletions src/chains/ethereum/ethereum/tests/forking/contracts/IERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;


/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);

/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);

/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);

/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);

/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);

/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);

/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);

/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
Loading