use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class CapsuleTest method testSignedExtra.
@Test
public void testSignedExtra() throws Exception {
Capsule c1 = new Capsule();
c1.setPublicData("hello", "world", "I'm", "the coffer");
PrivateKey k1 = TestKeys.privateKey(0);
PrivateKey k2 = TestKeys.privateKey(1);
c1.addSigners(k1);
String kid = c1.addSigner(k2, new Binder("extra", "data", "is", "OK"));
Capsule c2 = new Capsule();
c2.setPublicData("hello", "world", "I'm", "the coffer");
byte[] packed = c1.pack();
Capsule c4 = new Capsule(packed, null);
assertEquals(c1, c4);
assertTrue(c4.isSigned());
assertFalse(c4.isPartiallySigned());
Map<String, Binder> signers = c4.getSigners();
assertEquals(2, signers.size());
ArrayList<AbstractKey> kk = new ArrayList<>();
for (Binder x : signers.values()) {
kk.add((AbstractKey) x.get("key"));
}
assertTrue(kk.contains(k1.getPublicKey()));
assertTrue(kk.contains(k2.getPublicKey()));
Binder extra = c4.getSignerData(k2.getPublicKey());
assertEquals("data", extra.getStringOrThrow("extra"));
assertEquals("OK", extra.getStringOrThrow("is"));
assertEquals(extra.size(), 2);
assertEquals(kid, c4.getSignerId(k2.getPublicKey()));
assertEquals(k2.getPublicKey(), c4.getSignerKey(kid));
assertEquals("data", extra.getStringOrThrow("extra"));
assertEquals("OK", extra.getStringOrThrow("is"));
assertEquals(extra.size(), 2);
extra = c4.getSignerData(kid);
assertEquals("data", extra.getStringOrThrow("extra"));
assertEquals("OK", extra.getStringOrThrow("is"));
assertEquals(extra.size(), 2);
packed[0x456]--;
exception.expect(Capsule.BadSignatureException.class);
c4 = new Capsule(packed, null);
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class BitrustedConnectorTest method connectOnAny.
@Test
public void connectOnAny() throws Exception {
StreamConnector sca = new StreamConnector();
StreamConnector scb = new StreamConnector();
BitrustedConnector ca = new BitrustedConnector(TestKeys.privateKey(0), sca.getInputStream(), scb.getOutputStream());
BitrustedConnector cb = new BitrustedConnector(TestKeys.privateKey(1), scb.getInputStream(), sca.getOutputStream());
Future<Object> connectA = pool.submit(() -> {
ca.connect(null);
return null;
});
// LogPrinter.showDebug(true);
cb.connect(null);
assertTrue(cb.isConnected());
connectA.get();
assertTrue(ca.isConnected());
assertEquals(ca.getMySessionKey(), cb.getRemoteSessionKey());
assertEquals(cb.getMySessionKey(), ca.getRemoteSessionKey());
Future<?> f1 = pool.submit(() -> {
cb.send(Binder.fromKeysValues("hello", "world"));
cb.send(Binder.fromKeysValues("hello", "world2"));
Binder res = Binder.from(cb.receive());
assertEquals("bar", res.getStringOrThrow("foo"));
res = Binder.from(cb.receive());
// System.out.println("------> res "+res);
assertEquals("bar2", res.getStringOrThrow("foo"));
res = Binder.from(cb.receive());
// System.out.println("------> res "+res);
assertEquals("bar", res.getStringOrThrow("foo3"));
return null;
});
Future<?> f2 = pool.submit(() -> {
ca.send(Binder.fromKeysValues("foo", "bar"));
ca.send(Binder.fromKeysValues("foo", "bar2"));
ca.send(Binder.fromKeysValues("foo3", "bar"));
Binder res = Binder.from(ca.receive());
assertEquals("world", res.getStringOrThrow("hello"));
res = Binder.from(ca.receive());
assertEquals("world2", res.getStringOrThrow("hello"));
return null;
});
f1.get();
f2.get();
// ca.
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class SimpleRoleTest method serializeOne.
@Test
public void serializeOne() throws Exception {
SimpleRole sr = new SimpleRole("tr1");
sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey()));
sr.addRequiredReference("ref1", Role.RequiredMode.ALL_OF);
sr.addRequiredReference("ref2", Role.RequiredMode.ALL_OF);
sr.addRequiredReference("ref3", Role.RequiredMode.ANY_OF);
Binder serialized = DefaultBiMapper.serialize(sr);
Role r1 = DefaultBiMapper.deserialize(serialized);
assertEquals(sr, r1);
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class MainTest method nodeStatsTest.
@Test
public void nodeStatsTest() throws Exception {
PrivateKey issuerKey = new PrivateKey(Do.read("./src/test_contracts/keys/reconfig_key.private.unikey"));
TestSpace testSpace = prepareTestSpace(issuerKey);
testSpace.nodes.get(0).config.setStatsIntervalSmall(Duration.ofSeconds(4));
testSpace.nodes.get(0).config.setStatsIntervalBig(Duration.ofSeconds(60));
testSpace.nodes.get(0).config.getKeysWhiteList().add(issuerKey.getPublicKey());
Binder binder = testSpace.client.getStats();
System.out.println(binder.toString());
Instant now;
for (int i = 0; i < 100; i++) {
now = Instant.now();
Contract contract = new Contract(issuerKey);
contract.seal();
testSpace.client.register(contract.getPackedTransaction(), 1500);
contract = new Contract(issuerKey);
contract.seal();
testSpace.client.register(contract.getPackedTransaction(), 1500);
Thread.sleep(4000 - (Instant.now().toEpochMilli() - now.toEpochMilli()));
binder = testSpace.client.getStats();
System.out.println(binder.toString());
}
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class MainTest method sendHello.
protected void sendHello(NodeInfo myNodeInfo, NodeInfo destination, UDPAdapter udpAdapter, DatagramSocket socket) throws InterruptedException {
// System.out.println(">> send froud from " + myNodeInfo.getNumber() + " to " + destination.getNumber());
Binder binder = Binder.fromKeysValues("data", myNodeInfo.getNumber());
UDPAdapter.Block block = udpAdapter.createTestBlock(myNodeInfo.getNumber(), destination.getNumber(), new Random().nextInt(Integer.MAX_VALUE), UDPAdapter.PacketTypes.HELLO, destination.getNodeAddress().getAddress(), destination.getNodeAddress().getPort(), Boss.pack(binder));
sendBlock(block, socket);
}
Aggregations