Search in sources :

Example 6 with Txn

use of com.radixdlt.atom.Txn in project radixdlt by radixdlt.

the class StateComputerLedgerTest method should_accumulate_when_next_command_valid.

@Test
public void should_accumulate_when_next_command_valid() {
    // Arrange
    genesisIsEndOfEpoch(false);
    when(stateComputer.prepare(any(), any(), anyLong())).thenReturn(new StateComputerResult(ImmutableList.of(successfulNextCommand), ImmutableMap.of()));
    // Act
    var unverifiedVertex = UnverifiedVertex.create(genesisQC, View.of(1), List.of(nextTxn), BFTNode.random());
    var proposedVertex = new VerifiedVertex(unverifiedVertex, hasher.hash(unverifiedVertex));
    Optional<PreparedVertex> nextPrepared = sut.prepare(new LinkedList<>(), proposedVertex);
    // Assert
    assertThat(nextPrepared).hasValueSatisfying(x -> assertThat(x.getLedgerHeader().isEndOfEpoch()).isFalse());
    assertThat(nextPrepared.flatMap(x -> accumulatorVerifier.verifyAndGetExtension(ledgerHeader.getAccumulatorState(), List.of(nextTxn), txn -> txn.getId().asHashCode(), x.getLedgerHeader().getAccumulatorState()))).contains(List.of(nextTxn));
}
Also used : VerifiedVertex(com.radixdlt.hotstuff.bft.VerifiedVertex) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) BFTValidatorSet(com.radixdlt.hotstuff.bft.BFTValidatorSet) LedgerProof(com.radixdlt.hotstuff.LedgerProof) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TypedMocks(com.radixdlt.utils.TypedMocks) Hasher(com.radixdlt.crypto.Hasher) Sha256Hasher(com.radixdlt.hotstuff.Sha256Hasher) PreparedTxn(com.radixdlt.ledger.StateComputerLedger.PreparedTxn) QuorumCertificate(com.radixdlt.hotstuff.QuorumCertificate) PreparedVertex(com.radixdlt.hotstuff.bft.PreparedVertex) LedgerHeader(com.radixdlt.hotstuff.LedgerHeader) View(com.radixdlt.hotstuff.bft.View) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) HashUtils(com.radixdlt.crypto.HashUtils) UInt256(com.radixdlt.utils.UInt256) Before(org.junit.Before) SystemCounters(com.radixdlt.counters.SystemCounters) TimeSupplier(com.radixdlt.utils.TimeSupplier) ImmutableMap(com.google.common.collect.ImmutableMap) Txn(com.radixdlt.atom.Txn) StateComputerResult(com.radixdlt.ledger.StateComputerLedger.StateComputerResult) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) VerifiedVertex(com.radixdlt.hotstuff.bft.VerifiedVertex) StateComputer(com.radixdlt.ledger.StateComputerLedger.StateComputer) TimestampedECDSASignatures(com.radixdlt.hotstuff.TimestampedECDSASignatures) Mockito.verify(org.mockito.Mockito.verify) UnverifiedVertex(com.radixdlt.hotstuff.UnverifiedVertex) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Stream(java.util.stream.Stream) BFTValidator(com.radixdlt.hotstuff.bft.BFTValidator) Pair(com.radixdlt.utils.Pair) Optional(java.util.Optional) Mempool(com.radixdlt.mempool.Mempool) Comparator(java.util.Comparator) Mockito.mock(org.mockito.Mockito.mock) PreparedVertex(com.radixdlt.hotstuff.bft.PreparedVertex) StateComputerResult(com.radixdlt.ledger.StateComputerLedger.StateComputerResult) Test(org.junit.Test)

Example 7 with Txn

use of com.radixdlt.atom.Txn in project radixdlt by radixdlt.

the class MempoolSanityTest method when_submitting_items_to_null_mempool__then_test_should_fail.

/**
 * TODO: This is more of a test for mempoolSubmissionSteadyState, should move somewhere else
 */
@Test
public void when_submitting_items_to_null_mempool__then_test_should_fail() {
    SimulationTest simulationTest = bftTestBuilder.addOverrideModuleToAllInitialNodes(new AbstractModule() {

        @Override
        protected void configure() {
            bind(new TypeLiteral<Mempool<Txn>>() {
            }).toInstance(Mempools.empty());
        }
    }).build();
    final var checkResults = simulationTest.run().awaitCompletion();
    assertThat(checkResults).hasEntrySatisfying(Monitor.MEMPOOL_COMMITTED, error -> assertThat(error).isPresent());
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) SimulationTest(com.radixdlt.harness.simulation.SimulationTest) Txn(com.radixdlt.atom.Txn) AbstractModule(com.google.inject.AbstractModule) Test(org.junit.Test) SimulationTest(com.radixdlt.harness.simulation.SimulationTest)

Example 8 with Txn

use of com.radixdlt.atom.Txn in project radixdlt by radixdlt.

the class ConsensusToLedgerCommittedInvariant method check.

@Override
public Observable<TestInvariantError> check(RunningNetwork network) {
    BehaviorSubject<Set<Txn>> committedTxns = BehaviorSubject.create();
    Disposable d = network.ledgerUpdates().<Set<Txn>>scan(new HashSet<>(), (set, next) -> {
        set.addAll(next.getSecond().getNewTxns());
        return set;
    }).subscribe(committedTxns::onNext);
    return Observable.<BFTCommittedUpdate>create(emitter -> commits.addListener((node, event) -> emitter.onNext(event), BFTCommittedUpdate.class)).serialize().concatMap(committedUpdate -> Observable.fromStream(committedUpdate.committed().stream().flatMap(PreparedVertex::successfulCommands))).flatMapMaybe(txn -> committedTxns.filter(cmdSet -> cmdSet.contains(txn.txn())).timeout(10, TimeUnit.SECONDS).firstOrError().ignoreElement().onErrorReturn(e -> new TestInvariantError("Committed command in vertex has not been inserted into the ledger" + " after 10 seconds"))).doFinally(d::dispose);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) BFTCommittedUpdate(com.radixdlt.hotstuff.bft.BFTCommittedUpdate) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) TestInvariant(com.radixdlt.harness.simulation.TestInvariant) PreparedVertex(com.radixdlt.hotstuff.bft.PreparedVertex) NodeEvents(com.radixdlt.harness.simulation.monitors.NodeEvents) Observable(io.reactivex.rxjava3.core.Observable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Txn(com.radixdlt.atom.Txn) Set(java.util.Set) RunningNetwork(com.radixdlt.harness.simulation.network.SimulationNodes.RunningNetwork) BehaviorSubject(io.reactivex.rxjava3.subjects.BehaviorSubject) HashSet(java.util.HashSet) Set(java.util.Set) PreparedVertex(com.radixdlt.hotstuff.bft.PreparedVertex) BFTCommittedUpdate(com.radixdlt.hotstuff.bft.BFTCommittedUpdate) Txn(com.radixdlt.atom.Txn) HashSet(java.util.HashSet)

Example 9 with Txn

use of com.radixdlt.atom.Txn in project radixdlt by radixdlt.

the class TransactionsHandler method construct.

private CommittedTransaction construct(Txn txn, RecoverableProcessedTxn recoveryInfo, AccumulatorState accumulatorState) {
    var parser = radixEngineProvider.get().getParser();
    ParsedTxn parsedTxn;
    try {
        parsedTxn = parser.parse(txn);
    } catch (TxnParseException e) {
        throw new IllegalStateException("Could not parse already committed transaction", e);
    }
    var committedTransaction = new CommittedTransaction();
    recoveryInfo.recoverStateUpdates(parsedTxn).stream().map(stateUpdateGroup -> {
        var operationGroup = new OperationGroup();
        stateUpdateGroup.stream().map(stateUpdate -> {
            var substateOperation = stateUpdate.recover(radixEngineProvider);
            return coreModelMapper.operation(substateOperation.getSubstate(), substateOperation.getSubstateId(), substateOperation.isBootUp(), this::symbol);
        }).forEach(operationGroup::addOperationsItem);
        return operationGroup;
    }).forEach(committedTransaction::addOperationGroupsItem);
    var signedBy = parsedTxn.getPayloadHashAndSig().map(hashAndSig -> {
        var hash = hashAndSig.getFirst();
        var sig = hashAndSig.getSecond();
        return ECPublicKey.recoverFrom(hash, sig).orElseThrow(() -> new IllegalStateException("Invalid signature on already committed transaction"));
    });
    var transactionIdentifier = coreModelMapper.transactionIdentifier(txn.getId());
    return committedTransaction.committedStateIdentifier(coreModelMapper.stateIdentifier(accumulatorState)).metadata(new CommittedTransactionMetadata().fee(coreModelMapper.nativeTokenAmount(parsedTxn.getFeePaid())).message(parsedTxn.getMsg().map(Bytes::toHexString).orElse(null)).size(txn.getPayload().length).hex(Bytes.toHexString(txn.getPayload())).signedBy(signedBy.map(coreModelMapper::publicKey).orElse(null))).transactionIdentifier(transactionIdentifier);
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) LedgerAndBFTProof(com.radixdlt.statecomputer.LedgerAndBFTProof) Inject(com.google.inject.Inject) RecoverableProcessedTxn(com.radixdlt.api.core.reconstruction.RecoverableProcessedTxn) StateIdentifier(com.radixdlt.api.core.openapitools.model.StateIdentifier) ECPublicKey(com.radixdlt.crypto.ECPublicKey) OperationGroup(com.radixdlt.api.core.openapitools.model.OperationGroup) ParsedTxn(com.radixdlt.engine.parser.ParsedTxn) CommittedTransaction(com.radixdlt.api.core.openapitools.model.CommittedTransaction) CoreJsonRpcHandler(com.radixdlt.api.core.CoreJsonRpcHandler) BerkeleyLedgerEntryStore(com.radixdlt.store.berkeley.BerkeleyLedgerEntryStore) SystemMapKey(com.radixdlt.constraintmachine.SystemMapKey) AccumulatorState(com.radixdlt.ledger.AccumulatorState) BerkeleyRecoverableProcessedTxnStore(com.radixdlt.api.core.reconstruction.BerkeleyRecoverableProcessedTxnStore) CoreModelMapper(com.radixdlt.api.core.model.CoreModelMapper) TokenResourceMetadata(com.radixdlt.application.tokens.state.TokenResourceMetadata) CoreApiException(com.radixdlt.api.core.model.CoreApiException) RadixEngine(com.radixdlt.engine.RadixEngine) Txn(com.radixdlt.atom.Txn) REAddr(com.radixdlt.identifiers.REAddr) CommittedTransactionsRequest(com.radixdlt.api.core.openapitools.model.CommittedTransactionsRequest) Provider(com.google.inject.Provider) Bytes(com.radixdlt.utils.Bytes) CommittedTransactionMetadata(com.radixdlt.api.core.openapitools.model.CommittedTransactionMetadata) CommittedTransactionsResponse(com.radixdlt.api.core.openapitools.model.CommittedTransactionsResponse) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) LedgerAccumulator(com.radixdlt.ledger.LedgerAccumulator) OperationGroup(com.radixdlt.api.core.openapitools.model.OperationGroup) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) CommittedTransactionMetadata(com.radixdlt.api.core.openapitools.model.CommittedTransactionMetadata) CommittedTransaction(com.radixdlt.api.core.openapitools.model.CommittedTransaction) ParsedTxn(com.radixdlt.engine.parser.ParsedTxn)

Example 10 with Txn

use of com.radixdlt.atom.Txn in project radixdlt by radixdlt.

the class VoteHandler method handleRequest.

@Override
public UpdateVoteResponse handleRequest(UpdateVoteRequest request) throws CoreApiException {
    coreModelMapper.verifyNetwork(request.getNetworkIdentifier());
    final Txn signedTx;
    try {
        signedTx = radixEngine.constructWithFees(this::buildVote, false, REAddr.ofPubKeyAccount(validatorKey), NotEnoughNativeTokensForFeesException::new).signAndBuild(hashSigner::sign);
    } catch (TxBuilderException e) {
        throw CoreApiException.badRequest(coreModelMapper.builderErrorDetails(e));
    }
    try {
        radixEngineStateComputer.addToMempool(signedTx);
        return new UpdateVoteResponse().transactionIdentifier(coreModelMapper.transactionIdentifier(signedTx.getId())).duplicate(false);
    } catch (MempoolDuplicateException e) {
        return new UpdateVoteResponse().transactionIdentifier(coreModelMapper.transactionIdentifier(signedTx.getId())).duplicate(true);
    } catch (MempoolFullException e) {
        throw coreModelMapper.mempoolFullException(e);
    } catch (MempoolRejectedException e) {
        throw coreModelMapper.radixEngineException((RadixEngineException) e.getCause());
    }
}
Also used : MempoolDuplicateException(com.radixdlt.mempool.MempoolDuplicateException) NotEnoughNativeTokensForFeesException(com.radixdlt.api.core.model.NotEnoughNativeTokensForFeesException) MempoolRejectedException(com.radixdlt.mempool.MempoolRejectedException) MempoolFullException(com.radixdlt.mempool.MempoolFullException) RadixEngineException(com.radixdlt.engine.RadixEngineException) Txn(com.radixdlt.atom.Txn) UpdateVoteResponse(com.radixdlt.api.core.openapitools.model.UpdateVoteResponse) TxBuilderException(com.radixdlt.atom.TxBuilderException)

Aggregations

Txn (com.radixdlt.atom.Txn)12 TxBuilderException (com.radixdlt.atom.TxBuilderException)3 REProcessedTxn (com.radixdlt.constraintmachine.REProcessedTxn)3 NotEnoughNativeTokensForFeesException (com.radixdlt.api.core.model.NotEnoughNativeTokensForFeesException)2 UpdateVoteResponse (com.radixdlt.api.core.openapitools.model.UpdateVoteResponse)2 SystemMapKey (com.radixdlt.constraintmachine.SystemMapKey)2 ECPublicKey (com.radixdlt.crypto.ECPublicKey)2 RadixEngineException (com.radixdlt.engine.RadixEngineException)2 TestInvariant (com.radixdlt.harness.simulation.TestInvariant)2 RunningNetwork (com.radixdlt.harness.simulation.network.SimulationNodes.RunningNetwork)2 PreparedVertex (com.radixdlt.hotstuff.bft.PreparedVertex)2 MempoolDuplicateException (com.radixdlt.mempool.MempoolDuplicateException)2 MempoolFullException (com.radixdlt.mempool.MempoolFullException)2 MempoolRejectedException (com.radixdlt.mempool.MempoolRejectedException)2 Observable (io.reactivex.rxjava3.core.Observable)2 IOException (java.io.IOException)2 List (java.util.List)2 Optional (java.util.Optional)2 Test (org.junit.Test)2 Stopwatch (com.google.common.base.Stopwatch)1