Skip to content

Commit

Permalink
Merge pull request #53 from textileio/asutula/powergate-0.2
Browse files Browse the repository at this point in the history
Update to Powergate 0.2.x, API updates
  • Loading branch information
asutula authored Jul 21, 2020
2 parents 9618727 + 5ce7a56 commit 83b76e6
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 143 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"lint": "eslint --fix --ext .js,.ts src/**/*",
"docs": "typedoc",
"localnet:up": "cd powergate-docker && make localnet",
"localnet:down": "cd powergate-docker && make down",
"localnet:down": "cd powergate-docker && make localnet-down",
"powergate-docker": "./powergate-docker.sh"
},
"keywords": [
Expand Down Expand Up @@ -65,6 +65,6 @@
},
"dependencies": {
"@improbable-eng/grpc-web-node-http-transport": "^0.12.0",
"@textile/grpc-powergate-client": "0.1.1"
"@textile/grpc-powergate-client": "0.2.0"
}
}
}
6 changes: 3 additions & 3 deletions src/asks/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ describe("asks", () => {
const c = createAsks({ host, transport: getTransport() })

it("should get", async () => {
const index = await c.get()
const { index } = await c.get()
expect(index).not.undefined
})

it("should query", async () => {
const q = new asksTypes.Query().toObject()
const res = await c.query(q)
expect(res).not.undefined
const { asksList } = await c.query(q)
expect(asksList).not.undefined
})
})
4 changes: 2 additions & 2 deletions src/asks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const createAsks = (config: Config) => {
get: () =>
promise(
(cb) => client.get(new asksTypes.GetRequest(), cb),
(resp: asksTypes.GetResponse) => resp.toObject().index,
(resp: asksTypes.GetResponse) => resp.toObject(),
),

/**
Expand All @@ -32,7 +32,7 @@ export const createAsks = (config: Config) => {
req.setQuery(queryObjectToMsg(query))
return promise(
(cb) => client.query(req, cb),
(resp: asksTypes.QueryResponse) => resp.toObject().asksList,
(resp: asksTypes.QueryResponse) => resp.toObject(),
)
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/faults/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("faults", () => {
const c = createFaults({ host, transport: getTransport() })

it("should get", async () => {
const index = await c.get()
const { index } = await c.get()
expect(index).not.undefined
})
})
2 changes: 1 addition & 1 deletion src/faults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const createFaults = (config: Config) => {
get: () =>
promise(
(cb) => client.get(new faultsTypes.GetRequest(), cb),
(resp: faultsTypes.GetResponse) => resp.toObject().index,
(resp: faultsTypes.GetResponse) => resp.toObject(),
),
}
}
172 changes: 105 additions & 67 deletions src/ffs/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { createFFS } from "."
import { ffsTypes } from "../types"
import { getTransport, host, useToken } from "../util"
import {
PushConfigOption,
withConfig,
PushStorageConfigOption,
withAscending,
withDataCids,
withFromAddresses,
withHistory,
withIncludeFinal,
withOverrideConfig,
withIncludePending,
withOverride,
withStorageConfig,
} from "./options"

describe("ffs", function () {
Expand Down Expand Up @@ -41,7 +45,7 @@ describe("ffs", function () {

it("should get the default config", async () => {
await expectNewInstance()
await expectDefaultConfig()
await expectDefaultStorageConfig()
})

it("should create a new addr", async () => {
Expand All @@ -53,8 +57,8 @@ describe("ffs", function () {

it("should set default config", async () => {
await expectNewInstance()
const defaultConfig = await expectDefaultConfig()
await c.setDefaultConfig(defaultConfig)
const defaultConfig = await expectDefaultStorageConfig()
await c.setDefaultStorageConfig(defaultConfig)
})

it("should get info", async () => {
Expand All @@ -65,35 +69,29 @@ describe("ffs", function () {

it("should add to hot", async () => {
await expectNewInstance()
await expectAddToHot("sample-data/samplefile")
})

it("should get default cid config", async () => {
await expectNewInstance()
const cid = await expectAddToHot("sample-data/samplefile")
await expectDefaultCidConfig(cid)
await expectStage("sample-data/samplefile")
})

it("should push config", async () => {
await expectNewInstance()
const cid = await expectAddToHot("sample-data/samplefile")
const config = await expectDefaultCidConfig(cid)
await expectPushConfig(cid, false, config)
const cid = await expectStage("sample-data/samplefile")
const config = await expectDefaultStorageConfig()
await expectPushStorageConfig(cid, false, config)
})

it("should watch job", async () => {
await expectNewInstance()
const addrs = await expectAddrs(1)
await waitForBalance(addrs[0].addr, 0)
const cid = await expectAddToHot("sample-data/samplefile")
const jobId = await expectPushConfig(cid)
const cid = await expectStage("sample-data/samplefile")
const jobId = await expectPushStorageConfig(cid)
await waitForJobStatus(jobId, ffsTypes.JobStatus.JOB_STATUS_SUCCESS)
})

it("should watch logs", async () => {
await expectNewInstance()
const cid = await expectAddToHot("sample-data/samplefile")
await expectPushConfig(cid)
const cid = await expectStage("sample-data/samplefile")
await expectPushStorageConfig(cid)
await new Promise<void>((resolve, reject) => {
const cancel = c.watchLogs(
(logEvent) => {
Expand All @@ -111,35 +109,84 @@ describe("ffs", function () {
})
})

it("should get a storage deal record", async () => {
it("should get storage deal records", async () => {
await expectNewInstance()
const addrs = await expectAddrs(1)
await waitForBalance(addrs[0].addr, 0)
const cid = await expectAddToHot("sample-data/samplefile")
const jobId = await expectPushConfig(cid)
await waitForJobStatus(jobId, ffsTypes.JobStatus.JOB_STATUS_SUCCESS)
const res = await c.listStorageDealRecords(withIncludeFinal(true))
expect(res).length.greaterThan(0)

const [cid1, cid2, cid3] = await Promise.all([
expectStage("sample-data/samplefile"),
expectStage("sample-data/samplefile2"),
expectStage("sample-data/samplefile3"),
])

const jobId1 = await expectPushStorageConfig(cid1)
await new Promise((r) => setTimeout(r, 1000))
const jobId2 = await expectPushStorageConfig(cid2)
await new Promise((r) => setTimeout(r, 1000))
const jobId3 = await expectPushStorageConfig(cid3)

await Promise.all([
waitForJobStatus(jobId1, ffsTypes.JobStatus.JOB_STATUS_EXECUTING),
waitForJobStatus(jobId2, ffsTypes.JobStatus.JOB_STATUS_EXECUTING),
waitForJobStatus(jobId3, ffsTypes.JobStatus.JOB_STATUS_EXECUTING),
])

// wait for a second so some async work of actually starting a deal happens
await new Promise((r) => setTimeout(r, 1000))

const { recordsList: pendingRecords } = await c.listStorageDealRecords(
withIncludePending(true),
withAscending(true),
withFromAddresses(addrs[0].addr),
withDataCids(cid1, cid2, cid3),
)
expect(pendingRecords, "pending length").length(3)
expect(pendingRecords[0].time).lessThan(pendingRecords[1].time).lessThan(pendingRecords[2].time)

const { recordsList: finalRecords } = await c.listStorageDealRecords(withIncludeFinal(true))
expect(finalRecords, "final empty").empty

await Promise.all([
waitForJobStatus(jobId1, ffsTypes.JobStatus.JOB_STATUS_SUCCESS),
waitForJobStatus(jobId2, ffsTypes.JobStatus.JOB_STATUS_SUCCESS),
waitForJobStatus(jobId3, ffsTypes.JobStatus.JOB_STATUS_SUCCESS),
])

const { recordsList: pendingRecords2 } = await c.listStorageDealRecords(
withIncludePending(true),
)
expect(pendingRecords2, "pending2 empty").empty

const { recordsList: finalRecords2 } = await c.listStorageDealRecords(
withIncludeFinal(true),
withAscending(true),
withFromAddresses(addrs[0].addr),
withDataCids(cid1, cid2, cid3),
)
expect(finalRecords2, "final2 length").length(3)
expect(finalRecords2[0].time).lessThan(finalRecords2[1].time).lessThan(finalRecords2[2].time)
})

it("should get a retrieval deal record", async () => {
// ToDo: Figure out hot to make sure the data in the previous push isn't cached in hot
})

it("should get cid config", async () => {
it("should get storage config", async () => {
await expectNewInstance()
const cid = await expectAddToHot("sample-data/samplefile")
await expectPushConfig(cid)
const res = await c.getCidConfig(cid)
expect(res.config?.cid).equal(cid)
const cid = await expectStage("sample-data/samplefile")
await expectPushStorageConfig(cid)
const res = await c.getStorageConfig(cid)
expect(res.config?.cold).not.undefined
expect(res.config?.hot).not.undefined
})

it("should show", async () => {
await expectNewInstance()
const addrs = await expectAddrs(1)
await waitForBalance(addrs[0].addr, 0)
const cid = await expectAddToHot("sample-data/samplefile")
const jobId = await expectPushConfig(cid)
const cid = await expectStage("sample-data/samplefile")
const jobId = await expectPushStorageConfig(cid)
await waitForJobStatus(jobId, ffsTypes.JobStatus.JOB_STATUS_SUCCESS)
const res = await c.show(cid)
expect(res.cidInfo).not.undefined
Expand All @@ -149,21 +196,21 @@ describe("ffs", function () {
await expectNewInstance()
const addrs = await expectAddrs(1)
await waitForBalance(addrs[0].addr, 0)
const cid = await expectAddToHot("sample-data/samplefile")
const jobId = await expectPushConfig(cid)
const cid = await expectStage("sample-data/samplefile")
const jobId = await expectPushStorageConfig(cid)
await waitForJobStatus(jobId, ffsTypes.JobStatus.JOB_STATUS_SUCCESS)
const res = await c.showAll()
expect(res).not.empty
expect(res.cidInfosList).not.empty
})

it("should replace", async () => {
await expectNewInstance()
const addrs = await expectAddrs(1)
await waitForBalance(addrs[0].addr, 0)
const cid = await expectAddToHot("sample-data/samplefile")
const jobId = await expectPushConfig(cid)
const cid = await expectStage("sample-data/samplefile")
const jobId = await expectPushStorageConfig(cid)
await waitForJobStatus(jobId, ffsTypes.JobStatus.JOB_STATUS_SUCCESS)
const cid2 = await expectAddToHot("sample-data/samplefile2")
const cid2 = await expectStage("sample-data/samplefile2")
const res = await c.replace(cid, cid2)
expect(res.jobId).length.greaterThan(0)
})
Expand All @@ -172,17 +219,17 @@ describe("ffs", function () {
await expectNewInstance()
const addrs = await expectAddrs(1)
await waitForBalance(addrs[0].addr, 0)
const cid = await expectAddToHot("sample-data/samplefile")
const jobId = await expectPushConfig(cid)
const cid = await expectStage("sample-data/samplefile")
const jobId = await expectPushStorageConfig(cid)
await waitForJobStatus(jobId, ffsTypes.JobStatus.JOB_STATUS_SUCCESS)
const bytes = await c.get(cid)
expect(bytes.byteLength).greaterThan(0)
})

it("should cancel a job", async () => {
await expectNewInstance()
const cid = await expectAddToHot("sample-data/samplefile")
const jobId = await expectPushConfig(cid)
const cid = await expectStage("sample-data/samplefile")
const jobId = await expectPushStorageConfig(cid)
await c.cancelJob(jobId)
})

Expand All @@ -200,9 +247,8 @@ describe("ffs", function () {

it("should remove", async () => {
await expectNewInstance()
const cid = await expectAddToHot("sample-data/samplefile")
const conf: ffsTypes.CidConfig.AsObject = {
cid,
const cid = await expectStage("sample-data/samplefile")
const conf: ffsTypes.StorageConfig.AsObject = {
repairable: false,
cold: {
enabled: false,
Expand All @@ -212,7 +258,7 @@ describe("ffs", function () {
enabled: false,
},
}
const jobId = await expectPushConfig(cid, false, conf)
const jobId = await expectPushStorageConfig(cid, false, conf)
await waitForJobStatus(jobId, ffsTypes.JobStatus.JOB_STATUS_SUCCESS)
await c.remove(cid)
})
Expand Down Expand Up @@ -250,39 +296,31 @@ describe("ffs", function () {
return res.addr
}

async function expectDefaultConfig() {
const res = await c.defaultConfig()
expect(res.defaultConfig).not.undefined
async function expectDefaultStorageConfig() {
const res = await c.defaultStorageConfig()
expect(res.defaultStorageConfig).not.undefined
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return res.defaultConfig!
return res.defaultStorageConfig!
}

async function expectAddToHot(path: string) {
async function expectStage(path: string) {
const buffer = fs.readFileSync(path)
const res = await c.addToHot(buffer)
const res = await c.stage(buffer)
expect(res.cid).length.greaterThan(0)
return res.cid
}

async function expectDefaultCidConfig(cid: string) {
const res = await c.getDefaultCidConfig(cid)
expect(res.config).not.undefined
expect(res.config?.cid).equal(cid)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return res.config!
}

async function expectPushConfig(
async function expectPushStorageConfig(
cid: string,
override: boolean = false,
config?: ffsTypes.CidConfig.AsObject,
config?: ffsTypes.StorageConfig.AsObject,
) {
const opts: PushConfigOption[] = []
opts.push(withOverrideConfig(override))
const opts: PushStorageConfigOption[] = []
opts.push(withOverride(override))
if (config) {
opts.push(withConfig(config))
opts.push(withStorageConfig(config))
}
const res = await c.pushConfig(cid, ...opts)
const res = await c.pushStorageConfig(cid, ...opts)
expect(res.jobId).length.greaterThan(0)
return res.jobId
}
Expand Down
Loading

0 comments on commit 83b76e6

Please sign in to comment.