mirror of
https://github.com/Qortal/Qortal-Hub.git
synced 2025-11-16 21:07:07 +00:00
added test for fetching chunk
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
handleActiveChat,
|
||||
handleAddressGroupInvitesMessage,
|
||||
handleArbitraryDataFileList,
|
||||
handleArbitraryDataFileMessage,
|
||||
handleArbitraryLatestTransaction,
|
||||
handleBlockDataMessage,
|
||||
handleGroupBansMessage,
|
||||
@@ -34,6 +35,7 @@ import {
|
||||
createGetAddressGroupInvitesPayload,
|
||||
createGetAddressNamesPayload,
|
||||
createGetArbitraryDataFileListPayload,
|
||||
createGetArbitraryDataFilePayload,
|
||||
createGetArbitraryLatestTransactionPayload,
|
||||
createGetBansPayload,
|
||||
createGetGroupInvitesPayload,
|
||||
@@ -178,8 +180,20 @@ export async function getArbitraryResource(
|
||||
null
|
||||
)
|
||||
);
|
||||
const dataFileList = handleArbitraryDataFileList(res2);
|
||||
if (dataFileList.hashes?.length > 0) {
|
||||
const res3: Buffer = await client.sendRequest(
|
||||
MessageType.GET_ARBITRARY_DATA_FILE,
|
||||
createGetArbitraryDataFilePayload(
|
||||
dataFileList.signature,
|
||||
dataFileList.hashes[0]
|
||||
)
|
||||
);
|
||||
|
||||
console.log('res22', handleArbitraryDataFileList(res2));
|
||||
console.log('res333', handleArbitraryDataFileMessage(res3));
|
||||
}
|
||||
|
||||
console.log('res22');
|
||||
}
|
||||
if (Array.isArray(data) && data.length > 0) {
|
||||
return data[0];
|
||||
|
||||
@@ -1151,3 +1151,34 @@ export function handleArbitraryDataFileList(buffer) {
|
||||
isRelayPossible,
|
||||
};
|
||||
}
|
||||
|
||||
export function handleArbitraryDataFileMessage(buffer) {
|
||||
let offset = 0;
|
||||
|
||||
if (buffer.length < 68) {
|
||||
throw new Error(
|
||||
`Invalid message length: got ${buffer.length}, expected at least 68`
|
||||
);
|
||||
}
|
||||
|
||||
const signature = buffer.subarray(offset, offset + 64);
|
||||
offset += 64;
|
||||
|
||||
const dataLength = buffer.readInt32BE(offset);
|
||||
offset += 4;
|
||||
|
||||
const remaining = buffer.length - offset;
|
||||
if (remaining < dataLength) {
|
||||
throw new Error(
|
||||
`Data length mismatch: expected ${dataLength} bytes, but only ${remaining} bytes available`
|
||||
);
|
||||
}
|
||||
|
||||
const data = buffer.subarray(offset, offset + dataLength);
|
||||
|
||||
return {
|
||||
signature,
|
||||
dataLength,
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ export enum MessageType {
|
||||
GET_SUPPLY = 74,
|
||||
LAST_BLOCK_HEIGHT = 75,
|
||||
GET_LAST_BLOCK_HEIGHT = 76,
|
||||
ARBITRARY_DATA_FILE = 110,
|
||||
GET_ARBITRARY_DATA_FILE = 111,
|
||||
ARBITRARY_DATA_FILE_LIST = 120,
|
||||
GET_ARBITRARY_DATA_FILE_LIST = 121,
|
||||
ARBITRARY_LATEST_TRANSACTION = 122,
|
||||
|
||||
@@ -610,3 +610,19 @@ export function createGetArbitraryDataFileListPayload(
|
||||
|
||||
return Buffer.concat(buffers);
|
||||
}
|
||||
|
||||
export function createGetArbitraryDataFilePayload(signature, hash) {
|
||||
if (signature.length !== 64) {
|
||||
throw new Error(
|
||||
`Invalid signature length: expected 64 bytes, got ${signature.length}`
|
||||
);
|
||||
}
|
||||
|
||||
if (hash.length !== 32) {
|
||||
throw new Error(
|
||||
`Invalid hash length: expected 32 bytes, got ${hash.length}`
|
||||
);
|
||||
}
|
||||
|
||||
return Buffer.concat([signature, hash]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user