Skip to content
Closed
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
97 changes: 97 additions & 0 deletions tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"mocha": "^8.3.2",
"mocha-steps": "^1.3.0",
"rimraf": "^3.0.2",
"solc": "^0.8.3",
"ts-node": "9.1",
"typescript": "^3.9.6",
"web3": "^1.3.5"
Expand Down
49 changes: 49 additions & 0 deletions tests/tests/constants/testContracts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
import { AbiItem } from "web3-utils";
import solc from "solc";

export function compileSolidity(contractContent: string, contractName: string = "Test") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is awesome! We should do all of our solidity-based tests this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So at first I thought to myself that the repo would be cleaner if we did this for every single contract but then I thought that recompiling every contract every test might slightly increase the duration of testing. Thoughts @crystalin @JoshOrndorff ?

let result = JSON.parse(
solc.compile(
JSON.stringify({
language: "Solidity",
sources: {
"main.sol": {
content: contractContent,
},
},
settings: {
outputSelection: {
"*": {
"*": ["*"],
},
},
},
})
)
);

const contract = result.contracts["main.sol"][contractName];
return {
bytecode: "0x" + contract.evm.bytecode.object,
contract,
};
}

// Solidity: contract test {function multiply(uint a) public pure returns(uint d) {return a * 7;}}
export const TEST_CONTRACT_BYTECODE =
Expand Down Expand Up @@ -201,6 +230,26 @@ export const FINITE_LOOP_CONTRACT_ABI = [
},
] as AbiItem[];

// Gas limit test contract

export const contractSourceBlockGasLimit = `
pragma solidity >=0.8.0;

// Docs I'm following to get these properties
// https://docs.soliditylang.org/en/v0.8.2/units-and-global-variables.html

contract CheckBlockGasLimit {
// This one is broken (evaluates to 0)
uint public gas;
// This one works
uint public chainid;

constructor() {
gas = block.gaslimit;
chainid = block.chainid;
}
}`;

export const ERC20_BYTECODE =
"0x608060405234801561001057600080fd5b50610024336202616061002960201b60201c565b610274565b600073ff" +
"ffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156100" +
Expand Down
31 changes: 28 additions & 3 deletions tests/tests/test-block.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { expect } from "chai";
import { step } from "mocha-steps";
import { contractCreation, GENESIS_ACCOUNT } from "./constants";

import { createAndFinalizeBlock, describeWithMoonbeam, fillBlockWithTx } from "./util";
import {
compileSolidity,
contractCreation,
contractSourceBlockGasLimit,
GENESIS_ACCOUNT,
GENESIS_ACCOUNT_PRIVATE_KEY,
} from "./constants";

import {
createAndFinalizeBlock,
customRequest,
deployContractManualSeal,
describeWithMoonbeam,
fillBlockWithTx,
} from "./util";

describeWithMoonbeam("Moonbeam RPC (Block)", `simple-specs.json`, (context) => {
let previousBlock;
Expand Down Expand Up @@ -198,4 +210,17 @@ describeWithMoonbeam("Moonbeam RPC (Block)", `simple-specs.json`, (context) => {
let { txPassed } = await fillBlockWithTx(context, 8193, contractCreation);
expect(txPassed).to.eq(0);
});

it("should be able to access block gas lmit within a contract", async function () {
this.timeout(15000);
const contractCompiled = compileSolidity(contractSourceBlockGasLimit, "CheckBlockGasLimit");
const contract = await deployContractManualSeal(
context.polkadotApi,
context.web3,
contractCompiled.bytecode,
contractCompiled.contract.abi
);
console.log("gas", await contract.methods.gas().call());
expect((await contract.methods.gas().call()) !== "0").to.eq(true);
});
});