Search in sources :

Example 66 with Contract

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

the class CLIMainTest method saveAndLoad.

@Test
public void saveAndLoad() throws Exception {
    // Should register contracts and use -cost as key to print cost of processing it.
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(rootPath + "keys/tu_key.private.unikey"));
    PrivateKey stepaPrivateKey = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey"));
    Contract stepaTU = Contract.fromDslFile(rootPath + "StepaTU.yml");
    stepaTU.addSignerKey(manufacturePrivateKey);
    stepaTU.seal();
    stepaTU.check();
    // stepaTU.setIsTU(true);
    stepaTU.traceErrors();
    CLIMain.saveContract(stepaTU, basePath + "save_and_load.unicon");
    callMain2("--register", basePath + "save_and_load.unicon", "--cost");
    System.out.println("--- save --- " + stepaTU.getId());
    Contract loaded = CLIMain.loadContract(basePath + "save_and_load.unicon", true);
    System.out.println("--- load --- " + loaded.getId());
    assertTrue(loaded.getId().equals(stepaTU.getId()));
    Contract paymentDecreased = loaded.createRevision(stepaPrivateKey);
    paymentDecreased.getStateData().set("transaction_units", 99);
    paymentDecreased.seal();
    CLIMain.saveContract(paymentDecreased, basePath + "save_and_load.unicon");
    System.out.println("--- save 2 --- " + paymentDecreased.getId());
    callMain("--register", basePath + "save_and_load.unicon", "--cost");
    Contract loaded2 = CLIMain.loadContract(basePath + "save_and_load.unicon", true);
    System.out.println("--- load 2 --- " + loaded2.getId());
    assertTrue(loaded2.getId().equals(paymentDecreased.getId()));
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 67 with Contract

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

the class CLIMainTest method revokeContractWithoutKey.

@Test
public void revokeContractWithoutKey() throws Exception {
    String contractFileName = basePath + "contract_for_revoke1.unicon";
    String tuContract = getApprovedTUContract();
    callMain2("--register", contractFileName, "--verbose", "--tu", tuContract, "--k", rootPath + "keys/stepan_mamontov.private.unikey");
    callMain2("-revoke", contractFileName, "-v");
    Thread.sleep(1500);
    System.out.println("probe after revoke");
    Contract c = CLIMain.loadContract(contractFileName);
    callMain("--probe", c.getId().toBase64String(), "--verbose");
    System.out.println(output);
    assertEquals(0, errors.size());
    assertTrue(output.indexOf(ItemState.REVOKED.name()) < 0);
}
Also used : Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 68 with Contract

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

the class CLIMainTest method packContractWithCounterPartsWithName.

@Test
public void packContractWithCounterPartsWithName() throws Exception {
    String contractFileName = basePath + "coin100.unicon";
    String savingFileName = basePath + "packed.unicon";
    Contract contract = createCoin();
    contract.getStateData().set(FIELD_NAME, new Decimal(100));
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    contract.seal();
    CLIMain.saveContract(contract, contractFileName);
    callMain2("--check", contractFileName, "-v");
    callMain2("-pack-with", contractFileName, "-add-sibling", basePath + "contract2.unicon", "-add-revoke", basePath + "contract_for_revoke1.unicon", "-name", savingFileName, "-v");
    callMain("--check", savingFileName, "-v");
    System.out.println(output);
// assertEquals(0, errors.size());
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 69 with Contract

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

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

the class SplitJoinPermission method checkSplitJoinCase.

private boolean checkSplitJoinCase(Contract changed) {
    Decimal splitJoinSum = Decimal.ZERO;
    for (Contract c : changed.getSiblings()) {
        splitJoinSum = splitJoinSum.add(new Decimal(c.getStateData().getString(fieldName)));
    }
    Decimal rSum = Decimal.ZERO;
    for (Approvable r : changed.getRevokingItems()) {
        if (r instanceof Contract) {
            Contract c = (Contract) r;
            if (!isMergeable(c) || !validateMergeFields(changed, c))
                return false;
            rSum = rSum.add(new Decimal(((Contract) r).getStateData().getString(fieldName)));
        }
    }
    return splitJoinSum.compareTo(rSum) == 0;
}
Also used : Decimal(com.icodici.universa.Decimal) Approvable(com.icodici.universa.Approvable) Contract(com.icodici.universa.contract.Contract)

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