Search in sources :

Example 46 with Contract

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

the class CLIMain method importContract.

/**
 * Import contract from specified yaml, xml or json file.
 *
 * @param sourceName
 *
 * @return loaded and from loaded data created Contract.
 */
private static Contract importContract(String sourceName) throws IOException {
    ContractFileTypes fileType = getFileType(sourceName);
    Contract contract = null;
    File pathFile = new File(sourceName);
    if (pathFile.exists()) {
        try {
            Binder binder;
            FileReader reader = new FileReader(sourceName);
            if (fileType == ContractFileTypes.YAML) {
                Yaml yaml = new Yaml();
                binder = Binder.convertAllMapsToBinders(yaml.load(reader));
            } else if (fileType == ContractFileTypes.JSON) {
                Gson gson = new GsonBuilder().create();
                binder = Binder.convertAllMapsToBinders(gson.fromJson(reader, Binder.class));
            } else {
                XStream xstream = new XStream(new DomDriver());
                xstream.registerConverter(new MapEntryConverter());
                xstream.alias("contract", Binder.class);
                binder = Binder.convertAllMapsToBinders(xstream.fromXML(reader));
            }
            BiDeserializer bm = DefaultBiMapper.getInstance().newDeserializer();
            contract = new Contract();
            contract.deserialize(binder, bm);
            report(">>> imported contract: " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(contract.getCreatedAt()));
            report("import from " + fileType.toString().toLowerCase() + " ok");
        } catch (Exception e) {
            addError(Errors.FAILURE.name(), sourceName, e.getMessage());
        }
    } else {
        addError(Errors.NOT_FOUND.name(), sourceName, "Path " + sourceName + " does not exist");
    // usage("Path " + sourceName + " does not exist");
    }
    return contract;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) XStream(com.thoughtworks.xstream.XStream) Gson(com.google.gson.Gson) BiDeserializer(net.sergeych.biserializer.BiDeserializer) Yaml(org.yaml.snakeyaml.Yaml) BackingStoreException(java.util.prefs.BackingStoreException) OptionException(joptsimple.OptionException) Binder(net.sergeych.tools.Binder) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) Contract(com.icodici.universa.contract.Contract)

Example 47 with Contract

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

the class CLIMain method findContracts.

/**
 * Find contracts in the given path. Looking for files with .unicon extensions.
 *
 * @param path
 * @param recursively - make search in subfolders too.
 *
 * @return
 */
public static HashMap<String, Contract> findContracts(String path, Boolean recursively) {
    HashMap<String, Contract> foundContracts = new HashMap<>();
    List<File> foundContractFiles = new ArrayList<>();
    File pathFile = new File(path);
    if (pathFile.exists()) {
        fillWithContractsFiles(foundContractFiles, path, recursively);
        Contract contract;
        for (File file : foundContractFiles) {
            try {
                contract = loadContract(file.getAbsolutePath());
                foundContracts.put(file.getAbsolutePath(), contract);
            } catch (RuntimeException e) {
                addError(Errors.FAILURE.name(), file.getAbsolutePath(), e.getMessage());
            } catch (IOException e) {
                addError(Errors.FAILURE.name(), file.getAbsolutePath(), e.getMessage());
            }
        }
    } else {
        addError(Errors.NOT_FOUND.name(), path, "Path " + path + " does not exist");
    // usage("Path " + path + " does not exist");
    }
    return foundContracts;
}
Also used : Contract(com.icodici.universa.contract.Contract)

Example 48 with Contract

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

the class CLIMainTest method calculateContractProcessingCostFromBinary.

@Test
public void calculateContractProcessingCostFromBinary() throws Exception {
    // Should use a binary contract, call -cost command 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_cost.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_cost.unicon");
    System.out.println(output);
    assertTrue(output.indexOf("Contract processing cost is " + costShouldBe + " TU") >= 0);
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 49 with Contract

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

the class CLIMainTest method revokeContractVirtual.

@Test
public void revokeContractVirtual() throws Exception {
    Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    c.addSignerKeyFromFile(rootPath + "keys/tu_key.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();
    System.out.println("---");
    System.out.println("register contract");
    System.out.println("---");
    CLIMain.registerContract(c);
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contract");
    System.out.println("---");
    callMain("--probe", c.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.APPROVED.name()) >= 0);
    PrivateKey issuer1 = TestKeys.privateKey(1);
    Contract tc = ContractsService.createRevocation(c, issuer1, goodKey);
    assertTrue(tc.check());
    System.out.println("---");
    System.out.println("register revoking contract");
    System.out.println("---");
    CLIMain.registerContract(tc);
    Thread.sleep(2500);
    System.out.println("---");
    System.out.println("check revoking contract");
    System.out.println("---");
    callMain("--probe", tc.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.APPROVED.name()) >= 1);
    Thread.sleep(1500);
    System.out.println("---");
    System.out.println("check contract");
    System.out.println("---");
    callMain("--probe", c.getId().toBase64String());
    System.out.println(output);
    assertTrue(output.indexOf(ItemState.REVOKED.name()) >= 0);
}
Also used : KeyRecord(com.icodici.universa.contract.KeyRecord) PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 50 with Contract

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

the class CLIMainTest method registerContractWithDefaultPayment.

@Test
public void registerContractWithDefaultPayment() 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();
    CLIMain.saveContract(contract, basePath + "contract_for_register_and_cost.unicon");
    System.out.println("--- get tu ---");
    String tuContract = getApprovedTUContract();
    Contract tu = CLIMain.loadContract(tuContract);
    System.out.println("check tu " + tu.getId().toBase64String());
    callMain2("--probe", tu.getId().toBase64String(), "--verbose");
    LogPrinter.showDebug(true);
    System.out.println("--- registering contract (with processing cost print) ---");
    callMain("--register", basePath + "contract_for_register_and_cost.unicon", "--tu", tuContract, "-k", rootPath + "keys/stepan_mamontov.private.unikey", "-wait", "5000");
    System.out.println(output);
    assertTrue(output.indexOf("paid contract " + contract.getId() + " submitted with result: ItemResult<APPROVED") >= 0);
}
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