Search in sources :

Example 6 with UInt256

use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.

the class WeightedRotatingLeadersTest method setUp.

private void setUp(int validatorSetSize, int sizeOfCache) {
    this.validatorsInOrder = Stream.generate(() -> ECKeyPair.generateNew().getPublicKey()).limit(validatorSetSize).map(BFTNode::create).map(node -> BFTValidator.from(node, UInt256.ONE)).sorted(Comparator.comparing(v -> v.getNode().getKey(), KeyComparator.instance().reversed())).collect(ImmutableList.toImmutableList());
    BFTValidatorSet validatorSet = BFTValidatorSet.from(validatorsInOrder);
    this.weightedRotatingLeaders = new WeightedRotatingLeaders(validatorSet, sizeOfCache);
    this.weightedRotatingLeaders2 = new WeightedRotatingLeaders(validatorSet, sizeOfCache);
}
Also used : IntStream(java.util.stream.IntStream) BFTValidatorSet(com.radixdlt.hotstuff.bft.BFTValidatorSet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collectors.counting(java.util.stream.Collectors.counting) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Test(org.junit.Test) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) Supplier(java.util.function.Supplier) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) ECKeyPair(com.radixdlt.crypto.ECKeyPair) View(com.radixdlt.hotstuff.bft.View) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) ImmutableList(com.google.common.collect.ImmutableList) BFTValidator(com.radixdlt.hotstuff.bft.BFTValidator) Map(java.util.Map) KeyComparator(com.radixdlt.utils.KeyComparator) Comparator(java.util.Comparator) UInt256(com.radixdlt.utils.UInt256) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) BFTValidatorSet(com.radixdlt.hotstuff.bft.BFTValidatorSet)

Example 7 with UInt256

use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.

the class REParser method parse.

public ParsedTxn parse(Txn txn) throws TxnParseException {
    UInt256 feePaid = null;
    ECDSASignature sig = null;
    int sigPosition = 0;
    var parserState = new ParserState(txn);
    var buf = ByteBuffer.wrap(txn.getPayload());
    while (buf.hasRemaining()) {
        if (sig != null) {
            throw new TxnParseException(parserState, "Signature must be last");
        }
        var curPos = buf.position();
        parserState.pos(curPos);
        final var inst = readInstruction(parserState, buf);
        parserState.nextInstruction(inst);
        if (inst.isStateUpdate()) {
            parserState.substateUpdate(inst.getMicroOp().getOp());
        } else if (inst.getMicroOp().getOp() == REOp.READ || inst.getMicroOp().getOp() == REOp.READINDEX) {
            parserState.read();
        } else if (inst.getMicroOp() == REInstruction.REMicroOp.HEADER) {
            parserState.header(inst.getData());
        } else if (inst.getMicroOp() == REInstruction.REMicroOp.SYSCALL) {
            try {
                var callData = inst.<CallData>getData();
                byte id = callData.get(0);
                var syscall = Syscall.of(id).orElseThrow(() -> new TxnParseException(parserState, "Invalid call data type: " + id));
                switch(syscall) {
                    case FEE_RESERVE_PUT:
                        if (feePaid != null) {
                            throw new TxnParseException(parserState, "Should only pay fees once.");
                        }
                        feePaid = callData.getUInt256(1);
                        break;
                    case FEE_RESERVE_TAKE:
                        if (feePaid == null) {
                            throw new TxnParseException(parserState, "No fees paid");
                        }
                        var takeAmount = callData.getUInt256(1);
                        if (takeAmount.compareTo(feePaid) > 0) {
                            throw new TxnParseException(parserState, "Trying to take more fees than paid");
                        }
                        feePaid = feePaid.subtract(takeAmount);
                        break;
                    case READDR_CLAIM:
                        break;
                    // TODO: Along with FeeConstraintScrypt.java
                    default:
                        throw new TxnParseException(parserState, "Invalid call data type: " + id);
                }
            } catch (CallDataAccessException | TrailingBytesException e) {
                throw new TxnParseException(parserState, e);
            }
        } else if (inst.getMicroOp() == REInstruction.REMicroOp.MSG) {
            parserState.msg(inst.getData());
        } else if (inst.getMicroOp() == REInstruction.REMicroOp.END) {
            parserState.end();
        } else if (inst.getMicroOp() == REInstruction.REMicroOp.SIG) {
            sigPosition = curPos;
            sig = inst.getData();
        } else {
            throw new TxnParseException(parserState, "Unknown CM Op " + inst.getMicroOp());
        }
    }
    parserState.finish();
    return new ParsedTxn(txn, feePaid, parserState.instructions, parserState.msg, sig == null ? null : Pair.of(calculatePayloadHash(txn, sigPosition), sig), parserState.disableResourceAllocAndDestroy);
}
Also used : ECDSASignature(com.radixdlt.crypto.ECDSASignature) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) UInt256(com.radixdlt.utils.UInt256)

Example 8 with UInt256

use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.

the class RadixEngineStateComputerTest method getExternalModule.

private Module getExternalModule() {
    return new AbstractModule() {

        @Override
        public void configure() {
            var validatorSet = BFTValidatorSet.from(registeredNodes.stream().map(ECKeyPair::getPublicKey).map(BFTNode::create).map(n -> BFTValidator.from(n, UInt256.ONE)));
            bind(ProposerElection.class).toInstance(new WeightedRotatingLeaders(validatorSet));
            bind(Serialization.class).toInstance(serialization);
            bind(Hasher.class).toInstance(Sha256Hasher.withDefaultSerialization());
            bind(new TypeLiteral<EngineStore<LedgerAndBFTProof>>() {
            }).toInstance(engineStore);
            bind(PersistentVertexStore.class).toInstance(mock(PersistentVertexStore.class));
            install(MempoolConfig.asModule(10, 10));
            install(new MainnetForksModule());
            install(new RadixEngineForksLatestOnlyModule());
            install(new ForksModule());
            // HACK
            bind(CommittedReader.class).toInstance(new NoOpCommittedReader());
            bind(ForksEpochStore.class).toInstance(new NoOpForksEpochStore());
            bind(LedgerAccumulator.class).to(SimpleLedgerAccumulatorAndVerifier.class);
            bind(new TypeLiteral<EventDispatcher<MempoolAddSuccess>>() {
            }).toInstance(TypedMocks.rmock(EventDispatcher.class));
            bind(new TypeLiteral<EventDispatcher<InvalidProposedTxn>>() {
            }).toInstance(TypedMocks.rmock(EventDispatcher.class));
            bind(new TypeLiteral<EventDispatcher<TxnsRemovedFromMempool>>() {
            }).toInstance(TypedMocks.rmock(EventDispatcher.class));
            bind(new TypeLiteral<EventDispatcher<REOutput>>() {
            }).toInstance(TypedMocks.rmock(EventDispatcher.class));
            bind(new TypeLiteral<EventDispatcher<MempoolRelayTrigger>>() {
            }).toInstance(TypedMocks.rmock(EventDispatcher.class));
            bind(new TypeLiteral<EventDispatcher<LedgerUpdate>>() {
            }).toInstance(TypedMocks.rmock(EventDispatcher.class));
            bind(SystemCounters.class).to(SystemCountersImpl.class);
        }
    };
}
Also used : Module(com.google.inject.Module) SimpleLedgerAccumulatorAndVerifier(com.radixdlt.ledger.SimpleLedgerAccumulatorAndVerifier) TxAction(com.radixdlt.atom.TxAction) NoOpCommittedReader(com.radixdlt.sync.NoOpCommittedReader) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) RadixEngineException(com.radixdlt.engine.RadixEngineException) RadixEngineForksLatestOnlyModule(com.radixdlt.statecomputer.forks.RadixEngineForksLatestOnlyModule) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Inject(com.google.inject.Inject) TypedMocks(com.radixdlt.utils.TypedMocks) DefaultSerialization(com.radixdlt.DefaultSerialization) Hasher(com.radixdlt.crypto.Hasher) MempoolAdd(com.radixdlt.mempool.MempoolAdd) RoundData(com.radixdlt.application.system.state.RoundData) QuorumCertificate(com.radixdlt.hotstuff.QuorumCertificate) LedgerHeader(com.radixdlt.hotstuff.LedgerHeader) ByzantineQuorumException(com.radixdlt.ledger.ByzantineQuorumException) CurrentForkView(com.radixdlt.statecomputer.forks.CurrentForkView) VerifiedTxnsAndProof(com.radixdlt.ledger.VerifiedTxnsAndProof) PersistentVertexStore(com.radixdlt.hotstuff.bft.PersistentVertexStore) HashUtils(com.radixdlt.crypto.HashUtils) Genesis(com.radixdlt.statecomputer.checkpoint.Genesis) SystemCounters(com.radixdlt.counters.SystemCounters) NoOpForksEpochStore(com.radixdlt.statecomputer.forks.NoOpForksEpochStore) REEvent(com.radixdlt.constraintmachine.REEvent) StateComputerResult(com.radixdlt.ledger.StateComputerLedger.StateComputerResult) VerifiedVertex(com.radixdlt.hotstuff.bft.VerifiedVertex) EngineStore(com.radixdlt.store.EngineStore) TimestampedECDSASignatures(com.radixdlt.hotstuff.TimestampedECDSASignatures) Collectors(java.util.stream.Collectors) ForksEpochStore(com.radixdlt.statecomputer.forks.ForksEpochStore) SystemCountersImpl(com.radixdlt.counters.SystemCountersImpl) List(java.util.List) Stream(java.util.stream.Stream) ConstraintMachineException(com.radixdlt.constraintmachine.exceptions.ConstraintMachineException) CommittedReader(com.radixdlt.sync.CommittedReader) MainnetForksModule(com.radixdlt.statecomputer.forks.modules.MainnetForksModule) Amount(com.radixdlt.application.tokens.Amount) TypeLiteral(com.google.inject.TypeLiteral) LedgerAccumulator(com.radixdlt.ledger.LedgerAccumulator) Mockito.mock(org.mockito.Mockito.mock) BFTValidatorSet(com.radixdlt.hotstuff.bft.BFTValidatorSet) LedgerProof(com.radixdlt.hotstuff.LedgerProof) Serialization(com.radixdlt.serialization.Serialization) WeightedRotatingLeaders(com.radixdlt.hotstuff.liveness.WeightedRotatingLeaders) Sha256Hasher(com.radixdlt.hotstuff.Sha256Hasher) PermissionLevel(com.radixdlt.constraintmachine.PermissionLevel) com.radixdlt.atom(com.radixdlt.atom) ProposerElection(com.radixdlt.hotstuff.liveness.ProposerElection) MockedGenesisModule(com.radixdlt.statecomputer.checkpoint.MockedGenesisModule) View(com.radixdlt.hotstuff.bft.View) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ImmutableList(com.google.common.collect.ImmutableList) MempoolConfig(com.radixdlt.mempool.MempoolConfig) AccumulatorState(com.radixdlt.ledger.AccumulatorState) RadixEngineCheckpointModule(com.radixdlt.statecomputer.checkpoint.RadixEngineCheckpointModule) UInt256(com.radixdlt.utils.UInt256) Before(org.junit.Before) MempoolAddSuccess(com.radixdlt.mempool.MempoolAddSuccess) BFTHeader(com.radixdlt.hotstuff.BFTHeader) EventDispatcher(com.radixdlt.environment.EventDispatcher) HashCode(com.google.common.hash.HashCode) RadixEngine(com.radixdlt.engine.RadixEngine) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) MempoolRelayTrigger(com.radixdlt.mempool.MempoolRelayTrigger) ForksModule(com.radixdlt.statecomputer.forks.ForksModule) Mockito.verify(org.mockito.Mockito.verify) UnverifiedVertex(com.radixdlt.hotstuff.UnverifiedVertex) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) LedgerUpdate(com.radixdlt.ledger.LedgerUpdate) InMemoryEngineStore(com.radixdlt.store.InMemoryEngineStore) ECKeyPair(com.radixdlt.crypto.ECKeyPair) Rule(org.junit.Rule) Ignore(org.junit.Ignore) BFTValidator(com.radixdlt.hotstuff.bft.BFTValidator) Condition(org.assertj.core.api.Condition) Guice(com.google.inject.Guice) InvalidPermissionException(com.radixdlt.constraintmachine.exceptions.InvalidPermissionException) TemporaryFolder(org.junit.rules.TemporaryFolder) AbstractModule(com.google.inject.AbstractModule) LedgerUpdate(com.radixdlt.ledger.LedgerUpdate) MainnetForksModule(com.radixdlt.statecomputer.forks.modules.MainnetForksModule) ForksModule(com.radixdlt.statecomputer.forks.ForksModule) RadixEngineForksLatestOnlyModule(com.radixdlt.statecomputer.forks.RadixEngineForksLatestOnlyModule) PersistentVertexStore(com.radixdlt.hotstuff.bft.PersistentVertexStore) EventDispatcher(com.radixdlt.environment.EventDispatcher) MempoolRelayTrigger(com.radixdlt.mempool.MempoolRelayTrigger) TypeLiteral(com.google.inject.TypeLiteral) MainnetForksModule(com.radixdlt.statecomputer.forks.modules.MainnetForksModule) ProposerElection(com.radixdlt.hotstuff.liveness.ProposerElection) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) LedgerAccumulator(com.radixdlt.ledger.LedgerAccumulator) SystemCounters(com.radixdlt.counters.SystemCounters) NoOpForksEpochStore(com.radixdlt.statecomputer.forks.NoOpForksEpochStore) AbstractModule(com.google.inject.AbstractModule) NoOpCommittedReader(com.radixdlt.sync.NoOpCommittedReader) NoOpForksEpochStore(com.radixdlt.statecomputer.forks.NoOpForksEpochStore) ForksEpochStore(com.radixdlt.statecomputer.forks.ForksEpochStore) DefaultSerialization(com.radixdlt.DefaultSerialization) Serialization(com.radixdlt.serialization.Serialization) Hasher(com.radixdlt.crypto.Hasher) Sha256Hasher(com.radixdlt.hotstuff.Sha256Hasher) NoOpCommittedReader(com.radixdlt.sync.NoOpCommittedReader) CommittedReader(com.radixdlt.sync.CommittedReader) MempoolAddSuccess(com.radixdlt.mempool.MempoolAddSuccess) WeightedRotatingLeaders(com.radixdlt.hotstuff.liveness.WeightedRotatingLeaders)

Example 9 with UInt256

use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.

the class MempoolTransactionHandlerTest method buildSignedTxn.

private Txn buildSignedTxn(REAddr from, REAddr to) throws Exception {
    final UInt256 toTransfer = getLiquidAmount().toSubunits().subtract(Amount.ofTokens(1).toSubunits());
    var entityOperationGroups = List.of(List.of(EntityOperation.from(new AccountVaultEntity(from), ResourceOperation.withdraw(new TokenResource("xrd", REAddr.ofNativeToken()), toTransfer)), EntityOperation.from(new AccountVaultEntity(to), ResourceOperation.deposit(new TokenResource("xrd", REAddr.ofNativeToken()), toTransfer))));
    var operationTxBuilder = new OperationTxBuilder(null, entityOperationGroups, currentForkView);
    var builder = radixEngine.constructWithFees(operationTxBuilder, false, from, NotEnoughNativeTokensForFeesException::new);
    return builder.signAndBuild(hashSigner::sign);
}
Also used : OperationTxBuilder(com.radixdlt.api.core.model.OperationTxBuilder) TokenResource(com.radixdlt.api.core.model.TokenResource) NotEnoughNativeTokensForFeesException(com.radixdlt.api.core.model.NotEnoughNativeTokensForFeesException) AccountVaultEntity(com.radixdlt.api.core.model.entities.AccountVaultEntity) UInt256(com.radixdlt.utils.UInt256)

Example 10 with UInt256

use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.

the class ConstructionFinalizeTest method buildUnsignedTransferTxn.

private UnsignedTxnData buildUnsignedTransferTxn(REAddr from, REAddr to) throws Exception {
    final UInt256 toTransfer = getLiquidAmount().toSubunits().subtract(Amount.ofTokens(1).toSubunits());
    var entityOperationGroups = List.of(List.of(EntityOperation.from(new AccountVaultEntity(from), ResourceOperation.withdraw(new TokenResource("xrd", REAddr.ofNativeToken()), toTransfer)), EntityOperation.from(new AccountVaultEntity(to), ResourceOperation.deposit(new TokenResource("xrd", REAddr.ofNativeToken()), toTransfer))));
    var operationTxBuilder = new OperationTxBuilder(null, entityOperationGroups, currentForkView);
    var builder = radixEngine.constructWithFees(operationTxBuilder, false, from, NotEnoughNativeTokensForFeesException::new);
    return builder.buildForExternalSign();
}
Also used : OperationTxBuilder(com.radixdlt.api.core.model.OperationTxBuilder) TokenResource(com.radixdlt.api.core.model.TokenResource) NotEnoughNativeTokensForFeesException(com.radixdlt.api.core.model.NotEnoughNativeTokensForFeesException) AccountVaultEntity(com.radixdlt.api.core.model.entities.AccountVaultEntity) UInt256(com.radixdlt.utils.UInt256)

Aggregations

UInt256 (com.radixdlt.utils.UInt256)19 Test (org.junit.Test)9 ImmutableList (com.google.common.collect.ImmutableList)8 ECKeyPair (com.radixdlt.crypto.ECKeyPair)7 Stream (java.util.stream.Stream)7 View (com.radixdlt.hotstuff.bft.View)6 List (java.util.List)6 Map (java.util.Map)6 IntStream (java.util.stream.IntStream)6 Inject (com.google.inject.Inject)4 NotEnoughNativeTokensForFeesException (com.radixdlt.api.core.model.NotEnoughNativeTokensForFeesException)4 OperationTxBuilder (com.radixdlt.api.core.model.OperationTxBuilder)4 TokenResource (com.radixdlt.api.core.model.TokenResource)4 AccountVaultEntity (com.radixdlt.api.core.model.entities.AccountVaultEntity)4 Supplier (java.util.function.Supplier)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 AbstractModule (com.google.inject.AbstractModule)3 Guice (com.google.inject.Guice)3 Module (com.google.inject.Module)3 TypeLiteral (com.google.inject.TypeLiteral)3