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));
}
}
}
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));
}
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;
}
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();
}
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);
}
Aggregations