Search in sources :

Example 91 with Binder

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);
}
Also used : Binder(net.sergeych.tools.Binder) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 92 with Binder

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.
}
Also used : Binder(net.sergeych.tools.Binder) StreamConnector(net.sergeych.tools.StreamConnector) Test(org.junit.Test)

Example 93 with Binder

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);
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) Binder(net.sergeych.tools.Binder) Test(org.junit.Test)

Example 94 with Binder

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());
    }
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Instant(java.time.Instant) Test(org.junit.Test)

Example 95 with Binder

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);
}
Also used : Binder(net.sergeych.tools.Binder)

Aggregations

Binder (net.sergeych.tools.Binder)136 Test (org.junit.Test)67 PrivateKey (com.icodici.crypto.PrivateKey)21 Contract (com.icodici.universa.contract.Contract)14 PublicKey (com.icodici.crypto.PublicKey)13 IOException (java.io.IOException)11 KeyRecord (com.icodici.universa.contract.KeyRecord)10 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)10 Yaml (org.yaml.snakeyaml.Yaml)9 Bytes (net.sergeych.utils.Bytes)8 Decimal (com.icodici.universa.Decimal)7 ListRole (com.icodici.universa.contract.roles.ListRole)6 NonNull (org.checkerframework.checker.nullness.qual.NonNull)5 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 XStream (com.thoughtworks.xstream.XStream)4 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)4 List (java.util.List)4 RoleLink (com.icodici.universa.contract.roles.RoleLink)3 ItemResult (com.icodici.universa.node.ItemResult)3