use of com.icodici.universa.node.ItemResult in project universa by UniversaBlockchain.
the class CLIMainTest method registerManyContractsFromVariousNodes.
//
// @Test
// public void registerManyContracts() throws Exception {
//
// int numContracts = 100;
// List<Contract> contracts = new ArrayList<>();
//
// for (int i = 0; i < numContracts; i++) {
// Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
// c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.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();
//
// contracts.add(c);
// }
//
// Thread.sleep(500);
//
// for (int i = 0; i < numContracts; i++) {
//
// System.out.println("---");
// System.out.println("register contract " + i);
// System.out.println("---");
// final Contract contract = contracts.get(i);
// Thread thread = new Thread(() -> {
// try {
// System.out.println("register contract -> run thread");
// CLIMain.registerContract(contract);
// } catch (IOException e) {
// e.printStackTrace();
// }
// });
//
// thread.start();
// }
//
// Thread.sleep(30000);
//
// for (int i = 0; i < numContracts; i++) {
// System.out.println("---");
// System.out.println("check contract " + i);
// System.out.println("---");
//
// final Contract contract = contracts.get(i);
// Thread thread = new Thread(() -> {
// System.out.println("check contract -> run thread");
// try {
// callMain2("--probe", contract.getId().toBase64String());
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
//
// thread.start();
// }
//
// Thread.sleep(30000);
//
// System.out.println("---");
// System.out.println("check contracts in order");
// System.out.println("---");
// for (int i = 0; i < numContracts; i++) {
//
// final Contract contract = contracts.get(i);
// try {
// callMain2("--probe", contract.getId().toBase64String());
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
// assertEquals(0, CLIMain.getReporter().getErrors().size());
// }
@Test
public void registerManyContractsFromVariousNodes() throws Exception {
ClientNetwork clientNetwork1 = new ClientNetwork("http://localhost:8080", CLIMain.getPrivateKey(), null);
ClientNetwork clientNetwork2 = new ClientNetwork("http://localhost:6002", CLIMain.getPrivateKey(), null);
ClientNetwork clientNetwork3 = new ClientNetwork("http://localhost:6004", CLIMain.getPrivateKey(), null);
int numContracts = 10;
List<Contract> contracts = new ArrayList<>();
for (int i = 0; i < numContracts; i++) {
Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.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();
contracts.add(c);
}
Thread.sleep(500);
for (int i = 0; i < numContracts; i++) {
// System.out.println("---");
// System.out.println("register contract " + i);
// System.out.println("---");
final Contract contract = contracts.get(i);
Thread thread1 = new Thread(() -> {
try {
// System.out.println("register contract on the client 1 -> run thread");
CLIMain.registerContract(contract);
ItemResult r1 = clientNetwork1.register(contract.getPackedTransaction(), 50);
// System.out.println("register contract on the client 1 -> result: " + r1.toString());
} catch (IOException e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof ConnectException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof IllegalStateException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else {
e.printStackTrace();
}
}
});
thread1.start();
Thread thread2 = new Thread(() -> {
try {
// System.out.println("register contracz on the client 2 -> run thread");
CLIMain.registerContract(contract);
ItemResult r2 = clientNetwork2.register(contract.getPackedTransaction(), 50);
// System.out.println("register contract on the client 2 -> result: " + r2.toString());
} catch (IOException e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.err.println(">>>> ERROR 2: " + e.getMessage());
} else if (e.getCause() instanceof ConnectException) {
System.err.println(">>>> ERROR 2: " + e.getMessage());
} else if (e.getCause() instanceof IllegalStateException) {
System.err.println(">>>> ERROR 2: " + e.getMessage());
} else {
e.printStackTrace();
}
}
});
thread2.start();
Thread thread3 = new Thread(() -> {
try {
// System.out.println("register contract on the client 3 -> run thread");
CLIMain.registerContract(contract);
ItemResult r3 = clientNetwork3.register(contract.getPackedTransaction(), 50);
// System.out.println("register contract on the client 3 -> result: " + r3.toString());
} catch (IOException e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.err.println(">>>> ERROR 3: " + e.getMessage());
} else if (e.getCause() instanceof ConnectException) {
System.err.println(">>>> ERROR 3: " + e.getMessage());
} else if (e.getCause() instanceof IllegalStateException) {
System.err.println(">>>> ERROR 3: " + e.getMessage());
} else {
e.printStackTrace();
}
}
});
thread3.start();
}
Thread.sleep(1000);
System.out.println("---");
System.out.println("check contracts in order");
System.out.println("---");
for (int i = 0; i < numContracts; i++) {
final Contract contract = contracts.get(i);
callMain2("--probe", contract.getId().toBase64String());
}
assertEquals(0, CLIMain.getReporter().getErrors().size());
}
use of com.icodici.universa.node.ItemResult in project universa by UniversaBlockchain.
the class CLIMainTest method registerContractFromVariousNetworks.
@Test
public void registerContractFromVariousNetworks() throws Exception {
final Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.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();
// CLIMain.registerContract(c);
List<ClientNetwork> clientNetworks = new ArrayList<>();
int numConnections = 10;
for (int i = 0; i < numConnections; i++) {
clientNetworks.add(new ClientNetwork("http://localhost:8080", new PrivateKey(2048), null));
}
for (int i = 0; i < numConnections; i++) {
final int index = i;
try {
clientNetworks.get(index).ping();
// System.out.println("result (" + index + "): " + r1.toString());
} catch (IOException e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof ConnectException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof IllegalStateException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else {
e.printStackTrace();
}
}
}
for (int i = 0; i < numConnections; i++) {
final int index = i;
Thread thread1 = new Thread(() -> {
try {
ItemResult r1 = clientNetworks.get(index).register(c.getPackedTransaction());
System.out.println("result from thread (" + index + "): " + r1.toString());
} catch (IOException e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof ConnectException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else if (e.getCause() instanceof IllegalStateException) {
System.err.println(">>>> ERROR 1: " + e.getMessage());
} else {
e.printStackTrace();
}
}
});
thread1.start();
}
// Thread.sleep(10000);
}
use of com.icodici.universa.node.ItemResult 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.node.ItemResult 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.node.ItemResult 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.
* @param fromPackedTransaction - register contract with Contract.getPackedTransaction()
*/
@Deprecated
public static void registerContract(Contract contract, int waitTime, Boolean fromPackedTransaction) throws IOException {
// checkContract(contract);
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 {
// contract.seal();
ItemResult r;
if (fromPackedTransaction) {
r = getClientNetwork().register(contract.getPackedTransaction(), waitTime);
} else {
r = getClientNetwork().register(contract.getLastSealedBinary(), waitTime);
}
report("submitted with result:");
report(r.toString());
}
}
Aggregations