Search in sources :

Example 1 with StateIdentifier

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

the class TransactionsHandler method handleRequest.

@Override
public CommittedTransactionsResponse handleRequest(CommittedTransactionsRequest request) throws CoreApiException {
    coreModelMapper.verifyNetwork(request.getNetworkIdentifier());
    var stateIdentifier = coreModelMapper.partialStateIdentifier(request.getStateIdentifier());
    long stateVersion = stateIdentifier.getFirst();
    var accumulator = stateIdentifier.getSecond();
    var currentAccumulator = txnStore.getAccumulator(stateVersion).orElseThrow(() -> CoreApiException.notFound(coreModelMapper.notFoundErrorDetails(request.getStateIdentifier())));
    if (accumulator != null) {
        var matchesInput = accumulator.equals(currentAccumulator);
        if (!matchesInput) {
            throw CoreApiException.notFound(coreModelMapper.notFoundErrorDetails(request.getStateIdentifier()));
        }
    }
    var limit = coreModelMapper.limit(request.getLimit());
    var recoverable = txnStore.get(stateVersion, limit);
    var accumulatorState = new AccumulatorState(stateVersion, currentAccumulator);
    var response = new CommittedTransactionsResponse();
    var txns = ledgerEntryStore.getCommittedTxns(stateVersion, recoverable.size());
    for (int i = 0; i < txns.size(); i++) {
        var txn = txns.get(i);
        var recoveryInfo = recoverable.get(i);
        var nextAccumulatorState = ledgerAccumulator.accumulate(accumulatorState, txn.getId().asHashCode());
        accumulatorState = nextAccumulatorState;
        response.addTransactionsItem(construct(txn, recoveryInfo, nextAccumulatorState));
    }
    return response.stateIdentifier(new StateIdentifier().stateVersion(stateVersion).transactionAccumulator(Bytes.toHexString(currentAccumulator.asBytes())));
}
Also used : AccumulatorState(com.radixdlt.ledger.AccumulatorState) CommittedTransactionsResponse(com.radixdlt.api.core.openapitools.model.CommittedTransactionsResponse) StateIdentifier(com.radixdlt.api.core.openapitools.model.StateIdentifier)

Aggregations

CommittedTransactionsResponse (com.radixdlt.api.core.openapitools.model.CommittedTransactionsResponse)1 StateIdentifier (com.radixdlt.api.core.openapitools.model.StateIdentifier)1 AccumulatorState (com.radixdlt.ledger.AccumulatorState)1