Skip to content

Commit

Permalink
update to limit size instead of forcing it to be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Jan 17, 2024
1 parent 0ed3820 commit 7a6ded9
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fc from 'fast-check';
import fc, { ArrayConstraints } from 'fast-check';
import { Vec } from '.';
import { CandidType } from '../../candid_type';
import { CorrespondingJSType } from '../../corresponding_js_type';
Expand All @@ -13,6 +13,19 @@ import {
import { CandidValues, CandidValueArb } from '../../candid_values_arb';
import { RecursiveShapes } from '../../recursive';

/*
From Candid Spec:
Since null, reserved, record {}, and records of such values, take no space, to
prevent unbounded sized message, we limit the total vector length of such
zero-sized values in a messagev (on the wire) to be 2,000,000 elements. For
example, if a message contains two vectors, one at type vec null and one at type
vec record {}, then the length of both vectors combined cannot exceed 2,000,000
elements.
const NULL_VEC_SIZE_LIMIT = 2_000_000;
*/
// chenyen explained that on the canister side this limit is set to zero
const EMPTYISH_VEC_SIZE_LIMIT = 0;

export function VecValuesArb(
vecDefinition: VecCandidDefinition,
recursiveShapes: RecursiveShapes,
Expand All @@ -23,14 +36,9 @@ export function VecValuesArb(
generateEmptyVec(vecDefinition.innerType.candidMeta.candidType)
);
}
if (isEmptyInnerType(vecDefinition.innerType)) {
return fc.constant(
generateEmptyVec(vecDefinition.innerType.candidMeta.candidType)
);
}
const arbitraryMemberValues = fc
.tuple(
fc.array(fc.constant(null)),
fc.array(fc.constant(null), determineVecConstraints(vecDefinition)),
fc.constant(vecDefinition.innerType)
)
.chain(([arrayTemplate, innerType]) =>
Expand Down Expand Up @@ -59,6 +67,15 @@ export function VecValuesArb(
});
}

function determineVecConstraints(
vecDefinition: VecCandidDefinition
): ArrayConstraints | undefined {
if (isEmptyInnerType(vecDefinition.innerType)) {
return { maxLength: EMPTYISH_VEC_SIZE_LIMIT };
}
return;
}

function generateEmptyVec(innerCandidType: CandidType): CandidValues<Vec> {
return {
valueLiteral: typeValueLiteral('[]', innerCandidType),
Expand Down

0 comments on commit 7a6ded9

Please sign in to comment.