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

feat: enforce minimum block size for data upload #279

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion src/content-items/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ import { PodPasswordBytes } from '../utils/encryption'
import { DataUploadOptions } from '../file/types'
import { getNextEpoch } from '../feed/lookup/utils'

/**
* Minimum block size for uploading data
*/
export const MINIMUM_BLOCK_SIZE = 1000000

export const DEFAULT_UPLOAD_OPTIONS: DataUploadOptions = {
blockSize: 1000000,
blockSize: MINIMUM_BLOCK_SIZE,
contentType: '',
}

Expand Down
5 changes: 3 additions & 2 deletions src/file/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stringToBytes, wrapBytesWithHelpers } from '../utils/bytes'
import { assertMinLength, stringToBytes, wrapBytesWithHelpers } from '../utils/bytes'
import { Bee, Data, BeeRequestOptions } from '@ethersphere/bee-js'
import { EthAddress } from '@ethersphere/bee-js/dist/types/utils/eth'
import {
Expand Down Expand Up @@ -33,7 +33,7 @@ import {
} from './types'
import { assertPodName, getExtendedPodsListByAccountData, META_VERSION } from '../pod/utils'
import { getUnixTimestamp } from '../utils/time'
import { addEntryToDirectory, DEFAULT_UPLOAD_OPTIONS } from '../content-items/handler'
import { addEntryToDirectory, DEFAULT_UPLOAD_OPTIONS, MINIMUM_BLOCK_SIZE } from '../content-items/handler'
import { writeFeedData } from '../feed/api'
import { AccountData } from '../account/account-data'
import { prepareEthAddress } from '../utils/wallet'
Expand Down Expand Up @@ -191,6 +191,7 @@ export async function uploadData(
assertWallet(accountData.wallet)

const blockSize = options.blockSize ?? Number(DEFAULT_UPLOAD_OPTIONS!.blockSize)
assertMinLength(blockSize, MINIMUM_BLOCK_SIZE, `Block size is too small. Minimum is ${MINIMUM_BLOCK_SIZE} bytes.`)
const contentType = options.contentType ?? String(DEFAULT_UPLOAD_OPTIONS!.contentType)
const connection = accountData.connection
updateUploadProgress(options, UploadProgressType.GetPodInfo)
Expand Down
12 changes: 12 additions & 0 deletions src/utils/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ export function wordArrayToBytes(data: CryptoJS.lib.WordArray): Uint8Array {
return Utils.hexToBytes(CryptoJS.enc.Hex.stringify(data))
}

/**
* Asserts that length is greater than or equal to min length
* @param currentLength current length
* @param minLength min length
* @param customMessage custom error message
*/
export function assertMinLength(currentLength: number, minLength: number, customMessage?: string): void {
if (currentLength < minLength) {
throw new Error(customMessage ? customMessage : `length ${currentLength} is less than min length ${minLength}`)
}
}

/**
* Asserts that length is less than or equal to max length
* @param currentLength currentLength
Expand Down
6 changes: 6 additions & 0 deletions test/integration/node/upload-by-index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createFdp, generateRandomHexString, generateUser, makeFileContent } from '../../utils'
import { wrapBytesWithHelpers } from '../../../src/utils/bytes'
import { getDataBlock } from '../../../src'
import { MINIMUM_BLOCK_SIZE } from '../../../src/content-items/handler'

jest.setTimeout(400000)
it('Upload by index', async () => {
Expand All @@ -24,6 +25,11 @@ it('Upload by index', async () => {
await expect(fdp.file.uploadData(pod, fullPath, mixedBlocks)).rejects.toThrow(
'The sequence of `ExternalDataBlock` is not correctly indexed.',
)
await expect(
fdp.file.uploadData(pod, fullPath, mixedBlocks, {
blockSize: 100,
}),
).rejects.toThrow(`Block size is too small. Minimum is ${MINIMUM_BLOCK_SIZE} bytes.`)

const fileMeta = await fdp.file.uploadData(pod, fullPath, blocks)
expect(fileMeta.fileName).toEqual(filename)
Expand Down
Loading