Search in sources :

Example 1 with ItemResult

use of com.icodici.universa.node.ItemResult in project universa by UniversaBlockchain.

the class CLIMainTest method registerManyContractsFromVariousNodes.

// 
// @Test
// public void registerManyContracts() throws Exception {
// 
// int numContracts = 100;
// List<Contract> contracts = new ArrayList<>();
// 
// for (int i = 0; i < numContracts; i++) {
// Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
// c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
// PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
// // let's make this key among owners
// ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
// c.seal();
// 
// contracts.add(c);
// }
// 
// Thread.sleep(500);
// 
// for (int i = 0; i < numContracts; i++) {
// 
// System.out.println("---");
// System.out.println("register contract " + i);
// System.out.println("---");
// final Contract contract = contracts.get(i);
// Thread thread = new Thread(() -> {
// try {
// System.out.println("register contract -> run thread");
// CLIMain.registerContract(contract);
// } catch (IOException e) {
// e.printStackTrace();
// }
// });
// 
// thread.start();
// }
// 
// Thread.sleep(30000);
// 
// for (int i = 0; i < numContracts; i++) {
// System.out.println("---");
// System.out.println("check contract " + i);
// System.out.println("---");
// 
// final Contract contract = contracts.get(i);
// Thread thread = new Thread(() -> {
// System.out.println("check contract -> run thread");
// try {
// callMain2("--probe", contract.getId().toBase64String());
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
// 
// thread.start();
// }
// 
// Thread.sleep(30000);
// 
// System.out.println("---");
// System.out.println("check contracts in order");
// System.out.println("---");
// for (int i = 0; i < numContracts; i++) {
// 
// final Contract contract = contracts.get(i);
// try {
// callMain2("--probe", contract.getId().toBase64String());
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// 
// assertEquals(0, CLIMain.getReporter().getErrors().size());
// }
@Test
public void registerManyContractsFromVariousNodes() throws Exception {
    ClientNetwork clientNetwork1 = new ClientNetwork("http://localhost:8080", CLIMain.getPrivateKey(), null);
    ClientNetwork clientNetwork2 = new ClientNetwork("http://localhost:6002", CLIMain.getPrivateKey(), null);
    ClientNetwork clientNetwork3 = new ClientNetwork("http://localhost:6004", CLIMain.getPrivateKey(), null);
    int numContracts = 10;
    List<Contract> contracts = new ArrayList<>();
    for (int i = 0; i < numContracts; i++) {
        Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
        c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
        PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
        // let's make this key among owners
        ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
        c.seal();
        contracts.add(c);
    }
    Thread.sleep(500);
    for (int i = 0; i < numContracts; i++) {
        // System.out.println("---");
        // System.out.println("register contract " + i);
        // System.out.println("---");
        final Contract contract = contracts.get(i);
        Thread thread1 = new Thread(() -> {
            try {
                // System.out.println("register contract on the client 1 -> run thread");
                CLIMain.registerContract(contract);
                ItemResult r1 = clientNetwork1.register(contract.getPackedTransaction(), 50);
            // System.out.println("register contract on the client 1 -> result: " + r1.toString());
            } catch (IOException e) {
                if (e.getCause() instanceof SocketTimeoutException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else if (e.getCause() instanceof ConnectException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else if (e.getCause() instanceof IllegalStateException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
        Thread thread2 = new Thread(() -> {
            try {
                // System.out.println("register contracz on the client 2 -> run thread");
                CLIMain.registerContract(contract);
                ItemResult r2 = clientNetwork2.register(contract.getPackedTransaction(), 50);
            // System.out.println("register contract on the client 2 -> result: " + r2.toString());
            } catch (IOException e) {
                if (e.getCause() instanceof SocketTimeoutException) {
                    System.err.println(">>>> ERROR 2: " + e.getMessage());
                } else if (e.getCause() instanceof ConnectException) {
                    System.err.println(">>>> ERROR 2: " + e.getMessage());
                } else if (e.getCause() instanceof IllegalStateException) {
                    System.err.println(">>>> ERROR 2: " + e.getMessage());
                } else {
                    e.printStackTrace();
                }
            }
        });
        thread2.start();
        Thread thread3 = new Thread(() -> {
            try {
                // System.out.println("register contract on the client 3 -> run thread");
                CLIMain.registerContract(contract);
                ItemResult r3 = clientNetwork3.register(contract.getPackedTransaction(), 50);
            // System.out.println("register contract on the client 3 -> result: " + r3.toString());
            } catch (IOException e) {
                if (e.getCause() instanceof SocketTimeoutException) {
                    System.err.println(">>>> ERROR 3: " + e.getMessage());
                } else if (e.getCause() instanceof ConnectException) {
                    System.err.println(">>>> ERROR 3: " + e.getMessage());
                } else if (e.getCause() instanceof IllegalStateException) {
                    System.err.println(">>>> ERROR 3: " + e.getMessage());
                } else {
                    e.printStackTrace();
                }
            }
        });
        thread3.start();
    }
    Thread.sleep(1000);
    System.out.println("---");
    System.out.println("check contracts in order");
    System.out.println("---");
    for (int i = 0; i < numContracts; i++) {
        final Contract contract = contracts.get(i);
        callMain2("--probe", contract.getId().toBase64String());
    }
    assertEquals(0, CLIMain.getReporter().getErrors().size());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) IOException(java.io.IOException) KeyRecord(com.icodici.universa.contract.KeyRecord) SocketTimeoutException(java.net.SocketTimeoutException) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ItemResult(com.icodici.universa.node.ItemResult) Contract(com.icodici.universa.contract.Contract) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 2 with ItemResult

use of com.icodici.universa.node.ItemResult in project universa by UniversaBlockchain.

the class CLIMainTest method registerContractFromVariousNetworks.

@Test
public void registerContractFromVariousNetworks() throws Exception {
    final Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    PrivateKey goodKey = c.getKeysToSignWith().iterator().next();
    // let's make this key among owners
    ((SimpleRole) c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey()));
    c.seal();
    // CLIMain.registerContract(c);
    List<ClientNetwork> clientNetworks = new ArrayList<>();
    int numConnections = 10;
    for (int i = 0; i < numConnections; i++) {
        clientNetworks.add(new ClientNetwork("http://localhost:8080", new PrivateKey(2048), null));
    }
    for (int i = 0; i < numConnections; i++) {
        final int index = i;
        try {
            clientNetworks.get(index).ping();
        // System.out.println("result (" + index + "): " + r1.toString());
        } catch (IOException e) {
            if (e.getCause() instanceof SocketTimeoutException) {
                System.err.println(">>>> ERROR 1: " + e.getMessage());
            } else if (e.getCause() instanceof ConnectException) {
                System.err.println(">>>> ERROR 1: " + e.getMessage());
            } else if (e.getCause() instanceof IllegalStateException) {
                System.err.println(">>>> ERROR 1: " + e.getMessage());
            } else {
                e.printStackTrace();
            }
        }
    }
    for (int i = 0; i < numConnections; i++) {
        final int index = i;
        Thread thread1 = new Thread(() -> {
            try {
                ItemResult r1 = clientNetworks.get(index).register(c.getPackedTransaction());
                System.out.println("result from thread (" + index + "): " + r1.toString());
            } catch (IOException e) {
                if (e.getCause() instanceof SocketTimeoutException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else if (e.getCause() instanceof ConnectException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else if (e.getCause() instanceof IllegalStateException) {
                    System.err.println(">>>> ERROR 1: " + e.getMessage());
                } else {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
    }
// Thread.sleep(10000);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) IOException(java.io.IOException) KeyRecord(com.icodici.universa.contract.KeyRecord) SocketTimeoutException(java.net.SocketTimeoutException) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ItemResult(com.icodici.universa.node.ItemResult) Contract(com.icodici.universa.contract.Contract) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 3 with ItemResult

use of com.icodici.universa.node.ItemResult in project universa by UniversaBlockchain.

the class Client method register.

public ItemResult register(byte[] packed, long millisToWait) throws ClientError {
    Object binderResult = protect(() -> httpClient.command("approve", "packedItem", packed).get("itemResult"));
    if (binderResult instanceof ItemResult) {
        ItemResult lastResult = (ItemResult) binderResult;
        if (millisToWait > 0 && lastResult.state.isPending()) {
            Instant end = Instant.now().plusMillis(millisToWait);
            try {
                Contract c = Contract.fromPackedTransaction(packed);
                while (Instant.now().isBefore(end) && lastResult.state.isPending()) {
                    Thread.currentThread().sleep(100);
                    lastResult = getState(c.getId());
                    System.out.println("test: " + lastResult);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Quantiser.QuantiserException e) {
                throw new ClientError(e);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return lastResult;
    }
    System.err.println("test: " + binderResult);
    return ItemResult.UNDEFINED;
}
Also used : ItemResult(com.icodici.universa.node.ItemResult) Instant(java.time.Instant) IOException(java.io.IOException) Contract(com.icodici.universa.contract.Contract)

Example 4 with ItemResult

use of com.icodici.universa.node.ItemResult in project universa by UniversaBlockchain.

the class CLIMain method registerContract.

/**
 * Register a specified contract.
 *
 * @param contract              must be a sealed binary.
 * @param waitTime - wait time for responce.
 */
public static Parcel registerContract(Contract contract, Contract tu, int amount, Set<PrivateKey> tuKeys, boolean withTestPayment, int waitTime) throws IOException {
    List<ErrorRecord> errors = contract.getErrors();
    if (errors.size() > 0) {
        report("contract has errors and can't be submitted for registration");
        report("contract id: " + contract.getId().toBase64String());
        addErrors(errors);
    } else {
        Parcel parcel = ContractsService.createParcel(contract, tu, amount, tuKeys, withTestPayment);
        getClientNetwork().registerParcel(parcel.pack(), waitTime);
        ItemResult r = getClientNetwork().check(contract.getId());
        report("paid contract " + contract.getId() + " submitted with result: " + r.toString());
        report("payment was " + tu.getId());
        report("payment became " + parcel.getPaymentContract().getId());
        report("payment rev " + parcel.getPaymentContract().getRevoking().get(0).getId());
        return parcel;
    }
    return null;
}
Also used : ItemResult(com.icodici.universa.node.ItemResult) Parcel(com.icodici.universa.contract.Parcel)

Example 5 with ItemResult

use of com.icodici.universa.node.ItemResult in project universa by UniversaBlockchain.

the class CLIMain method registerContract.

/**
 * Register a specified contract.
 *
 * @param contract              must be a sealed binary.
 * @param waitTime - wait time for responce.
 * @param fromPackedTransaction - register contract with Contract.getPackedTransaction()
 */
@Deprecated
public static void registerContract(Contract contract, int waitTime, Boolean fromPackedTransaction) throws IOException {
    // checkContract(contract);
    List<ErrorRecord> errors = contract.getErrors();
    if (errors.size() > 0) {
        report("contract has errors and can't be submitted for registration");
        report("contract id: " + contract.getId().toBase64String());
        addErrors(errors);
    } else {
        // contract.seal();
        ItemResult r;
        if (fromPackedTransaction) {
            r = getClientNetwork().register(contract.getPackedTransaction(), waitTime);
        } else {
            r = getClientNetwork().register(contract.getLastSealedBinary(), waitTime);
        }
        report("submitted with result:");
        report(r.toString());
    }
}
Also used : ItemResult(com.icodici.universa.node.ItemResult)

Aggregations

ItemResult (com.icodici.universa.node.ItemResult)12 IOException (java.io.IOException)5 Contract (com.icodici.universa.contract.Contract)4 PrivateKey (com.icodici.crypto.PrivateKey)3 Parcel (com.icodici.universa.contract.Parcel)3 Instant (java.time.Instant)3 Binder (net.sergeych.tools.Binder)3 Test (org.junit.Test)3 ErrorRecord (com.icodici.universa.ErrorRecord)2 HashId (com.icodici.universa.HashId)2 KeyRecord (com.icodici.universa.contract.KeyRecord)2 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)2 ConnectException (java.net.ConnectException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 EncryptionError (com.icodici.crypto.EncryptionError)1 PublicKey (com.icodici.crypto.PublicKey)1 Approvable (com.icodici.universa.Approvable)1 ItemState (com.icodici.universa.node.ItemState)1 com.icodici.universa.node2 (com.icodici.universa.node2)1 HttpURLConnection (java.net.HttpURLConnection)1