Skip to content

Commit

Permalink
Use a single Blockchain interface and remove unused modules
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Mar 31, 2021
1 parent 0733755 commit 242da2f
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import { BN, zeros } from "ethereumjs-util";
import { BlockchainData } from "./BlockchainData";
import { FilterParams } from "./node-types";
import { RpcLogOutput, RpcReceiptOutput } from "./output";
import { Blockchain } from "./types/Blockchain";
import { PBlockchain, toBlockchain } from "./types/PBlockchain";
import { BlockchainInterface } from "./types/BlockchainInterface";

/* tslint:disable only-hardhat-error */

export class HardhatBlockchain implements PBlockchain {
export class HardhatBlockchain implements BlockchainInterface {
private readonly _data = new BlockchainData();
private _length = 0;

Expand Down Expand Up @@ -103,10 +102,6 @@ export class HardhatBlockchain implements PBlockchain {
return this._data.getLogs(filterParams);
}

public asBlockchain(): Blockchain {
return toBlockchain(this);
}

private _validateBlock(block: Block) {
const blockNumber = block.header.number.toNumber();
const parentHash = block.header.parentHash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ import {
toRpcLogOutput,
toRpcReceiptOutput,
} from "../output";
import { Blockchain } from "../types/Blockchain";
import { PBlockchain, toBlockchain } from "../types/PBlockchain";
import { BlockchainInterface } from "../types/BlockchainInterface";

import { ForkTransaction } from "./ForkTransaction";
import { rpcToBlockData } from "./rpcToBlockData";
import { rpcToTxData } from "./rpcToTxData";

/* tslint:disable only-hardhat-error */

export class ForkBlockchain implements PBlockchain {
export class ForkBlockchain implements BlockchainInterface {
private _data = new BlockchainData();
private _latestBlockNumber = this._forkBlockNumber;

Expand Down Expand Up @@ -200,10 +199,6 @@ export class ForkBlockchain implements PBlockchain {
return this._data.getLogs(filterParams);
}

public asBlockchain(): Blockchain {
return toBlockchain(this);
}

private async _getBlockByHash(blockHash: Buffer) {
const block = this._data.getBlockByHash(blockHash);
if (block !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import {
} from "./output";
import { TxPool } from "./TxPool";
import { TxPriorityHeap } from "./TxPriorityHeap";
import { PBlockchain } from "./types/PBlockchain";
import { BlockchainInterface } from "./types/BlockchainInterface";
import { FakeTransaction } from "./utils/fakeTransaction";
import { getCurrentTimestamp } from "./utils/getCurrentTimestamp";
import { makeCommon } from "./utils/makeCommon";
Expand Down Expand Up @@ -170,7 +170,7 @@ export class HardhatNode extends EventEmitter {
common,
activatePrecompiles: true,
stateManager,
blockchain: blockchain.asBlockchain() as any,
blockchain: blockchain as any,
allowUnlimitedContractSize,
});

Expand Down Expand Up @@ -208,7 +208,7 @@ export class HardhatNode extends EventEmitter {
private constructor(
private readonly _vm: VM,
private readonly _stateManager: StateManager,
private readonly _blockchain: PBlockchain,
private readonly _blockchain: BlockchainInterface,
private readonly _txPool: TxPool,
private _automine: boolean,
private _blockTimeOffsetSeconds: BN = new BN(0),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { BN } from "ethereumjs-util";
import { FilterParams } from "../node-types";
import { RpcLogOutput, RpcReceiptOutput } from "../output";

import { Blockchain } from "./Blockchain";
import { Callback } from "./Callback";

export interface PBlockchain {
// TODO: Replace this with BlockchainInterface from @ethereumjs/blockchain
export interface BlockchainInterface {
getLatestBlock(): Promise<Block>;
getBlock(blockHashOrNumber: Buffer | number | BN): Promise<Block | undefined>;
addBlock(block: Block): Promise<Block>;
Expand All @@ -28,39 +26,3 @@ export interface PBlockchain {
addTransactionReceipts(receipts: RpcReceiptOutput[]): void;
getLogs(filterParams: FilterParams): Promise<RpcLogOutput[]>;
}

export function toBlockchain(pb: PBlockchain): Blockchain {
async function getBlock(blockTag: number | Buffer | BN) {
const block = await pb.getBlock(blockTag);
if (block === undefined) {
// tslint:disable-next-line only-hardhat-error
throw new Error("Block not found");
}
return block;
}

function delBlock(blockHash: Buffer, cb: Callback) {
try {
pb.deleteBlock(blockHash);
} catch (e) {
return;
}
}

return {
getBlock,
putBlock: pb.addBlock.bind(pb),
delBlock,
getDetails,
iterator,
};
}

function getDetails(_: string, cb: Callback) {
cb(null);
}

function iterator() {
// tslint:disable-next-line only-hardhat-error
throw new Error(".iterator() is not supported");
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { Block } from "@ethereumjs/block";
import { assert } from "chai";
import { BN, BufferLike, zeros } from "ethereumjs-util";
import { promisify } from "util";

import { randomHashBuffer } from "../../../../src/internal/hardhat-network/provider/fork/random";
import { HardhatBlockchain } from "../../../../src/internal/hardhat-network/provider/HardhatBlockchain";
import { Blockchain } from "../../../../src/internal/hardhat-network/provider/types/Blockchain";
import {
PBlockchain,
toBlockchain,
} from "../../../../src/internal/hardhat-network/provider/types/PBlockchain";
import { BlockchainInterface } from "../../../../src/internal/hardhat-network/provider/types/BlockchainInterface";
import {
createTestLog,
createTestReceipt,
createTestTransaction,
} from "../helpers/blockchain";

describe("HardhatBlockchain", () => {
let blockchain: PBlockchain;
let callbackifiedBlockchain: Blockchain;
let blockchain: BlockchainInterface;
let blocks: Block[];

function createBlock(number: number, _difficulty?: BufferLike) {
Expand All @@ -33,7 +27,6 @@ describe("HardhatBlockchain", () => {

beforeEach(() => {
blockchain = new HardhatBlockchain();
callbackifiedBlockchain = toBlockchain(new HardhatBlockchain());
blocks = [];
});

Expand Down Expand Up @@ -63,17 +56,6 @@ describe("HardhatBlockchain", () => {
assert.equal(await blockchain.getBlock(one.hash()), one);
});

it.skip("can get existing block by hash (callbackified)", async () => {
const genesis = createBlock(0);
const one = createBlock(1);
await promisify<Block>(callbackifiedBlockchain.putBlock)(genesis);
await promisify<Block>(callbackifiedBlockchain.putBlock)(one);
const block = await promisify<number | Buffer | BN, Block>(
callbackifiedBlockchain.getBlock
)(one.hash());
assert.equal(block, one);
});

it("can get existing block by number", async () => {
await blockchain.addBlock(createBlock(0));
const one = createBlock(1);
Expand Down Expand Up @@ -103,18 +85,6 @@ describe("HardhatBlockchain", () => {
assert.equal(savedBlock, genesis);
});

it.skip("can save genesis block (callbackified)", async () => {
const genesis = createBlock(0);
const returnedBlock = await promisify<Block, Block>(
callbackifiedBlockchain.putBlock
)(genesis);
const savedBlock = await promisify<number | Buffer | BN, Block>(
callbackifiedBlockchain.getBlock
)(0);
assert.equal(returnedBlock, genesis);
assert.equal(savedBlock, genesis);
});

it("rejects blocks with invalid block number", async () => {
const block = Block.fromBlockData({ header: { number: 1 } });
await assert.isRejected(
Expand Down Expand Up @@ -178,34 +148,6 @@ describe("HardhatBlockchain", () => {
assert.equal(await blockchain.getBlock(blockThree.hash()), undefined);
});

it.skip("removes the block and all subsequent ones (callbackified)", async () => {
const blockOne = createBlock(0);
const blockTwo = createBlock(1);
const blockThree = createBlock(2);

await promisify<Block>(callbackifiedBlockchain.putBlock)(blockOne);
await promisify<Block>(callbackifiedBlockchain.putBlock)(blockTwo);
await promisify<Block>(callbackifiedBlockchain.putBlock)(blockThree);

await promisify<Buffer>(callbackifiedBlockchain.delBlock)(
blockOne.hash()
);

const savedBlockOne = await promisify<number | Buffer | BN, Block>(
callbackifiedBlockchain.getBlock
)(blockOne.hash());
const savedBlockTwo = await promisify<number | Buffer | BN, Block>(
callbackifiedBlockchain.getBlock
)(blockTwo.hash());
const savedBlockThree = await promisify<number | Buffer | BN, Block>(
callbackifiedBlockchain.getBlock
)(blockThree.hash());

assert.equal(savedBlockOne, undefined);
assert.equal(savedBlockTwo, undefined);
assert.equal(savedBlockThree, undefined);
});

it("updates the latest block number", async () => {
const blockOne = createBlock(0);
const blockTwo = createBlock(1);
Expand Down

0 comments on commit 242da2f

Please sign in to comment.