diff --git a/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt b/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt index ac0913604c..9c90c36dd3 100644 --- a/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt +++ b/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt @@ -706,9 +706,6 @@ class NodeVaultService( paging: PageSpecification, sorting: Sort, contractStateType: Class): Vault.Page { - // calculate total results where a page specification has been defined - val totalStatesAvailable = if (paging.isDefault) -1 else queryTotalStateCount(criteria, contractStateType) - val (query, stateTypes) = createQuery(criteria, contractStateType, sorting) query.setResultWindow(paging) @@ -732,6 +729,13 @@ class NodeVaultService( ArrayList() ) + val totalStatesAvailable = when { + paging.isDefault -> -1L + // If the first page isn't full then we know that's all the states that are available + paging.pageNumber == DEFAULT_PAGE_NUM && states.size < paging.pageSize -> states.size.toLong() + else -> queryTotalStateCount(criteria, contractStateType) + } + return Vault.Page(states, statesMetadata, totalStatesAvailable, stateTypes, otherResults) }