use of com.icodici.universa.node2.network.Client in project jmonkeyengine by jMonkeyEngine.
the class RpcClientService method onInitialize.
/**
* Used internally to setup the RpcConnection and MessageDelegator.
*/
@Override
protected void onInitialize(ClientServiceManager serviceManager) {
Client client = serviceManager.getClient();
this.rpc = new RpcConnection(client);
delegator = new ObjectMessageDelegator(rpc, true);
client.addMessageListener(delegator, delegator.getMessageTypes());
}
use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.
the class ResearchTest method reCreateNodeClient.
private static void reCreateNodeClient() throws Exception {
// String nodeUrl = "http://node-1-com.universa.io:8080";
String nodeUrl = "http://localhost:6002";
nodeClient = new Client(nodeUrl, nodeClientKey, null, false);
}
use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.
the class MainClass method main.
public static void main(String... args) throws Exception {
String network = "test";
if (args.length > 0) {
network = args[0];
}
PrivateKey clientKey = getDevKeyForNetwork(network);
if (clientKey == null) {
System.out.println("Dev key not found for network '" + network + "'. Make sure directory '" + BASE_DEVKEYS_PATH + network + "' exists and contains universa private key file");
return;
}
Client client = checkDevKeyForNetwork(clientKey, network);
if (client == null) {
System.out.println("Dev key check is failed. Make sure network is accessible and one of the addresses is in its whilelist:\n" + clientKey.getPublicKey().getLongAddress() + "\n" + clientKey.getPublicKey().getShortAddress() + "\n");
}
PrivateKey person1 = new PrivateKey(2048);
PrivateKey person2 = new PrivateKey(2048);
PrivateKey bank = new PrivateKey(2048);
Contract commissionAccRur = new Contract(bank);
commissionAccRur.getStateData().put(ACC_TYPE_FIELD, ACC_TYPE_BANK);
commissionAccRur.getStateData().put(ACC_CURRENCY_FIELD, ACC_CURRENCY_RUR);
commissionAccRur.getStateData().put(ACC_COMMISSION_PERCENT_FIELD, "0.0");
commissionAccRur.getStateData().put(ACC_COMMISSION_ACCOUNT_FIELD, null);
commissionAccRur.seal();
registerContract(commissionAccRur, client, "Registering account for commissions (RUR): " + commissionAccRur.getId());
Contract commissionAccEur = new Contract(bank);
commissionAccEur.getStateData().put(ACC_TYPE_FIELD, ACC_TYPE_BANK);
commissionAccEur.getStateData().put(ACC_CURRENCY_FIELD, ACC_CURRENCY_EUR);
commissionAccEur.getStateData().put(ACC_COMMISSION_PERCENT_FIELD, "0.0");
commissionAccEur.getStateData().put(ACC_COMMISSION_ACCOUNT_FIELD, null);
commissionAccEur.seal();
registerContract(commissionAccEur, client, "Registering account for commissions (EUR): " + commissionAccEur.getId());
System.out.println("\n====================\n");
System.out.println("Person #1 address:" + person1.getPublicKey().getShortAddress());
System.out.println("Person #2 address:" + person2.getPublicKey().getShortAddress());
Contract account1rur = new Contract(person1);
account1rur.getStateData().put(ACC_TYPE_FIELD, ACC_TYPE_PERSONAL);
account1rur.getStateData().put(ACC_CURRENCY_FIELD, ACC_CURRENCY_RUR);
account1rur.getStateData().put(ACC_COMMISSION_PERCENT_FIELD, "0.03");
account1rur.getStateData().put(ACC_COMMISSION_ACCOUNT_FIELD, commissionAccRur.getId().toBase64String());
account1rur.seal();
registerContract(account1rur, client, "Registering RUR account for person #1: " + account1rur.getId() + "\nAccount commission is " + account1rur.get(ACC_COMMISSION_PERCENT_PATH));
Contract account2rur = new Contract(person2);
account2rur.getStateData().put(ACC_TYPE_FIELD, ACC_TYPE_PERSONAL);
account2rur.getStateData().put(ACC_CURRENCY_FIELD, ACC_CURRENCY_RUR);
account2rur.getStateData().put(ACC_COMMISSION_PERCENT_FIELD, "0.0");
account2rur.getStateData().put(ACC_COMMISSION_ACCOUNT_FIELD, commissionAccRur.getId().toBase64String());
account2rur.seal();
registerContract(account2rur, client, "Registering RUR account for person #2: " + account2rur.getId() + "\nAccount commission is " + account2rur.get(ACC_COMMISSION_PERCENT_PATH));
Contract account2eur = new Contract(person2);
account2eur.getStateData().put(ACC_TYPE_FIELD, ACC_TYPE_PERSONAL);
account2eur.getStateData().put(ACC_CURRENCY_FIELD, ACC_CURRENCY_EUR);
account2eur.getStateData().put(ACC_COMMISSION_PERCENT_FIELD, "0.05");
account2eur.getStateData().put(ACC_COMMISSION_ACCOUNT_FIELD, commissionAccEur.getId().toBase64String());
account2eur.seal();
registerContract(account2eur, client, "Registering EUR account for person #2: " + account2eur.getId() + "\nAccount commission is " + account2eur.get(ACC_COMMISSION_PERCENT_PATH));
// TOKEN CREATION
Contract rurTokenAcc1 = new Contract(bank);
SimpleRole tokenOwner = new SimpleRole("owner");
tokenOwner.addRequiredReference("canplayaccowner", Role.RequiredMode.ALL_OF);
rurTokenAcc1.registerRole(tokenOwner);
rurTokenAcc1.getStateData().put("account", account1rur.getId().toBase64String());
rurTokenAcc1.getStateData().put(TOKEN_VALUE_FIELD, totalAmount);
RoleLink rl = new RoleLink("@owner", "owner");
rurTokenAcc1.registerRole(rl);
SplitJoinPermission sjp = new SplitJoinPermission(rl, Binder.of("field_name", TOKEN_VALUE_FIELD, "join_match_fields", Do.listOf("state.origin", TOKEN_ACCOUNT_PATH)));
rurTokenAcc1.addPermission(sjp);
ModifyDataPermission mdp = new ModifyDataPermission(rl, Binder.of("fields", Binder.of(TOKEN_ACCOUNT_FIELD, null)));
rurTokenAcc1.addPermission(mdp);
Reference canplayaccowner = new Reference(rurTokenAcc1);
canplayaccowner.name = "canplayaccowner";
List<String> conditions = new ArrayList<>();
conditions.add("ref.id==this." + TOKEN_ACCOUNT_PATH);
conditions.add("this can_play ref.owner");
canplayaccowner.setConditions(Binder.of("all_of", conditions));
rurTokenAcc1.addReference(canplayaccowner);
Reference refAccount = new Reference(rurTokenAcc1);
refAccount.name = "refAccount";
refAccount.setConditions(Binder.of("all_of", Do.listOf("this." + TOKEN_ACCOUNT_PATH + " == ref.id")));
rurTokenAcc1.addReference(refAccount);
Reference refParent = new Reference(rurTokenAcc1);
refParent.name = "refParent";
refParent.setConditions(Binder.of("any_of", Do.listOf("this.state.parent == ref.id", "this.state.parent undefined")));
rurTokenAcc1.addReference(refParent);
Reference refParentAccount = new Reference(rurTokenAcc1);
refParentAccount.name = "refParentAccount";
refParentAccount.setConditions(Binder.of("any_of", Do.listOf("refParent." + TOKEN_ACCOUNT_PATH + " == ref.id", "this.state.parent undefined")));
rurTokenAcc1.addReference(refParentAccount);
Reference transferCheck = new Reference(rurTokenAcc1);
transferCheck.name = "transferCheck";
transferCheck.setConditions(Binder.of("any_of", Do.listOf(// root contract
"this.state.parent undefined", // OR there was no transfer
"refParent." + TOKEN_ACCOUNT_PATH + " == this." + TOKEN_ACCOUNT_PATH, // OR transfer is correct
Binder.of("all_of", Do.listOf(// transfer to account with same currency
"refAccount." + ACC_CURRENCY_PATH + " == refParentAccount." + ACC_CURRENCY_PATH, // AND one of the cases
Binder.of("any_of", Do.listOf(// contract is a commission itself
Binder.of("all_of", Do.listOf("this." + TOKEN_ACCOUNT_PATH + "==refParentAccount." + ACC_COMMISSION_ACCOUNT_PATH)), // OR commission is set to zero
Binder.of("all_of", Do.listOf("refParentAccount." + ACC_COMMISSION_PERCENT_PATH + "::number==0.0")), // OR commission exists in pack
Binder.of("all_of", Do.listOf("ref." + TOKEN_VALUE_PATH + "::number == this." + TOKEN_VALUE_PATH + "::number * refParentAccount." + ACC_COMMISSION_PERCENT_PATH + "::number", "ref." + TOKEN_ACCOUNT_PATH + " == refParentAccount." + ACC_COMMISSION_ACCOUNT_PATH, "ref.transactional.data.transfer_id == this.id")))))))));
rurTokenAcc1.addReference(transferCheck);
rurTokenAcc1.seal();
rurTokenAcc1.getTransactionPack().addReferencedItem(account1rur);
registerContract(rurTokenAcc1, client, "Issuing " + totalAmount + " RUR for account #1: " + rurTokenAcc1.getId());
System.out.println("\n\n============ CORRECT OPERATIONS ==================\n\n");
BigDecimal transfer1 = new BigDecimal(totalAmount).divide(new BigDecimal("4"));
BigDecimal comission = transfer1.multiply(new BigDecimal(account1rur.get(ACC_COMMISSION_PERCENT_PATH).toString()));
Contract[] ptransfer = prepareTransfer(rurTokenAcc1, transfer1, account1rur, account2rur, commissionAccRur, new HashSet<>(Do.listOf(person1)), comission);
registerContract(ptransfer[0], client, "Sending " + transfer1 + " RUR from acc #1 (RUR) to acc #2 (RUR) with commission " + comission + " RUR");
rurTokenAcc1 = ptransfer[1];
Contract rurTokenAcc2 = ptransfer[2];
BigDecimal transfer2 = new BigDecimal(rurTokenAcc2.get(TOKEN_VALUE_PATH).toString()).divide(new BigDecimal("4"));
ptransfer = prepareTransfer(rurTokenAcc2, transfer2, account2rur, account1rur, commissionAccRur, new HashSet<>(Do.listOf(person2)), null);
registerContract(ptransfer[0], client, "Sending " + transfer2 + " RUR from acc #2 (RUR) to acc #1 (RUR) without commission");
rurTokenAcc2 = ptransfer[1];
Contract rurTokenAcc1_2 = ptransfer[2];
rurTokenAcc1 = rurTokenAcc1.createRevision(person1);
rurTokenAcc1.addRevokingItems(rurTokenAcc1_2);
rurTokenAcc1_2 = null;
rurTokenAcc1.getStateData().set(TOKEN_VALUE_FIELD, new BigDecimal(rurTokenAcc1.get(TOKEN_VALUE_PATH).toString()).add(transfer2));
rurTokenAcc1.seal();
rurTokenAcc1.getTransactionPack().addReferencedItem(account1rur);
registerContract(rurTokenAcc1, client, "Joining acc1 tokens");
System.out.println("\n\n============ INCORRECT OPERATIONS ==================\n\n");
Contract joinDiffAccs = rurTokenAcc1.createRevision(person1, person2);
joinDiffAccs.addRevokingItems(rurTokenAcc2);
joinDiffAccs.getStateData().set(TOKEN_VALUE_FIELD, new BigDecimal(rurTokenAcc1.get(TOKEN_VALUE_PATH).toString()).add(new BigDecimal(rurTokenAcc2.get(TOKEN_VALUE_PATH).toString())));
joinDiffAccs.seal();
joinDiffAccs.getTransactionPack().addReferencedItem(account1rur);
joinDiffAccs.getTransactionPack().addReferencedItem(account2rur);
registerContract(joinDiffAccs, client, "Joining acc #1 and acc #2 tokens - signed by both persons (TOKENS AREN'T JOINABLE ACROSS DIFFERENT ACCOUNTS)", ItemState.DECLINED);
BigDecimal transfer3 = new BigDecimal(rurTokenAcc2.get(TOKEN_VALUE_PATH).toString()).divide(new BigDecimal("4"));
ptransfer = prepareTransfer(rurTokenAcc2, transfer3, account2rur, account2eur, commissionAccRur, new HashSet<>(Do.listOf(person2)), null);
registerContract(ptransfer[0], client, "Sending " + transfer3 + " RUR from acc #2 (RUR) to acc #2 (EUR) without commission (RUR -> EUR account transfer)", ItemState.DECLINED);
BigDecimal transfer4 = new BigDecimal(rurTokenAcc1.get(TOKEN_VALUE_PATH).toString()).divide(new BigDecimal("4"));
ptransfer = prepareTransfer(rurTokenAcc1, transfer4, account1rur, account2rur, commissionAccRur, new HashSet<>(Do.listOf(person1)), null);
registerContract(ptransfer[0], client, "Sending " + transfer4 + " RUR from acc #1 (RUR) to acc #2 (RUR) without commission (COMMISSION REQUIRED)", ItemState.DECLINED);
BigDecimal transfer5 = new BigDecimal(rurTokenAcc1.get(TOKEN_VALUE_PATH).toString()).divide(new BigDecimal("4"));
ptransfer = prepareTransfer(rurTokenAcc1, transfer5, account1rur, account2rur, commissionAccRur, new HashSet<>(Do.listOf(person1)), new BigDecimal("0.0001"));
registerContract(ptransfer[0], client, "Sending " + transfer5 + " RUR from acc #1 (RUR) to acc #2 (RUR) with insufficient commission (COMMISSION INSUFFICIENT)", ItemState.DECLINED);
}
use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.
the class MainClass method checkDevKeyForNetwork.
private static Client checkDevKeyForNetwork(PrivateKey key, String network) {
try {
Client client = new Client("http://node-1-" + network + ".utoken.io", key, null);
Binder res = client.getStats();
if (res.containsKey("itemResult")) {
return ((ItemResult) res.get("itemResult")).errors.isEmpty() ? client : null;
}
return client;
} catch (IOException e) {
return null;
}
}
use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.
the class Client method getState.
/**
* Get the state of the contract according to the network consensus.
*
* This method uses multiple connections to several nodes simultaneously to collect the statuses.
* Note: limits are applied to number of {@link #getState(Approvable)} calls per minute for each particular
* client key.
* Make sure the method is not called too often with the same client connection.
*
* @param reporter reporter used to log the current status, or errors from nodes if any etc
* @param itemId to get state by
* @return known {@link ItemState} if exist or ItemState.UNDEFINED
* @throws ClientError
*/
public ItemResult getState(HashId itemId, Reporter reporter) throws ClientError {
final ExecutorService pool = Executors.newCachedThreadPool();
final List<ErrorRecord> errors = new ArrayList<>();
final AsyncEvent<Void> consensusFound = new AsyncEvent<>();
final int checkConsensus = size() / 3;
final AtomicInteger nodesLeft = new AtomicInteger(nodes.size());
return protect(() -> {
final Map<ItemState, List<ItemResult>> states = new HashMap<>();
for (int i = 0; i < nodes.size(); i++) {
final int nn = i;
pool.submit(() -> {
for (int retry = 0; retry < 5; retry++) {
// reporter.verbose("trying "+reporter+" for node "+nn);
try {
Client c = getClient(nn);
ItemResult r = c.command("getState", "itemId", itemId).getOrThrow("itemResult");
r.meta.put("url", c.getNodeNumber());
// System.out.println(c.getUrl()+" #"+c.getNodeNumber()+" -> "+r);
synchronized (states) {
List<ItemResult> list = states.get(r.state);
if (list == null) {
list = new ArrayList();
states.put(r.state, list);
}
list.add(r);
if (r.errors.size() > 0)
reporter.warning("errors from " + c.getNodeNumber() + ": " + r.errors);
break;
}
} catch (IOException e) {
// reporter.warning("can't get answer from node " + nn + ", retry #" + retry + ": " + e);
}
}
// Now we should check the consensus
states.forEach((itemState, itemResults) -> {
if (itemResults.size() >= checkConsensus)
if (itemResults.size() >= checkConsensus) {
consensusFound.fire();
return;
}
});
if (nodesLeft.decrementAndGet() < 1)
consensusFound.fire();
});
}
consensusFound.await(10000);
pool.shutdownNow();
final ItemResult[] consensus = new ItemResult[1];
states.forEach((itemState, itemResults) -> {
if (itemResults.size() >= checkConsensus)
consensus[0] = itemResults.get(0);
});
if (consensus[0] != null)
reporter.message("State consensus found:" + consensus[0]);
else {
reporter.warning("no consensus found " + states.size());
}
if (states.size() > 1) {
states.entrySet().stream().sorted(Comparator.comparingInt(o -> o.getValue().size())).forEach(kv -> {
List<ItemResult> itemResults = kv.getValue();
reporter.message("" + kv.getKey() + ": " + itemResults.size() + ": " + itemResults.stream().map(x -> x.meta.getStringOrThrow("url")).collect(Collectors.toSet()));
});
}
return consensus[0];
});
}
Aggregations