Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: getAllCIDs index out of bound issue #3

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contracts/DealStatus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ contract DealStatus is IAggregatorOracle, Proof {
}

function getAllCIDs() external view returns (bytes[] memory) {
bytes[] memory cids;
bytes[] memory cids = new bytes[](transactionId);
for (uint256 i = 0; i < transactionId; i++) {
cids[i] = txIdToCid[i];
cids[i] = txIdToCid[i + 1];
}
return cids;
}
Expand Down
10 changes: 9 additions & 1 deletion contracts/mocks/DealStatusMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ contract DealStatusMock is IAggregatorOracle, ProofMock {
}

// getExpiringDeals should return all the deals' dealIds if they are expiring within `epochs`
function getExpiringDeals(bytes memory _cid, uint64 epochs) external view returns (Deal[] memory) {
function getExpiringDeals(bytes memory _cid,uint64 epochs) external view returns (Deal[] memory) {
// the logic is similar to the above, but use this api call:
// https://github.com/Zondax/filecoin-solidity/blob/master/contracts/v0.8/MarketAPI.sol#LL110C9-L110C9
Deal[] memory expiringDealIds;
Expand All @@ -103,4 +103,12 @@ contract DealStatusMock is IAggregatorOracle, ProofMock {

return expiringDealIds;
}

function getAllCIDs() external view returns (bytes[] memory) {
bytes[] memory cids = new bytes[](transactionId);
for (uint256 i = 0; i < transactionId; i++) {
cids[i] = txIdToCid[i + 1];
}
return cids;
}
}
101 changes: 58 additions & 43 deletions tests/DealStatus.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
const { expect } = require("chai");
const fs = require('fs');
const CIDTool = require('cid-tool')
const { expect } = require("chai")
const fs = require("fs")
const CIDTool = require("cid-tool")

async function deploy(name) {
const Cid = await ethers.getContractFactory("Cid");
const cid = await Cid.deploy();
const Cid = await ethers.getContractFactory("Cid")
const cid = await Cid.deploy()

const Contract = await ethers.getContractFactory(name, {
libraries: {
Cid: cid.address,
},
});
return await Contract.deploy().then(f => f.deployed());
})
return await Contract.deploy().then((f) => f.deployed())
}

describe("Aggregator Tests", function () {
before(async function () {
this.dealstatus = await deploy("DealStatusMock")
})

before(async function() {
this.dealstatus = await deploy('DealStatusMock');
});

describe("Validate Aggregator", function() {
it("Should submit a valid request", async function() {
cid = "0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127";
await expect(this.dealstatus.submit(cid)).to.emit(this.dealstatus, "SubmitAggregatorRequest").withArgs(1, cid);
});
describe("Validate Aggregator", function () {
it("Should submit a valid request", async function () {
cid = "0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127"
await expect(this.dealstatus.submit(cid))
.to.emit(this.dealstatus, "SubmitAggregatorRequest")
.withArgs(1, cid)
})

it("Should submit a callback with the expected Aux Data", async function () {
verifData = {
Expand Down Expand Up @@ -81,25 +82,26 @@ describe("Aggregator Tests", function () {
}
expectedAux = {
commPa: "0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127",
sizePa: 0x800000000
sizePa: 0x800000000,
}
await expect(this.dealstatus.complete(1, 1234, 4321, incProof, verifData)).to.emit(this.dealstatus, "CompleteAggregatorRequest").withArgs(1, 1234);
await expect(this.dealstatus.complete(1, 1234, 4321, incProof, verifData))
.to.emit(this.dealstatus, "CompleteAggregatorRequest")
.withArgs(1, 1234)

/*
const newAux = await this.dealstatus.complete(1, 1234, incProof, verifData);
console.log(newAux);
expect(newAux.data.commPa).to.equal(expectedAux.commPa);
expect(newAux.data.sizePa).to.equal(expectedAux.sizePa);
*/
});
})


function ipfsCidToHex(ipfsCid) {
rval = CIDTool.format(ipfsCid, { base: 'base16' })
return rval.substr(1, rval.length - 1);
rval = CIDTool.format(ipfsCid, { base: "base16" })
return rval.substr(1, rval.length - 1)
}

it("Should return all dealIDs created by the aggregator", async function() {
it("Should return all dealIDs created by the aggregator", async function () {
verifData = {
commPc: "0x0181e2039220200d0e0a0100030000000000000000000000000000000000000000000000000000",
sizePc: 0x20000000,
Expand Down Expand Up @@ -154,26 +156,39 @@ describe("Aggregator Tests", function () {
}
expectedAux = {
commPa: "0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127",
sizePa: 0x800000000
sizePa: 0x800000000,
}
await expect(this.dealstatus.complete(1, 2222, 4321, incProof, verifData)).to.emit(this.dealstatus, "CompleteAggregatorRequest").withArgs(1, 2222);
const allDeals = await this.dealstatus.getAllDeals("0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127");
expect(allDeals.toString()).to.be.equal("1234,4321,2222,4321");
});

it("Should return all the input cid's active dealIds", async function() {
const activeDeals = await this.dealstatus.callStatic.getActiveDeals("0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127");
expect(activeDeals.toString()).to.be.equal("1234,4321,2222,4321");
});
await expect(this.dealstatus.complete(1, 2222, 4321, incProof, verifData)).to.emit(this.dealstatus, "CompleteAggregatorRequest").withArgs(1, 2222)
const allDeals = await this.dealstatus.getAllDeals(
"0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127"
)
expect(allDeals.toString()).to.be.equal("1234,4321,2222,4321")
})

it("Should return all the input cid's active dealIds", async function () {
const activeDeals = await this.dealstatus.callStatic.getActiveDeals(
"0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127"
)
expect(activeDeals.toString()).to.be.equal("1234,4321,2222,4321")
})

it("Should return all the deals' dealIds if they are expiring within a certain input epoch", async function() {
const expiringDeals = await this.dealstatus.callStatic.getExpiringDeals("0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127", 1000);
expect(expiringDeals.toString()).to.be.equal("1234,4321,2222,4321");
});
it("Should be able to return the miner of a deal", async function() {
const allDeals = await this.dealstatus.getAllDeals("0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127");
it("Should return all the deals' dealIds if they are expiring within a certain input epoch", async function () {
const expiringDeals = await this.dealstatus.callStatic.getExpiringDeals("0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127", 1000)
expect(expiringDeals.toString()).to.be.equal("1234,4321,2222,4321")
})
it("Should be able to return the miner of a deal", async function () {
const allDeals = await this.dealstatus.getAllDeals(
"0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127"
)
// console.log(allDeals);
expect(allDeals[1].minerId.toString()).to.be.equal("4321");
});
});
});
expect(allDeals[1].minerId.toString()).to.be.equal("4321")
})
it("Should return all the CIDs", async function () {
const allCIDs = await this.dealstatus.callStatic.getAllCIDs()
expect(allCIDs.toString()).to.be.equal(
"0x0181e2039220203f46bc645b07a3ea2c04f066f939ddf7e269dd77671f9e1e61a3a3797e665127"
)
this.dealstatus.submit(cid)
})
})
})