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