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

Test refactor: enable-constantinople-hardfork.js #359

Merged
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
107 changes: 42 additions & 65 deletions test/enable_constantinople_hardfork.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,64 @@
const assert = require("assert");
const Web3 = require("web3");
const Ganache = require(process.env.TEST_BUILD
? "../build/ganache.core." + process.env.TEST_BUILD + ".js"
: "../index.js");
const fs = require("fs");
const path = require("path");
const solc = require("solc");

const mnemonic = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat";
const bootstrap = require("./helpers/contract/bootstrap");

describe("Constantinople Hardfork", function() {
let contract;

before("compile contract", function() {
this.timeout(200000);
let result = solc.compile(
{
sources: {
"ConstantinopleContract.sol": fs.readFileSync(path.join(__dirname, "ConstantinopleContract.sol"), "utf8")
}
},
1
);
contract = {
bytecode: "0x" + result.contracts["ConstantinopleContract.sol:ConstantinopleContract"].bytecode,
abi: JSON.parse(result.contracts["ConstantinopleContract.sol:ConstantinopleContract"].interface)
};
});
const mnemonic = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat";

describe("Disallow Constantinople features", function() {
const provider = Ganache.provider({
mnemonic,
gasLimit: 20000000,
hardfork: "byzantium"
});
const web3 = new Web3(provider);
let accounts;
let context;

before("get accounts", async function() {
accounts = await web3.eth.getAccounts();
});
before("Setting up web3 and contract", async function() {
this.timeout(10000);

it("should fail execution", async function() {
const DummyContract = new web3.eth.Contract(contract.abi);
const contractRef = {
contractFiles: ["ConstantinopleContract"],
contractSubdirectory: "constantinople"
};

let promiEvent = DummyContract.deploy({
data: contract.bytecode
}).send({ from: accounts[0], gas: 20000000 });
const ganacheProviderOptions = {
mnemonic,
gasLimit: 20000000,
hardfork: "byzantium"
};

let dummyContractInstance = await promiEvent;
context = await bootstrap(contractRef, ganacheProviderOptions);
});

try {
await dummyContractInstance.methods.test(2).call();
assert.fail("Call did not fail execution like it was supposed to");
} catch (err) {
assert.strictEqual(err.message, "VM Exception while processing transaction: invalid opcode");
}
it("should fail execution", async function() {
const { instance } = context;

await assert.rejects(
() => instance.methods.test(2).call(),
/VM Exception while processing transaction: invalid opcode/,
"Call did not fail execution like it was supposed to"
);
});
});

describe("Allow Constantinople features", function() {
var provider = Ganache.provider({
mnemonic,
hardfork: "constantinople",
gasLimit: 20000000
});
var web3 = new Web3(provider);
var accounts;
let context;

before("get accounts", async function() {
accounts = await web3.eth.getAccounts();
});
before("Setting up web3 and contract", async function() {
this.timeout(10000);

it("should succeed execution", async function() {
const DummyContract = new web3.eth.Contract(contract.abi);
const contractRef = {
contractFiles: ["ConstantinopleContract"],
contractSubdirectory: "constantinople"
};

let promiEvent = DummyContract.deploy({
data: contract.bytecode
}).send({ from: accounts[0], gas: 20000000 });
const ganacheProviderOptions = {
gasLimit: 20000000,
hardfork: "constantinople",
mnemonic
};

let dummyContractInstance = await promiEvent;
context = await bootstrap(contractRef, ganacheProviderOptions);
});

it("should succeed execution", async function() {
const { instance } = context;

let result = await dummyContractInstance.methods.test(2).call();
const result = await instance.methods.test(2).call();
assert(result, "successful execution");
});
});
Expand Down