Skip to content

Instantly share code, notes, and snippets.

@ravachol70
Last active April 26, 2023 17:40
Show Gist options
  • Select an option

  • Save ravachol70/8210cd85575b6ff2e7366cf306c63ed6 to your computer and use it in GitHub Desktop.

Select an option

Save ravachol70/8210cd85575b6ff2e7366cf306c63ed6 to your computer and use it in GitHub Desktop.
Compare Selected Inflections (A)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "ipfs-core/0.8.0/bytes" as bytesIPFS;
contract Comparator {
bytesIPFS.IPFS public ipfs;
struct Inflection {
bytes32[] blobsHashes;
}
constructor(bytesIPFS.IPFS memory _ipfs) {
ipfs = _ipfs;
}
function setIPFS(bytesIPFS.IPFS memory _ipfs) external {
ipfs = _ipfs;
}
function compareInflections(uint256 _inflectionId1, uint256 _inflectionId2) public view returns (bool) {
Inflection memory inflection1 = Inflection(getInflectionBlobs(_inflectionId1));
Inflection memory inflection2 = Inflection(getInflectionBlobs(_inflectionId2));
if (inflection1.blobsHashes.length != inflection2.blobsHashes.length) {
return false;
}
for (uint256 i = 0; i < inflection1.blobsHashes.length; i++) {
bytes memory blob1 = getBlobFromIPFS(inflection1.blobsHashes[i]);
bytes memory blob2 = getBlobFromIPFS(inflection2.blobsHashes[i]);
if (!compareBlobs(blob1, blob2)) {
return false;
}
}
return true;
}
function compareSelectedInflections(uint256[] memory _inflectionIds) public view returns (bool) {
for (uint256 i = 0; i < _inflectionIds.length; i++) {
for (uint256 j = i + 1; j < _inflectionIds.length; j++) {
if (!compareInflections(_inflectionIds[i], _inflectionIds[j])) {
return false;
}
}
}
return true;
}
function getInflectionBlobs(uint256 _inflectionId) internal view returns (bytes32[] memory) {
// Get blobs hashes for the inflection from storage
// ...
return blobsHashes;
}
function getBlobFromIPFS(bytes32 _hash) internal view returns (bytes memory) {
(bool success, bytes memory data) = ipfs.cat(_hash);
require(success, "IPFS: failed to get blob");
return data;
}
function compareBlobs(bytes memory _blob1, bytes memory _blob2) internal pure returns (bool) {
if (_blob1.length != _blob2.length) {
return false;
}
for (uint256 i = 0; i < _blob1.length; i++) {
if (_blob1[i] != _blob2[i]) {
return false;
}
}
return true;
}
}
@ravachol70
Copy link
Copy Markdown
Author

In the compareInflections function, we first retrieve the hashes of the IPFS blobs for each inflection using the inflections mapping. We then use the getDataFromIPFS function inherited from the IPFS contract to retrieve the data associated with each hash from IPFS. Once we have the data for each blob, we can compare them byte-by-byte to see if they are equal.

With these changes, the Comparator contract should be fully functional for comparing inflections that include IPFS blobs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment