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());
}
Aggregations