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