From f92c6ff5f9e4b6bdb9758df0d974eedf9f33e2bf Mon Sep 17 00:00:00 2001 From: themandalore Date: Wed, 16 Oct 2024 12:51:36 -0400 Subject: [PATCH] all tests passing --- contracts/SampleFallbackOracleUser.sol | 63 ++--- test/FunctionTests.js | 320 ++++++++++++++++++++----- 2 files changed, 293 insertions(+), 90 deletions(-) diff --git a/contracts/SampleFallbackOracleUser.sol b/contracts/SampleFallbackOracleUser.sol index 1dc25e4..7e1d8ea 100644 --- a/contracts/SampleFallbackOracleUser.sol +++ b/contracts/SampleFallbackOracleUser.sol @@ -61,9 +61,10 @@ contract SampleFallbackOracleUser { } function changeFallback(address _newOracle) external{ - if((block.timestamp - data[data.length - 1].timestamp) < 7 days){ - blobstreamO = IBlobstreamO(_newOracle); - } + require(data.length > 0); + require(msg.sender == guardian); + require((block.timestamp - data[data.length - 1].timestamp) > 7 days); + blobstreamO = IBlobstreamO(_newOracle); emit FallbackChanged(_newOracle); } @@ -89,7 +90,7 @@ contract SampleFallbackOracleUser { emit OracleChange(updateOracleTimestamp, _newOracle); }else{ require(block.timestamp - updateOracleTimestamp > 7 days); - blobstreamO = IBlobstreamO(proposedOracle); + centralizedOracle = proposedOracle; emit OracleChange(block.timestamp, proposedOracle); proposedOracle = address(0); } @@ -107,10 +108,35 @@ contract SampleFallbackOracleUser { Validator[] calldata _currentValidatorSet, Signature[] calldata _sigs ) external { - require(_attestData.report.timestamp > data[data.length - 1].timestamp, "cannot go back in time");//cannot go back in time + require(data.length == 0 || _attestData.report.timestamp > data[data.length - 1].timestamp, "cannot go back in time");//cannot go back in time uint256 _value = abi.decode(_attestData.report.value, (uint256)); - if((data.length == 0 || (block.timestamp - pauseTimestamp) < 24 hours && (block.timestamp - data[data.length - 1].timestamp) < 1 hours)){ - require(msg.sender == centralizedOracle, "must be proper signer"); + if(msg.sender == centralizedOracle){ + if((data.length == 0 || (block.timestamp - pauseTimestamp) < 24 hours)){ + data.push(Data( + _value, + _attestData.report.timestamp, + _attestData.report.aggregatePower, + _attestData.report.previousTimestamp, + _attestData.report.nextTimestamp, + block.timestamp + ) + ); + emit OracleUpdated(_value,_attestData.report.timestamp, _attestData.report.aggregatePower); + return; + } + } + else if((block.timestamp - data[data.length - 1].timestamp) > 1 hours){ + require(_attestData.queryId == queryId, "Invalid queryId"); + blobstreamO.verifyOracleData(_attestData, _currentValidatorSet, _sigs); + if(_attestData.report.aggregatePower < blobstreamO.powerThreshold()){//if not consensus data + require(_attestData.attestationTimestamp - _attestData.report.timestamp >= 15 minutes);//must be at least 15 minutes old + require(_attestData.report.aggregatePower > blobstreamO.powerThreshold()/2);//must have >1/3 aggregate power + require(_attestData.report.nextTimestamp == 0 || + _attestData.attestationTimestamp - _attestData.report.nextTimestamp < 15 minutes);//cannot have newer data you can push + }else{ + require(_attestData.report.nextTimestamp == 0, "should be no newer timestamp"); // must push the newest data + } + require(block.timestamp - _attestData.attestationTimestamp < 5 minutes);//data cannot be more than 5 minutes old (the relayed attestation) data.push(Data( _value, _attestData.report.timestamp, @@ -120,29 +146,8 @@ contract SampleFallbackOracleUser { block.timestamp ) ); - return; - } - require(_attestData.queryId == queryId, "Invalid queryId"); - blobstreamO.verifyOracleData(_attestData, _currentValidatorSet, _sigs); - if(_attestData.report.aggregatePower < blobstreamO.powerThreshold()){//if not consensus data - require(_attestData.attestationTimestamp - _attestData.report.timestamp >= 15 minutes);//must be at least 15 minutes old - require(_attestData.report.aggregatePower > blobstreamO.powerThreshold()/2);//must have >1/3 aggregate power - require(_attestData.report.nextTimestamp == 0 || - _attestData.attestationTimestamp - _attestData.report.nextTimestamp < 15 minutes);//cannot have newer data you can push - }else{ - require(_attestData.report.nextTimestamp == 0, "should be no newer timestamp"); // must push the newest data + emit OracleUpdated(_value,_attestData.report.timestamp, _attestData.report.aggregatePower); } - require(block.timestamp - _attestData.attestationTimestamp < 5 minutes);//data cannot be more than 5 minutes old (the relayed attestation) - data.push(Data( - _value, - _attestData.report.timestamp, - _attestData.report.aggregatePower, - _attestData.report.previousTimestamp, - _attestData.report.nextTimestamp, - block.timestamp - ) - ); - emit OracleUpdated(_value,_attestData.report.timestamp, _attestData.report.aggregatePower); } function getAllData() external view returns(Data[] memory){ diff --git a/test/FunctionTests.js b/test/FunctionTests.js index 1bbbf1f..7dd380c 100644 --- a/test/FunctionTests.js +++ b/test/FunctionTests.js @@ -2,7 +2,7 @@ var assert = require('assert'); const abiCoder = new ethers.AbiCoder(); const { expect } = require("chai"); const h = require("./helpers/evmHelpers.js"); - +const { report } = require('process'); const PRICEFEED_DATA_ARGS = abiCoder.encode(["string","string"], ["trb","usd"]) const PRICEFEED_QUERY_DATA = abiCoder.encode(["string", "bytes"], ["SpotPrice", PRICEFEED_DATA_ARGS]) @@ -30,12 +30,12 @@ describe("Sample Layer User - function tests", function () { let accounts, cpiUser, evmCallUser, fallbackUser, predictionMarketUser, priceFeedUser, blobstream, guardian, governance, centralizedOracle let threshold, val1, val2, initialPowers, initialValAddrs; - async function submitData(queryId, value, aggregatePower){ + async function submitData(queryId, value, aggregatePower, reportTimestamp){ //e.g. value = abiCoder.encode(["uint256"], [2000]) // e.g. queryId = h.hash("myquery") blocky = await h.getBlock() - timestamp = (blocky.timestamp - 2) attestTimestamp = blocky.timestamp + timestamp = reportTimestamp - 2 previousTimestamp = 0 nextTimestamp = 0 newValHash = await h.calculateValHash(initialValAddrs, initialPowers) @@ -55,7 +55,6 @@ describe("Sample Layer User - function tests", function () { sig2 = await h.layerSign(dataDigest, val2.privateKey) sig3 = await h.layerSign(dataDigest, val3.privateKey) sigStructArray = await h.getSigStructArray([ethers.Signature.from(sig1), ethers.Signature.from(sig2),ethers.Signature.from(sig3)]) - //console.log(sigStructArray) oracleDataStruct = await h.getOracleDataStruct( queryId, value, @@ -75,7 +74,6 @@ describe("Sample Layer User - function tests", function () { beforeEach(async function () { accounts = await ethers.getSigners(); guardian = accounts[1] - val1 = ethers.Wallet.createRandom() val2 = ethers.Wallet.createRandom() val3 = ethers.Wallet.createRandom() @@ -93,21 +91,14 @@ describe("Sample Layer User - function tests", function () { valTimestamp = (blocky.timestamp - 2) * 1000 newValHash = await h.calculateValHash(initialValAddrs, initialPowers) valCheckpoint = h.calculateValCheckpoint(newValHash, threshold, valTimestamp) - - blobstream= await ethers.deployContract("BlobstreamO", [guardian.address]); await blobstream.init(threshold, valTimestamp, UNBONDING_PERIOD, valCheckpoint) - cpiUser = await ethers.deployContract("SampleCPIUser", [blobstream.target,CPI_QUERY_ID,guardian.address]); - governance = accounts[2] evmCallUser = await ethers.deployContract("SampleEVMCallUser",[blobstream.target,EVMCALL_QUERY_ID,guardian.address, governance.address]); - centralizedOracle = accounts[3] fallbackUser = await ethers.deployContract("SampleFallbackOracleUser",[blobstream.target,PRICEFEED_QUERY_ID,guardian.address, governance.address, centralizedOracle.address]); - predictionMarketUser = await ethers.deployContract("SamplePredictionMarketUser",[blobstream.target,PREDICTIONMARKET_QUERY_ID,guardian.address]); - priceFeedUser = await ethers.deployContract("SamplePriceFeedUser",[blobstream.target,PRICEFEED_QUERY_ID,guardian.address]); }) @@ -124,11 +115,10 @@ describe("Sample Layer User - function tests", function () { assert(await cpiUser.paused.call(), "should be pause") }); it("SampleCPIUser - updateOracleData, getCurrentData", async function () { - let _b0= await h.getBlock() let _power = 6; let _value = abiCoder.encode(["uint256"], [3000]) - let res = await submitData(CPI_QUERY_ID,_value, _power); + let res = await submitData(CPI_QUERY_ID,_value, _power, _b0.timestamp); let _attestData = res[0] let _currentValidatorSet = res[1] let _sigs = res[2] @@ -141,10 +131,10 @@ describe("Sample Layer User - function tests", function () { assert(vars[5] == _b1.timestamp) }); it("SampleCPIUser - getAllData, getValueCount", async function () { - let _b0= h.getBlock + let _b0= await h.getBlock() let _power = 6; let _value = abiCoder.encode(["uint256"], [1000]) - let res = await submitData(CPI_QUERY_ID,_value, _power); + let res = await submitData(CPI_QUERY_ID,_value, _power,_b0.timestamp); let _attestData = res[0] let _currentValidatorSet = res[1] let _sigs = res[2] @@ -152,23 +142,22 @@ describe("Sample Layer User - function tests", function () { let _b1= await h.getBlock() assert(await cpiUser.getValueCount.call() == 1) await h.advanceTime(86400) - let _b2= h.getBlock + let _b2= await h.getBlock() let _power2 = 6; let _value2 = abiCoder.encode(["uint256"], [2000]) - res = await submitData(CPI_QUERY_ID,_value2, _power2); + res = await submitData(CPI_QUERY_ID,_value2, _power2,_b2.timestamp); let _attestData2= res[0] let _currentValidatorSet2 = res[1] let _sigs2 = res[2] await cpiUser.updateOracleData(_attestData2, _currentValidatorSet2, _sigs2); let _b3= await h.getBlock() let vars = await cpiUser.getAllData() - console.log(vars) assert(vars[0].value == _value); - assert(vars[0].time == _b0.timestamp) + assert(vars[0].timestamp== _b0.timestamp - 2) assert(vars[0].aggregatePower == _power) assert(vars[0].relayTimestamp == _b1.timestamp) assert(vars[1].value == abiCoder.encode(["uint256"], [1100]));//capped at 10% move - assert(vars[1].time == _b2.timestamp) + assert(vars[1].timestamp == _b2.timestamp - 2) assert(vars[1].aggregatePower == _power2) assert(vars[1].relayTimestamp == _b3.timestamp) assert(await cpiUser.getValueCount.call() == 2) @@ -184,7 +173,7 @@ describe("Sample Layer User - function tests", function () { await h.expectThrow(evmCallUser.changeGuardian(accounts[8].address)) //must be governance await evmCallUser.connect(governance).changeGuardian(accounts[9].address) - //must wait 7 days + //must wait 7 days assert(await evmCallUser.guardian.call() == guardian.address); assert(await evmCallUser.proposedGuardian.call() == accounts[9].address); await h.expectThrow(evmCallUser.changeGuardian(accounts[8].address)) @@ -194,9 +183,9 @@ describe("Sample Layer User - function tests", function () { }); it("SampleEVMCallUser -changeOracle", async function () { await h.expectThrow(evmCallUser.changeOracle(accounts[8].address)) - //must be governance + //must be governance await evmCallUser.connect(governance).changeOracle(accounts[9].address) - //must wait 7 days + //must wait 7 days assert(await evmCallUser.blobstreamO.call() == blobstream.target); assert(await evmCallUser.proposedOracle.call() == accounts[9].address); await h.expectThrow(evmCallUser.changeOracle(accounts[8].address)) @@ -205,36 +194,111 @@ describe("Sample Layer User - function tests", function () { assert(await evmCallUser.blobstreamO.call() == accounts[9].address); }); it("SampleEVMCallUser - togglePause", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); + await h.expectThrow(evmCallUser.togglePause())//must be gauardian + //guardian pauses, cannot update oracle\ + assert(await evmCallUser.paused.call() == false); + await evmCallUser.connect(guardian).togglePause() + assert(await evmCallUser.paused.call() == true, "should be paused"); + let _b0= await h.getBlock() + let _power = 6; + let _reportTimestamp = _b0.timestamp - 60*61//at least one hour old + let _value = abiCoder.encode(["uint256"], [3000]) + let res = await submitData(EVMCALL_QUERY_ID,_value, _power, _reportTimestamp); + let _attestData = res[0] + let _currentValidatorSet = res[1] + let _sigs = res[2] + await h.expectThrow(evmCallUser.updateOracleData(_attestData, _currentValidatorSet, _sigs)); + //unpauses, can updateOracle + await evmCallUser.connect(guardian).togglePause() + assert(await evmCallUser.paused.call() == false); + await evmCallUser.updateOracleData(_attestData, _currentValidatorSet, _sigs); }); it("SampleEVMCallUser - updateOracleData, getCurrentData", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); + let _b0= await h.getBlock() + let _power = 6; + let _reportTimestamp = _b0.timestamp - 60*61//at least one hour old + let _value = abiCoder.encode(["uint256"], [3000]) + let res = await submitData(EVMCALL_QUERY_ID,_value, _power, _reportTimestamp); + let _attestData = res[0] + let _currentValidatorSet = res[1] + let _sigs = res[2] + await evmCallUser.updateOracleData(_attestData, _currentValidatorSet, _sigs); + let _b1= await h.getBlock() + let vars = await evmCallUser.getCurrentData(); + assert(vars[0] = _value); + assert(vars[1] = _b0.timestamp, "timestamp should be correct") + assert(vars[2] = _power) + assert(vars[5] == _b1.timestamp) }); it("SampleEVMCallUser - getAllData, getValueCount", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); + let _b0= await h.getBlock() + let _power = 6; + let _value = abiCoder.encode(["uint256"], [1000]) + let _reportTimestamp = _b0.timestamp - 60*61//at least one hour old + let res = await submitData(EVMCALL_QUERY_ID,_value, _power,_reportTimestamp); + let _attestData = res[0] + let _currentValidatorSet = res[1] + let _sigs = res[2] + await evmCallUser.updateOracleData(_attestData, _currentValidatorSet, _sigs); + let _b1= await h.getBlock() + assert(await evmCallUser.getValueCount.call() == 1) + await h.advanceTime(86400) + let _b2= await h.getBlock() + let _power2 = 6; + let _value2 = abiCoder.encode(["uint256"], [2000]) + res = await submitData(EVMCALL_QUERY_ID,_value2, _power2,_b2.timestamp - 60*61); + let _attestData2= res[0] + let _currentValidatorSet2 = res[1] + let _sigs2 = res[2] + await evmCallUser.updateOracleData(_attestData2, _currentValidatorSet2, _sigs2); + let _b3= await h.getBlock() + let vars = await evmCallUser.getAllData() + assert(vars[0].value == _value); + assert(vars[0].timestamp== _b0.timestamp - 60*61 - 2) + assert(vars[0].aggregatePower == _power) + assert(vars[0].relayTimestamp == _b1.timestamp) + assert(vars[1].value == abiCoder.encode(["uint256"], [2000])); + assert(vars[1].timestamp == _b2.timestamp - 60*61 - 2) + assert(vars[1].aggregatePower == _power2) + assert(vars[1].relayTimestamp == _b3.timestamp) + assert(await evmCallUser.getValueCount.call() == 2) }); console.log("SampleFallbackOracleUser") it("SampleFallbackOracleUser - Constructor", async function () { assert(await fallbackUser.blobstreamO.call() == blobstream.target, "blobstream should be set right") - assert(await fallbackUser.queryId.call() == PRICEFEEDL_QUERY_ID, "queryID should be set correct") + assert(await fallbackUser.queryId.call() == PRICEFEED_QUERY_ID, "queryID should be set correct") assert(await fallbackUser.guardian.call() == guardian.address); assert(await fallbackUser.governance.call() == governance.address, "governance should set") - assert(await fallbackUser.centralizedOracle.call() == centralizedOracle.address(), "centralized oracle should be set") + assert(await fallbackUser.centralizedOracle.call() == centralizedOracle.address, "centralized oracle should be set") }); it("SampleFallbackOracleUser - changeFallback", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); + await h.expectThrow(fallbackUser.connect(guardian).changeFallback(accounts[9].address)) + let _b0= await h.getBlock() + let _power = 6; + let _reportTimestamp = _b0.timestamp - 60*15//at least 15 minutes + let _value = abiCoder.encode(["uint256"], [3000]) + let res = await submitData(PRICEFEED_QUERY_ID,_value, _power, _reportTimestamp); + let _attestData = res[0] + let _currentValidatorSet = res[1] + let _sigs = res[2] + await fallbackUser.connect(centralizedOracle).updateOracleData(_attestData, _currentValidatorSet, _sigs); + let vars = await fallbackUser.getCurrentData(); + assert(vars[0] = _value); + assert(vars[1] = _b0.timestamp - 2, "timestamp should be correct") + let _b1= await h.getBlock() + await h.expectThrow(fallbackUser.connect(guardian).changeFallback(accounts[9].address)) + await h.advanceTime(86400 *7) + await h.expectThrow(fallbackUser.changeFallback(accounts[9].address)) + await fallbackUser.connect(guardian).changeFallback(accounts[9].address) + assert(await fallbackUser.blobstreamO.call() == accounts[9].address) }); it("SampleFallbackOracleUser - changeGuardian", async function () { - await h.expectThrow(fallbacklUser.changeGuardian(accounts[8].address)) - //must be governance + await h.expectThrow(fallbackUser.changeGuardian(accounts[8].address)) + //must be governance await fallbackUser.connect(governance).changeGuardian(accounts[9].address) - //must wait 7 days - assert(await fallbacklUser.guardian.call() == guardian.address); - assert(await fallbacklUser.proposedGuardian.call() == accounts[9].address); + //must wait 7 days + assert(await fallbackUser.guardian.call() == guardian.address); + assert(await fallbackUser.proposedGuardian.call() == accounts[9].address); await h.expectThrow(fallbackUser.changeGuardian(accounts[8].address)) await h.advanceTime(86400*7) await fallbackUser.changeGuardian(accounts[8].address) @@ -245,24 +309,75 @@ describe("Sample Layer User - function tests", function () { //must be governance await fallbackUser.connect(governance).changeOracle(accounts[9].address) //must wait 7 days - assert(await fallbackUser.blobstreamO.call() == blobstream.target); + assert(await fallbackUser.centralizedOracle.call() == centralizedOracle.address); assert(await fallbackUser.proposedOracle.call() == accounts[9].address); await h.expectThrow(fallbackUser.changeOracle(accounts[8].address)) await h.advanceTime(86400*7) await fallbackUser.changeOracle(accounts[8].address) - assert(await fallbackUser.blobstreamO.call() == accounts[9].address); + assert(await fallbackUser.centralizedOracle.call() == accounts[9].address); }); - it("SampleFallbackOracleUser - togglePause", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); - }); - it("SampleFallbackOracleUser - updateOracleData, getCurrentData", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); + it("SampleFallbackOracleUser - pauseContract", async function () { + await h.expectThrow(fallbackUser.pauseContract()) + //only gaurdian + await fallbackUser.connect(guardian).pauseContract() + let _b1= await h.getBlock() + assert(await fallbackUser.pauseTimestamp.call() == _b1.timestamp, "should be pause"); + //cannot run updateOracle or call pause again + let _b0= await h.getBlock() + let _power = 6; + let _value = abiCoder.encode(["uint256"], [3000]) + let res = await submitData(PRICEFEED_QUERY_ID,_value, _power,_b0.timestamp); + let _attestData = res[0] + let _currentValidatorSet = res[1] + let _sigs = res[2] + await h.expectThrow(fallbackUser.updateOracleData(_attestData, _currentValidatorSet, _sigs)); + await h.expectThrow(fallbackUser.connect(guardian).pauseContract()) + //unpauses, can updateOracle + await h.advanceTime(86400*2) + await fallbackUser.connect(centralizedOracle).updateOracleData(_attestData, _currentValidatorSet, _sigs) + await fallbackUser.connect(guardian).pauseContract() + _b1= await h.getBlock() + assert(await fallbackUser.pauseTimestamp.call() == _b1.timestamp, "should be pause"); }); - it("SampleFallbackOracleUser - getAllData, getValueCount", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); + it("SampleFallbackOracleUser - updateOracleData, getCurrentData,getAllData, getValueCount", async function () { + let _b0= await h.getBlock() + let _power = 6; + let _reportTimestamp = _b0.timestamp + let _value = abiCoder.encode(["uint256"], [3000]) + let res = await submitData(PRICEFEED_QUERY_ID,_value, _power, _reportTimestamp); + let _attestData = res[0] + let _currentValidatorSet = res[1] + let _sigs = res[2] + await h.expectThrow(fallbackUser.updateOracleData(_attestData, _currentValidatorSet, _sigs))//not oracle + await fallbackUser.connect(centralizedOracle).updateOracleData(_attestData, _currentValidatorSet, _sigs); + let _b1= await h.getBlock() + let vars = await fallbackUser.getCurrentData(); + assert(vars[0] = _value); + assert(vars[1] = _b0.timestamp - 2, "timestamp should be correct") + assert(vars[2] = _power) + assert(vars[5] == _b1.timestamp) + //now fallback + await h.advanceTime(60*60)//1 hr advance + _b2= await h.getBlock() + _power2 = 6; + _reportTimestamp2 = _b2.timestamp - 60*15//at least 15 minutes + _value2 = abiCoder.encode(["uint256"], [9000]) + res = await submitData(PRICEFEED_QUERY_ID,_value2, _power, _reportTimestamp2); + _attestData = res[0] + _currentValidatorSet = res[1] + _sigs = res[2] + await fallbackUser.updateOracleData(_attestData, _currentValidatorSet, _sigs); + assert(await fallbackUser.getValueCount.call() == 2) + let _b3= await h.getBlock() + vars = await fallbackUser.getAllData() + assert(vars[0].value == _value); + assert(vars[0].timestamp== _b0.timestamp - 2) + assert(vars[0].aggregatePower == _power) + assert(vars[0].relayTimestamp == _b1.timestamp) + assert(vars[1].value == abiCoder.encode(["uint256"], [9000])); + assert(vars[1].timestamp == _reportTimestamp2 - 2) + assert(vars[1].aggregatePower == _power2) + assert(vars[1].relayTimestamp == _b3.timestamp) }); console.log("SamplePredictionMarketUser - Function Tests") it("SamplePredictionMarketUser - Constructor", async function () { @@ -277,16 +392,57 @@ describe("Sample Layer User - function tests", function () { assert(await predictionMarketUser.paused.call(), "should be pause") }); it("SamplePredictionMarketUser - updateOracleData, getCurrentData", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); + let _b0= await h.getBlock() + let _power = 6; + let _reportTimestamp = _b0.timestamp - 60*61//at least one hour old + let _value = abiCoder.encode(["uint256"], [3000]) + let res = await submitData(PREDICTIONMARKET_QUERY_ID,_value, _power, _reportTimestamp); + let _attestData = res[0] + let _currentValidatorSet = res[1] + let _sigs = res[2] + await predictionMarketUser.updateOracleData(_attestData, _currentValidatorSet, _sigs); + let _b1= await h.getBlock() + let vars = await predictionMarketUser.getCurrentData(); + assert(vars[0] = _value); + assert(vars[1] = _b0.timestamp, "timestamp should be correct") + assert(vars[2] = _power) + assert(vars[5] == _b1.timestamp) }); - it("SamplePredictionMarketUser - getAllDatam getValueCount", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); + it("SamplePredictionMarketUser - getAllData getValueCount", async function () { + let _b0= await h.getBlock() + let _power = 6; + let _value = abiCoder.encode(["uint256"], [1000]) + let _reportTimestamp = _b0.timestamp - 60*61//at least one hour old + let res = await submitData(PREDICTIONMARKET_QUERY_ID,_value, _power,_reportTimestamp); + let _attestData = res[0] + let _currentValidatorSet = res[1] + let _sigs = res[2] + await predictionMarketUser.updateOracleData(_attestData, _currentValidatorSet, _sigs); + let _b1= await h.getBlock() + assert(await predictionMarketUser.getValueCount.call() == 1) + await h.advanceTime(86400) + let _b2= await h.getBlock() + let _power2 = 6; + let _value2 = abiCoder.encode(["uint256"], [2000]) + res = await submitData(PREDICTIONMARKET_QUERY_ID,_value2, _power2,_b2.timestamp - 60*61); + let _attestData2= res[0] + let _currentValidatorSet2 = res[1] + let _sigs2 = res[2] + await predictionMarketUser.updateOracleData(_attestData2, _currentValidatorSet2, _sigs2); + let _b3= await h.getBlock() + let vars = await predictionMarketUser.getAllData() + assert(vars[0].value == _value); + assert(vars[0].timestamp== _b0.timestamp - 60*61 - 2) + assert(vars[0].aggregatePower == _power) + assert(vars[0].relayTimestamp == _b1.timestamp) + assert(vars[1].value == abiCoder.encode(["uint256"], [2000])); + assert(vars[1].timestamp == _b2.timestamp - 60*61 - 2) + assert(vars[1].aggregatePower == _power2) + assert(vars[1].relayTimestamp == _b3.timestamp) + assert(await predictionMarketUser.getValueCount.call() == 2) }); console.log("SamplePriceFeedUser - Function Tests") it("SamplePriceFeedUser - Constructor", async function () { - console.log(await priceFeedUser.blobstreamO.call() , blobstream.target) assert(await priceFeedUser.blobstreamO.call() == blobstream.target, "blobstream should be set right") assert(await priceFeedUser.queryId.call() == PRICEFEED_QUERY_ID, "queryID should be set correct") assert(await priceFeedUser.guardian.call() == guardian.address); @@ -297,11 +453,53 @@ describe("Sample Layer User - function tests", function () { assert(await priceFeedUser.paused.call(), "should be pause") }); it("SamplePriceFeedUser - updateOracleData, getCurrentData", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); + let _b0= await h.getBlock() + let _power = 6; + let _reportTimestamp = _b0.timestamp - 60*61//at least one hour old + let _value = abiCoder.encode(["uint256"], [3000]) + let res = await submitData(PRICEFEED_QUERY_ID,_value, _power, _reportTimestamp); + let _attestData = res[0] + let _currentValidatorSet = res[1] + let _sigs = res[2] + await priceFeedUser.updateOracleData(_attestData, _currentValidatorSet, _sigs); + let _b1= await h.getBlock() + let vars = await priceFeedUser.getCurrentData(); + assert(vars[0] = _value); + assert(vars[1] = _b0.timestamp, "timestamp should be correct") + assert(vars[2] = _power) + assert(vars[5] == _b1.timestamp) }); it("SamplePriceFeedUser - getAllData, getValueCount", async function () { - - expect(await lock.unlockTime()).to.equal(unlockTime); + let _b0= await h.getBlock() + let _power = 6; + let _value = abiCoder.encode(["uint256"], [1000]) + let _reportTimestamp = _b0.timestamp - 60*61//at least one hour old + let res = await submitData(PRICEFEED_QUERY_ID,_value, _power,_reportTimestamp); + let _attestData = res[0] + let _currentValidatorSet = res[1] + let _sigs = res[2] + await priceFeedUser.updateOracleData(_attestData, _currentValidatorSet, _sigs); + let _b1= await h.getBlock() + assert(await priceFeedUser.getValueCount.call() == 1) + await h.advanceTime(86400) + let _b2= await h.getBlock() + let _power2 = 6; + let _value2 = abiCoder.encode(["uint256"], [2000]) + res = await submitData(PRICEFEED_QUERY_ID,_value2, _power2,_b2.timestamp - 60*61); + let _attestData2= res[0] + let _currentValidatorSet2 = res[1] + let _sigs2 = res[2] + await priceFeedUser.updateOracleData(_attestData2, _currentValidatorSet2, _sigs2); + let _b3= await h.getBlock() + let vars = await priceFeedUser.getAllData() + assert(vars[0].value == _value); + assert(vars[0].timestamp== _b0.timestamp - 60*61 - 2) + assert(vars[0].aggregatePower == _power) + assert(vars[0].relayTimestamp == _b1.timestamp) + assert(vars[1].value == abiCoder.encode(["uint256"], [2000])); + assert(vars[1].timestamp == _b2.timestamp - 60*61 - 2) + assert(vars[1].aggregatePower == _power2) + assert(vars[1].relayTimestamp == _b3.timestamp) + assert(await priceFeedUser.getValueCount.call() == 2) }); });