use of com.icodici.universa.contract.Parcel in project universa by UniversaBlockchain.
the class CLIMain method revokeContract.
/**
* Revoke specified contract and create a revocation transactional contract.
*
* @param contract
*
* @return Parcel - revoking transaction contract.
*/
public static Parcel revokeContract(Contract contract, Contract tu, int amount, Set<PrivateKey> tuKeys, boolean withTestPayment, PrivateKey... key) throws IOException {
report("keys num: " + key.length);
Contract tc = ContractsService.createRevocation(contract, key);
Parcel parcel = registerContract(tc, tu, amount, tuKeys, withTestPayment, 0);
return parcel;
}
use of com.icodici.universa.contract.Parcel in project universa by UniversaBlockchain.
the class CLIMain method doRegister.
private static void doRegister() throws IOException {
List<String> sources = new ArrayList<String>((List) options.valuesOf("register"));
String tuSource = (String) options.valueOf("tu");
int tuAmount = (int) options.valueOf("amount");
boolean tutest = options.has("tutest");
List<String> nonOptions = new ArrayList<String>((List) options.nonOptionArguments());
for (String opt : nonOptions) {
sources.addAll(asList(opt.split(",")));
}
cleanNonOptionalArguments(sources);
for (int s = 0; s < sources.size(); s++) {
String source = sources.get(s);
Contract contract = loadContract(source);
Contract tu = null;
if (tuSource != null) {
tu = loadContract(tuSource, true);
report("load payment revision: " + tu.getState().getRevision() + " id: " + tu.getId());
}
Set<PrivateKey> tuKeys = new HashSet<>(keysMap().values());
if (contract != null) {
if (tu != null && tuKeys != null && tuKeys.size() > 0) {
report("registering the paid contract " + contract.getId() + " from " + source + " for " + tuAmount + " TU");
Parcel parcel = registerContract(contract, tu, tuAmount, tuKeys, tutest, (int) options.valueOf("wait"));
if (parcel != null) {
report("save payment revision: " + parcel.getPaymentContract().getState().getRevision() + " id: " + parcel.getPaymentContract().getId());
CopyOption[] copyOptions = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES };
Files.copy(Paths.get(tuSource), Paths.get(tuSource.replaceAll("(?i)\\.(unicon)$", "_rev" + tu.getRevision() + ".unicon")), copyOptions);
saveContract(parcel.getPaymentContract(), tuSource, true, false);
}
} else {
report("registering the contract " + contract.getId().toBase64String() + " from " + source);
registerContract(contract, (int) options.valueOf("wait"));
}
}
}
// print cost of processing if asked
if (options.has("cost")) {
doCost();
} else {
finish();
}
}
use of com.icodici.universa.contract.Parcel in project universa by UniversaBlockchain.
the class CLIMain method doRevoke.
private static void doRevoke() throws IOException {
List<String> sources = new ArrayList<String>((List) options.valuesOf("revoke"));
String tuSource = (String) options.valueOf("tu");
int tuAmount = (int) options.valueOf("amount");
boolean tutest = options.has("tutest");
List<String> nonOptions = new ArrayList<String>((List) options.nonOptionArguments());
for (String opt : nonOptions) {
sources.addAll(asList(opt.split(",")));
}
cleanNonOptionalArguments(sources);
report("doRevoke");
for (int s = 0; s < sources.size(); s++) {
String source = sources.get(s);
Contract contract = loadContract(source);
report("doRevoke " + contract);
Contract tu = null;
if (tuSource != null) {
tu = loadContract(tuSource, true);
report("load payment revision: " + tu.getState().getRevision() + " id: " + tu.getId());
}
Set<PrivateKey> tuKeys = new HashSet<>(keysMap().values());
report("tuKeys num: " + tuKeys.size());
if (contract != null) {
if (tu != null && tuKeys != null && tuKeys.size() > 0) {
report("registering the paid contract " + contract.getId() + " from " + source + " for " + tuAmount + " TU");
Parcel parcel = null;
try {
if (contract.check()) {
report("revoke contract from " + source);
parcel = revokeContract(contract, tu, tuAmount, tuKeys, tutest, keysMap().values().toArray(new PrivateKey[0]));
} else {
addErrors(contract.getErrors());
}
} catch (Quantiser.QuantiserException e) {
addError("QUANTIZER_COST_LIMIT", contract.toString(), e.getMessage());
}
if (parcel != null) {
report("save payment revision: " + parcel.getPaymentContract().getState().getRevision() + " id: " + parcel.getPaymentContract().getId());
CopyOption[] copyOptions = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES };
Files.copy(Paths.get(tuSource), Paths.get(tuSource.replaceAll("(?i)\\.(unicon)$", "_rev" + tu.getRevision() + ".unicon")), copyOptions);
saveContract(parcel.getPaymentContract(), tuSource, true, false);
}
} else {
try {
if (contract.check()) {
report("revoke contract from " + source);
revokeContract(contract, keysMap().values().toArray(new PrivateKey[0]));
} else {
addErrors(contract.getErrors());
}
} catch (Quantiser.QuantiserException e) {
addError("QUANTIZER_COST_LIMIT", contract.toString(), e.getMessage());
}
}
}
}
finish();
}
use of com.icodici.universa.contract.Parcel in project universa by UniversaBlockchain.
the class CLIMain method registerContract.
/**
* Register a specified contract.
*
* @param contract must be a sealed binary.
* @param waitTime - wait time for responce.
*/
public static Parcel registerContract(Contract contract, Contract tu, int amount, Set<PrivateKey> tuKeys, boolean withTestPayment, int waitTime) throws IOException {
List<ErrorRecord> errors = contract.getErrors();
if (errors.size() > 0) {
report("contract has errors and can't be submitted for registration");
report("contract id: " + contract.getId().toBase64String());
addErrors(errors);
} else {
Parcel parcel = ContractsService.createParcel(contract, tu, amount, tuKeys, withTestPayment);
getClientNetwork().registerParcel(parcel.pack(), waitTime);
ItemResult r = getClientNetwork().check(contract.getId());
report("paid contract " + contract.getId() + " submitted with result: " + r.toString());
report("payment was " + tu.getId());
report("payment became " + parcel.getPaymentContract().getId());
report("payment rev " + parcel.getPaymentContract().getRevoking().get(0).getId());
return parcel;
}
return null;
}
use of com.icodici.universa.contract.Parcel in project universa by UniversaBlockchain.
the class Client method registerParcel.
public boolean registerParcel(byte[] packed, long millisToWait) throws ClientError {
Object result = protect(() -> httpClient.command("approveParcel", "packedItem", packed).get("result"));
if (result instanceof String) {
System.out.println(">> registerParcel " + result);
} else {
if (millisToWait > 0) {
Instant end = Instant.now().plusMillis(millisToWait);
try {
Parcel parcel = Parcel.unpack(packed);
Node.ParcelProcessingState pState = getParcelProcessingState(parcel.getId());
while (Instant.now().isBefore(end) && pState.isProcessing()) {
System.out.println("parcel state is: " + pState);
Thread.currentThread().sleep(100);
pState = getParcelProcessingState(parcel.getId());
}
System.out.println("parcel state is: " + pState);
ItemResult lastResult = getState(parcel.getPayloadContract().getId());
while (Instant.now().isBefore(end) && lastResult.state.isPending()) {
Thread.currentThread().sleep(100);
lastResult = getState(parcel.getPayloadContract().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 (boolean) result;
}
return false;
}
Aggregations