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

Commit

Permalink
fix event emission
Browse files Browse the repository at this point in the history
  • Loading branch information
JGcarv committed Feb 4, 2021
1 parent 14edea1 commit ec86a90
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 151 deletions.
14 changes: 4 additions & 10 deletions contracts/libraries/TellorLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import "./TellorDispute.sol";
import "./TellorStake.sol";
import "./TellorGettersLibrary.sol";

import "hardhat/console.sol";

/**
* @title Tellor Oracle System Library
* @dev Contains the functions' logic for the Tellor contract where miners can submit the proof of work
Expand Down Expand Up @@ -130,6 +128,7 @@ library TellorLibrary {
uint256 _timeOfLastNewValue = block.timestamp;
self.uintVars[timeOfLastNewValue] = _timeOfLastNewValue;
uint256[5] memory a;
uint256[5] memory b;
for (uint256 k = 0; k < 5; k++) {
for (uint256 i = 1; i < 5; i++) {
uint256 temp = _tblock.valuesByTimestamp[k][i];
Expand All @@ -154,6 +153,7 @@ library TellorLibrary {
//Save the official(finalValue), timestamp of it, 5 miners and their submitted values for it, and its block number
a = _tblock.valuesByTimestamp[k];
_request.finalValues[_timeOfLastNewValue] = a[2];
b[k] = a[2];
_request.minersByValue[_timeOfLastNewValue] = _tblock.minersByValue[
k
];
Expand All @@ -168,7 +168,7 @@ library TellorLibrary {
emit NewValue(
_requestId,
_timeOfLastNewValue,
a,
b,
self.uintVars[currentTotalTips],
_currChallenge
);
Expand Down Expand Up @@ -335,11 +335,6 @@ library TellorLibrary {
TellorStorage.TellorStorageStruct storage self,
string memory _nonce
) internal view {
// console.log(now - (now % 1 minutes));
// console.log(self.uintVars[timeOfLastNewValue]);
console.log(
(now - (now % 1 minutes)) - self.uintVars[timeOfLastNewValue]
);
require(
uint256(
sha256(
Expand All @@ -360,8 +355,7 @@ library TellorLibrary {
) %
self.uintVars[difficulty] ==
0 ||
(now - (now % 1 minutes)) - self.uintVars[timeOfLastNewValue] >=
15 minutes,
now - self.uintVars[timeOfLastNewValue] >= 15 minutes,
"Incorrect nonce for current challenge"
);
}
Expand Down
273 changes: 132 additions & 141 deletions test/difficulty.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,41 @@ contract("Difficulty tests", function(accounts) {
};
});

// it("Test Difficulty Adjustment", async function() {
// await helper.advanceTime(60 * 60 * 16);
// await master.manuallySetDifficulty(100000000000);
// await TestLib.mineBlock(env);

// let diff1 = await master.getNewCurrentVariables();
// assert(diff1[2] > 1, "difficulty greater than 1"); //difficulty not changing.....
// let vars = await master.getNewCurrentVariables();
// await helper.advanceTime(60 * 60 * 16);
// await TestLib.mineBlock(env);
// vars = await master.getNewCurrentVariables();
// assert(vars[2] < diff1[2], "difficulty should continue to move down");
// });

// it("Test time travel in data -- really long time since last Poof and proper difficulty adjustment", async function() {
// await helper.advanceTime(60 * 60 * 16);
// await TestLib.mineBlock(env);
// await master.manuallySetDifficulty(1000);
// vars = await master.getNewCurrentVariables();
// var oldDiff = vars[2];
// assert(vars[2] > 1, "difficulty should be greater than 1"); //difficulty not changing.....
// await helper.advanceTime(86400 * 20);
// await TestLib.mineBlock(env);
// vars = await master.getNewCurrentVariables();
// var newDiff = vars[2];
// assert(newDiff * 1 + 0 < oldDiff * 1 + 0, "difficulty should be lower");
// });

// it("Test lower difficulty target (4 min)", async function() {
// assert(
// (await master.getUintVar(web3.utils.keccak256("timeTarget"))) ==
// timeTarget
// ),
// "difficulty should be 240";
// });
it("Test Difficulty Adjustment", async function() {
await helper.advanceTime(60 * 60 * 16);
await master.manuallySetDifficulty(100000000000);
await TestLib.mineBlock(env);

let diff1 = await master.getNewCurrentVariables();
assert(diff1[2] > 1, "difficulty greater than 1"); //difficulty not changing.....
let vars = await master.getNewCurrentVariables();
await helper.advanceTime(60 * 60 * 16);
await TestLib.mineBlock(env);
vars = await master.getNewCurrentVariables();
assert(vars[2] < diff1[2], "difficulty should continue to move down");
});

it("Test time travel in data -- really long time since last Poof and proper difficulty adjustment", async function() {
await helper.advanceTime(60 * 60 * 16);
await TestLib.mineBlock(env);
await master.manuallySetDifficulty(1000);
vars = await master.getNewCurrentVariables();
var oldDiff = vars[2];
assert(vars[2] > 1, "difficulty should be greater than 1"); //difficulty not changing.....
await helper.advanceTime(86400 * 20);
await TestLib.mineBlock(env);
vars = await master.getNewCurrentVariables();
var newDiff = vars[2];
assert(newDiff * 1 + 0 < oldDiff * 1 + 0, "difficulty should be lower");
});

it("Test lower difficulty target (4 min)", async function() {
assert(
(await master.getUintVar(web3.utils.keccak256("timeTarget"))) ==
timeTarget
),
"difficulty should be 240";
});

describe("Difficulty on 4th slot", async () => {
beforeEach(async () => {
Expand All @@ -80,73 +80,46 @@ contract("Difficulty tests", function(accounts) {
}
});

// it("Difficulty decrease based on the 4th slot", async () => {
// await helper.takeFifteen();
// await TestLib.mineBlock(env);
// await master.manuallySetDifficulty(1000);
// let currentDiff = await getDiff();

// await helper.takeFifteen();

// // Mine 4 slots:
// let vars = await env.master.getNewCurrentVariables();
// const values = [1000, 1000, 1000, 1000, 1000];
// for (var i = 0; i < 4; i++) {
// res = await master.testSubmitMiningSolution(
// "nonce",
// vars["1"],
// values,
// {
// from: accounts[i + 25],
// }
// );
// }

// let afterDiff = await getDiff();
// assert(currentDiff > afterDiff, "Difficulty should have decreased");
// });

// it("Difficulty increase based on the 4th slot", async () => {
// await helper.takeFifteen();
// await TestLib.mineBlock(env);
// await master.manuallySetDifficulty(1000);
// let currentDiff = await getDiff();

// //move only 1 minute
// await helper.advanceTime(60);

// // Mine 4 slots:
// let vars = await env.master.getNewCurrentVariables();
// const values = [1000, 1000, 1000, 1000, 1000];
// for (var i = 0; i < 4; i++) {
// res = await master.testSubmitMiningSolution(
// "nonce",
// vars["1"],
// values,
// {
// from: accounts[i + 25],
// }
// );
// }

// let afterDiff = await getDiff();
// assert(currentDiff < afterDiff, "Difficulty should have increase");
// });
it("Difficulty decrease based on the 4th slot", async () => {
await helper.takeFifteen();
await TestLib.mineBlock(env);
await master.manuallySetDifficulty(1000);
let currentDiff = await getDiff();

it("Test nonce bypass", async () => {
await helper.takeFifteen();

// Mine 4 slots:
let vars = await env.master.getNewCurrentVariables();
const values = [1000, 1000, 1000, 1000, 1000];
for (var i = 0; i < 4; i++) {
res = await master.testSubmitMiningSolution(
"nonce",
vars["1"],
values,
{
from: accounts[i + 25],
}
);
}

let afterDiff = await getDiff();
assert(currentDiff > afterDiff, "Difficulty should have decreased");
});

it("Difficulty increase based on the 4th slot", async () => {
await helper.takeFifteen();
await TestLib.mineBlock(env);
await master.manuallySetDifficulty(1000);
let currentDiff = await getDiff();

//move only 1 minute
await helper.advanceTime(46)
await helper.advanceTime(60);

// Mine 4 slots:
let vars = await env.master.getNewCurrentVariables();
const values = [1000, 1000, 1000, 1000, 1000];
for (var i = 0; i < 5; i++) {
res = await master.submitMiningSolution(
for (var i = 0; i < 4; i++) {
res = await master.testSubmitMiningSolution(
"nonce",
vars["1"],
values,
Expand All @@ -155,57 +128,75 @@ contract("Difficulty tests", function(accounts) {
}
);
}

let afterDiff = await getDiff();
assert(currentDiff < afterDiff, "Difficulty should have increase");
});

// it("Zero difficulty on the 5th slot", async () => {
// // Increasing the diff
// await TestLib.mineBlock({
// master: master,
// accounts: accounts.slice(30, 35),
// });
// await master.manuallySetDifficulty(1000);

// await helper.advanceTime(61);

// let vars = await env.master.getNewCurrentVariables();
// const values = [1000, 1000, 1000, 1000, 1000];

// //Try mine first slot with incorrect nonce
// await helper.expectThrow(
// master.submitMiningSolution("nonce", vars["1"], values, {
// from: accounts[20],
// })
// );

// // Mine 4 slots bypassing the nonce:
// for (var i = 0; i < 4; i++) {
// res = await master.testSubmitMiningSolution(
// "nonceer",
// vars["1"],
// values,
// {
// from: accounts[i + 25],
// }
// );
// }

// //Mine the 5th with any nonce
// await master.submitMiningSolution("nonce", vars["1"], values, {
// from: accounts[20],
// });

// let requestId = vars["1"][0];
// let count = await master.getNewValueCountbyRequestId(requestId);
// let timestamp = await master.getTimestampbyRequestIDandIndex(
// requestId,
// count.toNumber() - 1
// );

// let miners = await master.getMinersByRequestIdAndTimestamp(
// requestId,
// timestamp
// );
// assert(miners.indexOf(accounts[20]) != -1, "miner should have mined");
// });
it("Test nonce bypass", async () => {
await helper.takeFifteen();
await TestLib.mineBlock(env);
await master.manuallySetDifficulty(1000);
let currentDiff = await getDiff();

let vars = await env.master.getNewCurrentVariables();
const values = [1000, 1000, 1000, 1000, 1000];
await helper.expectThrow(
master.submitMiningSolution("nonce", vars["1"], values, {
from: accounts[26],
})
);
});

it("Zero difficulty on the 5th slot", async () => {
// Increasing the diff
await TestLib.mineBlock({
master: master,
accounts: accounts.slice(30, 35),
});
await master.manuallySetDifficulty(1000);

await helper.advanceTime(61);

let vars = await env.master.getNewCurrentVariables();
const values = [1000, 1000, 1000, 1000, 1000];

//Try mine first slot with incorrect nonce
await helper.expectThrow(
master.submitMiningSolution("nonce", vars["1"], values, {
from: accounts[20],
})
);

// Mine 4 slots bypassing the nonce:
for (var i = 0; i < 4; i++) {
res = await master.testSubmitMiningSolution(
"nonceer",
vars["1"],
values,
{
from: accounts[i + 25],
}
);
}

//Mine the 5th with any nonce
await master.submitMiningSolution("nonce", vars["1"], values, {
from: accounts[20],
});

let requestId = vars["1"][0];
let count = await master.getNewValueCountbyRequestId(requestId);
let timestamp = await master.getTimestampbyRequestIDandIndex(
requestId,
count.toNumber() - 1
);

let miners = await master.getMinersByRequestIdAndTimestamp(
requestId,
timestamp
);
assert(miners.indexOf(accounts[20]) != -1, "miner should have mined");
});
});
});

0 comments on commit ec86a90

Please sign in to comment.