Search in sources :

Example 76 with Contract

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

the class Node method checkSpecialItem.

private void checkSpecialItem(Approvable item) {
    if (item instanceof Contract) {
        Contract contract = (Contract) item;
        if (contract.getIssuer().getKeys().equals(new HashSet<>(Arrays.asList(config.getNetworkConfigIssuerKey())))) {
            if (contract.getParent() == null)
                return;
            if (contract.getRevoking().size() == 0 || !contract.getRevoking().get(0).getId().equals(contract.getParent()))
                return;
            Contract parent = contract.getRevoking().get(0);
            if (!checkContractCorrespondsToConfig(parent, network.allNodes())) {
                return;
            }
            if (!checkIfContractContainsNetConfig(contract)) {
                return;
            }
            List<NodeInfo> networkNodes = network.allNodes();
            List contractNodes = (List) DefaultBiMapper.getInstance().deserializeObject(contract.getStateData().get("net_config"));
            contractNodes.stream().forEach(nodeInfo -> {
                if (!networkNodes.contains(nodeInfo)) {
                    addNode((NodeInfo) nodeInfo);
                }
                networkNodes.remove(nodeInfo);
            });
            networkNodes.stream().forEach(nodeInfo -> removeNode(nodeInfo));
        }
    }
}
Also used : Contract(com.icodici.universa.contract.Contract)

Example 77 with Contract

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

the class WalletTest method shouldTransferAndSplitRest.

@Test
public void shouldTransferAndSplitRest() throws Exception {
    Decimal valueToSend = new Decimal(15);
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(1, 2, 3, 5, 8));
    List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount);
    assertEquals(1, wallets.size());
    assertEquals(5, wallets.get(0).getContracts().size());
    Wallet wallet = wallets.get(0);
    Contract contract = wallet.buildContractWithValue(FIELD_NAME, valueToSend);
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    assertEquals(1, wallet.getContracts().size());
    sealCheckTrace(contract, true);
    Contract restContract = wallet.getContracts().get(0);
    assertEquals(4, restContract.getStateData().getIntOrThrow(FIELD_NAME));
    sealCheckTrace(restContract, true);
    TransactionPack tp = contract.getTransactionPack();
    Contract contractToSend = new Contract(contract.seal(), tp);
    contractToSend.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contractToSend, true);
    assertEquals(valueToSend.intValue(), contractToSend.getStateData().getIntOrThrow(FIELD_NAME));
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 78 with Contract

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

the class CLIMain method loadContract.

/**
 * Load contract from specified path.
 *
 * @param fileName
 * @param fromPackedTransaction - create contract from loaded data with Contract.fromPackedTransaction(data)
 *
 * @return loaded and from loaded data created Contract.
 */
public static Contract loadContract(String fileName, Boolean fromPackedTransaction) throws IOException {
    Contract contract = null;
    File pathFile = new File(fileName);
    if (pathFile.exists()) {
        // reporter.verbose("Loading contract from: " + fileName);
        Path path = Paths.get(fileName);
        byte[] data = Files.readAllBytes(path);
        try {
            if (fromPackedTransaction) {
                contract = Contract.fromPackedTransaction(data);
            } else {
                contract = new Contract(data);
            }
        } catch (Quantiser.QuantiserException e) {
            addError("QUANTIZER_COST_LIMIT", fileName, e.toString());
        }
    } else {
        addError(Errors.NOT_FOUND.name(), fileName, "Path " + fileName + " does not exist");
    // usage("Path " + fileName + " does not exist");
    }
    return contract;
}
Also used : Quantiser(com.icodici.universa.node2.Quantiser) Contract(com.icodici.universa.contract.Contract)

Example 79 with Contract

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

the class CLIMain method doImport.

private static void doImport() throws IOException {
    List<String> sources = new ArrayList<String>((List) options.valuesOf("i"));
    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);
        Contract contract = importContract(source);
        if (contract != null) {
            HashMap<String, String> updateFieldsHashMap = new HashMap<>();
            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);
            }
            if (name == null) {
                name = source.replaceAll("(?i)\\.(json|xml|yml|yaml)$", ".unicon");
            }
            contract.seal();
            saveContract(contract, name);
        }
    }
    finish();
}
Also used : Arrays.asList(java.util.Arrays.asList) Contract(com.icodici.universa.contract.Contract) BackingStoreException(java.util.prefs.BackingStoreException) OptionException(joptsimple.OptionException)

Example 80 with Contract

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

the class CLIMain method checkSj.

private static void checkSj(Contract contract, Permission sj) {
    Binder params = sj.getParams();
    String fieldName = "state.data." + params.getStringOrThrow("field_name");
    reporter.verbose("splitjoins permission fond on field '" + fieldName + "'");
    StringBuilder outcome = new StringBuilder();
    List<Decimal> values = new ArrayList<>();
    contract.getRevoking().forEach(c -> {
        Decimal x;
        if (c.get(fieldName) != null) {
            x = new Decimal(c.get(fieldName).toString());
            values.add(x);
            if (outcome.length() > 0)
                outcome.append(" + ");
            outcome.append(x.toString());
        }
    });
    List<Contract> news = Do.listOf(contract);
    news.addAll(contract.getNew());
    outcome.append(" -> ");
    news.forEach(c -> {
        Decimal x;
        if (c.get(fieldName) != null) {
            if (c != contract)
                outcome.append(" + ");
            x = new Decimal(c.get(fieldName).toString());
            outcome.append(x.toString());
            values.add(x.negate());
        }
    });
    reporter.verbose("operation is: " + outcome.toString());
    Decimal saldo = values.stream().reduce(Decimal.ZERO, (a, b) -> a.add(b));
    if (saldo.compareTo(Decimal.ZERO) == 0)
        reporter.verbose("Saldo looks good (zero)");
    else
        reporter.warning("Saldo is not zero: " + saldo);
}
Also used : Binder(net.sergeych.tools.Binder) 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