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

Add /files/contracts/partial/{chain} endpoint and explicit ordering, resolve SQL NULL comparison bug #1416

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions services/server/test/helpers/LocalChainFixture.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import { LOCAL_CHAINS } from "../../src/sourcify-chains";
import nock from "nock";
import storageContractArtifact from "../testcontracts/Storage/Storage.json";
import storageContractMetadata from "../testcontracts/Storage/metadata.json";
import storageContractMetadataModified from "../testcontracts/Storage/metadataModified.json";

const storageContractSourcePath = path.join(
__dirname,
@@ -30,7 +31,10 @@ export class LocalChainFixture {
defaultContractMetadata = Buffer.from(
JSON.stringify(storageContractMetadata)
);
defaultContractModifiedIpfsMetadata = getModifiedIpfsMetadata();
defaultContractModifiedMetadata = Buffer.from(
JSON.stringify(storageContractMetadataModified)
);
defaultContractModifiedSourceIpfs = getModifiedSourceIpfs();
defaultContractArtifact = storageContractArtifact;

private _chainId?: string;
@@ -111,7 +115,8 @@ export class LocalChainFixture {
}
}

function getModifiedIpfsMetadata(): Buffer {
// Changes the IPFS hash inside the metadata file to make the source unfetchable
function getModifiedSourceIpfs(): Buffer {
const ipfsAddress =
storageContractMetadata.sources["project:/contracts/Storage.sol"].urls[1];
// change the last char in ipfs hash of the source file
1 change: 1 addition & 0 deletions services/server/test/helpers/ServerFixture.ts
Original file line number Diff line number Diff line change
@@ -83,6 +83,7 @@ export class ServerFixture {
beforeEach(async () => {
rimraf.sync(this.server.repository);
await resetDatabase(this.storageService);
console.log("Resetting the StorageService");
});

after(() => {
11 changes: 9 additions & 2 deletions services/server/test/helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -71,7 +71,8 @@ export async function deployFromAbiAndBytecodeForCreatorTxHash(
export async function deployAndVerifyContract(
chai: Chai.ChaiStatic,
chainFixture: LocalChainFixture,
serverFixture: ServerFixture
serverFixture: ServerFixture,
partial: boolean = false
) {
const contractAddress = await deployFromAbiAndBytecode(
chainFixture.localSigner,
@@ -84,7 +85,13 @@ export async function deployAndVerifyContract(
.post("/")
.field("address", contractAddress)
.field("chain", chainFixture.chainId)
.attach("files", chainFixture.defaultContractMetadata, "metadata.json")
.attach(
"files",
partial
? chainFixture.defaultContractModifiedMetadata
: chainFixture.defaultContractMetadata,
"metadata.json"
)
.attach("files", chainFixture.defaultContractSource);
return contractAddress;
}
132 changes: 76 additions & 56 deletions services/server/test/integration/repository-handlers/files.spec.ts
Original file line number Diff line number Diff line change
@@ -40,64 +40,84 @@ describe("Verify repository endpoints", function () {
chai.expect(res4.body.full).has.a.lengthOf(1);
});

it("should handle pagination in /files/contracts/any/{chain}", async function () {
// Deploy 5 contracts
const contractAddresses: string[] = [];
for (let i = 0; i < 5; i++) {
const address = await deployAndVerifyContract(
chai,
chainFixture,
serverFixture
);
contractAddresses.push(address);
}
describe.only(`Pagination in /files/contracts/{full|any|partial}/${chainFixture.chainId}`, async function () {
const endpointMatchTypes = ["full", "any", "partial"];
for (const endpointMatchType of endpointMatchTypes) {
it(`should handle pagination in /files/contracts/${endpointMatchType}/${chainFixture.chainId}`, async function () {
const contractAddresses: string[] = [];

// Deploy 5 contracts
for (let i = 0; i < 5; i++) {
// Deploy partial matching contract if endpoint is partial or choose randomly if endpointMachtype is any. 'any' endpoint results should be consistent regardless.
const shouldDeployPartial =
endpointMatchType === "partial" ||
(endpointMatchType === "any" && Math.random() > 0.5);
marcocastignoli marked this conversation as resolved.
Show resolved Hide resolved

// Verify pagination
const res0 = await chai
.request(serverFixture.server.app)
.get(`/files/contracts/any/${chainFixture.chainId}?page=1&limit=2`);
chai.expect(res0.body.pagination).to.deep.equal({
currentPage: 1,
hasNextPage: true,
hasPreviousPage: true,
resultsCurrentPage: 2,
resultsPerPage: 2,
totalPages: 3,
totalResults: 5,
});
const res1 = await chai
.request(serverFixture.server.app)
.get(`/files/contracts/any/${chainFixture.chainId}?limit=5`);
chai.expect(res1.body.pagination).to.deep.equal({
currentPage: 0,
hasNextPage: false,
hasPreviousPage: false,
resultsCurrentPage: 5,
resultsPerPage: 5,
totalPages: 1,
totalResults: 5,
});
const address = await deployAndVerifyContract(
chai,
chainFixture,
serverFixture,
shouldDeployPartial
);
contractAddresses.push(address);
}

// Verify ascending order
const resAsc = await chai
.request(serverFixture.server.app)
.get(`/files/contracts/any/${chainFixture.chainId}?order=asc`);
chai
.expect(resAsc.body.results)
.to.deep.equal(
contractAddresses,
"Contract addresses are not in ascending order"
);
// Test pagination
const res0 = await chai
.request(serverFixture.server.app)
.get(
`/files/contracts/${endpointMatchType}/${chainFixture.chainId}?page=1&limit=2`
);
chai.expect(res0.body.pagination).to.deep.equal({
currentPage: 1,
hasNextPage: true,
hasPreviousPage: true,
resultsCurrentPage: 2,
resultsPerPage: 2,
totalPages: 3,
totalResults: 5,
});
const res1 = await chai
.request(serverFixture.server.app)
.get(
`/files/contracts/${endpointMatchType}/${chainFixture.chainId}?limit=5`
);
chai.expect(res1.body.pagination).to.deep.equal({
currentPage: 0,
hasNextPage: false,
hasPreviousPage: false,
resultsCurrentPage: 5,
resultsPerPage: 5,
totalPages: 1,
totalResults: 5,
});

// Verify descending order
const resDesc = await chai
.request(serverFixture.server.app)
.get(`/files/contracts/any/${chainFixture.chainId}?order=desc`);
chai
.expect(resDesc.body.results)
.to.deep.equal(
contractAddresses.reverse(),
"Contract addresses are not in reverse order"
);
// Test ascending order
const resAsc = await chai
.request(serverFixture.server.app)
.get(
`/files/contracts/${endpointMatchType}/${chainFixture.chainId}?order=asc`
);
chai
.expect(resAsc.body.results)
.to.deep.equal(
contractAddresses,
"Contract addresses are not in ascending order"
);

// Test descending order
const resDesc = await chai
.request(serverFixture.server.app)
.get(
`/files/contracts/${endpointMatchType}/${chainFixture.chainId}?order=desc`
);
chai
.expect(resDesc.body.results)
.to.deep.equal(
contractAddresses.reverse(),
"Contract addresses are not in reverse order"
);
});
}
});
});
Original file line number Diff line number Diff line change
@@ -297,7 +297,7 @@ describe("/session", function () {
const agent = chai.request.agent(serverFixture.server.app);
agent
.post("/session/input-files")
.attach("files", chainFixture.defaultContractModifiedIpfsMetadata)
.attach("files", chainFixture.defaultContractModifiedSourceIpfs)
.then((res) => {
assertAddressAndChainMissing(res, [], {
"project:/contracts/Storage.sol": {
Original file line number Diff line number Diff line change
@@ -137,7 +137,7 @@ describe("/", function () {
.field("chain", chainFixture.chainId)
.attach(
"files",
chainFixture.defaultContractModifiedIpfsMetadata,
chainFixture.defaultContractModifiedSourceIpfs,
"metadata.json"
)
.end((err, res) => {