Search in sources :

Example 1 with Decimal

use of com.icodici.universa.Decimal in project universa by UniversaBlockchain.

the class Node2EmulatedNetworkTest method unexpectedStrangeCaseWithConcurrent.

// @Test
public void unexpectedStrangeCaseWithConcurrent() throws Exception {
    String FIELD_NAME = "amount";
    PrivateKey ownerKey2 = TestKeys.privateKey(1);
    String PRIVATE_KEY = "_xer0yfe2nn1xthc.private.unikey";
    Contract root = Contract.fromDslFile("./src/test_contracts/coin.yml");
    root.getStateData().set(FIELD_NAME, new Decimal(200));
    root.addSignerKeyFromFile("./src/test_contracts/" + PRIVATE_KEY);
    root.setOwnerKey(ownerKey2);
    root.seal();
    assertTrue(root.check());
    Contract c1 = root.splitValue(FIELD_NAME, new Decimal(100));
    c1.seal();
    assertTrue(root.check());
    assertTrue(c1.isOk());
    // c1 split 50 50
    c1 = c1.createRevision(ownerKey2);
    c1.seal();
    Contract c50_1 = c1.splitValue(FIELD_NAME, new Decimal(50));
    c50_1.seal();
    assertTrue(c50_1.isOk());
    // good join
    Contract finalC = c50_1.createRevision(ownerKey2);
    finalC.addSignerKeyFromFile(Config.tuKeyPath);
    finalC.seal();
    finalC.getStateData().set(FIELD_NAME, new Decimal(100));
    finalC.addRevokingItems(c50_1);
    finalC.addRevokingItems(c1);
    for (int j = 0; j < 500; j++) {
        HashId id;
        StateRecord orCreate;
        int p = 0;
        for (Approvable c : finalC.getRevokingItems()) {
            id = c.getId();
            for (int i = 0; i < nodes.size(); i++) {
                if (i == nodes.size() - 1 && p == 1)
                    break;
                Node nodeS = nodes.get(i);
                orCreate = nodeS.getLedger().findOrCreate(id);
                orCreate.setState(ItemState.APPROVED).save();
            }
            ++p;
        }
        destroyFromAllNodesExistingNew(finalC);
        destroyCurrentFromAllNodesIfExists(finalC);
        node.registerItem(finalC);
        ItemResult itemResult = node.waitItem(finalC.getId(), 1500);
        System.out.println(itemResult.state);
    // if (ItemState.APPROVED != itemResult.state)
    // System.out.println("\r\nWrong state on repetition " + j + ": " + itemResult + ", " + itemResult.errors +
    // " \r\ncontract_errors: " + finalC.getErrors());
    // else
    // System.out.println("\r\nGood. repetition: " + j + " ledger:" + node.getLedger().toString());
    // fail("Wrong state on repetition " + j + ": " + itemResult + ", " + itemResult.errors +
    // " \r\ncontract_errors: " + finalC.getErrors());
    // assertEquals(ItemState.APPROVED, itemResult.state);
    }
}
Also used : HashId(com.icodici.universa.HashId) PrivateKey(com.icodici.crypto.PrivateKey) Decimal(com.icodici.universa.Decimal) Approvable(com.icodici.universa.Approvable) Contract(com.icodici.universa.contract.Contract)

Example 2 with Decimal

use of com.icodici.universa.Decimal in project universa by UniversaBlockchain.

the class ContractTest method calculateSplit7To2ProcessingCost.

public Contract calculateSplit7To2ProcessingCost(String privateKeyPath, boolean createContractWith2048KeyIssuer) throws Exception {
    // Should create 7 contracts, sign and seal it all, then create revision and split to 2 contracts. Then calculate cost of processing.
    // should repeat contract processing procedure on the Node
    // (Contract.fromPackedTransaction() -> Contract(byte[], TransactionPack) -> Contract.check())
    Contract contract = createContractWith2048KeyIssuer ? createCoin100k2048apiv3() : createCoin100apiv3();
    contract.addSignerKeyFromFile(privateKeyPath);
    sealCheckTrace(contract, true);
    System.out.println("----- split -----");
    List<Contract> splittedList = new ArrayList<>();
    for (int i = 0; i < 6; i++) {
        contract = contract.createRevision();
        Contract splitted = contract.splitValue(FIELD_NAME, new Decimal(15));
        splitted.addSignerKeyFromFile(privateKeyPath);
        sealCheckTrace(splitted, true);
        assertEquals(new Decimal(15), new Decimal(Long.valueOf(splitted.getStateData().get("amount").toString())));
        sealCheckTrace(contract, true);
        assertEquals(new Decimal(85 - i * 15), contract.getStateData().get("amount"));
        splittedList.add(splitted);
        Thread.sleep(1000);
    }
    Contract forJoin = contract.createRevision();
    Contract splittedChanges;
    for (int i = 0; i < 6; i++) {
        if (i < 5) {
            forJoin.getRevokingItems().add(splittedList.get(i));
        } else {
            Contract splitting = splittedList.get(i).createRevision();
            splittedChanges = splitting.splitValue(FIELD_NAME, new Decimal(5));
            splittedChanges.addSignerKeyFromFile(privateKeyPath);
            sealCheckTrace(splittedChanges, true);
            sealCheckTrace(splitting, true);
            forJoin.getRevokingItems().add(splitting);
            forJoin.addNewItems(splittedChanges);
        }
    }
    forJoin.getStateData().set(FIELD_NAME, new Decimal(95));
    forJoin.addSignerKeyFromFile(privateKeyPath);
    sealCheckTrace(forJoin, true);
    Contract processingContract = processContractAsItWillBeOnTheNode(forJoin);
    System.out.println("Calculated processing cost (forJoin): " + processingContract.getProcessedCost() + " (Quanta)");
    System.out.println("Calculated processing cost (forJoin): " + processingContract.getProcessedCostTU() + " (TU)");
    return processingContract;
}
Also used : Decimal(com.icodici.universa.Decimal)

Example 3 with Decimal

use of com.icodici.universa.Decimal in project universa by UniversaBlockchain.

the class ContractTest method calculateSplitTwiceJoinProcessingCost.

@Test
public void calculateSplitTwiceJoinProcessingCost() throws Exception {
    // Should create contract, sign and seal it, then create revision and split twice.
    // Then join all and calculate cost of processing.
    // should repeat contract processing procedure on the Node
    // (Contract.fromPackedTransaction() -> Contract(byte[], TransactionPack) -> Contract.check())
    Contract contract = createCoin100apiv3();
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contract, true);
    System.out.println("-------- split 1 --------");
    Contract forSplit = contract.createRevision();
    Contract splitted1 = forSplit.splitValue(FIELD_NAME, new Decimal(20));
    splitted1.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(splitted1, true);
    sealCheckTrace(forSplit, true);
    System.out.println("-------- split 2 --------");
    splitted1 = splitted1.createRevision();
    Contract splitted2 = splitted1.splitValue(FIELD_NAME, new Decimal(5));
    splitted2.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(splitted2, true);
    sealCheckTrace(splitted1, true);
    assertEquals(new Decimal(80), forSplit.getStateData().get("amount"));
    assertEquals(new Decimal(15), new Decimal(Long.valueOf(splitted1.getStateData().get("amount").toString())));
    assertEquals(new Decimal(5), new Decimal(Long.valueOf(splitted2.getStateData().get("amount").toString())));
    System.out.println("-------- join --------");
    Contract forJoin = splitted2.createRevision();
    forJoin.getStateData().set("amount", ((Decimal) forSplit.getStateData().get("amount")).add((Decimal) splitted1.getStateData().get("amount")).add(new Decimal(Integer.valueOf((String) forJoin.getStateData().get("amount")))));
    forJoin.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    forJoin.addRevokingItems(forSplit);
    forJoin.addRevokingItems(splitted1);
    sealCheckTrace(forJoin, true);
    assertEquals(new Decimal(100), forJoin.getStateData().get("amount"));
    // Check 4096 bits signature own (8) +
    // Check 4096 bits signature revoking item (8) +
    // Check 4096 bits signature revoking item (8) +
    // Check 4096 bits signature revoking item (8) +
    // Register a self version (20) +
    // Register revoking item a version (20) +
    // Register revoking item a version (20) +
    // Register revoking item a version (20) +
    // Check self change owner permission (1) +
    // Check self change split join permission (1+2) +
    // Check self change revoke permission (1)
    int costShouldBeForJoin = 117;
    Contract processingContract = processContractAsItWillBeOnTheNode(forJoin);
    System.out.println("Calculated processing cost (forJoin): " + processingContract.getProcessedCost() + " (UTN)");
    assertEquals(costShouldBeForJoin, processingContract.getProcessedCost());
}
Also used : Decimal(com.icodici.universa.Decimal) Test(org.junit.Test)

Example 4 with Decimal

use of com.icodici.universa.Decimal in project universa by UniversaBlockchain.

the class ContractTest method calculateSplitProcessingCost.

@Test
public void calculateSplitProcessingCost() throws Exception {
    // Should create contract, sign and seal it, then create revision and split. Then calculate cost of processing.
    // should repeat contract processing procedure on the Node
    // (Contract.fromPackedTransaction() -> Contract(byte[], TransactionPack) -> Contract.check())
    Contract contract = createCoin100apiv3();
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contract, true);
    System.out.println("----- split -----");
    Contract forSplit = contract.createRevision();
    Contract splitted = forSplit.splitValue(FIELD_NAME, new Decimal(20));
    splitted.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(splitted, true);
    sealCheckTrace(forSplit, true);
    assertEquals(new Decimal(80), forSplit.getStateData().get("amount"));
    assertEquals(new Decimal(20), new Decimal(Long.valueOf(splitted.getStateData().get("amount").toString())));
    // Check 4096 bits signature forSplit (8) +
    // Check 4096 bits signature splitted (8) +
    // Check 4096 bits signature revoking in forSplit (8) +
    // Register forSplit (20) +
    // Register splitted (20) +
    // Register revoking in forSplit (20) +
    // Check forSplit change owner permission (1) +
    // Check forSplit change split join permission (1+2) +
    // Check forSplit change revoke permission (1) +
    // Check splitted change owner permission (1) +
    // Check splitted change split join permission (1+2) +
    // Check splitted change revoke permission (1)
    int costShouldBeForSplit = 94;
    Contract processingContract = processContractAsItWillBeOnTheNode(forSplit);
    System.out.println("Calculated processing cost (forSplit): " + processingContract.getProcessedCost() + " (UTN)");
    assertEquals(costShouldBeForSplit, processingContract.getProcessedCost());
    System.out.println("----- join -----");
    Contract forJoin = splitted.createRevision();
    forJoin.getStateData().set("amount", ((Decimal) forSplit.getStateData().get("amount")).add(new Decimal(Integer.valueOf((String) forJoin.getStateData().get("amount")))));
    forJoin.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    forJoin.addRevokingItems(forSplit);
    sealCheckTrace(forJoin, true);
    assertEquals(new Decimal(100), forJoin.getStateData().get("amount"));
    // Check 4096 bits signature own (8) +
    // Check 4096 bits signature revoking item (8) +
    // Check 4096 bits signature revoking item (8) +
    // Register a self version (20) +
    // Register revoking item a version (20) +
    // Register revoking item a version (20) +
    // Check self change owner permission (1) +
    // Check self change split join permission (1+2) +
    // Check self change revoke permission (1)
    int costShouldBeForJoin = 89;
    processingContract = processContractAsItWillBeOnTheNode(forJoin);
    System.out.println("Calculated processing cost (forJoin): " + processingContract.getProcessedCost() + " (UTN)");
    assertEquals(costShouldBeForJoin, processingContract.getProcessedCost());
}
Also used : Decimal(com.icodici.universa.Decimal) Test(org.junit.Test)

Example 5 with Decimal

use of com.icodici.universa.Decimal in project universa by UniversaBlockchain.

the class ContractsServiceTest method goodToken.

@Test
public void goodToken() throws Exception {
    Set<PrivateKey> martyPrivateKeys = new HashSet<>();
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> martyPublicKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    martyPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/marty_mcfly.private.unikey")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) stepaPublicKeys.add(pk.getPublicKey());
    for (PrivateKey pk : martyPrivateKeys) martyPublicKeys.add(pk.getPublicKey());
    Contract notaryContract = ContractsService.createTokenContract(martyPrivateKeys, stepaPublicKeys, "100");
    notaryContract.check();
    notaryContract.traceErrors();
    assertTrue(notaryContract.isOk());
    assertTrue(notaryContract.getOwner().isAllowedForKeys(stepaPublicKeys));
    assertTrue(notaryContract.getIssuer().isAllowedForKeys(martyPrivateKeys));
    assertTrue(notaryContract.getCreator().isAllowedForKeys(martyPrivateKeys));
    assertFalse(notaryContract.getOwner().isAllowedForKeys(martyPrivateKeys));
    assertFalse(notaryContract.getIssuer().isAllowedForKeys(stepaPublicKeys));
    assertFalse(notaryContract.getCreator().isAllowedForKeys(stepaPublicKeys));
    assertTrue(notaryContract.getExpiresAt().isAfter(ZonedDateTime.now().plusMonths(3)));
    assertTrue(notaryContract.getCreatedAt().isBefore(ZonedDateTime.now()));
    assertEquals(InnerContractsService.getDecimalField(notaryContract, "amount"), new Decimal(100));
    assertEquals(notaryContract.getPermissions().get("split_join").size(), 1);
    Binder splitJoinParams = notaryContract.getPermissions().get("split_join").iterator().next().getParams();
    assertEquals(splitJoinParams.get("min_value"), 0.01);
    assertEquals(splitJoinParams.get("min_unit"), 0.01);
    assertEquals(splitJoinParams.get("field_name"), "amount");
    assertTrue(splitJoinParams.get("join_match_fields") instanceof List);
    assertEquals(((List) splitJoinParams.get("join_match_fields")).get(0), "state.origin");
    assertTrue(notaryContract.isPermitted("revoke", stepaPublicKeys));
    assertTrue(notaryContract.isPermitted("revoke", martyPublicKeys));
    assertTrue(notaryContract.isPermitted("change_owner", stepaPublicKeys));
    assertFalse(notaryContract.isPermitted("change_owner", martyPublicKeys));
    assertTrue(notaryContract.isPermitted("split_join", stepaPublicKeys));
    assertFalse(notaryContract.isPermitted("split_join", martyPublicKeys));
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Decimal(com.icodici.universa.Decimal) PublicKey(com.icodici.crypto.PublicKey) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Decimal (com.icodici.universa.Decimal)41 Contract (com.icodici.universa.contract.Contract)31 Test (org.junit.Test)30 Binder (net.sergeych.tools.Binder)7 TransactionPack (com.icodici.universa.contract.TransactionPack)5 PrivateKey (com.icodici.crypto.PrivateKey)4 Approvable (com.icodici.universa.Approvable)3 PublicKey (com.icodici.crypto.PublicKey)2 HashId (com.icodici.universa.HashId)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Permission (com.icodici.universa.contract.permissions.Permission)1 SplitJoinPermission (com.icodici.universa.contract.permissions.SplitJoinPermission)1 Quantiser (com.icodici.universa.node2.Quantiser)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1