Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

feat(node): make epoch index optional #179

Merged
merged 2 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions node/Drive.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class Drive {
*
* @param {DataContract} dataContract
* @param {string} documentType
* @param {BlockInfo} [blockInfo]
* @param {number} [epochIndex]
* @param [query]
* @param [query.where]
* @param [query.limit]
Expand All @@ -192,15 +192,21 @@ class Drive {
*
* @returns {Promise<[Document[], number]>}
*/
async queryDocuments(dataContract, documentType, blockInfo = undefined, query = {}, useTransaction = false) {
async queryDocuments(
dataContract,
documentType,
epochIndex = undefined,
query = {},
useTransaction = false,
) {
const encodedQuery = await cbor.encodeAsync(query);

const [encodedDocuments, , processingFee] = await driveQueryDocumentsAsync.call(
this.drive,
encodedQuery,
dataContract.id.toBuffer(),
documentType,
blockInfo,
epochIndex,
useTransaction,
);

Expand Down
19 changes: 16 additions & 3 deletions node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl DriveWrapper {
let js_query_cbor = cx.argument::<JsBuffer>(0)?;
let js_contract_id = cx.argument::<JsBuffer>(1)?;
let js_document_type_name = cx.argument::<JsString>(2)?;
let js_epoch_info = cx.argument::<JsNumber>(3)?;
let js_maybe_epoch_index = cx.argument::<JsValue>(3)?;
// TODO We need dry run for validation
let js_using_transaction = cx.argument::<JsBoolean>(4)?;
let js_callback = cx.argument::<JsFunction>(5)?.root(&mut cx);
Expand All @@ -590,7 +590,20 @@ impl DriveWrapper {
let query_cbor = converter::js_buffer_to_vec_u8(js_query_cbor, &mut cx);
let contract_id = converter::js_buffer_to_vec_u8(js_contract_id, &mut cx);
let document_type_name = js_document_type_name.value(&mut cx);
let epoch_info = Epoch::new(js_epoch_info.value(&mut cx) as u16);

let maybe_epoch: Option<Epoch> = if !js_maybe_epoch_index.is_a::<JsUndefined, _>(&mut cx) {
let js_epoch_index = js_maybe_epoch_index.downcast_or_throw::<JsNumber, _>(&mut cx)?;

let epoch_index = u16::try_from(js_epoch_index.value(&mut cx) as i64)
.or_else(|_| cx.throw_range_error("`epochs` must fit in u16"))?;

let epoch = Epoch::new(epoch_index);

Some(epoch)
} else {
None
};

let using_transaction = js_using_transaction.value(&mut cx);

drive
Expand All @@ -599,7 +612,7 @@ impl DriveWrapper {
&query_cbor,
<[u8; 32]>::try_from(contract_id).unwrap(),
document_type_name.as_str(),
Some(&epoch_info),
maybe_epoch.as_ref(),
using_transaction.then_some(transaction).flatten(),
);

Expand Down
12 changes: 12 additions & 0 deletions node/test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"env": {
"node": true,
"mocha": true
},
"rules": {
"import/no-extraneous-dependencies": "off"
},
"globals": {
"expect": true
}
}
10 changes: 3 additions & 7 deletions node/test/Drive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ describe('Drive', () => {
it('should not update a document with dry run flag', async () => {
const documentWithoutIndices = documents[0];

// TODO: It should work without document creation
// await drive.createDocument(documentWithoutIndices, blockInfo);

documentWithoutIndices.set('name', 'Boooooooooooooooooooooob');

const result = await drive.updateDocument(documentWithoutIndices, blockInfo, false, true);
Expand All @@ -203,8 +200,7 @@ describe('Drive', () => {
expect(result).to.have.property('storageFee');
expect(result).to.have.property('removedFromIdentities');

// TODO: Processing fee doesn't work for v0.23
expect(result.processingFee).to.be.equals(0);
expect(result.processingFee).to.be.greaterThan(0);
expect(result.storageFee).to.be.greaterThan(0, 'storage fee must be higher than 0');

expect(await drive.getGroveDB().getRootHash()).to.deep.equals(initialRootHash);
Expand Down Expand Up @@ -335,7 +331,7 @@ describe('Drive', () => {
);

// eslint-disable-next-line no-unused-vars
const [fetchedDocuments, processingCost] = await drive.queryDocuments(dataContract, 'indexedDocument', blockInfo, {
const [fetchedDocuments, processingCost] = await drive.queryDocuments(dataContract, 'indexedDocument', blockInfo.epoch, {
where: [['lastName', '==', 'Kennedy']],
});

Expand All @@ -348,7 +344,7 @@ describe('Drive', () => {

it('should return empty array if documents are not exist', async () => {
// eslint-disable-next-line no-unused-vars
const [fetchedDocuments, processingCost] = await drive.queryDocuments(dataContract, 'indexedDocument', blockInfo, {
const [fetchedDocuments, processingCost] = await drive.queryDocuments(dataContract, 'indexedDocument', blockInfo.epoch, {
where: [['lastName', '==', 'Kennedy']],
});

Expand Down
7 changes: 6 additions & 1 deletion node/test/GroveDB.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,12 @@ describe('GroveDB', () => {
await groveDb.insert(
ePath,
Buffer.from('ebKey'),
{ type: 'item', epoch: 0, ownerId, value: ebValue },
{
type: 'item',
epoch: 0,
ownerId,
value: ebValue,
},
);
});

Expand Down
2 changes: 1 addition & 1 deletion node/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ function expectFeeResult(feeResult) {

module.exports = {
expectFeeResult,
};
};