Search in sources :

Example 26 with ECKeyPair

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

the class BFTValidatorSetTest method testValidate.

@Test
public void testValidate() {
    ECKeyPair k1 = ECKeyPair.generateNew();
    ECKeyPair k2 = ECKeyPair.generateNew();
    ECKeyPair k3 = ECKeyPair.generateNew();
    ECKeyPair k4 = ECKeyPair.generateNew();
    // Rogue signature
    ECKeyPair k5 = ECKeyPair.generateNew();
    BFTNode node1 = BFTNode.create(k1.getPublicKey());
    BFTNode node2 = BFTNode.create(k2.getPublicKey());
    BFTNode node3 = BFTNode.create(k3.getPublicKey());
    BFTNode node4 = BFTNode.create(k4.getPublicKey());
    BFTNode node5 = BFTNode.create(k5.getPublicKey());
    BFTValidator v1 = BFTValidator.from(node1, UInt256.ONE);
    BFTValidator v2 = BFTValidator.from(node2, UInt256.ONE);
    BFTValidator v3 = BFTValidator.from(node3, UInt256.ONE);
    BFTValidator v4 = BFTValidator.from(node4, UInt256.ONE);
    BFTValidatorSet vs = BFTValidatorSet.from(ImmutableSet.of(v1, v2, v3, v4));
    HashCode message = HashUtils.random256();
    // 2 signatures for 4 validators -> fail
    ValidationState vst1 = vs.newValidationState();
    assertTrue(vst1.addSignature(node1, 1L, k1.sign(message)));
    assertFalse(vst1.complete());
    assertTrue(vst1.addSignature(node2, 1L, k2.sign(message)));
    assertFalse(vst1.complete());
    assertEquals(2, vst1.signatures().count());
    // 3 signatures for 4 validators -> pass
    ValidationState vst2 = vs.newValidationState();
    assertTrue(vst2.addSignature(node1, 1L, k1.sign(message)));
    assertFalse(vst1.complete());
    assertTrue(vst2.addSignature(node2, 1L, k2.sign(message)));
    assertFalse(vst1.complete());
    assertTrue(vst2.addSignature(node3, 1L, k3.sign(message)));
    assertTrue(vst2.complete());
    assertEquals(3, vst2.signatures().count());
    // 2 signatures + 1 signature not from set for 4 validators -> fail
    ValidationState vst3 = vs.newValidationState();
    assertTrue(vst3.addSignature(node1, 1L, k1.sign(message)));
    assertFalse(vst3.complete());
    assertTrue(vst3.addSignature(node2, 1L, k2.sign(message)));
    assertFalse(vst3.complete());
    assertFalse(vst3.addSignature(node5, 1L, k5.sign(message)));
    assertFalse(vst3.complete());
    assertEquals(2, vst3.signatures().count());
    // 3 signatures + 1 signature not from set for 4 validators -> pass
    ValidationState vst4 = vs.newValidationState();
    assertTrue(vst4.addSignature(node1, 1L, k1.sign(message)));
    assertFalse(vst3.complete());
    assertTrue(vst4.addSignature(node2, 1L, k2.sign(message)));
    assertFalse(vst3.complete());
    assertFalse(vst4.addSignature(node5, 1L, k5.sign(message)));
    assertFalse(vst3.complete());
    assertTrue(vst4.addSignature(node3, 1L, k3.sign(message)));
    assertTrue(vst4.complete());
    assertEquals(3, vst4.signatures().count());
}
Also used : HashCode(com.google.common.hash.HashCode) 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