Skip to content

Commit ad5d959

Browse files
committed
fix: uploading using new id
1 parent 3900d13 commit ad5d959

File tree

8 files changed

+85
-55
lines changed

8 files changed

+85
-55
lines changed

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
},
5555
"dependencies": {
5656
"@ethersphere/bee-js": "^3.1.0",
57-
"@fairdatasociety/bmt-js": "^2.0.1",
5857
"@fairdatasociety/fdp-contracts": "^1.0.4",
5958
"crypto-js": "^4.1.1",
6059
"ethers": "^5.5.2",

src/account/account.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ export async function uploadPortableAccount(
123123
const topic = createCredentialsTopic(username, password)
124124
const socWriter = connection.bee.makeSOCWriter(privateKey)
125125

126-
return socWriter.upload(connection.postageBatchId, topic, encryptedBytes, {
127-
pin: true,
128-
})
126+
return socWriter.upload(connection.postageBatchId, topic, encryptedBytes)
129127
}
130128

131129
/**

src/feed/api.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import { getUnixTimestamp } from '../utils/time'
88
import { LookupAnswer } from './types'
99
import { Connection } from '../connection/connection'
1010
import { encryptBytes, PodPasswordBytes } from '../utils/encryption'
11-
import { CHUNK_ALREADY_EXISTS_ERROR, errorStartWith, getError } from '../utils/error'
12-
import { getBmtDataAddress } from '../utils/bytes'
1311

1412
/**
1513
* Finds and downloads the latest feed content
@@ -83,15 +81,8 @@ export async function writeFeedDataRaw(
8381
const topicHash = bmtHashString(topic)
8482
const id = getId(topicHash, epoch.time, epoch.level)
8583
const socWriter = connection.bee.makeSOCWriter(privateKey)
86-
try {
87-
return await socWriter.upload(connection.postageBatchId, id, data, {
88-
pin: true,
89-
})
90-
} catch (e) {
91-
if (errorStartWith(e, CHUNK_ALREADY_EXISTS_ERROR)) {
92-
return getBmtDataAddress(data)
93-
} else {
94-
throw new Error(getError(e)?.message || 'Unknown error')
95-
}
96-
}
84+
85+
return await socWriter.upload(connection.postageBatchId, id, data, {
86+
pin: true,
87+
})
9788
}

src/utils/bytes.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
* generic `Length` type parameter which is runtime compatible with
66
* the original, because it extends from the `number` type.
77
*/
8-
import { Data, Reference, Utils } from '@ethersphere/bee-js'
8+
import { Data, Utils } from '@ethersphere/bee-js'
99
import { bytesToHex } from './hex'
1010
import { BeeArgumentError } from './error'
1111
import CryptoJS from 'crypto-js'
12-
import { makeChunkedFile } from '@fairdatasociety/bmt-js'
1312

1413
export const SPAN_SIZE = 8
15-
export const REFERENCE_SIZE = 64
1614

1715
// we limit the maximum span size in 32 bits to avoid BigInt compatibility issues
1816
const MAX_SPAN_LENGTH = 2 ** 32 - 1
@@ -166,12 +164,3 @@ export function bytesToWordArray(data: Uint8Array): CryptoJS.lib.WordArray {
166164
export function wordArrayToBytes(data: CryptoJS.lib.WordArray): Uint8Array {
167165
return Utils.hexToBytes(CryptoJS.enc.Hex.stringify(data))
168166
}
169-
170-
/**
171-
* Calculates data address without uploading data to Swarm
172-
*/
173-
export function getBmtDataAddress(data: Uint8Array): Reference {
174-
const chunkedFile = makeChunkedFile(data)
175-
176-
return bytesToHex(chunkedFile.address(), REFERENCE_SIZE)
177-
}

test/integration/fdp-class.browser.spec.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -628,25 +628,33 @@ describe('Fair Data Protocol class - in browser', () => {
628628
const filenameSmall = generateRandomHexString() + '.txt'
629629
const fullFilenameSmallPath = '/' + filenameSmall
630630

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

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

640-
await fdp.file.delete(pod, fullFilenameSmallPath)
641-
const list2 = await fdp.directory.read(pod, '/')
641+
let list2
642+
let list3
643+
for (let i = 0; i < reuploadTimes; i++) {
644+
await fdp.file.delete(pod, fullFilenameSmallPath)
645+
list2 = await fdp.directory.read(pod, '/')
642646

643-
await fdp.file.uploadData(pod, fullFilenameSmallPath, contentSmall)
644-
const list3 = await fdp.directory.read(pod, '/')
647+
await fdp.file.uploadData(pod, fullFilenameSmallPath, contentSmall)
648+
list3 = await fdp.directory.read(pod, '/')
649+
}
650+
651+
const data1 = await fdp.file.downloadData(pod, fullFilenameSmallPath)
645652

646653
return {
647654
listFiles1: list1.getFiles(),
648-
listFiles2: list2.getFiles(),
649-
listFiles3: list3.getFiles(),
655+
listFiles2: list2?.getFiles(),
656+
listFiles3: list3?.getFiles(),
657+
data1: data1.text(),
650658
}
651659
},
652660
pod,
@@ -658,8 +666,10 @@ describe('Fair Data Protocol class - in browser', () => {
658666
expect(listFiles1).toHaveLength(1)
659667
expect(listFiles2).toHaveLength(0)
660668
expect(listFiles3).toHaveLength(1)
669+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
670+
// @ts-ignore
661671
expect(listFiles3[0].name).toEqual(filenameSmall)
662-
expect(listFiles1[0].reference).toEqual(listFiles3[0].reference)
672+
expect(data1).toEqual(contentSmall)
663673
})
664674

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

test/integration/fdp-class.fairos.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ describe('Fair Data Protocol with FairOS-dfs', () => {
411411
const user = generateUser(fdp)
412412
const podName1 = generateRandomHexString()
413413
const fileSizeBig = 1000015
414+
const fileSizeBig2 = 1000017
414415
const contentBig = generateRandomHexString(fileSizeBig)
416+
const contentBig2 = generateRandomHexString(fileSizeBig2)
415417
const filenameBig = generateRandomHexString() + '.txt'
416418
const fullFilenameBigPath = '/' + filenameBig
417419

@@ -431,6 +433,27 @@ describe('Fair Data Protocol with FairOS-dfs', () => {
431433
const response2 = await fairos.dirLs(podName1)
432434
const dirs2 = response2.data?.files
433435
expect(dirs2).toBeUndefined()
436+
437+
// upload the same file again under the same name
438+
await fdp.file.uploadData(podName1, fullFilenameBigPath, contentBig)
439+
const response3 = await fairos.dirLs(podName1)
440+
const dirs3 = response3?.data?.files
441+
expect(dirs3).toHaveLength(1)
442+
expect(dirs3[0].name).toEqual(filenameBig)
443+
444+
// upload other file again under the same name
445+
await fdp.file.delete(podName1, fullFilenameBigPath)
446+
await fdp.file.uploadData(podName1, fullFilenameBigPath, contentBig2)
447+
const response4 = await fairos.dirLs(podName1)
448+
const dirs4 = response4?.data?.files
449+
expect(dirs4).toHaveLength(1)
450+
expect(dirs4[0].name).toEqual(filenameBig)
451+
expect(Number(dirs4[0].size)).toEqual(fileSizeBig2)
452+
453+
// check new file content
454+
const response5 = await fairos.fileDownload(podName1, fullFilenameBigPath)
455+
expect(response5.status).toEqual(200)
456+
expect(response5.data).toEqual(contentBig2)
434457
})
435458

436459
it('should delete file in fairos and it will disappear in fdp', async () => {

test/integration/fdp-class.spec.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ describe('Fair Data Protocol class', () => {
424424

425425
describe('File', () => {
426426
it('should upload the same file after deletion', async () => {
427+
const reuploadTimes = 3
427428
const fdp = createFdp()
428429
generateUser(fdp)
429430
const pod = generateRandomHexString()
@@ -437,15 +438,45 @@ describe('Fair Data Protocol class', () => {
437438
const list1 = await fdp.directory.read(pod, '/')
438439
expect(list1.getFiles()).toHaveLength(1)
439440

440-
await fdp.file.delete(pod, fullFilenameSmallPath)
441-
const list2 = await fdp.directory.read(pod, '/')
442-
expect(list2.getFiles()).toHaveLength(0)
441+
for (let i = 0; i < reuploadTimes; i++) {
442+
await fdp.file.delete(pod, fullFilenameSmallPath)
443+
const list2 = await fdp.directory.read(pod, '/')
444+
expect(list2.getFiles()).toHaveLength(0)
445+
446+
await fdp.file.uploadData(pod, fullFilenameSmallPath, contentSmall)
447+
const list3 = await fdp.directory.read(pod, '/')
448+
expect(list3.getFiles()).toHaveLength(1)
449+
expect(list3.getFiles()[0].name).toEqual(filenameSmall)
450+
const data1 = await fdp.file.downloadData(pod, fullFilenameSmallPath)
451+
expect(data1.text()).toEqual(contentSmall)
452+
}
453+
})
443454

455+
it('should replace file with different content', async () => {
456+
const reuploadTimes = 3
457+
const fdp = createFdp()
458+
generateUser(fdp)
459+
const pod = generateRandomHexString()
460+
const fileSizeSmall = 100
461+
const contentSmall = generateRandomHexString(fileSizeSmall)
462+
const filenameSmall = generateRandomHexString() + '.txt'
463+
const fullFilenameSmallPath = '/' + filenameSmall
464+
465+
await fdp.personalStorage.create(pod)
444466
await fdp.file.uploadData(pod, fullFilenameSmallPath, contentSmall)
445-
const list3 = await fdp.directory.read(pod, '/')
446-
expect(list3.getFiles()).toHaveLength(1)
447-
expect(list3.getFiles()[0].name).toEqual(filenameSmall)
448-
expect(list1.getFiles()[0].reference).toEqual(list3.getFiles()[0].reference)
467+
const list1 = await fdp.directory.read(pod, '/')
468+
expect(list1.getFiles()).toHaveLength(1)
469+
470+
for (let i = 0; i < reuploadTimes; i++) {
471+
await fdp.file.delete(pod, fullFilenameSmallPath)
472+
const list2 = await fdp.directory.read(pod, '/')
473+
expect(list2.getFiles()).toHaveLength(0)
474+
475+
const newContent = generateRandomHexString(fileSizeSmall)
476+
await fdp.file.uploadData(pod, fullFilenameSmallPath, newContent)
477+
const data1 = await fdp.file.downloadData(pod, fullFilenameSmallPath)
478+
expect(data1.text()).toEqual(newContent)
479+
}
449480
})
450481

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

0 commit comments

Comments
 (0)