Add more unit tests for DioneOracle, add enabling/disabling logging for contracts deployment

This commit is contained in:
ChronosX88 2021-04-27 18:52:28 +03:00
parent 6e87bbe644
commit 857c21a232
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
5 changed files with 58 additions and 10 deletions

View File

@ -17,9 +17,12 @@ interface DeploymentOptions {
maxStake: number; // for randomizer
actualStake: number; // of each node
nodeCount: number;
logging: boolean;
}
async function deploy(opts: DeploymentOptions): Promise<Environment> {
const logger = new LogWrapper(opts.logging);
const accounts = (await ethers.getSigners()).slice(0, opts.nodeCount);
const DioneToken = await ethers.getContractFactory("DioneToken");
@ -30,23 +33,23 @@ async function deploy(opts: DeploymentOptions): Promise<Environment> {
const dioneToken = await DioneToken.deploy();
await dioneToken.deployed();
console.log("DioneToken deployed to:", dioneToken.address);
logger.log("DioneToken deployed to:", dioneToken.address);
const dioneStaking = await DioneStaking.deploy(dioneToken.address, ethers.constants.WeiPerEther.mul(opts.reward), 0, ethers.constants.WeiPerEther.mul(opts.minStake));
await dioneStaking.deployed();
console.log("staking_contract_address = \"" + dioneStaking.address+ "\"");
logger.log("staking_contract_address = \"" + dioneStaking.address+ "\"");
const dioneDispute = await DioneDispute.deploy(dioneStaking.address, opts.voteWindowTime);
await dioneDispute.deployed();
console.log("dispute_contract_address = \"" + dioneDispute.address+ "\"");
logger.log("dispute_contract_address = \"" + dioneDispute.address+ "\"");
const dioneOracle = await DioneOracle.deploy(dioneStaking.address);
await dioneOracle.deployed();
console.log("oracle_contract_address = \"" + dioneOracle.address+ "\"");
logger.log("oracle_contract_address = \"" + dioneOracle.address+ "\"");
const mediator = await Mediator.deploy(dioneOracle.address);
await mediator.deployed();
console.log("mediator_contract_address = \"" + mediator.address +"\"")
logger.log("mediator_contract_address = \"" + mediator.address +"\"")
const env: Environment = {
dioneToken: dioneToken,
@ -96,7 +99,7 @@ async function deploy(opts: DeploymentOptions): Promise<Environment> {
await token.approve(dioneStaking.address, ethers.constants.WeiPerEther.mul(stakeValue));
await staking.stake(ethers.constants.WeiPerEther.mul(stakeValue));
const stake = await dioneStaking.minerStake(accounts[i].address);
console.log(accounts[i].address, stake.toString());
logger.log(accounts[i].address, stake.toString());
}
return env;
@ -107,4 +110,16 @@ export default deploy;
// min and max included
function randomInt(min: number, max: number){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
class LogWrapper {
private enabled: boolean;
constructor(_enabled: boolean) {
this.enabled = _enabled;
}
public log(message?: any, ...optionalParams: any[]): void {
if(this.enabled) console.log(message, optionalParams);
}
}

View File

@ -50,7 +50,7 @@ contract DioneOracle {
);
modifier onlyPendingRequest(uint256 _reqID) {
require(pendingRequests[_reqID].requestSender != address(0), "This request is not pending");
require(pendingRequests[_reqID].requestSender != address(0), "this request is not pending");
_;
}
@ -86,6 +86,7 @@ contract DioneOracle {
}
function submitOracleRequest(uint256 _reqID, bytes memory _data) public onlyPendingRequest(_reqID) returns (bool) {
require((pendingRequests[_reqID].deadline - block.timestamp) <= MAXIMUM_DELAY, "submission has exceeded the deadline");
delete pendingRequests[_reqID];
dioneStaking.mine(msg.sender);
(bool success, ) = pendingRequests[_reqID].callbackAddress.call(abi.encodeWithSelector(pendingRequests[_reqID].callbackMethodID, _reqID, _data));

View File

@ -12,7 +12,8 @@ async function main() {
randomizeStake: false,
maxStake: 0, // don't use this deployment feature
actualStake: 5000,
nodeCount: 4
nodeCount: 4,
logging: true
});
}

View File

@ -16,7 +16,8 @@ describe("DioneDispute", function () {
randomizeStake: false,
maxStake: 0, // don't use this deployment feature
actualStake: 9000,
nodeCount: 4
nodeCount: 4,
logging: false
});
dioneDispute = contracts.dioneDispute;

View File

@ -15,7 +15,8 @@ describe("DioneOracle", function () {
randomizeStake: false,
maxStake: 0, // don't use this deployment feature
actualStake: 9000,
nodeCount: 4
nodeCount: 4,
logging: false
});
dioneOracle = contracts.dioneOracle;
});
@ -31,5 +32,34 @@ describe("DioneOracle", function () {
await expect(dioneOracle.cancelOracleRequest(1))
.to.emit(dioneOracle, 'CancelOracleRequest')
.withArgs(1);
// let's check whether submission fails
await expect(dioneOracle.submitOracleRequest(1, BigNumber.from(0x8da5cb5b)))
.to.be.revertedWith("this request is not pending");
});
it("should create request and submit it", async function() {
await dioneOracle.requestOracles(1, "getTransaction", "bafy2bzaceaaab3kkoaocal2dzh3okzy4gscqpdt42hzrov3df6vjumalngc3g", dioneOracle.address, BigNumber.from(0x8da5cb5b));
await expect(dioneOracle.submitOracleRequest(1, BigNumber.from(0x8da5cb5b)))
.to.emit(dioneOracle, "SubmittedOracleRequest")
.withArgs(1, BigNumber.from(0x8da5cb5b));
});
it("should fail submission after request deadline", async function () {
await dioneOracle.requestOracles(1, "getTransaction", "bafy2bzaceaaab3kkoaocal2dzh3okzy4gscqpdt42hzrov3df6vjumalngc3g", dioneOracle.address, BigNumber.from(0x8da5cb5b));
await ethers.provider.send("evm_increaseTime", [301]);
await expect(dioneOracle.submitOracleRequest(1, BigNumber.from(0x8da5cb5b)))
.to.be.reverted;
});
it("should fail submission on invalid request id", async function () {
await expect(dioneOracle.submitOracleRequest(333, BigNumber.from(0x8da5cb5b)))
.to.be.revertedWith("this request is not pending");
});
it("should fail cancel of request with invalid request id", async function () {
await expect(dioneOracle.cancelOracleRequest(333))
.to.be.revertedWith("this request is not pending");
});
});