Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add e2e tests #2655

Merged
merged 10 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 2 additions & 2 deletions ts-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"run-test": "yarn mocha -r ts-node/register/transpile-only --timeout 50000 --no-warnings"
},
"dependencies": {
"@acala-network/api": "~6.0.0",
"@acala-network/bodhi": "~2.7.8",
"@acala-network/api": "~6.0.4",
"@acala-network/bodhi": "~2.7.15",
"@babel/runtime": "^7.21.5",
"@openzeppelin/contracts": "4.9.3",
"@polkadot/api": "^10.9.1",
Expand Down
2 changes: 1 addition & 1 deletion ts-tests/tests/test-bodhi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ describeWithAcala("Acala RPC (bodhi.js)", (context) => {

expect(data.usedGas.toNumber()).to.be.eq(22038);
expect(data.usedStorage.toNumber()).to.be.eq(0);
expect(data.gasLimit.toNumber()).to.be.eq(22409);
expect(data.gasLimit.toNumber()).to.be.eq(26445);
});
});
53 changes: 53 additions & 0 deletions ts-tests/tests/test-evm-call-fill-block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { expect } from "chai";
import { step } from "mocha-steps";
import { describeWithAcala } from "./util";
import { BodhiSigner } from "@acala-network/bodhi";
import { submitExtrinsic } from "./util";
import { BigNumber } from "ethers";

describeWithAcala("Acala RPC (EVM call fill block)", (context) => {
let alice: BodhiSigner;

before("init wallets", async function () {
[alice] = context.wallets;
});

step("evm call fill block", async function () {
const input = "0xa9059cbb0000000000000000000000001000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000174876e800";

// transfer 100000000000 ACA
const transfers = Array(300).fill(context.provider.api.tx.evm.call(
"0x0000000000000000000100000000000000000000",
input,
0,
100_000,
100_000,
[]
));

const beforeHeight = (await context.provider.api.query.system.number()).toNumber();
let nonce = (await context.provider.api.query.system.account(alice.substrateAddress)).nonce.toNumber();

for (const tx of transfers) {
await tx.signAndSend(alice.substrateAddress, { nonce: nonce++ });
}

while (true) {
const currentHeight = await context.provider.api.query.system.number();

if (currentHeight.toNumber() > beforeHeight) {
break;
}

setTimeout(() => { }, 1000);
zjb0807 marked this conversation as resolved.
Show resolved Hide resolved
}

let currentBlockHash = await context.provider.api.rpc.chain.getBlockHash(beforeHeight + 1);

const events = await context.provider.api.derive.tx.events(currentBlockHash);

const evmCreateEvents = events.events.filter((item) => context.provider.api.events.evm.Executed.is(item.event));

expect(evmCreateEvents.length).to.equal(283);
});
});
56 changes: 56 additions & 0 deletions ts-tests/tests/test-evm-create-fill-block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { expect } from "chai";
import { step } from "mocha-steps";
import { describeWithAcala } from "./util";
import { BodhiSigner } from "@acala-network/bodhi";
import { submitExtrinsic } from "./util";
import { BigNumber } from "ethers";

describeWithAcala("Acala RPC (EVM create fill block)", (context) => {
let alice: BodhiSigner;

before("init wallets", async function () {
[alice] = context.wallets;
});

step("evm create fill block", async function () {
/*
pragma solidity ^0.8.0;
contract Contract {}
*/

const contract = "0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220b9cbc7f3d9528c236f2c6bdf64e25ac8ca17489f9b4e91a6d92bea793883d5d764736f6c63430008020033";

const creates = Array(300).fill(context.provider.api.tx.evm.create(
contract,
0,
2_000_000,
100_000,
[]
));

const beforeHeight = (await context.provider.api.query.system.number()).toNumber();
let nonce = (await context.provider.api.query.system.account(alice.substrateAddress)).nonce.toNumber();

for (const tx of creates) {
await tx.signAndSend(alice.substrateAddress, { nonce: nonce++ });
}

while (true) {
const currentHeight = await context.provider.api.query.system.number();

if (currentHeight.toNumber() > beforeHeight) {
break;
}

setTimeout(() => { }, 1000);
}

let currentBlockHash = await context.provider.api.rpc.chain.getBlockHash(beforeHeight + 1);

const events = await context.provider.api.derive.tx.events(currentBlockHash);

const evmCreateEvents = events.events.filter((item) => context.provider.api.events.evm.Created.is(item.event));

expect(evmCreateEvents.length).to.equal(223);
});
});
6 changes: 3 additions & 3 deletions ts-tests/tests/test-gas-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { describeWithAcala } from "./util";
import { deployContract } from "ethereum-waffle";
import { Option } from "@polkadot/types/codec";
import { u32 } from "@polkadot/types";
import { EvmAccountInfo, CodeInfo } from "@acala-network/types/interfaces";
import { CodeInfo } from "@acala-network/types/interfaces";
import { BodhiSigner } from "@acala-network/bodhi";

describeWithAcala("Acala RPC (GasLimit)", (context) => {
Expand All @@ -19,7 +19,7 @@ describeWithAcala("Acala RPC (GasLimit)", (context) => {
const contract = await deployContract(alice, Factory);
// limited by used_storage
const result = await contract.createContractLoop(350);
expect(result.gasLimit.toNumber()).to.be.eq(28954750);
expect(result.gasLimit.toNumber()).to.be.eq(3570298622);

const result2 = await contract.incrementLoop(8480);
expect(result2.gasLimit.toNumber()).to.be.eq(29788849);
Expand All @@ -30,7 +30,7 @@ describeWithAcala("Acala RPC (GasLimit)", (context) => {
// 1 increment value
expect(storages.length).to.be.eq(352);

const info = await context.provider.api.query.evm.accounts(contract.address) as Option<EvmAccountInfo>;
const info = await context.provider.api.query.evm.accounts(contract.address);
const codeInfo = await context.provider.api.query.evm.codeInfos(info.unwrap().contractInfo.unwrap().codeHash) as Option<CodeInfo>;
const extra_bytes = Number(context.provider.api.consts.evm.newContractExtraBytes.toHex());

Expand Down
6 changes: 3 additions & 3 deletions ts-tests/tests/test-gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describeWithAcala("Acala RPC (Gas)", (context) => {
from: alice.getAddress(),
data: "0x" + Block.bytecode,
});
expect(gas.toNumber()).to.be.eq(11301014);
expect(gas.toNumber()).to.be.eq(11301114);
zjb0807 marked this conversation as resolved.
Show resolved Hide resolved
});

it("eth_estimateResources for contract creation", async function () {
Expand All @@ -28,7 +28,7 @@ describeWithAcala("Acala RPC (Gas)", (context) => {
});

expect(data.usedGas.toNumber()).to.be.eq(251726);
expect(data.gasLimit.toNumber()).to.be.eq(273373);
expect(data.gasLimit.toNumber()).to.be.eq(302071);
expect(data.usedStorage.toNumber()).to.be.eq(10921);
});

Expand All @@ -46,7 +46,7 @@ describeWithAcala("Acala RPC (Gas)", (context) => {
);

expect(data.usedGas.toNumber()).to.be.eq(22038);
expect(data.gasLimit.toNumber()).to.be.eq(22409);
expect(data.gasLimit.toNumber()).to.be.eq(26445);
expect(data.usedStorage.toNumber()).to.be.eq(0);
});
});
152 changes: 152 additions & 0 deletions ts-tests/tests/test-mempool-priority.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { expect } from "chai";
import { step } from "mocha-steps";
import { describeWithAcala } from "./util";
import { BodhiSigner } from "@acala-network/bodhi";
import { submitExtrinsic } from "./util";
import { BigNumber } from "ethers";

describeWithAcala("Acala RPC (Mempool Priority Order)", (context) => {
let alice: BodhiSigner;
let alice_stash: BodhiSigner;

const FixedU128 = BigNumber.from('1000000000000000000');

before("init wallets", async function () {
[alice, alice_stash] = context.wallets;
});

step("transaction pool priority order is correct", async function () {
const interestRatePerSec = BigNumber.from('10').mul(FixedU128).div(BigNumber.from('100000')).toBigInt();
const liquidationRatio = BigNumber.from('3').mul(FixedU128).div(BigNumber.from('2')).toBigInt();
const liquidationPenalty = BigNumber.from('2').mul(FixedU128).div(BigNumber.from('10')).toBigInt();
const requiredCollateralRatio = BigNumber.from('9').mul(FixedU128).div(BigNumber.from('5')).toBigInt();
const maximumTotalDebitValue = BigNumber.from("10000000000000000").toBigInt();

// setup an unsafe cdp
const tx1 = context.provider.api.tx.utility.batchAll([
context.provider.api.tx.sudo.sudo(
context.provider.api.tx.acalaOracle.feedValues(
[
[{ Token: 'ACA' }, BigNumber.from('1').mul(FixedU128).toString()] // 1 USD
]
)
),
context.provider.api.tx.sudo.sudo(context.provider.api.tx.cdpEngine.setCollateralParams(
{ Token: 'ACA' },
{ NewValue: interestRatePerSec },
{ NewValue: liquidationRatio },
{ NewValue: liquidationPenalty },
{ NewValue: requiredCollateralRatio },
{ NewValue: maximumTotalDebitValue }
)),
context.provider.api.tx.honzon.adjustLoan(
{ Token: 'ACA' }, // currency_id
100000000000000, // collateral_adjustment
500000000000000 // debit_adjustment
)
]);
await submitExtrinsic(tx1, alice.substrateAddress);

// change the ACA price
const tx2 = context.provider.api.tx.sudo.sudo(
context.provider.api.tx.acalaOracle.feedValues(
[
[{ Token: 'ACA' }, FixedU128.div(BigNumber.from('10')).toBigInt()] // 0.1 USD
]
)
);
await submitExtrinsic(tx2, alice.substrateAddress);

const nonce = (await context.provider.api.rpc.system.accountNextIndex(alice.substrateAddress)).toNumber() + 1;
const parentHash = await context.provider.api.rpc.chain.getBlockHash();

// send operational extrinsic
const tx3 = context.provider.api.tx.sudo.sudo(
context.provider.api.tx.emergencyShutdown.emergencyShutdown()
);
await tx3.signAndSend(alice.substrateAddress, { nonce });

const operationalTransactionvalidity = await context.provider.api.call.taggedTransactionQueue.validateTransaction(
"Local",
tx3.toHex(),
parentHash
);

const expected = {
"Ok": {
"longevity": "31",
"priority": "65,695,101,118,020,000",
"propagate": true,
"provides": [
"0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d04000000"
],
"requires": [
"0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d03000000"
]
}
};
const priorityRegex = /^65,695,1\d\d,\d{3},\d{3},\d{3}$/;

expect(operationalTransactionvalidity.toHuman()).to.satisfy((x: any) => {
return x === expected || (
expect(x.Ok.priority).match(priorityRegex) &&
x.Ok.longevity === expected.Ok.longevity &&
x.Ok.propagate === expected.Ok.propagate &&
x.Ok.provides[0] === expected.Ok.provides[0] &&
x.Ok.requires[0] === expected.Ok.requires[0]
);
});

// send normal extrinsic
const tx4 = context.provider.api.tx.balances.transferAllowDeath(
alice_stash.substrateAddress,
80_000
);
await tx4.signAndSend(alice.substrateAddress, { nonce });
const normalTransactionvalidity = await context.provider.api.call.taggedTransactionQueue.validateTransaction(
"Local",
tx4.toHex(),
parentHash
);
expect(normalTransactionvalidity.toHuman()).to.deep.eq({
"Ok": {
"longevity": "31",
"priority": "0",
"propagate": true,
"provides": [
"0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d04000000"
],
"requires": [
"0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d03000000"
]
}
});

// send unsigned extrinsic
const tx5 = context.provider.api.tx.cdpEngine.liquidate(
{ Token: 'ACA' }, // currency_id
alice.substrateAddress, // target
);
const unsignedTransactionvalidity = await context.provider.api.call.taggedTransactionQueue.validateTransaction(
"Local",
tx5.toHex(),
parentHash
);
expect(unsignedTransactionvalidity.toHuman()).to.deep.eq({
"Ok": {
"longevity": "64",
"priority": "14,999,999,999,000",
"propagate": true,
"provides": [
"0x5c434450456e67696e654f6666636861696e576f726b657207000000000000d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"
],
"requires": []
}
});

// Ensure tx priority order:
// Inherent -> Operational tx -> Unsigned tx -> Signed normal tx
expect(operationalTransactionvalidity.asOk.priority > unsignedTransactionvalidity.asOk.priority).to.be.true;
expect(unsignedTransactionvalidity.asOk.priority > normalTransactionvalidity.asOk.priority).to.be.true;
});
});
4 changes: 2 additions & 2 deletions ts-tests/tests/test-precompiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ describeWithAcala("Acala RPC (Precompile)", (context) => {
from: await alice.getAddress(),
confirmations: 0,
nonce: 1,
// gasLimit: BigNumber.from("28535"),
// gasLimit: BigNumber.from("100200"),
// gasPrice: BigNumber.from("1"),
//data: "",
// value: BigNumber.from(0),
chainId: 595,
});
expect(res.gasLimit.toNumber()).to.eq(28535)
expect(res.gasLimit.toNumber()).to.eq(100200)
expect(res.gasPrice.toNumber()).to.eq(1)
expect(res.value.toNumber()).to.eq(0)

Expand Down
Loading
Loading