Skip to content

Commit

Permalink
fix: uploading using new id
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorShadurin committed Jan 19, 2023
1 parent b4ec4d1 commit fd584fa
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 57 deletions.
11 changes: 0 additions & 11 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
},
"dependencies": {
"@ethersphere/bee-js": "^3.1.0",
"@fairdatasociety/bmt-js": "^2.0.1",
"@fairdatasociety/fdp-contracts": "^1.0.4",
"crypto-js": "^4.1.1",
"ethers": "^5.5.2",
Expand Down
4 changes: 1 addition & 3 deletions src/account/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ export async function uploadPortableAccount(
const topic = createCredentialsTopic(username, password)
const socWriter = connection.bee.makeSOCWriter(privateKey)

return socWriter.upload(connection.postageBatchId, topic, encryptedBytes, {
pin: true,
})
return socWriter.upload(connection.postageBatchId, topic, encryptedBytes)
}

/**
Expand Down
17 changes: 4 additions & 13 deletions src/feed/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { getUnixTimestamp } from '../utils/time'
import { LookupAnswer } from './types'
import { Connection } from '../connection/connection'
import { encryptBytes, PodPasswordBytes } from '../utils/encryption'
import { CHUNK_ALREADY_EXISTS_ERROR, errorStartWith, getError } from '../utils/error'
import { getBmtDataAddress } from '../utils/bytes'

/**
* Finds and downloads the latest feed content
Expand Down Expand Up @@ -83,15 +81,8 @@ export async function writeFeedDataRaw(
const topicHash = bmtHashString(topic)
const id = getId(topicHash, epoch.time, epoch.level)
const socWriter = connection.bee.makeSOCWriter(privateKey)
try {
return await socWriter.upload(connection.postageBatchId, id, data, {
pin: true,
})
} catch (e) {
if (errorStartWith(e, CHUNK_ALREADY_EXISTS_ERROR)) {
return getBmtDataAddress(data)
} else {
throw new Error(getError(e)?.message || 'Unknown error')
}
}

return await socWriter.upload(connection.postageBatchId, id, data, {
pin: true,
})
}
19 changes: 17 additions & 2 deletions src/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
updateFileMetadata,
uploadBytes,
} from './utils'
import { writeFeedData } from '../feed/api'
import { getFeedData, writeFeedData } from '../feed/api'
import { downloadData } from './handler'
import { blocksToManifest, getFileMetadataRawBytes, rawFileMetadataToFileMetadata } from './adapter'
import { Blocks, DataUploadOptions, FileReceiveOptions, FileShareInfo } from './types'
Expand All @@ -21,6 +21,7 @@ import { Data, Reference } from '@ethersphere/bee-js'
import { getRawMetadata } from '../content-items/utils'
import { assertRawFileMetadata, combine } from '../directory/utils'
import { assertEncryptedReference, EncryptedReference } from '../utils/hex'
import { prepareEthAddress } from '../utils/wallet'

/**
* Files management class
Expand Down Expand Up @@ -108,8 +109,22 @@ export class File {
blocksReference,
}

let nextEpoch
try {
const feedData = await getFeedData(connection.bee, fullPath, prepareEthAddress(podWallet.address))
feedData.epoch.level = feedData.epoch.getNextLevel(feedData.epoch.time)
nextEpoch = feedData.epoch
// eslint-disable-next-line no-empty
} catch (e) {}
await addEntryToDirectory(connection, podWallet, pod.password, pathInfo.path, pathInfo.filename, true)
await writeFeedData(connection, fullPath, getFileMetadataRawBytes(meta), podWallet.privateKey, pod.password)
await writeFeedData(
connection,
fullPath,
getFileMetadataRawBytes(meta),
podWallet.privateKey,
pod.password,
nextEpoch,
)

return meta
}
Expand Down
13 changes: 1 addition & 12 deletions src/utils/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
* generic `Length` type parameter which is runtime compatible with
* the original, because it extends from the `number` type.
*/
import { Data, Reference, Utils } from '@ethersphere/bee-js'
import { Data, Utils } from '@ethersphere/bee-js'
import { bytesToHex } from './hex'
import { BeeArgumentError } from './error'
import CryptoJS from 'crypto-js'
import { makeChunkedFile } from '@fairdatasociety/bmt-js'

export const SPAN_SIZE = 8
export const REFERENCE_SIZE = 64

// we limit the maximum span size in 32 bits to avoid BigInt compatibility issues
const MAX_SPAN_LENGTH = 2 ** 32 - 1
Expand Down Expand Up @@ -166,12 +164,3 @@ export function bytesToWordArray(data: Uint8Array): CryptoJS.lib.WordArray {
export function wordArrayToBytes(data: CryptoJS.lib.WordArray): Uint8Array {
return Utils.hexToBytes(CryptoJS.enc.Hex.stringify(data))
}

/**
* Calculates data address without uploading data to Swarm
*/
export function getBmtDataAddress(data: Uint8Array): Reference {
const chunkedFile = makeChunkedFile(data)

return bytesToHex(chunkedFile.address(), REFERENCE_SIZE)
}
26 changes: 18 additions & 8 deletions test/integration/fdp-class.browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,25 +628,33 @@ describe('Fair Data Protocol class - in browser', () => {
const filenameSmall = generateRandomHexString() + '.txt'
const fullFilenameSmallPath = '/' + filenameSmall

const { listFiles1, listFiles2, listFiles3 } = await page.evaluate(
const { listFiles1, listFiles2, listFiles3, data1 } = await page.evaluate(
async (pod: string, filenameSmall: string, fullFilenameSmallPath: string, contentSmall: string) => {
const reuploadTimes = 3
const fdp = eval(await window.initFdp()) as FdpStorage
fdp.account.createWallet()

await fdp.personalStorage.create(pod)
await fdp.file.uploadData(pod, fullFilenameSmallPath, contentSmall)
const list1 = await fdp.directory.read(pod, '/')

await fdp.file.delete(pod, fullFilenameSmallPath)
const list2 = await fdp.directory.read(pod, '/')
let list2
let list3
for (let i = 0; i < reuploadTimes; i++) {
await fdp.file.delete(pod, fullFilenameSmallPath)
list2 = await fdp.directory.read(pod, '/')

await fdp.file.uploadData(pod, fullFilenameSmallPath, contentSmall)
const list3 = await fdp.directory.read(pod, '/')
await fdp.file.uploadData(pod, fullFilenameSmallPath, contentSmall)
list3 = await fdp.directory.read(pod, '/')
}

const data1 = await fdp.file.downloadData(pod, fullFilenameSmallPath)

return {
listFiles1: list1.getFiles(),
listFiles2: list2.getFiles(),
listFiles3: list3.getFiles(),
listFiles2: list2?.getFiles(),
listFiles3: list3?.getFiles(),
data1: data1.text(),
}
},
pod,
Expand All @@ -658,8 +666,10 @@ describe('Fair Data Protocol class - in browser', () => {
expect(listFiles1).toHaveLength(1)
expect(listFiles2).toHaveLength(0)
expect(listFiles3).toHaveLength(1)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(listFiles3[0].name).toEqual(filenameSmall)
expect(listFiles1[0].reference).toEqual(listFiles3[0].reference)
expect(data1).toEqual(contentSmall)
})

it('should upload small text data as a file', async () => {
Expand Down
23 changes: 23 additions & 0 deletions test/integration/fdp-class.fairos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ describe('Fair Data Protocol with FairOS-dfs', () => {
const user = generateUser(fdp)
const podName1 = generateRandomHexString()
const fileSizeBig = 1000015
const fileSizeBig2 = 1000017
const contentBig = generateRandomHexString(fileSizeBig)
const contentBig2 = generateRandomHexString(fileSizeBig2)
const filenameBig = generateRandomHexString() + '.txt'
const fullFilenameBigPath = '/' + filenameBig

Expand All @@ -431,6 +433,27 @@ describe('Fair Data Protocol with FairOS-dfs', () => {
const response2 = await fairos.dirLs(podName1)
const dirs2 = response2.data?.files
expect(dirs2).toBeUndefined()

// upload the same file again under the same name
await fdp.file.uploadData(podName1, fullFilenameBigPath, contentBig)
const response3 = await fairos.dirLs(podName1)
const dirs3 = response3?.data?.files
expect(dirs3).toHaveLength(1)
expect(dirs3[0].name).toEqual(filenameBig)

// upload other file again under the same name
await fdp.file.delete(podName1, fullFilenameBigPath)
await fdp.file.uploadData(podName1, fullFilenameBigPath, contentBig2)
const response4 = await fairos.dirLs(podName1)
const dirs4 = response4?.data?.files
expect(dirs4).toHaveLength(1)
expect(dirs4[0].name).toEqual(filenameBig)
expect(Number(dirs4[0].size)).toEqual(fileSizeBig2)

// check new file content
const response5 = await fairos.fileDownload(podName1, fullFilenameBigPath)
expect(response5.status).toEqual(200)
expect(response5.data).toEqual(contentBig2)
})

it('should delete file in fairos and it will disappear in fdp', async () => {
Expand Down
45 changes: 38 additions & 7 deletions test/integration/fdp-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ describe('Fair Data Protocol class', () => {

describe('File', () => {
it('should upload the same file after deletion', async () => {
const reuploadTimes = 3
const fdp = createFdp()
generateUser(fdp)
const pod = generateRandomHexString()
Expand All @@ -371,15 +372,45 @@ describe('Fair Data Protocol class', () => {
const list1 = await fdp.directory.read(pod, '/')
expect(list1.getFiles()).toHaveLength(1)

await fdp.file.delete(pod, fullFilenameSmallPath)
const list2 = await fdp.directory.read(pod, '/')
expect(list2.getFiles()).toHaveLength(0)
for (let i = 0; i < reuploadTimes; i++) {
await fdp.file.delete(pod, fullFilenameSmallPath)
const list2 = await fdp.directory.read(pod, '/')
expect(list2.getFiles()).toHaveLength(0)

await fdp.file.uploadData(pod, fullFilenameSmallPath, contentSmall)
const list3 = await fdp.directory.read(pod, '/')
expect(list3.getFiles()).toHaveLength(1)
expect(list3.getFiles()[0].name).toEqual(filenameSmall)
const data1 = await fdp.file.downloadData(pod, fullFilenameSmallPath)
expect(data1.text()).toEqual(contentSmall)
}
})

it('should replace file with different content', async () => {
const reuploadTimes = 3
const fdp = createFdp()
generateUser(fdp)
const pod = generateRandomHexString()
const fileSizeSmall = 100
const contentSmall = generateRandomHexString(fileSizeSmall)
const filenameSmall = generateRandomHexString() + '.txt'
const fullFilenameSmallPath = '/' + filenameSmall

await fdp.personalStorage.create(pod)
await fdp.file.uploadData(pod, fullFilenameSmallPath, contentSmall)
const list3 = await fdp.directory.read(pod, '/')
expect(list3.getFiles()).toHaveLength(1)
expect(list3.getFiles()[0].name).toEqual(filenameSmall)
expect(list1.getFiles()[0].reference).toEqual(list3.getFiles()[0].reference)
const list1 = await fdp.directory.read(pod, '/')
expect(list1.getFiles()).toHaveLength(1)

for (let i = 0; i < reuploadTimes; i++) {
await fdp.file.delete(pod, fullFilenameSmallPath)
const list2 = await fdp.directory.read(pod, '/')
expect(list2.getFiles()).toHaveLength(0)

const newContent = generateRandomHexString(fileSizeSmall)
await fdp.file.uploadData(pod, fullFilenameSmallPath, newContent)
const data1 = await fdp.file.downloadData(pod, fullFilenameSmallPath)
expect(data1.text()).toEqual(newContent)
}
})

it('should upload small text data as a file', async () => {
Expand Down

0 comments on commit fd584fa

Please sign in to comment.