Skip to content

Commit

Permalink
Add missing config for ts-tests (paritytech#730)
Browse files Browse the repository at this point in the history
* Add BLOCK_TIMESTAMP config for ts-tests

Signed-off-by: koushiro <[email protected]>

* Add NODE_BINARY_NAME

Signed-off-by: koushiro <[email protected]>

* Improve test-balances

Signed-off-by: koushiro <[email protected]>

* Some nits

Signed-off-by: koushiro <[email protected]>

* Add RUNTIME_SPEC_NAME, RUNTIME_SPEC_VERSION, RUNTIME_IMPL_VERSION

Signed-off-by: koushiro <[email protected]>

* Some nits

Signed-off-by: koushiro <[email protected]>

* Use CHAIN_ID

Signed-off-by: koushiro <[email protected]>

* Some nits

Signed-off-by: koushiro <[email protected]>

* Improve test-revert-receipt

Signed-off-by: koushiro <[email protected]>

* Use camelCase

Signed-off-by: koushiro <[email protected]>

* Use tabs

Signed-off-by: koushiro <[email protected]>
  • Loading branch information
koushiro authored Jul 4, 2022
1 parent da3f879 commit 175e42f
Show file tree
Hide file tree
Showing 19 changed files with 181 additions and 188 deletions.
1 change: 1 addition & 0 deletions ts-tests/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
printWidth: 120,
useTabs: true,
tabWidth: 4
}
9 changes: 9 additions & 0 deletions ts-tests/tests/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
export const GENESIS_ACCOUNT = "0x6be02d1d3665660d22ff9624b7be0551ee1ac91b";
export const GENESIS_ACCOUNT_PRIVATE_KEY = "0x99B3C12287537E38C90A9219D4CB074A89A16E9CDB20BF85728EBD97C343E342";
export const GENESIS_ACCOUNT_BALANCE = "340282366920938463463374607431768210955";

export const FIRST_CONTRACT_ADDRESS = "0xc2bf5f29a4384b1ab0c063e1c666f02121b6084a";

export const NODE_BINARY_NAME = "frontier-template-node";

export const RUNTIME_SPEC_NAME = "node-frontier-template";
export const RUNTIME_SPEC_VERSION = 1;
export const RUNTIME_IMPL_VERSION = 1;

export const CHAIN_ID = 42;
export const BLOCK_TIMESTAMP = 6; // 6 seconds per block
export const BLOCK_HASH_COUNT = 256;
export const EXISTENTIAL_DEPOSIT = 500; // The minimum amount required to keep an account open
export const BLOCK_GAS_LIMIT = 75000000;
22 changes: 15 additions & 7 deletions ts-tests/tests/test-balance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { expect } from "chai";
import { step } from "mocha-steps";

import { GENESIS_ACCOUNT, GENESIS_ACCOUNT_PRIVATE_KEY } from "./config";
import { GENESIS_ACCOUNT, GENESIS_ACCOUNT_PRIVATE_KEY, GENESIS_ACCOUNT_BALANCE, EXISTENTIAL_DEPOSIT } from "./config";
import { createAndFinalizeBlock, describeWithFrontier, customRequest } from "./util";

describeWithFrontier("Frontier RPC (Balance)", (context) => {
const GENESIS_ACCOUNT_BALANCE = "340282366920938463463374607431768210955";
const TEST_ACCOUNT = "0x1111111111111111111111111111111111111111";

step("genesis balance is setup correctly", async function () {
Expand All @@ -15,23 +14,32 @@ describeWithFrontier("Frontier RPC (Balance)", (context) => {
step("balance to be updated after transfer", async function () {
this.timeout(15000);

const value = "0x200"; // 512, must be higher than ExistentialDeposit
const gasPrice = "0x3B9ACA00"; // 1000000000
const tx = await context.web3.eth.accounts.signTransaction(
{
from: GENESIS_ACCOUNT,
to: TEST_ACCOUNT,
value: "0x200", // Must be higher than ExistentialDeposit (500)
gasPrice: "0x3B9ACA00",
value: value,
gasPrice: gasPrice,
gas: "0x100000",
},
GENESIS_ACCOUNT_PRIVATE_KEY
);
await customRequest(context.web3, "eth_sendRawTransaction", [tx.rawTransaction]);
const expectedGenesisBalance = "340282366920938463463374586431768210443";
const expectedTestBalance = "12";

// GENESIS_ACCOUNT_BALANCE - (21000 * gasPrice) - value;
const expectedGenesisBalance = (
BigInt(GENESIS_ACCOUNT_BALANCE) -
BigInt(21000) * BigInt(gasPrice) -
BigInt(value)
).toString();
const expectedTestBalance = (Number(value) - EXISTENTIAL_DEPOSIT).toString();
expect(await context.web3.eth.getBalance(GENESIS_ACCOUNT, "pending")).to.equal(expectedGenesisBalance);
expect(await context.web3.eth.getBalance(TEST_ACCOUNT, "pending")).to.equal(expectedTestBalance);

await createAndFinalizeBlock(context.web3);
// 340282366920938463463374607431768210955 - (21000 * 1000000000) + 512;

expect(await context.web3.eth.getBalance(GENESIS_ACCOUNT)).to.equal(expectedGenesisBalance);
expect(await context.web3.eth.getBalance(TEST_ACCOUNT)).to.equal(expectedTestBalance);
});
Expand Down
6 changes: 3 additions & 3 deletions ts-tests/tests/test-block.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import { step } from "mocha-steps";

import { BLOCK_GAS_LIMIT } from "./config";
import { BLOCK_TIMESTAMP, BLOCK_GAS_LIMIT } from "./config";
import { createAndFinalizeBlock, describeWithFrontier } from "./util";

describeWithFrontier("Frontier RPC (Block)", (context) => {
Expand Down Expand Up @@ -65,7 +65,7 @@ describeWithFrontier("Frontier RPC (Block)", (context) => {

step("should have valid timestamp after block production", async function () {
const block = await context.web3.eth.getBlock("latest");
expect(block.timestamp).to.be.eq(6);
expect(block.timestamp).to.be.eq(BLOCK_TIMESTAMP);
});

it("genesis block should be already available by hash", async function () {
Expand Down Expand Up @@ -110,7 +110,7 @@ describeWithFrontier("Frontier RPC (Block)", (context) => {
//parentHash: "0x04540257811b46d103d9896e7807040e7de5080e285841c5430d1a81588a0ce4",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
size: 507,
timestamp: 6,
timestamp: BLOCK_TIMESTAMP,
totalDifficulty: "0",
//transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
Expand Down
6 changes: 3 additions & 3 deletions ts-tests/tests/test-contract-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ describeWithFrontier("Frontier RPC (Contract Methods)", (context) => {
const latestBlock = await context.web3.eth.getBlock("latest");
expect(latestBlock.transactions.length).to.equal(1);

const tx_hash = latestBlock.transactions[0];
const tx = await context.web3.eth.getTransaction(tx_hash);
expect(tx.hash).to.equal(tx_hash);
const txHash = latestBlock.transactions[0];
const tx = await context.web3.eth.getTransaction(txHash);
expect(tx.hash).to.equal(txHash);
});

it("should return contract method result", async function () {
Expand Down
48 changes: 23 additions & 25 deletions ts-tests/tests/test-fee-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describeWithFrontier("Frontier RPC (Fee History)", (context) => {

let nonce = 0;

function get_percentile(percentile, array) {
function getPercentile(percentile, array) {
array.sort(function (a, b) {
return a - b;
});
Expand Down Expand Up @@ -63,46 +63,44 @@ describeWithFrontier("Frontier RPC (Fee History)", (context) => {
.catch((err) => expect(err.message).to.equal("Error getting header at BlockId::Number(1)"));
});

step("result lenght should match spec", async function () {
step("result length should match spec", async function () {
this.timeout(100000);
let block_count = 2;
let reward_percentiles = [20, 50, 70];
let priority_fees = [1, 2, 3];
await createBlocks(block_count, priority_fees);
let result = (await customRequest(context.web3, "eth_feeHistory", ["0x2", "latest", reward_percentiles]))
.result;
let blockCount = 2;
let rewardPercentiles = [20, 50, 70];
let priorityFees = [1, 2, 3];
await createBlocks(blockCount, priorityFees);
let result = (await customRequest(context.web3, "eth_feeHistory", ["0x2", "latest", rewardPercentiles])).result;

// baseFeePerGas is always the requested block range + 1 (the next derived base fee).
expect(result.baseFeePerGas.length).to.be.eq(block_count + 1);
expect(result.baseFeePerGas.length).to.be.eq(blockCount + 1);
// gasUsedRatio for the requested block range.
expect(result.gasUsedRatio.length).to.be.eq(block_count);
expect(result.gasUsedRatio.length).to.be.eq(blockCount);
// two-dimensional reward list for the requested block range.
expect(result.reward.length).to.be.eq(block_count);
expect(result.reward.length).to.be.eq(blockCount);
// each block has a reward list which's size is the requested percentile list.
for (let i = 0; i < block_count; i++) {
expect(result.reward[i].length).to.be.eq(reward_percentiles.length);
for (let i = 0; i < blockCount; i++) {
expect(result.reward[i].length).to.be.eq(rewardPercentiles.length);
}
});

step("should calculate percentiles", async function () {
this.timeout(100000);
let block_count = 11;
let reward_percentiles = [20, 50, 70, 85, 100];
let priority_fees = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
await createBlocks(block_count, priority_fees);
let result = (await customRequest(context.web3, "eth_feeHistory", ["0xA", "latest", reward_percentiles]))
.result;
let blockCount = 11;
let rewardPercentiles = [20, 50, 70, 85, 100];
let priorityFees = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
await createBlocks(blockCount, priorityFees);
let result = (await customRequest(context.web3, "eth_feeHistory", ["0xA", "latest", rewardPercentiles])).result;

// Calculate the percentiles in javascript.
let local_rewards = [];
for (let i = 0; i < reward_percentiles.length; i++) {
local_rewards.push(get_percentile(reward_percentiles[i], priority_fees));
let localRewards = [];
for (let i = 0; i < rewardPercentiles.length; i++) {
localRewards.push(getPercentile(rewardPercentiles[i], priorityFees));
}
// Compare the rpc result with the javascript percentiles.
for (let i = 0; i < result.reward.length; i++) {
expect(result.reward[i].length).to.be.eq(local_rewards.length);
for (let j = 0; j < local_rewards.length; j++) {
expect(context.web3.utils.hexToNumber(result.reward[i][j])).to.be.eq(local_rewards[j]);
expect(result.reward[i].length).to.be.eq(localRewards.length);
for (let j = 0; j < localRewards.length; j++) {
expect(context.web3.utils.hexToNumber(result.reward[i][j])).to.be.eq(localRewards[j]);
}
}
});
Expand Down
46 changes: 23 additions & 23 deletions ts-tests/tests/test-filter-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ describeWithFrontier("Frontier RPC (EthFilterApi)", (context) => {
}

step("should create a Log filter and return the ID", async function () {
let create_filter = await customRequest(context.web3, "eth_newFilter", [
let createFilter = await customRequest(context.web3, "eth_newFilter", [
{
fromBlock: "0x0",
toBlock: "latest",
address: ["0xC2Bf5F29a4384b1aB0C063e1c666f02121B6084a", "0x5c4242beB94dE30b922f57241f1D02f36e906915"],
topics: ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],
},
]);
expect(create_filter.result).to.be.eq("0x1");
expect(createFilter.result).to.be.eq("0x1");
});

step("should increment filter ID", async function () {
let create_filter = await customRequest(context.web3, "eth_newFilter", [
let createFilter = await customRequest(context.web3, "eth_newFilter", [
{
fromBlock: "0x1",
toBlock: "0x2",
address: "0xC2Bf5F29a4384b1aB0C063e1c666f02121B6084a",
topics: ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],
},
]);
expect(create_filter.result).to.be.eq("0x2");
expect(createFilter.result).to.be.eq("0x2");
});

step("should create a Block filter and return the ID", async function () {
let create_filter = await customRequest(context.web3, "eth_newBlockFilter", []);
expect(create_filter.result).to.be.eq("0x3");
let createFilter = await customRequest(context.web3, "eth_newBlockFilter", []);
expect(createFilter.result).to.be.eq("0x3");
});

step("should return unsupported error for Pending Transaction filter creation", async function () {
Expand Down Expand Up @@ -96,22 +96,22 @@ describeWithFrontier("Frontier RPC (EthFilterApi)", (context) => {
expect(receipt.logs.length).to.be.eq(1);

// Create a filter for the created contract.
let create_filter = await customRequest(context.web3, "eth_newFilter", [
let createFilter = await customRequest(context.web3, "eth_newFilter", [
{
fromBlock: "0x0",
toBlock: "latest",
address: receipt.contractAddress,
topics: receipt.logs[0].topics,
},
]);
let poll = await customRequest(context.web3, "eth_getFilterChanges", [create_filter.result]);
let poll = await customRequest(context.web3, "eth_getFilterChanges", [createFilter.result]);

expect(poll.result.length).to.be.eq(1);
expect(poll.result[0].address.toLowerCase()).to.be.eq(receipt.contractAddress.toLowerCase());
expect(poll.result[0].topics).to.be.deep.eq(receipt.logs[0].topics);

// A subsequent request must be empty.
poll = await customRequest(context.web3, "eth_getFilterChanges", [create_filter.result]);
poll = await customRequest(context.web3, "eth_getFilterChanges", [createFilter.result]);
expect(poll.result.length).to.be.eq(0);
});

Expand All @@ -124,64 +124,64 @@ describeWithFrontier("Frontier RPC (EthFilterApi)", (context) => {
expect(receipt.logs.length).to.be.eq(1);

// Create a filter for the created contract.
let create_filter = await customRequest(context.web3, "eth_newFilter", [
let createFilter = await customRequest(context.web3, "eth_newFilter", [
{
fromBlock: "0x0",
toBlock: "latest",
address: receipt.contractAddress,
topics: receipt.logs[0].topics,
},
]);
let poll = await customRequest(context.web3, "eth_getFilterLogs", [create_filter.result]);
let poll = await customRequest(context.web3, "eth_getFilterLogs", [createFilter.result]);

expect(poll.result.length).to.be.eq(1);
expect(poll.result[0].address.toLowerCase()).to.be.eq(receipt.contractAddress.toLowerCase());
expect(poll.result[0].topics).to.be.deep.eq(receipt.logs[0].topics);

// A subsequent request must return the same response.
poll = await customRequest(context.web3, "eth_getFilterLogs", [create_filter.result]);
poll = await customRequest(context.web3, "eth_getFilterLogs", [createFilter.result]);

expect(poll.result.length).to.be.eq(1);
expect(poll.result[0].address.toLowerCase()).to.be.eq(receipt.contractAddress.toLowerCase());
expect(poll.result[0].topics).to.be.deep.eq(receipt.logs[0].topics);
});

step("should uninstall created filters.", async function () {
let create_filter = await customRequest(context.web3, "eth_newBlockFilter", []);
let filter_id = create_filter.result;
let createFilter = await customRequest(context.web3, "eth_newBlockFilter", []);
let filterId = createFilter.result;

// Should return true when removed from the filter pool.
let uninstall = await customRequest(context.web3, "eth_uninstallFilter", [filter_id]);
let uninstall = await customRequest(context.web3, "eth_uninstallFilter", [filterId]);
expect(uninstall.result).to.be.eq(true);

// Should return error if does not exist.
let r = await customRequest(context.web3, "eth_uninstallFilter", [filter_id]);
let r = await customRequest(context.web3, "eth_uninstallFilter", [filterId]);
expect(r.error).to.include({
message: "Filter id 6 does not exist.",
});
});

step("should drain the filter pool.", async function () {
this.timeout(15000);
const block_lifespan_threshold = 100;
const blockLifespanThreshold = 100;

let create_filter = await customRequest(context.web3, "eth_newBlockFilter", []);
let filter_id = create_filter.result;
let createFilter = await customRequest(context.web3, "eth_newBlockFilter", []);
let filterId = createFilter.result;

for (let i = 0; i <= block_lifespan_threshold; i++) {
for (let i = 0; i <= blockLifespanThreshold; i++) {
await createAndFinalizeBlockNowait(context.web3);
}

let r = await customRequest(context.web3, "eth_getFilterChanges", [filter_id]);
let r = await customRequest(context.web3, "eth_getFilterChanges", [filterId]);
expect(r.error).to.include({
message: "Filter id 6 does not exist.",
});
});

step("should have a filter pool max size of 500.", async function () {
const max_filter_pool = 500;
const maxFilterPool = 500;

for (let i = 0; i < max_filter_pool; i++) {
for (let i = 0; i < maxFilterPool; i++) {
await customRequest(context.web3, "eth_newBlockFilter", []);
}

Expand Down
Loading

0 comments on commit 175e42f

Please sign in to comment.