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

Ability to debug over a forked network #186

Merged
merged 4 commits into from
Aug 5, 2022
Merged
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
29 changes: 20 additions & 9 deletions src/debugAdapter/transaction/transactionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,34 @@ export class TransactionProvider {
}

public async getLastTransactionHashes(take: number = TRANSACTION_NUMBER_TO_SHOW): Promise<string[]> {
let latestBlockNumber: any;
let latestBlockNumber: number;

try {
latestBlockNumber = await this._web3.eth.getBlockNumber();
} catch {
throw new Error(Constants.informationMessage.transactionNotFound);
}

const latestBlock = await this._web3.eth.getBlock(latestBlockNumber);
const totalBlocks = latestBlockNumber - take > 0 ? take : latestBlockNumber;
const startingBlockNumber = latestBlockNumber - take > 0 ? latestBlockNumber - take : 0;
const blockNumbers = Array.from({length: totalBlocks}, (_, i) => i + 1 + startingBlockNumber).reverse();
const batchRequest = this._web3.createBatchRequest();

blockNumbers.forEach((block) => {
batchRequest.add(this._web3.eth.getBlock, block, true);
});

const blocks: any[] = await batchRequest.execute();
const accounts: string[] = await this._web3.eth.getAccounts();
const txHashes: string[] = [];
let block = latestBlock;
while (txHashes.length <= take && block.number > 0) {
for (let i = 0; i < block.transactions.length && txHashes.length < TRANSACTION_NUMBER_TO_SHOW; i++) {
txHashes.push(block.transactions[i]);
}
block = await this._web3.eth.getBlock(block.number - 1);
}

blocks.forEach((block) => {
const txs: any = Object.values(block.transactions)
.filter((tx: any) => accounts.includes(tx.from))
.map((tx: any) => tx.hash);

txHashes.push(...txs);
});

return txHashes;
}
Expand Down
2 changes: 2 additions & 0 deletions src/debugAdapter/types/web3.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ declare namespace Web3 {
getTransactionReceipt: any;
getBlockNumber: any;
currentProvider: any;
getAccounts: any;
}

const providers: {
Expand All @@ -49,6 +50,7 @@ declare namespace Web3 {

// eslint-disable-next-line no-redeclare
declare class Web3 {
static IBatchRequest: any;
constructor(provider: Web3.IProvider);

eth: Web3.IWeb3Eth;
Expand Down
4 changes: 2 additions & 2 deletions src/debugAdapter/web3Wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ function PromiseBatch(batch: Web3.IBatchRequest) {
const _batch = batch;
const _requests: Array<Promise<any>> = [];
return {
add(method: any, hash: string) {
add(method: any, ...args: any[]) {
const promise = new Promise<any>((accept, reject) => {
_batch.add(
method.request(hash, (error: any, result: any) => {
method.request(...args, (error: any, result: any) => {
if (error) {
reject(error);
}
Expand Down