Search in sources :

Example 11 with EntityRequest

use of com.radixdlt.api.core.openapitools.model.EntityRequest in project radixdlt by radixdlt.

the class EntityHandlerTest method retrieve_non_existent_token_on_genesis.

@Test
public void retrieve_non_existent_token_on_genesis() throws Exception {
    // Arrange
    start();
    // Act
    var tokenAddress = REAddr.ofHashedKey(selfKey(), "test");
    var request = new EntityRequest().networkIdentifier(new NetworkIdentifier().network("localnet")).entityIdentifier(coreModelMapper.entityIdentifier(tokenAddress, "test"));
    var response = handleRequestWithExpectedResponse(sut, request, EntityResponse.class);
    // Assert
    var stateAccumulator = response.getStateIdentifier().getTransactionAccumulator();
    var genesisAccumulator = genesis.getProof().getAccumulatorState().getAccumulatorHash().asBytes();
    assertThat(stateAccumulator).isEqualTo(Bytes.toHexString(genesisAccumulator));
    assertThat(response.getBalances()).isEmpty();
    assertThat(response.getDataObjects()).containsExactly(new UnclaimedRadixEngineAddress().type(SubstateTypeMapping.getName(SubstateTypeId.UNCLAIMED_READDR)));
}
Also used : EntityRequest(com.radixdlt.api.core.openapitools.model.EntityRequest) NetworkIdentifier(com.radixdlt.api.core.openapitools.model.NetworkIdentifier) UnclaimedRadixEngineAddress(com.radixdlt.api.core.openapitools.model.UnclaimedRadixEngineAddress) ApiTest(com.radixdlt.api.ApiTest) Test(org.junit.Test)

Example 12 with EntityRequest

use of com.radixdlt.api.core.openapitools.model.EntityRequest in project radixdlt by radixdlt.

the class EntityHandler method handleRequest.

@Override
public EntityResponse handleRequest(EntityRequest request) throws CoreApiException {
    modelMapper.verifyNetwork(request.getNetworkIdentifier());
    var entity = modelMapper.entity(request.getEntityIdentifier());
    var keyQueries = entity.getKeyQueries();
    var resourceQueries = entity.getResourceQueries();
    // This must be read atomically
    return radixEngine.read(reader -> {
        Function<REAddr, String> addressToSymbol = addr -> {
            var mapKey = SystemMapKey.ofResourceData(addr, SubstateTypeId.TOKEN_RESOURCE_METADATA.id());
            var substate = reader.get(mapKey).orElseThrow();
            var tokenResource = (TokenResourceMetadata) substate;
            return tokenResource.symbol();
        };
        var proof = reader.getMetadata().getProof();
        var response = new EntityResponse().stateIdentifier(modelMapper.stateIdentifier(proof.getAccumulatorState()));
        for (var resourceQuery : resourceQueries) {
            resourceQuery.fold((index, bucketPredicate) -> reader.reduceResources(index, ResourceInBucket::bucket, bucketPredicate).entrySet().stream().map(e -> modelMapper.resourceOperation(e.getKey(), e.getValue(), addressToSymbol)), systemMapKey -> reader.get(systemMapKey).map(ResourceInBucket.class::cast).stream().map(b -> modelMapper.resourceOperation(b, true, addressToSymbol))).forEach(response::addBalancesItem);
        }
        for (var keyQuery : keyQueries) {
            var substate = keyQuery.get(reader);
            substate.flatMap(modelMapper::dataObject).ifPresent(response::addDataObjectsItem);
        }
        return response;
    });
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) TokenResourceMetadata(com.radixdlt.application.tokens.state.TokenResourceMetadata) CoreApiException(com.radixdlt.api.core.model.CoreApiException) LedgerAndBFTProof(com.radixdlt.statecomputer.LedgerAndBFTProof) Inject(com.google.inject.Inject) RadixEngine(com.radixdlt.engine.RadixEngine) REAddr(com.radixdlt.identifiers.REAddr) Function(java.util.function.Function) ResourceInBucket(com.radixdlt.application.tokens.ResourceInBucket) CoreJsonRpcHandler(com.radixdlt.api.core.CoreJsonRpcHandler) SystemMapKey(com.radixdlt.constraintmachine.SystemMapKey) EntityResponse(com.radixdlt.api.core.openapitools.model.EntityResponse) CoreModelMapper(com.radixdlt.api.core.model.CoreModelMapper) EntityRequest(com.radixdlt.api.core.openapitools.model.EntityRequest) ResourceInBucket(com.radixdlt.application.tokens.ResourceInBucket) EntityResponse(com.radixdlt.api.core.openapitools.model.EntityResponse) REAddr(com.radixdlt.identifiers.REAddr)

Aggregations

EntityRequest (com.radixdlt.api.core.openapitools.model.EntityRequest)12 NetworkIdentifier (com.radixdlt.api.core.openapitools.model.NetworkIdentifier)11 ApiTest (com.radixdlt.api.ApiTest)10 Test (org.junit.Test)10 CoreApiException (com.radixdlt.api.core.model.CoreApiException)2 EntityIdentifier (com.radixdlt.api.core.openapitools.model.EntityIdentifier)2 Inject (com.google.inject.Inject)1 CoreJsonRpcHandler (com.radixdlt.api.core.CoreJsonRpcHandler)1 CoreModelMapper (com.radixdlt.api.core.model.CoreModelMapper)1 EngineStatusRequest (com.radixdlt.api.core.openapitools.model.EngineStatusRequest)1 EntityResponse (com.radixdlt.api.core.openapitools.model.EntityResponse)1 SubEntity (com.radixdlt.api.core.openapitools.model.SubEntity)1 UnclaimedRadixEngineAddress (com.radixdlt.api.core.openapitools.model.UnclaimedRadixEngineAddress)1 ResourceInBucket (com.radixdlt.application.tokens.ResourceInBucket)1 TokenResourceMetadata (com.radixdlt.application.tokens.state.TokenResourceMetadata)1 SubstateTypeId (com.radixdlt.atom.SubstateTypeId)1 SystemMapKey (com.radixdlt.constraintmachine.SystemMapKey)1 RadixEngine (com.radixdlt.engine.RadixEngine)1 REAddr (com.radixdlt.identifiers.REAddr)1 LedgerAndBFTProof (com.radixdlt.statecomputer.LedgerAndBFTProof)1