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 maxStake: number; // for randomizer
actualStake: number; // of each node actualStake: number; // of each node
nodeCount: number; nodeCount: number;
logging: boolean;
} }
async function deploy(opts: DeploymentOptions): Promise<Environment> { async function deploy(opts: DeploymentOptions): Promise<Environment> {
const logger = new LogWrapper(opts.logging);
const accounts = (await ethers.getSigners()).slice(0, opts.nodeCount); const accounts = (await ethers.getSigners()).slice(0, opts.nodeCount);
const DioneToken = await ethers.getContractFactory("DioneToken"); const DioneToken = await ethers.getContractFactory("DioneToken");
@ -30,23 +33,23 @@ async function deploy(opts: DeploymentOptions): Promise<Environment> {
const dioneToken = await DioneToken.deploy(); const dioneToken = await DioneToken.deploy();
await dioneToken.deployed(); 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)); const dioneStaking = await DioneStaking.deploy(dioneToken.address, ethers.constants.WeiPerEther.mul(opts.reward), 0, ethers.constants.WeiPerEther.mul(opts.minStake));
await dioneStaking.deployed(); 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); const dioneDispute = await DioneDispute.deploy(dioneStaking.address, opts.voteWindowTime);
await dioneDispute.deployed(); await dioneDispute.deployed();
console.log("dispute_contract_address = \"" + dioneDispute.address+ "\""); logger.log("dispute_contract_address = \"" + dioneDispute.address+ "\"");
const dioneOracle = await DioneOracle.deploy(dioneStaking.address); const dioneOracle = await DioneOracle.deploy(dioneStaking.address);
await dioneOracle.deployed(); await dioneOracle.deployed();
console.log("oracle_contract_address = \"" + dioneOracle.address+ "\""); logger.log("oracle_contract_address = \"" + dioneOracle.address+ "\"");
const mediator = await Mediator.deploy(dioneOracle.address); const mediator = await Mediator.deploy(dioneOracle.address);
await mediator.deployed(); await mediator.deployed();
console.log("mediator_contract_address = \"" + mediator.address +"\"") logger.log("mediator_contract_address = \"" + mediator.address +"\"")
const env: Environment = { const env: Environment = {
dioneToken: dioneToken, dioneToken: dioneToken,
@ -96,7 +99,7 @@ async function deploy(opts: DeploymentOptions): Promise<Environment> {
await token.approve(dioneStaking.address, ethers.constants.WeiPerEther.mul(stakeValue)); await token.approve(dioneStaking.address, ethers.constants.WeiPerEther.mul(stakeValue));
await staking.stake(ethers.constants.WeiPerEther.mul(stakeValue)); await staking.stake(ethers.constants.WeiPerEther.mul(stakeValue));
const stake = await dioneStaking.minerStake(accounts[i].address); const stake = await dioneStaking.minerStake(accounts[i].address);
console.log(accounts[i].address, stake.toString()); logger.log(accounts[i].address, stake.toString());
} }
return env; return env;
@ -108,3 +111,15 @@ export default deploy;
function randomInt(min: number, max: number){ function randomInt(min: number, max: number){
return Math.floor(Math.random() * (max - min + 1)) + min; 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) { 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) { 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]; delete pendingRequests[_reqID];
dioneStaking.mine(msg.sender); dioneStaking.mine(msg.sender);
(bool success, ) = pendingRequests[_reqID].callbackAddress.call(abi.encodeWithSelector(pendingRequests[_reqID].callbackMethodID, _reqID, _data)); (bool success, ) = pendingRequests[_reqID].callbackAddress.call(abi.encodeWithSelector(pendingRequests[_reqID].callbackMethodID, _reqID, _data));

View File

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

View File

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

View File

@ -15,7 +15,8 @@ describe("DioneOracle", function () {
randomizeStake: false, randomizeStake: false,
maxStake: 0, // don't use this deployment feature maxStake: 0, // don't use this deployment feature
actualStake: 9000, actualStake: 9000,
nodeCount: 4 nodeCount: 4,
logging: false
}); });
dioneOracle = contracts.dioneOracle; dioneOracle = contracts.dioneOracle;
}); });
@ -31,5 +32,34 @@ describe("DioneOracle", function () {
await expect(dioneOracle.cancelOracleRequest(1)) await expect(dioneOracle.cancelOracleRequest(1))
.to.emit(dioneOracle, 'CancelOracleRequest') .to.emit(dioneOracle, 'CancelOracleRequest')
.withArgs(1); .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");
}); });
}); });