@@ -12,7 +12,6 @@ import {
1212 UnknownError ,
1313 HashTreeDecodeErrorCode ,
1414 UNREACHABLE_ERROR ,
15- MalformedLookupFoundValueErrorCode ,
1615 MissingLookupValueErrorCode ,
1716 UnexpectedErrorCode ,
1817} from './errors.ts' ;
@@ -815,6 +814,11 @@ function list_paths(path: Array<NodeLabel>, tree: HashTree): Array<Array<NodeLab
815814 }
816815}
817816
817+ type CheckCanisterRangesParams = {
818+ canisterId : Principal ;
819+ subnetId : Principal ;
820+ tree : HashTree ;
821+ } ;
818822type CanisterRanges = Array < [ Principal , Principal ] > ;
819823
820824/**
@@ -825,41 +829,36 @@ type CanisterRanges = Array<[Principal, Principal]>;
825829 * @param params.tree the hash tree in which to lookup the subnet's canister ranges
826830 * @returns {boolean } `true` if the canister is in the range, `false` otherwise
827831 */
828- export function check_canister_ranges ( params : {
829- canisterId : Principal ;
830- subnetId : Principal ;
831- tree : HashTree ;
832- } ) : boolean {
833- const { canisterId, subnetId, tree } = params ;
834-
835- const rangesLookupValue = lookupCanisterRanges ( subnetId , tree ) ;
832+ export function check_canister_ranges ( params : CheckCanisterRangesParams ) : boolean {
833+ const rangesLookupValue = lookupCanisterRanges ( params ) ;
836834 const ranges = decodeCanisterRanges ( rangesLookupValue ) ;
837835
836+ const { canisterId } = params ;
838837 const canisterInRange = ranges . some ( r => r [ 0 ] . ltEq ( canisterId ) && r [ 1 ] . gtEq ( canisterId ) ) ;
839838 return canisterInRange ;
840839}
841840
842- function lookupCanisterRanges ( subnetId : Principal , tree : HashTree ) : Uint8Array {
843- const rangeLookup = lookup_path ( [ 'subnet' , subnetId . toUint8Array ( ) , 'canister_ranges' ] , tree ) ;
844-
845- if ( rangeLookup . status !== LookupPathStatus . Found ) {
841+ /**
842+ * Lookup the canister ranges using the `/subnet/<subnet_id>/canister_ranges` path.
843+ * Certificates returned by `/api/v3/canister/<effective_canister_id>/call`
844+ * and `/api/v2/canister/<effective_canister_id>/read_state` use this path.
845+ * @param params the parameters with which to lookup the canister ranges
846+ * @param params.subnetId the subnet ID to lookup the canister ranges for
847+ * @param params.tree the tree to search
848+ * @returns the encoded canister ranges. Use {@link decodeCanisterRanges} to decode them.
849+ * @see https://internetcomputer.org/docs/references/ic-interface-spec#http-read-state
850+ */
851+ function lookupCanisterRanges ( { subnetId, tree } : CheckCanisterRangesParams ) : Uint8Array {
852+ const lookupResult = lookup_path ( [ 'subnet' , subnetId . toUint8Array ( ) , 'canister_ranges' ] , tree ) ;
853+ if ( lookupResult . status !== LookupPathStatus . Found ) {
846854 throw ProtocolError . fromCode (
847855 new LookupErrorCode (
848856 `Could not find canister ranges for subnet ${ subnetId . toText ( ) } ` ,
849- rangeLookup . status ,
850- ) ,
851- ) ;
852- }
853-
854- if ( ! ( rangeLookup . value instanceof Uint8Array ) ) {
855- throw ProtocolError . fromCode (
856- new MalformedLookupFoundValueErrorCode (
857- `Could not find canister ranges for subnet ${ subnetId . toText ( ) } ` ,
857+ lookupResult . status ,
858858 ) ,
859859 ) ;
860860 }
861-
862- return rangeLookup . value ;
861+ return lookupResult . value ;
863862}
864863
865864function decodeCanisterRanges ( lookupValue : Uint8Array ) : CanisterRanges {
0 commit comments