Search in sources :

Example 56 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class CLIMainTest method anonymizeRole.

@Test
public void anonymizeRole() throws Exception {
    callMain2("-create", rootPath + "TokenDSLTemplate.yml", "-name", basePath + "forRoleAnonymizing.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
    assertTrue(new File(basePath + "forRoleAnonymizing.unicon").exists());
    callMain("-anonymize", basePath + "forRoleAnonymizing.unicon", "-role", "issuer");
    assertTrue(new File(basePath + "forRoleAnonymizing_anonymized.unicon").exists());
    System.out.println(output);
    PrivateKey key = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
    Contract contract = CLIMain.loadContract(basePath + "forRoleAnonymizing_anonymized.unicon", true);
    assertFalse(contract.getIssuer().getKeys().contains(key.getPublicKey()));
    Contract anonPublishedContract = new Contract(contract.getLastSealedBinary());
    assertFalse(anonPublishedContract.getIssuer().getKeys().contains(key.getPublicKey()));
    assertFalse(anonPublishedContract.getSealedByKeys().contains(key.getPublicKey()));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) File(java.io.File) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 57 with Contract

use of com.icodici.universa.contract.Contract 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 58 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class CLIMainTest method exportTUTest.

// @Test
public void exportTUTest() throws Exception {
    callMain2("--network");
    Contract c1 = CLIMain.loadContract(rootPath + "test_access.unicon");
    System.out.println(c1.getId());
    callMain2("--probe", c1.getId().toBase64String());
    // System.out.println(Bytes.toHex(c1.getIssuer().getKeys().iterator().next().pack()));
    // Contract c2 = CLIMain.loadContract(rootPath + "test_access_rev1.unicon");
    // System.out.println(c2.getId());
    // callMain2("--probe", c2.getId().toBase64String());
    // Contract c3 = CLIMain.loadContract(rootPath + "test_access_2_rev1_rev2.unicon");
    // System.out.println(c3.getId());
    // callMain2("--probe", c3.getId().toBase64String());
    callMain2("-create", rootPath + "TokenDSLTemplate.yml", "-name", rootPath + "realToken.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
    assertTrue(new File(rootPath + "realToken.unicon").exists());
    callMain2("-register", rootPath + "realToken.unicon", "-tu", rootPath + "test_access.unicon", "-tutest", "-k", rootPath + "at70.privateKey.unikey", "-wait", "1000");
    callMain("-e", rootPath + "test_access.unicon", "-pretty");
    System.out.println(output);
    Thread.sleep(10000);
    Contract c5 = CLIMain.loadContract(rootPath + "test_access.unicon");
    System.out.println(c5.getId());
    callMain2("--probe", c5.getId().toBase64String());
    Contract c6 = CLIMain.loadContract(rootPath + "realToken.unicon");
    System.out.println(c6.getId());
    callMain2("--probe", c6.getId().toBase64String());
}
Also used : Contract(com.icodici.universa.contract.Contract) File(java.io.File)

Example 59 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class CLIMainTest method checkTransactionPack.

@Test
public void checkTransactionPack() throws Exception {
    Contract r = new Contract(ownerKey1);
    r.seal();
    Contract c = r.createRevision(ownerKey1);
    Contract n = c.split(1)[0];
    n.seal();
    c.seal();
    c.addNewItems(n);
    String path = rootPath + "/testtranspack.unicon";
    // path = "/Users/sergeych/dev/!/e7810197-d148-4936-866b-44daae182e83.transaction";
    c.seal();
    CLIMain.saveContract(c, path, true, true);
    // try (FileOutputStream fs = new FileOutputStream(path)) {
    // fs.write(c.getPackedTransaction());
    // fs.close();
    // }
    callMain("--check", path, "-v");
    System.out.println(output);
}
Also used : Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 60 with Contract

use of com.icodici.universa.contract.Contract 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)

Aggregations

Contract (com.icodici.universa.contract.Contract)131 Test (org.junit.Test)67 Decimal (com.icodici.universa.Decimal)31 PrivateKey (com.icodici.crypto.PrivateKey)24 File (java.io.File)16 AsyncEvent (net.sergeych.tools.AsyncEvent)16 TimeoutException (java.util.concurrent.TimeoutException)14 Binder (net.sergeych.tools.Binder)14 KeyRecord (com.icodici.universa.contract.KeyRecord)13 HashSet (java.util.HashSet)9 Parcel (com.icodici.universa.contract.Parcel)8 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)7 Quantiser (com.icodici.universa.node2.Quantiser)7 PublicKey (com.icodici.crypto.PublicKey)6 TransactionPack (com.icodici.universa.contract.TransactionPack)6 IOException (java.io.IOException)6 BackingStoreException (java.util.prefs.BackingStoreException)6 OptionException (joptsimple.OptionException)6 HashId (com.icodici.universa.HashId)5 Arrays.asList (java.util.Arrays.asList)5