Skip to content

Commit

Permalink
feat: Add test for checking BSP skipping volunteer if file key is alr…
Browse files Browse the repository at this point in the history
…eady stored (#313)

* add test for checking bsp skipping volunteer if file key is already stored

* change back to describeBspNet
  • Loading branch information
snowmead authored Jan 9, 2025
1 parent cbb99ae commit 04f44cc
Showing 1 changed file with 42 additions and 33 deletions.
75 changes: 42 additions & 33 deletions test/suites/integration/bsp/single-volunteer.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import assert, { notEqual, strictEqual } from "node:assert";
import { describeBspNet, shUser, sleep, type EnrichedBspApi } from "../../../util";
import type { H256 } from "@polkadot/types/interfaces";
import type { Bytes, u64, U8aFixed } from "@polkadot/types";

describeBspNet("Single BSP Volunteering", ({ before, createBspApi, it, createUserApi }) => {
let userApi: EnrichedBspApi;
let bspApi: EnrichedBspApi;

const source = "res/whatsup.jpg";
const destination = "test/whatsup.jpg";
const bucketName = "nothingmuch-2";

let file_size: u64;
let fingerprint: U8aFixed;
let location: Bytes;
let bucketId: H256;

before(async () => {
userApi = await createUserApi();
bspApi = await createBspApi();
Expand All @@ -18,38 +29,7 @@ describeBspNet("Single BSP Volunteering", ({ before, createBspApi, it, createUse
strictEqual(bspNodePeerId.toString(), userApi.shConsts.NODE_INFOS.bsp.expectedPeerId);
});

it("file is finger printed correctly", async () => {
const source = "res/adolphus.jpg";
const destination = "test/adolphus.jpg";
const bucketName = "nothingmuch-0";

const newBucketEventEvent = await userApi.createBucket(bucketName);
const newBucketEventDataBlob =
userApi.events.fileSystem.NewBucket.is(newBucketEventEvent) && newBucketEventEvent.data;

if (!newBucketEventDataBlob) {
throw new Error("Event doesn't match Type");
}

const {
file_metadata: { location, fingerprint, file_size }
} = await userApi.rpc.storagehubclient.loadFileInStorage(
source,
destination,
userApi.shConsts.NODE_INFOS.user.AddressId,
newBucketEventDataBlob.bucketId
);

strictEqual(location.toHuman(), destination);
strictEqual(fingerprint.toString(), userApi.shConsts.TEST_ARTEFACTS[source].fingerprint);
strictEqual(file_size.toBigInt(), userApi.shConsts.TEST_ARTEFACTS[source].size);
});

it("bsp volunteers when issueStorageRequest sent", async () => {
const source = "res/whatsup.jpg";
const destination = "test/whatsup.jpg";
const bucketName = "nothingmuch-2";

const initialBspForestRoot = await bspApi.rpc.storagehubclient.getForestRoot(null);

strictEqual(
Expand All @@ -63,15 +43,21 @@ describeBspNet("Single BSP Volunteering", ({ before, createBspApi, it, createUse

assert(newBucketEventDataBlob, "Event doesn't match Type");

bucketId = newBucketEventDataBlob.bucketId;

const {
file_metadata: { location, fingerprint, file_size }
file_metadata: { location: loc, fingerprint: fp, file_size: s }
} = await userApi.rpc.storagehubclient.loadFileInStorage(
source,
destination,
userApi.shConsts.NODE_INFOS.user.AddressId,
newBucketEventDataBlob.bucketId
);

location = loc;
fingerprint = fp;
file_size = s;

await userApi.sealBlock(
userApi.tx.fileSystem.issueStorageRequest(
newBucketEventDataBlob.bucketId,
Expand All @@ -80,7 +66,7 @@ describeBspNet("Single BSP Volunteering", ({ before, createBspApi, it, createUse
file_size,
userApi.shConsts.DUMMY_MSP_ID,
[userApi.shConsts.NODE_INFOS.user.expectedPeerId],
null
1
),
shUser
);
Expand Down Expand Up @@ -156,6 +142,29 @@ describeBspNet("Single BSP Volunteering", ({ before, createBspApi, it, createUse
strictEqual(sha, userApi.shConsts.TEST_ARTEFACTS["res/whatsup.jpg"].checksum);
});
});

it("bsp skips volunteering for the same file key already being stored", async () => {
await userApi.sealBlock(
userApi.tx.fileSystem.issueStorageRequest(
bucketId,
location,
fingerprint,
file_size,
userApi.shConsts.DUMMY_MSP_ID,
[userApi.shConsts.NODE_INFOS.user.expectedPeerId],
1
),
shUser
);

await userApi.assert.eventPresent("fileSystem", "NewStorageRequest");

await bspApi.docker.waitForLog({
containerName: "docker-sh-bsp-1",
searchString: "Skipping file key",
timeout: 15000
});
});
});

describeBspNet("Single BSP multi-volunteers", ({ before, createBspApi, createUserApi, it }) => {
Expand Down

0 comments on commit 04f44cc

Please sign in to comment.