Search in sources :

Example 86 with Contract

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

the class CLIMain method doCreateContract.

private static void doCreateContract() throws IOException {
    List<String> sources = new ArrayList<String>((List) options.valuesOf("c"));
    List<String> nonOptions = new ArrayList<String>((List) options.nonOptionArguments());
    for (String opt : nonOptions) {
        sources.addAll(asList(opt.split(",")));
    }
    cleanNonOptionalArguments(sources);
    List<String> names = (List) options.valuesOf("name");
    List updateFields = options.valuesOf("set");
    List updateValues = options.valuesOf("value");
    for (int s = 0; s < sources.size(); s++) {
        String source = sources.get(s);
        String name = null;
        if (names.size() > s)
            name = names.get(s);
        HashMap<String, String> updateFieldsHashMap = new HashMap<>();
        Contract contract = Contract.fromDslFile(source);
        try {
            for (int i = 0; i < updateFields.size(); i++) {
                updateFieldsHashMap.put((String) updateFields.get(i), (String) updateValues.get(i));
            }
        } catch (Exception e) {
        }
        if (updateFieldsHashMap.size() > 0) {
            updateFields(contract, updateFieldsHashMap);
        }
        keysMap().values().forEach(k -> contract.addSignerKey(k));
        // try sign
        if (name == null) {
            name = source.replaceAll("(?i)\\.(yml|yaml)$", ".unicon");
        }
        contract.seal();
        saveContract(contract, name);
        checkContract(contract);
    }
    finish();
}
Also used : Arrays.asList(java.util.Arrays.asList) Contract(com.icodici.universa.contract.Contract) BackingStoreException(java.util.prefs.BackingStoreException) OptionException(joptsimple.OptionException)

Example 87 with Contract

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

the class CLIMain method doUnpackWith.

private static void doUnpackWith() throws IOException {
    List<String> sources = new ArrayList<String>((List) options.valuesOf("unpack"));
    List<String> nonOptions = new ArrayList<String>((List) options.nonOptionArguments());
    for (String opt : nonOptions) {
        sources.addAll(asList(opt.split(",")));
    }
    cleanNonOptionalArguments(sources);
    for (int s = 0; s < sources.size(); s++) {
        String source = sources.get(s);
        Contract contract = loadContract(source, true);
        if (contract != null) {
            try {
                if (contract.check()) {
                    report("unpack contract from " + source);
                    int i = 1;
                    if (contract.getNewItems() != null) {
                        for (Approvable newItem : contract.getNewItems()) {
                            String newItemFileName = source.replaceAll("(?i)\\.(unicon)$", "_new_item_" + i + ".unicon");
                            report("save newItem to " + newItemFileName);
                            // ((Contract) newItem).seal();
                            saveContract((Contract) newItem, newItemFileName);
                            i++;
                        }
                    }
                    i = 1;
                    if (contract.getRevokingItems() != null) {
                        for (Approvable revokeItem : contract.getRevokingItems()) {
                            String revokeItemFileName = source.replaceAll("(?i)\\.(unicon)$", "_revoke_" + i + ".unicon");
                            report("save revokeItem to " + revokeItemFileName);
                            // ((Contract) revokeItem).seal();
                            saveContract((Contract) revokeItem, revokeItemFileName);
                            i++;
                        }
                    }
                // String parentFileName = source.replaceAll("(?i)\\.(unicon)$", "_parent.unicon");
                // report("save parentFileName to " + parentFileName);
                // saveContract(contract, parentFileName);
                } else {
                    addErrors(contract.getErrors());
                }
            } catch (Quantiser.QuantiserException e) {
                addError("QUANTIZER_COST_LIMIT", contract.toString(), e.getMessage());
            }
        }
    }
    finish();
}
Also used : Quantiser(com.icodici.universa.node2.Quantiser) Contract(com.icodici.universa.contract.Contract)

Example 88 with Contract

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

the class CLIMainTest method calculateContractProcessingCostFromManySources.

@Test
public void calculateContractProcessingCostFromManySources() throws Exception {
    // Should use contracts from all sources, call one -cost command for all of them and print cost of processing it.
    Contract contract = createCoin();
    contract.getStateData().set(FIELD_NAME, new Decimal(100));
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    contract.seal();
    // sealCheckTrace(contract, true);
    CLIMain.saveContract(contract, basePath + "contract_for_cost1.unicon");
    CLIMain.saveContract(contract, basePath + "contract_for_cost2.unicon");
    System.out.println("--- cost checking ---");
    // Check 4096 bits signature (8) +
    // Register a version (20)
    int costShouldBe = (int) Math.floor(28 / Quantiser.quantaPerUTN) + 1;
    callMain("--cost", basePath + "contract_for_cost1.unicon", basePath + "contract_for_cost2.unicon");
    System.out.println(output);
    assertTrue(output.indexOf("Contract processing cost is " + costShouldBe + " TU") >= 2);
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 89 with Contract

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

the class CLIMainTest method findContractsInPath.

@Test
public void findContractsInPath() throws Exception {
    // Create contract files (coins and some non-coins)
    File dirFile = new File(rootPath + "contract_subfolder/");
    if (!dirFile.exists())
        dirFile.mkdir();
    dirFile = new File(rootPath + "contract_subfolder/contract_subfolder_level2/");
    if (!dirFile.exists())
        dirFile.mkdir();
    List<Integer> coinValues = Arrays.asList(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60);
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(coinValues);
    for (Contract coin : listOfCoinsWithAmount) {
        int rnd = new Random().nextInt(2);
        String dir = "";
        switch(rnd) {
            case 0:
                dir += "contract_subfolder/";
                break;
            case 1:
                dir += "contract_subfolder/contract_subfolder_level2/";
                break;
        }
        CLIMain.saveContract(coin, rootPath + dir + "Coin_" + coin.getStateData().getIntOrThrow(FIELD_NAME) + ".unicon");
    }
    Contract nonCoin = Contract.fromDslFile("./src/test_files/simple_root_contract_v2.yml");
    nonCoin.seal();
    CLIMain.saveContract(nonCoin, rootPath + "contract_subfolder/NonCoin.unicon");
    CLIMain.saveContract(nonCoin, rootPath + "contract_subfolder/contract_subfolder_level2/NonCoin.unicon");
    // Found wallets
    callMain("-f", rootPath + "contract_subfolder/", "-v", "-r");
    System.out.println(output);
    // Clean up files
    File[] filesToRemove = new File(rootPath + "contract_subfolder/").listFiles();
    for (File file : filesToRemove) {
        file.delete();
    }
    filesToRemove = new File(rootPath + "contract_subfolder/contract_subfolder_level2/").listFiles();
    for (File file : filesToRemove) {
        file.delete();
    }
    Integer total = 0;
    for (Integer i : coinValues) {
        total += i;
    }
    assertTrue(output.indexOf(total + " (TUNC)") >= 0);
}
Also used : File(java.io.File) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 90 with Contract

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

the class CLIMainTest method registerContractAndPrintProcessingCostBreakWhileUnpacking.

@Test
public void registerContractAndPrintProcessingCostBreakWhileUnpacking() throws Exception {
    // Should register contracts and use -cost as key to print cost of processing it.
    Contract contract = createCoin();
    contract.getStateData().set(FIELD_NAME, new Decimal(100));
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    contract.seal();
    // sealCheckTrace(contract, true);
    CLIMain.saveContract(contract, basePath + "contract_for_register_and_cost.unicon");
    System.out.println("--- registering contract (with processing cost print) ---");
    // Check 4096 bits signature (8) +
    // Register a version (20)
    int costShouldBe = 28;
    Contract.setTestQuantaLimit(1);
    callMain("--register", basePath + "contract_for_register_and_cost.unicon", "--cost");
    System.out.println(output);
    assertTrue(output.indexOf("ERROR: QUANTIZER_COST_LIMIT") >= 0);
    Contract.setTestQuantaLimit(-1);
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) 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