Skip to content

Commit

Permalink
Merge pull request #111 from tellor-io/tim-multipleValues
Browse files Browse the repository at this point in the history
getter updates for handling disputes
  • Loading branch information
tkernell authored Dec 7, 2022
2 parents 07b73d6 + 0b29037 commit f779592
Show file tree
Hide file tree
Showing 7 changed files with 3,705 additions and 3,485 deletions.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion artifacts/contracts/UsingTellor.sol/UsingTellor.dbg.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/9abd4c6fe655b381d8c11577bce48186.json"
"buildInfo": "../../build-info/4c240997ce7684dc62c0fbd50cbbe1eb.json"
}
4 changes: 2 additions & 2 deletions artifacts/contracts/UsingTellor.sol/UsingTellor.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../build-info/9abd4c6fe655b381d8c11577bce48186.json"
"buildInfo": "../../../build-info/4c240997ce7684dc62c0fbd50cbbe1eb.json"
}

Large diffs are not rendered by default.

82 changes: 51 additions & 31 deletions contracts/UsingTellor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ contract UsingTellor is IERC2362 {
// since the value is within our boundaries, do a binary search
while (_search) {
_middle = (_end + _start) / 2;
_timestampRetrieved = getTimestampbyQueryIdandIndex(_queryId, _middle);
_timestampRetrieved = getTimestampbyQueryIdandIndex(
_queryId,
_middle
);
if (_timestampRetrieved > _timestamp) {
// get immediate previous value
uint256 _prevTime = getTimestampbyQueryIdandIndex(
Expand Down Expand Up @@ -130,16 +133,23 @@ contract UsingTellor is IERC2362 {
}
}
// candidate found, check for disputed values
if(!isInDispute(_queryId, _timestampRetrieved)) {
if (!isInDispute(_queryId, _timestampRetrieved)) {
// _timestampRetrieved is correct
return (true, _middle);
} else {
// iterate forward until we find a non-disputed value
while(isInDispute(_queryId, _timestampRetrieved) && _middle < _count) {
while (
isInDispute(_queryId, _timestampRetrieved) && _middle < _count
) {
_middle++;
_timestampRetrieved = getTimestampbyQueryIdandIndex(_queryId, _middle);
_timestampRetrieved = getTimestampbyQueryIdandIndex(
_queryId,
_middle
);
}
if(_middle == _count && isInDispute(_queryId, _timestampRetrieved)) {
if (
_middle == _count && isInDispute(_queryId, _timestampRetrieved)
) {
return (false, 0);
}
// _timestampRetrieved is correct
Expand Down Expand Up @@ -182,6 +192,7 @@ contract UsingTellor is IERC2362 {
view
returns (bytes[] memory _values, uint256[] memory _timestamps)
{
// get index of first possible value
(bool _ifRetrieve, uint256 _startIndex) = getIndexForDataAfter(
_queryId,
_timestamp - _maxAge
Expand All @@ -191,27 +202,34 @@ contract UsingTellor is IERC2362 {
return (new bytes[](0), new uint256[](0));
}
uint256 _endIndex;
// get index of last possible value
(_ifRetrieve, _endIndex) = getIndexForDataBefore(_queryId, _timestamp);
// no value before _timestamp
if (!_ifRetrieve) {
return (new bytes[](0), new uint256[](0));
}
uint256 _valCount = _endIndex - _startIndex + 1;
// more than _maxCount values found within range
if (_valCount > _maxCount) {
_startIndex = _endIndex - _maxCount + 1;
_valCount = _maxCount;
uint256 _valCount = 0;
uint256 _index = 0;
uint256[] memory _timestampsArrayTemp = new uint256[](_maxCount);
// generate array of non-disputed timestamps within range
while (_valCount < _maxCount && _endIndex + 1 - _index > _startIndex) {
uint256 _timestampRetrieved = getTimestampbyQueryIdandIndex(
_queryId,
_endIndex - _index
);
if (!isInDispute(_queryId, _timestampRetrieved)) {
_timestampsArrayTemp[_valCount] = _timestampRetrieved;
_valCount++;
}
_index++;
}

bytes[] memory _valuesArray = new bytes[](_valCount);
uint256[] memory _timestampsArray = new uint256[](_valCount);
bytes memory _valueRetrieved;
// retrieve values and reverse timestamps order
for (uint256 _i = 0; _i < _valCount; _i++) {
_timestampsArray[_i] = getTimestampbyQueryIdandIndex(
_queryId,
(_startIndex + _i)
);
_valueRetrieved = retrieveData(_queryId, _timestampsArray[_i]);
_valuesArray[_i] = _valueRetrieved;
_timestampsArray[_i] = _timestampsArrayTemp[_valCount - 1 - _i];
_valuesArray[_i] = retrieveData(_queryId, _timestampsArray[_i]);
}
return (_valuesArray, _timestampsArray);
}
Expand Down Expand Up @@ -285,15 +303,14 @@ contract UsingTellor is IERC2362 {
return tellor.retrieveData(_queryId, _timestamp);
}


/**
* @dev allows dev to set mapping contract for valueFor (EIP2362)
* @param _addy address of mapping contract
*/
function setIdMappingContract(address _addy) external{
require(address(idMappingContract) == address(0));
idMappingContract = IMappingContract(_addy);
}
function setIdMappingContract(address _addy) external {
require(address(idMappingContract) == address(0));
idMappingContract = IMappingContract(_addy);
}

/**
* @dev Retrieve most recent int256 value from oracle based on queryId
Expand All @@ -312,14 +329,13 @@ contract UsingTellor is IERC2362 {
uint256 _statusCode
)
{
_id = idMappingContract.getTellorID(_id);
uint256 _count = getNewValueCountbyQueryId(_id);
if (_count == 0) {
return (0, 0, 404);
}
_timestamp = getTimestampbyQueryIdandIndex(_id, _count - 1);
bytes memory _valueBytes = retrieveData(_id, _timestamp);
if (_valueBytes.length == 0) {
bytes32 _queryId = idMappingContract.getTellorID(_id);
bytes memory _valueBytes;
(_valueBytes, _timestamp) = getDataBefore(
_queryId,
block.timestamp + 1
);
if (_timestamp == 0) {
return (0, 0, 404);
}
uint256 _valueUint = _sliceUint(_valueBytes);
Expand All @@ -333,7 +349,11 @@ contract UsingTellor is IERC2362 {
* @param _b bytes value to convert to uint256
* @return _number uint256 converted from bytes
*/
function _sliceUint(bytes memory _b) internal pure returns(uint256 _number){
function _sliceUint(bytes memory _b)
internal
pure
returns (uint256 _number)
{
for (uint256 _i = 0; _i < _b.length; _i++) {
_number = _number * 256 + uint8(_b[_i]);
}
Expand Down
97 changes: 97 additions & 0 deletions test/e2eTests-UsingTellor.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,4 +809,101 @@ describe("UsingTellor Function Tests", function() {
expect(dataRetrieved[0]).to.equal('0x')
expect(dataRetrieved[1]).to.equal(0)
})

it("getMultipleValuesBefore", async function() {
// submit 4 values
await playground.connect(addr1).submitValue(h.uintTob32(1),h.uintTob32(150),0,'0x')
blocky1 = await h.getBlock()
await playground.connect(addr1).submitValue(h.uintTob32(1),h.uintTob32(160),0,'0x')
blocky2 = await h.getBlock()
await playground.connect(addr1).submitValue(h.uintTob32(1),h.uintTob32(170),0,'0x')
blocky3 = await h.getBlock()
await playground.connect(addr1).submitValue(h.uintTob32(1),h.uintTob32(180),0,'0x')
blocky4 = await h.getBlock()

await h.advanceTime(10)
blockyNow0 = await h.getBlock()

// dispute 2nd value
await playground.connect(addr1).beginDispute(h.uintTob32(1), blocky2.timestamp)

// check from blockyNow
result = await bench.getMultipleValuesBefore(h.uintTob32(1), blockyNow0.timestamp, 3600, 4)
expect(result[0].length).to.equal(3)
expect(result[1].length).to.equal(3)
expect(result[0][0]).to.equal(h.uintTob32(150))
expect(result[1][0]).to.equal(blocky1.timestamp)
expect(result[0][1]).to.equal(h.uintTob32(170))
expect(result[1][1]).to.equal(blocky3.timestamp)
expect(result[0][2]).to.equal(h.uintTob32(180))
expect(result[1][2]).to.equal(blocky4.timestamp)

// check from blocky4
result = await bench.getMultipleValuesBefore(h.uintTob32(1), blocky4.timestamp, 3600, 4)
expect(result[0].length).to.equal(2)
expect(result[1].length).to.equal(2)
expect(result[0][0]).to.equal(h.uintTob32(150))
expect(result[1][0]).to.equal(blocky1.timestamp)
expect(result[0][1]).to.equal(h.uintTob32(170))
expect(result[1][1]).to.equal(blocky3.timestamp)

// check from blocky3
result = await bench.getMultipleValuesBefore(h.uintTob32(1), blocky3.timestamp, 3600, 4)
expect(result[0].length).to.equal(1)
expect(result[1].length).to.equal(1)
expect(result[0][0]).to.equal(h.uintTob32(150))
expect(result[1][0]).to.equal(blocky1.timestamp)

// check from blocky2
result = await bench.getMultipleValuesBefore(h.uintTob32(1), blocky2.timestamp, 3600, 4)
expect(result[0].length).to.equal(1)
expect(result[1].length).to.equal(1)
expect(result[0][0]).to.equal(h.uintTob32(150))
expect(result[1][0]).to.equal(blocky1.timestamp)

// check from blocky1
result = await bench.getMultipleValuesBefore(h.uintTob32(1), blocky1.timestamp, 3600, 4)
expect(result[0].length).to.equal(0)
expect(result[1].length).to.equal(0)

// dispute 3rd value
await playground.connect(addr1).beginDispute(h.uintTob32(1), blocky3.timestamp)

// check from blockyNow
result = await bench.getMultipleValuesBefore(h.uintTob32(1), blockyNow0.timestamp, 3600, 4)
expect(result[0].length).to.equal(2)
expect(result[1].length).to.equal(2)
expect(result[0][0]).to.equal(h.uintTob32(150))
expect(result[1][0]).to.equal(blocky1.timestamp)
expect(result[0][1]).to.equal(h.uintTob32(180))
expect(result[1][1]).to.equal(blocky4.timestamp)

// check from blocky4
result = await bench.getMultipleValuesBefore(h.uintTob32(1), blocky4.timestamp, 3600, 4)
expect(result[0].length).to.equal(1)
expect(result[1].length).to.equal(1)
expect(result[0][0]).to.equal(h.uintTob32(150))
expect(result[1][0]).to.equal(blocky1.timestamp)

// check from blocky3
result = await bench.getMultipleValuesBefore(h.uintTob32(1), blocky3.timestamp, 3600, 4)
expect(result[0].length).to.equal(1)
expect(result[1].length).to.equal(1)
expect(result[0][0]).to.equal(h.uintTob32(150))
expect(result[1][0]).to.equal(blocky1.timestamp)

// check from blocky2
result = await bench.getMultipleValuesBefore(h.uintTob32(1), blocky2.timestamp, 3600, 4)
expect(result[0].length).to.equal(1)
expect(result[1].length).to.equal(1)
expect(result[0][0]).to.equal(h.uintTob32(150))
expect(result[1][0]).to.equal(blocky1.timestamp)

// check from blocky1
result = await bench.getMultipleValuesBefore(h.uintTob32(1), blocky1.timestamp, 3600, 4)
expect(result[0].length).to.equal(0)
expect(result[1].length).to.equal(0)


})
})

0 comments on commit f779592

Please sign in to comment.