Skip to content

Commit 8e8f878

Browse files
bkonturclaude
andcommitted
fix(sdk): consolidate MAX_CHUNK_SIZE to 2 MiB per Bitswap limit
- Define MAX_CHUNK_SIZE once in chunker module (2 MiB) - Import constant in utils instead of redefining locally - Removes inconsistent 4 MiB and 8 MiB definitions - Aligns with Bitswap protocol constraints Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent e5902e7 commit 8e8f878

File tree

4 files changed

+10
-14
lines changed

4 files changed

+10
-14
lines changed

sdk/rust/src/chunker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ extern crate alloc;
88
use crate::types::{Chunk, ChunkerConfig, Error, Result};
99
use alloc::vec::Vec;
1010

11-
/// Maximum chunk size allowed (8 MiB, matches pallet limit).
12-
pub const MAX_CHUNK_SIZE: usize = 8 * 1024 * 1024;
11+
/// Maximum chunk size allowed (2 MiB, matches Bitswap limit).
12+
pub const MAX_CHUNK_SIZE: usize = 2 * 1024 * 1024;
1313

1414
/// Default chunk size (1 MiB).
1515
pub const DEFAULT_CHUNK_SIZE: usize = 1024 * 1024;

sdk/rust/src/utils.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! Utility functions and helpers for Bulletin SDK
55
66
use crate::{
7+
chunker::MAX_CHUNK_SIZE,
78
cid::ContentHash,
89
types::{CidCodec, Error, HashAlgorithm, Result},
910
};
@@ -185,16 +186,14 @@ where
185186
/// use bulletin_sdk_rust::utils::validate_chunk_size;
186187
///
187188
/// assert!(validate_chunk_size(1_048_576).is_ok()); // 1 MiB - valid
188-
/// assert!(validate_chunk_size(10_000_000).is_err()); // > 8 MiB - invalid
189+
/// assert!(validate_chunk_size(3_000_000).is_err()); // > 2 MiB - invalid
189190
/// ```
190191
pub fn validate_chunk_size(size: u64) -> Result<()> {
191-
const MAX_CHUNK_SIZE: u64 = 8 * 1024 * 1024; // 8 MiB
192-
193192
if size == 0 {
194193
return Err(Error::InvalidConfig("Chunk size cannot be zero".into()))
195194
}
196195

197-
if size > MAX_CHUNK_SIZE {
196+
if size > MAX_CHUNK_SIZE as u64 {
198197
return Err(Error::InvalidConfig(alloc::format!(
199198
"Chunk size {} exceeds maximum {}",
200199
size,
@@ -218,7 +217,6 @@ pub fn validate_chunk_size(size: u64) -> Result<()> {
218217
/// ```
219218
pub fn optimal_chunk_size(data_size: u64) -> u64 {
220219
const MIN_CHUNK_SIZE: u64 = 1024 * 1024; // 1 MiB
221-
const MAX_CHUNK_SIZE: u64 = 4 * 1024 * 1024; // 4 MiB
222220
const OPTIMAL_CHUNKS: u64 = 100; // Target chunk count
223221

224222
if data_size <= MIN_CHUNK_SIZE {
@@ -229,8 +227,8 @@ pub fn optimal_chunk_size(data_size: u64) -> u64 {
229227

230228
if optimal_size < MIN_CHUNK_SIZE {
231229
MIN_CHUNK_SIZE
232-
} else if optimal_size > MAX_CHUNK_SIZE {
233-
MAX_CHUNK_SIZE
230+
} else if optimal_size > MAX_CHUNK_SIZE as u64 {
231+
MAX_CHUNK_SIZE as u64
234232
} else {
235233
// Round to nearest MiB
236234
(optimal_size / 1_048_576) * 1_048_576

sdk/typescript/src/chunker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import { Chunk, ChunkerConfig, DEFAULT_CHUNKER_CONFIG, BulletinError } from './types.js';
99

10-
/** Maximum chunk size allowed (8 MiB, matches pallet limit) */
11-
export const MAX_CHUNK_SIZE = 8 * 1024 * 1024;
10+
/** Maximum chunk size allowed (2 MiB, matches Bitswap limit) */
11+
export const MAX_CHUNK_SIZE = 2 * 1024 * 1024;
1212

1313
/**
1414
* Fixed-size chunker that splits data into equal-sized chunks

sdk/typescript/src/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { CID } from 'multiformats/cid';
99
import * as digest from 'multiformats/hashes/digest';
1010
import { blake2AsU8a, sha256AsU8a } from '@polkadot/util-crypto';
1111
import { HashAlgorithm, BulletinError } from './types.js';
12+
import { MAX_CHUNK_SIZE } from './chunker.js';
1213

1314
/**
1415
* Calculate content hash using the specified algorithm
@@ -162,8 +163,6 @@ export function formatBytes(bytes: number, decimals: number = 2): string {
162163
* @throws BulletinError if chunk size is invalid
163164
*/
164165
export function validateChunkSize(size: number): void {
165-
const MAX_CHUNK_SIZE = 8 * 1024 * 1024; // 8 MiB
166-
167166
if (size <= 0) {
168167
throw new BulletinError('Chunk size must be positive', 'INVALID_CHUNK_SIZE');
169168
}
@@ -189,7 +188,6 @@ export function validateChunkSize(size: number): void {
189188
*/
190189
export function optimalChunkSize(dataSize: number): number {
191190
const MIN_CHUNK_SIZE = 1024 * 1024; // 1 MiB
192-
const MAX_CHUNK_SIZE = 4 * 1024 * 1024; // 4 MiB
193191
const OPTIMAL_CHUNKS = 100; // Target chunk count
194192

195193
if (dataSize <= MIN_CHUNK_SIZE) {

0 commit comments

Comments
 (0)