Search in sources :

Example 46 with Client

use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.

the class ResearchTest method registerManySimpleContractsWhite.

@Test
public void registerManySimpleContractsWhite() throws Exception {
    int CONTRACTS_PER_THREAD = 1000;
    // max 10
    int THREADS_COUNT = 10;
    AtomicLong totalCounter = new AtomicLong(0);
    RunnablePrm r = (threadIndex) -> {
        try {
            Client cln = clients.get(threadIndex);
            for (int i = 0; i < CONTRACTS_PER_THREAD; ++i) {
                Contract whiteContract = new Contract(TestKeys.privateKey(threadIndex));
                whiteContract.seal();
                ItemResult itemResult = cln.register(whiteContract.getPackedTransaction(), 15000);
                assertEquals(ItemState.APPROVED, itemResult.state);
                totalCounter.incrementAndGet();
            }
        } catch (Exception e) {
            System.out.println("error: " + e.toString());
        }
    };
    List<Thread> threadList = new ArrayList<>();
    for (int i = 0; i < THREADS_COUNT; ++i) {
        final int threadIndex = i;
        Thread t = new Thread(() -> r.run(threadIndex));
        t.start();
        threadList.add(t);
    }
    Thread heartbeat = new Thread(() -> {
        try {
            while (true) {
                Thread.sleep(1000);
                System.out.println("totalCounter: " + totalCounter.get());
            }
        } catch (Exception e) {
            System.out.println("error: " + e.toString());
        }
    });
    heartbeat.start();
    for (Thread t : threadList) t.join();
    heartbeat.interrupt();
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) TestCase(com.icodici.universa.TestCase) TestKeys(com.icodici.universa.TestKeys) com.icodici.universa.node(com.icodici.universa.node) KeyAddress(com.icodici.crypto.KeyAddress) Do(net.sergeych.tools.Do) Set(java.util.Set) Client(com.icodici.universa.node2.network.Client) Bytes(net.sergeych.utils.Bytes) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) PublicKey(com.icodici.crypto.PublicKey) HashId(com.icodici.universa.HashId) java.io(java.io) Contract(com.icodici.universa.contract.Contract) org.junit(org.junit) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) Client(com.icodici.universa.node2.network.Client) Contract(com.icodici.universa.contract.Contract)

Example 47 with Client

use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.

the class ScriptEngineTest method prepareTestSpace.

private TestSpace prepareTestSpace(PrivateKey key) throws Exception {
    TestSpace testSpace = new TestSpace();
    testSpace.nodes = new ArrayList<>();
    for (int i = 0; i < 4; i++) testSpace.nodes.add(createMain("node" + (i + 1), "", false));
    testSpace.node = testSpace.nodes.get(0);
    assertEquals("http://localhost:8080", testSpace.node.myInfo.internalUrlString());
    assertEquals("http://localhost:8080", testSpace.node.myInfo.publicUrlString());
    testSpace.myKey = key;
    testSpace.client = new Client(testSpace.myKey, testSpace.node.myInfo, null);
    testSpace.clients = new ArrayList();
    for (int i = 0; i < 4; i++) testSpace.clients.add(new Client(testSpace.myKey, testSpace.nodes.get(i).myInfo, null));
    for (Main m : testSpace.nodes) {
        while (m.node.isSanitating()) Thread.sleep(100);
    }
    return testSpace;
}
Also used : Client(com.icodici.universa.node2.network.Client)

Example 48 with Client

use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.

the class JarNetworkTest method registerManySimpleContractsWhite.

@Test
public void registerManySimpleContractsWhite() throws Exception {
    int CONTRACTS_PER_THREAD = 20;
    int THREADS_COUNT = 4;
    AtomicLong totalCounter = new AtomicLong(0);
    Runnable r = () -> {
        try {
            Client cln = createWhiteClient();
            int nodeNumber = cln.getNodeNumber();
            System.out.println("nodeNumber: " + nodeNumber);
            for (int i = 0; i < CONTRACTS_PER_THREAD; ++i) {
                Contract whiteContract = new Contract(TestKeys.privateKey(nodeNumber - 1));
                whiteContract.seal();
                ItemResult itemResult = cln.register(whiteContract.getPackedTransaction(), 15000);
                assertEquals(ItemState.APPROVED, itemResult.state);
                totalCounter.incrementAndGet();
            }
        } catch (Exception e) {
            System.out.println("error: " + e.toString());
        }
    };
    List<Thread> threadList = new ArrayList<>();
    for (int i = 0; i < THREADS_COUNT; ++i) {
        Thread t = new Thread(r);
        t.start();
        threadList.add(t);
    }
    Thread heartbeat = new Thread(() -> {
        try {
            while (true) {
                Thread.sleep(1000);
                System.out.println("totalCounter: " + totalCounter.get());
            }
        } catch (Exception e) {
            System.out.println("totalCounter: " + totalCounter.get());
        }
    });
    heartbeat.start();
    for (Thread t : threadList) t.join();
    heartbeat.interrupt();
    heartbeat.join();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Client(com.icodici.universa.node2.network.Client) Test(org.junit.Test)

Example 49 with Client

use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.

the class UBotSessionsProTest method createSessionConcurrentRequests.

@Test
public void createSessionConcurrentRequests() throws Exception {
    Client client = new Client("universa.pro", null, TestKeys.privateKey(0));
    Contract executableContract = new Contract(TestKeys.privateKey(1));
    executableContract.getStateData().put("cloud_methods", Binder.of("getRandom", Binder.of("pool", Binder.of("size", 5), "quorum", Binder.of("size", 4))));
    executableContract.getStateData().put("js", "");
    executableContract.seal();
    assertEquals(client.register(executableContract.getPackedTransaction(), 100000).state, ItemState.APPROVED);
    System.out.println("EID = " + executableContract.getId());
    List<Contract> requestContracts = new ArrayList<>();
    for (int i = 0; i < client.size(); i++) {
        Contract requestContract = new Contract(TestKeys.privateKey(2));
        requestContract.getStateData().put("executable_contract_id", executableContract.getId());
        requestContract.getStateData().put("method_name", "getRandom");
        requestContract.getStateData().put("method_args", Do.listOf(1000));
        ContractsService.addReferenceToContract(requestContract, executableContract, "executable_contract_constraint", Reference.TYPE_EXISTING_DEFINITION, Do.listOf("ref.id==this.state.data.executable_contract_id"), true);
        requestContract.seal();
        requestContract.getTransactionPack().addReferencedItem(executableContract);
        requestContracts.add(requestContract);
    }
    for (int i = 0; i < client.size(); i++) {
        int finalI = i;
        Do.inParallel(() -> {
            System.out.println(client.getClient(finalI).command("ubotCreateSession", "packedRequest", requestContracts.get(finalI).getPackedTransaction()));
        });
    }
    AtomicInteger readyCounter = new AtomicInteger();
    AsyncEvent readyEvent = new AsyncEvent();
    for (int i = 0; i < client.size(); i++) {
        int finalI = i;
        Do.inParallel(() -> {
            while (true) {
                Binder res = client.getClient(finalI).command("ubotGetSession", "executableContractId", executableContract.getId());
                System.out.println(client.getClient(finalI).getNodeNumber() + " " + res);
                Thread.sleep(500);
                if (res.get("session") != null && res.getBinderOrThrow("session").getString("state", "").equals("OPERATIONAL")) {
                    if (readyCounter.incrementAndGet() == client.size()) {
                        readyEvent.fire();
                    }
                    break;
                }
            }
        });
    }
    readyEvent.await();
}
Also used : Binder(net.sergeych.tools.Binder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Client(com.icodici.universa.node2.network.Client) AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 50 with Client

use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.

the class UBotSessionsTest method registerUBotRegistryContract.

@Ignore
@Test
public void registerUBotRegistryContract() throws Exception {
    // KeyAddress ka = new KeyAddress("Zau3tT8YtDkj3UDBSznrWHAjbhhU4SXsfQLWDFsv5vw24TLn6s");
    // //for(int i = 0; i < TestKeys.binaryKeys.length;i++) {
    // //    if(TestKeys.publicKey(i).isMatchingKeyAddress(ka)) {
    // //        System.out.println(i);
    // //    }
    // //}
    // 
    // PrivateKey key = new PrivateKey(Do.read("/Users/romanu/Downloads/ru/roman.uskov.privateKey.unikey"));
    // System.out.println(key.getPublicKey().isMatchingKeyAddress(new KeyAddress("Zau3tT8YtDkj3UDBSznrWHAjbhhU4SXsfQLWDFsv5vw24TLn6s")));
    Client client = new Client("universa.local", null, TestKeys.privateKey(0));
    Contract ubotRegistry = Contract.fromPackedTransaction(client.getServiceContracts().getBinaryOrThrow("ubot_registry_contract"));
    System.out.println(client.register(ubotRegistry.getPackedTransaction(), 10000));
}
Also used : Client(com.icodici.universa.node2.network.Client) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Client (com.icodici.universa.node2.network.Client)44 Test (org.junit.Test)37 PrivateKey (com.icodici.crypto.PrivateKey)30 ItemResult (com.icodici.universa.node.ItemResult)29 Contract (com.icodici.universa.contract.Contract)22 Binder (net.sergeych.tools.Binder)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 IOException (java.io.IOException)20 HashId (com.icodici.universa.HashId)19 Ignore (org.junit.Ignore)16 KeyAddress (com.icodici.crypto.KeyAddress)15 AsyncEvent (net.sergeych.tools.AsyncEvent)12 PublicKey (com.icodici.crypto.PublicKey)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 BigDecimal (java.math.BigDecimal)9 java.util (java.util)9 RoleLink (com.icodici.universa.contract.roles.RoleLink)8 com.icodici.universa.contract (com.icodici.universa.contract)7 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)7 ConnectException (java.net.ConnectException)7