Skip to content
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
21 changes: 21 additions & 0 deletions pallets/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ pub mod pallet {
UnableToDeleteFile,
/// Insufficient stash
InsufficientDeposit,
/// Invalid cid
InvalidCid,
}

#[pallet::hooks]
Expand Down Expand Up @@ -782,6 +784,7 @@ pub mod pallet {
file_size > 0 && file_size <= T::MaxFileSize::get(),
Error::<T>::InvalidFileSize
);
ensure!(is_cid(&cid), Error::<T>::InvalidCid);

if let Some(mut file) = Files::<T>::get(&cid) {
let new_reserved = fee.saturating_add(file.reserved);
Expand Down Expand Up @@ -1217,3 +1220,21 @@ fn encode_del_files(list: &Vec<FileId>) -> Vec<u8> {
}
output
}

fn is_cid(cid: &[u8]) -> bool {
let len = cid.len();
if len == 46 {
return &cid[0..2] == b"Qm" &&
cid[2..].iter().all(|x| match *x {
49..=57 | 65..=90 | 97..=122 => true,
_ => false,
})
} else if len == 59 {
return &cid[0..7] == b"bafkrei" &&
cid[7..].iter().all(|x| match *x {
49..=57 | 65..=90 | 97..=122 => true,
_ => false,
})
}
false
}
2 changes: 1 addition & 1 deletion pallets/storage/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub fn change_file_byte_price(v: Balance) {
}

pub fn mock_file_id(suffix: char) -> FileId {
str2bytes(&format!("QmS9ErDVxHXRNMJRJ5i3bp1zxCZzKP8QXXNH1yeeeeeee{}", suffix))
str2bytes(&format!("QmQywLBjvLgDabmv8QZmJxxj6AqmZdGNZxCvnKh644JfF{}", suffix))
}

#[derive(Debug, Clone, Default)]
Expand Down