Search in sources :

Example 11 with ECKeyPair

use of com.radixdlt.crypto.ECKeyPair 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 12 with ECKeyPair

use of com.radixdlt.crypto.ECKeyPair in project radixdlt by radixdlt.

the class P2PTestNetworkRunner method createInjector.

private static Injector createInjector(MockP2PNetwork p2pNetwork, DeterministicNetwork network, P2PConfig p2pConfig, ECKeyPair nodeKey, RadixNodeUri selfUri, int selfNodeIndex) throws ParseException {
    final var properties = new RuntimeProperties(new JSONObject(), new String[] {});
    return Guice.createInjector(Modules.override(new P2PModule(properties)).with(new AbstractModule() {

        @Override
        protected void configure() {
            bind(TestCounters.class).toInstance(new TestCounters());
            bind(P2PConfig.class).toInstance(p2pConfig);
            bind(RadixNodeUri.class).annotatedWith(Self.class).toInstance(selfUri);
            bind(SystemCounters.class).to(SystemCountersImpl.class).in(Scopes.SINGLETON);
        }

        @Provides
        public PeerOutboundBootstrap peerOutboundBootstrap(TestCounters testCounters) {
            return uri -> {
                testCounters.outboundChannelsBootstrapped += 1;
                p2pNetwork.createChannel(selfNodeIndex, uri);
            };
        }
    }), new PeerDiscoveryModule(), new PeerLivenessMonitorModule(), new DispatcherModule(), new AbstractModule() {

        @Override
        protected void configure() {
            final var dbDir = new TemporaryFolder();
            try {
                dbDir.create();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            bindConstant().annotatedWith(NetworkId.class).to(Network.LOCALNET.getId());
            bind(Addressing.class).toInstance(Addressing.ofNetwork(Network.LOCALNET));
            bindConstant().annotatedWith(DatabaseLocation.class).to(dbDir.getRoot().getAbsolutePath());
            bindConstant().annotatedWith(DatabaseCacheSize.class).to(100_000L);
            bind(ECKeyPair.class).annotatedWith(Self.class).toInstance(nodeKey);
            bind(ECPublicKey.class).annotatedWith(Self.class).toInstance(nodeKey.getPublicKey());
            bind(BFTNode.class).annotatedWith(Self.class).toInstance(BFTNode.create(nodeKey.getPublicKey()));
            bind(String.class).annotatedWith(Self.class).toInstance(Addressing.ofNetwork(Network.LOCALNET).forValidators().of(nodeKey.getPublicKey()).substring(0, 10));
            bind(ECKeyOps.class).toInstance(ECKeyOps.fromKeyPair(nodeKey));
            bind(Environment.class).toInstance(network.createSender(BFTNode.create(nodeKey.getPublicKey())));
            bind(RuntimeProperties.class).toInstance(properties);
            bind(Serialization.class).toInstance(DefaultSerialization.getInstance());
            bind(DeterministicProcessor.class);
            Multibinder.newSetBinder(binder(), StartProcessorOnRunner.class);
            bind(ForkConfig.class).annotatedWith(NewestForkConfig.class).toInstance(new FixedEpochForkConfig("genesis", null, 0L));
        }
    });
}
Also used : Key(com.google.inject.Key) DefaultSerialization(com.radixdlt.DefaultSerialization) DatabaseCacheSize(com.radixdlt.store.DatabaseCacheSize) DispatcherModule(com.radixdlt.modules.DispatcherModule) JSONObject(org.json.JSONObject) ForkConfig(com.radixdlt.statecomputer.forks.ForkConfig) PeerOutboundBootstrap(com.radixdlt.network.p2p.transport.PeerOutboundBootstrap) SystemCounters(com.radixdlt.counters.SystemCounters) DatabaseEnvironment(com.radixdlt.store.DatabaseEnvironment) Multibinder(com.google.inject.multibindings.Multibinder) Objects(java.util.Objects) SystemCountersImpl(com.radixdlt.counters.SystemCountersImpl) P2PModule(com.radixdlt.network.p2p.P2PModule) FixedEpochForkConfig(com.radixdlt.statecomputer.forks.FixedEpochForkConfig) ParseException(org.apache.commons.cli.ParseException) NetworkId(com.radixdlt.networks.NetworkId) StartProcessorOnRunner(com.radixdlt.environment.StartProcessorOnRunner) IntStream(java.util.stream.IntStream) PeerManager(com.radixdlt.network.p2p.PeerManager) Serialization(com.radixdlt.serialization.Serialization) Modules(com.google.inject.util.Modules) PeerLivenessMonitorModule(com.radixdlt.network.p2p.PeerLivenessMonitorModule) ECPublicKey(com.radixdlt.crypto.ECPublicKey) DeterministicNetwork(com.radixdlt.environment.deterministic.network.DeterministicNetwork) RuntimeProperties(com.radixdlt.properties.RuntimeProperties) ImmutableList(com.google.common.collect.ImmutableList) DatabaseLocation(com.radixdlt.store.DatabaseLocation) MessageSelector(com.radixdlt.environment.deterministic.network.MessageSelector) PeerDiscoveryModule(com.radixdlt.network.p2p.PeerDiscoveryModule) AddressBook(com.radixdlt.network.p2p.addressbook.AddressBook) Addressing(com.radixdlt.networks.Addressing) NewestForkConfig(com.radixdlt.statecomputer.forks.NewestForkConfig) Network(com.radixdlt.networks.Network) ECKeyOps(com.radixdlt.crypto.ECKeyOps) Environment(com.radixdlt.environment.Environment) RadixNodeUri(com.radixdlt.network.p2p.RadixNodeUri) IOException(java.io.IOException) DeterministicProcessor(com.radixdlt.environment.deterministic.DeterministicProcessor) Scopes(com.google.inject.Scopes) Injector(com.google.inject.Injector) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) P2PConfig(com.radixdlt.network.p2p.P2PConfig) Provides(com.google.inject.Provides) ECKeyPair(com.radixdlt.crypto.ECKeyPair) Guice(com.google.inject.Guice) Self(com.radixdlt.hotstuff.bft.Self) MessageMutator(com.radixdlt.environment.deterministic.network.MessageMutator) TemporaryFolder(org.junit.rules.TemporaryFolder) AbstractModule(com.google.inject.AbstractModule) DeterministicProcessor(com.radixdlt.environment.deterministic.DeterministicProcessor) Self(com.radixdlt.hotstuff.bft.Self) StartProcessorOnRunner(com.radixdlt.environment.StartProcessorOnRunner) TemporaryFolder(org.junit.rules.TemporaryFolder) FixedEpochForkConfig(com.radixdlt.statecomputer.forks.FixedEpochForkConfig) RuntimeProperties(com.radixdlt.properties.RuntimeProperties) PeerLivenessMonitorModule(com.radixdlt.network.p2p.PeerLivenessMonitorModule) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) ForkConfig(com.radixdlt.statecomputer.forks.ForkConfig) FixedEpochForkConfig(com.radixdlt.statecomputer.forks.FixedEpochForkConfig) NewestForkConfig(com.radixdlt.statecomputer.forks.NewestForkConfig) P2PConfig(com.radixdlt.network.p2p.P2PConfig) ECKeyPair(com.radixdlt.crypto.ECKeyPair) IOException(java.io.IOException) AbstractModule(com.google.inject.AbstractModule) JSONObject(org.json.JSONObject) ECPublicKey(com.radixdlt.crypto.ECPublicKey) SystemCountersImpl(com.radixdlt.counters.SystemCountersImpl) DispatcherModule(com.radixdlt.modules.DispatcherModule) P2PModule(com.radixdlt.network.p2p.P2PModule) PeerDiscoveryModule(com.radixdlt.network.p2p.PeerDiscoveryModule)

Example 13 with ECKeyPair

use of com.radixdlt.crypto.ECKeyPair in project radixdlt by radixdlt.

the class KeySignTestScenarioRunner method doRunTestVector.

@Override
public void doRunTestVector(KeySignTestVector testVector) throws AssertionError {
    ECKeyPair keyPair = null;
    try {
        keyPair = ECKeyPair.fromPrivateKey(Bytes.fromHexString(testVector.input.privateKey));
    } catch (Exception e) {
        throw new AssertionError("Failed to construct private key from hex", e);
    }
    byte[] unhashedEncodedMessage = testVector.input.messageToSign.getBytes(StandardCharsets.UTF_8);
    byte[] hashedMessageToSign = sha256Hash(unhashedEncodedMessage);
    ECDSASignature signature = keyPair.sign(hashedMessageToSign, true, true);
    assertEquals(testVector.expected.signature.r, signature.getR().toString(16));
    assertEquals(testVector.expected.signature.s, signature.getS().toString(16));
}
Also used : ECKeyPair(com.radixdlt.crypto.ECKeyPair) ECDSASignature(com.radixdlt.crypto.ECDSASignature)

Example 14 with ECKeyPair

use of com.radixdlt.crypto.ECKeyPair in project radixdlt by radixdlt.

the class MempoolTest method mempool_removes_multiple_conflicts_on_commit.

@Test
public void mempool_removes_multiple_conflicts_on_commit() throws Exception {
    // Arrange
    getInjector().injectMembers(this);
    ECKeyPair keyPair = ECKeyPair.generateNew();
    var txn = createTxn(keyPair, 2);
    MempoolAdd mempoolAdd = MempoolAdd.create(txn);
    processor.handleMessage(getFirstPeer(), mempoolAdd, null);
    var txn2 = createTxn(keyPair, 3);
    processor.handleMessage(getFirstPeer(), MempoolAdd.create(txn2), null);
    // Act
    var txn3 = createTxn(keyPair, 1);
    var proof = mock(LedgerProof.class);
    when(proof.getAccumulatorState()).thenReturn(new AccumulatorState(genesisTxns.getTxns().size() + 1, HashUtils.random256()));
    when(proof.getStateVersion()).thenReturn((long) genesisTxns.getTxns().size() + 1);
    when(proof.getView()).thenReturn(View.of(1));
    var commandsAndProof = VerifiedTxnsAndProof.create(List.of(txn3), proof);
    stateComputer.commit(commandsAndProof, null);
    // Assert
    assertThat(systemCounters.get(CounterType.MEMPOOL_CURRENT_SIZE)).isZero();
}
Also used : AccumulatorState(com.radixdlt.ledger.AccumulatorState) ECKeyPair(com.radixdlt.crypto.ECKeyPair) Test(org.junit.Test)

Example 15 with ECKeyPair

use of com.radixdlt.crypto.ECKeyPair in project radixdlt by radixdlt.

the class MempoolTest method add_conflicting_commands_to_mempool.

@Test
public void add_conflicting_commands_to_mempool() throws Exception {
    // Arrange
    getInjector().injectMembers(this);
    ECKeyPair keyPair = ECKeyPair.generateNew();
    var txn = createTxn(keyPair, 2);
    MempoolAdd mempoolAdd = MempoolAdd.create(txn);
    processor.handleMessage(getFirstPeer(), mempoolAdd, null);
    // Act
    var txn2 = createTxn(keyPair, 1);
    MempoolAdd mempoolAddSuccess2 = MempoolAdd.create(txn2);
    processor.handleMessage(getFirstPeer(), mempoolAddSuccess2, null);
    // Assert
    assertThat(systemCounters.get(CounterType.MEMPOOL_CURRENT_SIZE)).isEqualTo(2);
}
Also used : ECKeyPair(com.radixdlt.crypto.ECKeyPair) Test(org.junit.Test)

Aggregations

ECKeyPair (com.radixdlt.crypto.ECKeyPair)26 Test (org.junit.Test)19 HashCode (com.google.common.hash.HashCode)10 AbstractModule (com.google.inject.AbstractModule)6 SystemCounters (com.radixdlt.counters.SystemCounters)5 BFTNode (com.radixdlt.hotstuff.bft.BFTNode)5 TypeLiteral (com.google.inject.TypeLiteral)4 TransferrableTokensParticle (com.radixdlt.application.tokens.TransferrableTokensParticle)4 CMError (com.radixdlt.constraintmachine.CMError)4 CMInstruction (com.radixdlt.constraintmachine.CMInstruction)4 ImmutableList (com.google.common.collect.ImmutableList)3 Guice (com.google.inject.Guice)3 Provides (com.google.inject.Provides)3 SystemCountersImpl (com.radixdlt.counters.SystemCountersImpl)3 ECPublicKey (com.radixdlt.crypto.ECPublicKey)3 EventDispatcher (com.radixdlt.environment.EventDispatcher)3 Self (com.radixdlt.hotstuff.bft.Self)3 RadixAddress (com.radixdlt.identifiers.RadixAddress)3 AccumulatorState (com.radixdlt.ledger.AccumulatorState)3 Key (com.google.inject.Key)2