use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.
the class UBotSessionsTest method paidOperationErrors.
@Test
public void paidOperationErrors() throws Exception {
List<Main> mm = new ArrayList<>();
for (int i = 0; i < 4; i++) mm.add(createMain("node" + (i + 1), false));
Main main = mm.get(0);
PrivateKey myKey = TestKeys.privateKey(0);
Client client = new Client(myKey, main.myInfo, null);
// System.out.println("\n\n\n\n\n\n\n\n === start ===\n");
// create and register U contract
mm.forEach(m -> m.config.setIsFreeRegistrationsAllowedFromYaml(true));
Contract uContract = InnerContractsService.createFreshU(9000, new HashSet<>(Arrays.asList(myKey.getPublicKey())));
uContract.seal();
ItemResult itemResult = client.register(uContract.getPackedTransaction(), 5000);
System.out.println("register uContract: " + itemResult);
Assert.assertEquals(ItemState.APPROVED, itemResult.state);
mm.forEach(m -> m.config.setIsFreeRegistrationsAllowedFromYaml(false));
Contract payment = uContract.createRevision(myKey);
payment.getStateData().set("transaction_units", uContract.getStateData().getIntOrThrow("transaction_units") - 100);
payment.seal();
PaidOperation paidOperation = new PaidOperation(payment.getTransactionPack(), "test_operation", new Binder("field1", "value1"));
System.out.println("\nregister... paymentId=" + paidOperation.getPaymentContract().getId() + ", operationId=" + paidOperation.getId());
ItemResult operationResult = client.registerPaidOperationWithState(paidOperation.pack(), 10000);
System.out.println("operationResult: " + operationResult);
ParcelProcessingState paidOperationState = client.getPaidOperationProcessingState(paidOperation.getId());
System.out.println("operationState: " + paidOperationState);
System.out.println("payment state: " + client.getState(payment.getId()));
System.out.println("uContract state: " + client.getState(uContract.getId()));
assertEquals(ItemState.APPROVED, client.getState(uContract.getId()).state);
assertEquals(ItemState.UNDEFINED, client.getState(payment.getId()).state);
assertEquals(ParcelProcessingState.FINISHED, paidOperationState);
// System.out.println("\n === done ===\n\n\n\n\n\n\n\n\n");
mm.forEach(x -> x.shutdown());
}
use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.
the class UBotSessionsTest method QuickConcurrentTransactions.
@Ignore
@Test
public void QuickConcurrentTransactions() throws Exception {
TestSpace ts = prepareTestSpace();
// ts.nodes.forEach(m->m.node.setVerboseLevel(DETAILED));
// ts.nodes.forEach(m->m.node.setNeworkVerboseLevel(DETAILED));
int quorumSize = 4;
int poolSize = 5;
Contract executableContract = new Contract(TestKeys.privateKey(1));
executableContract.getStateData().put("cloud_methods", Binder.of("simple", Binder.of("pool", Binder.of("size", poolSize), "quorum", Binder.of("size", quorumSize))));
executableContract.getStateData().put("js", "simple JS code");
executableContract.seal();
ts.node.node.registerItem(executableContract);
ItemResult ir = ts.node.node.waitItem(executableContract.getId(), 10000);
assertEquals(ir.state, ItemState.APPROVED);
int COST = 3;
int n = 20;
List<Contract> requestContracts = new ArrayList<>();
List<Contract> uContracts = new ArrayList<>();
List<Client> clients = new ArrayList<>();
for (int i = 0; i < n; i++) {
Client client = new Client("test_node_config_v2", null, new PrivateKey(2048));
clients.add(client);
Contract requestContract = new Contract(TestKeys.privateKey(2));
requestContract.getStateData().put("executable_contract_id", executableContract.getId());
requestContract.getStateData().put("method_name", "simple");
ContractsService.addReferenceToContract(requestContract, executableContract, "executable_contract_constraint", Reference.TYPE_EXISTING_DEFINITION, Do.listOf("ref.id==this.state.data.executable_contract_id"), true);
requestContracts.add(requestContract);
Contract u = getApprovedUContract(ts);
u = u.createRevision(ts.myKey);
u.getStateData().put("transaction_units", u.getStateData().getIntOrThrow("transaction_units") - COST);
u.seal();
uContracts.add(u);
ts.uContract = null;
}
for (int i = 0; i < n; i++) {
int finalI = i;
Do.inParallel(() -> {
System.out.println(clients.get(finalI).command("ubotCreateSessionPaid", "packedU", uContracts.get(finalI).getPackedTransaction(), "packedRequest", requestContracts.get(finalI).getPackedTransaction()));
});
}
AtomicInteger readyCounter = new AtomicInteger();
AsyncEvent readyEvent = new AsyncEvent();
ArrayList<AtomicReference<List<Integer>>> pools = new ArrayList<>();
for (int i = 0; i < n; i++) pools.add(new AtomicReference<>());
for (int i = 0; i < n; i++) {
int finalI = i;
Do.inParallel(() -> {
int attempts = 0;
while (true) {
Binder res = clients.get(finalI).command("ubotGetSession", "requestId", requestContracts.get(finalI).getId());
System.out.println("Client #" + finalI + ": " + clients.get(finalI).getNodeNumber() + " " + res);
if (res.isEmpty())
attempts++;
else
attempts = 0;
if (attempts > 3)
readyEvent.fire(false);
Thread.sleep(200);
if (res.get("session") != null && res.getBinderOrThrow("session").getString("state", "").equals("OPERATIONAL")) {
pools.get(finalI).set(res.getBinderOrThrow("session").getListOrThrow("sessionPool"));
if (readyCounter.incrementAndGet() == n)
readyEvent.fire(true);
break;
}
}
});
}
assertTrue((boolean) readyEvent.await());
ConcurrentHashMap<Integer, Client> allClients = new ConcurrentHashMap<>();
ArrayList<Set<Client>> allQuorumClients = new ArrayList<>();
for (int i = 0; i < n; i++) {
System.out.println(pools.get(i));
Set<Integer> poolQuorum = new HashSet<>();
while (poolQuorum.size() < quorumSize) poolQuorum.add(Do.sample(pools.get(i).get()));
Set<Client> quorumClients = new HashSet<>();
poolQuorum.forEach(p -> {
try {
Client c = allClients.get(p);
if (c == null)
c = new Client("./src/test_node_config_v2/test_node_config_v2.json", null, ubotKeys.get(p));
allClients.put(p, c);
quorumClients.add(c);
} catch (IOException e) {
e.printStackTrace();
}
});
assertEquals(quorumClients.size(), poolQuorum.size());
allQuorumClients.add(quorumClients);
}
AsyncEvent readyEvent2 = new AsyncEvent();
AtomicInteger readyCounter2 = new AtomicInteger();
for (int i = 0; i < n; i++) {
int finalI = i;
allQuorumClients.get(finalI).forEach(c -> {
for (int ic = 0; ic < c.size(); ic++) {
int finalIC = ic;
Do.inParallel(() -> {
while (true) {
try {
Binder res = c.getClient(finalIC).command("ubotStartTransaction", "requestId", requestContracts.get(finalI).getId(), "transactionName", "transaction", "transactionNumber", 0);
System.out.println("Client #" + finalI + ": " + c.getClient(finalIC).getNodeNumber() + " " + res);
Thread.sleep(10);
if (res.get("current") != null && res.get("current").equals(requestContracts.get(finalI).getId())) {
c.getClient(finalIC).command("ubotFinishTransaction", "requestId", requestContracts.get(finalI).getId(), "transactionName", "transaction", "transactionNumber", 0);
if (readyCounter2.incrementAndGet() == n * quorumSize * c.size())
readyEvent2.fire();
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
readyEvent2.fire();
}
}
});
}
});
}
Client checker = new Client("./src/test_node_config_v2/test_node_config_v2.json", null, TestKeys.privateKey(3));
// check transaction status and wait transaction requests
int wrongs = 0;
while (!readyEvent2.isFired()) {
if (wrongs == 0)
Thread.sleep(500);
HashId transactionRequest = null;
boolean wrong = false;
for (int x = 0; x < checker.size(); x++) {
Binder status = checker.getClient(x).command("ubotGetTransactionState", "requestId", requestContracts.get(0).getId(), "transactionName", "transaction");
System.out.println("Checker: " + checker.getClient(x).getNodeNumber() + " " + status);
assertTrue(!status.isEmpty());
if (status.get("current") != null) {
if (transactionRequest == null)
transactionRequest = (HashId) status.get("current");
else if (!transactionRequest.equals(status.get("current"))) {
System.err.println("Wrong: " + transactionRequest + " != " + status.get("current"));
wrong = true;
break;
}
}
}
if (wrong)
wrongs++;
else
wrongs = 0;
assertTrue(wrongs < 3);
}
assertTrue(readyCounter2.get() >= n * quorumSize * checker.size());
ts.shutdown();
}
use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.
the class VotingTest method persistentVotingFailed.
@Test
public void persistentVotingFailed() throws Exception {
TestSpace ts = prepareTestSpace();
Client client = new Client("test_node_config_v2", null, TestKeys.privateKey(1));
Contract contract = new Contract(TestKeys.privateKey(1));
QuorumVoteRole quorumVoteRole = new QuorumVoteRole("issuer", contract, "this.state.data.list", "99%");
contract.addRole(quorumVoteRole);
List<KeyAddress> addresses = new ArrayList<>();
for (int i = 0; i < 20; i++) {
addresses.add(TestKeys.publicKey(i).getLongAddress());
}
contract.getStateData().put("list", addresses);
contract.seal();
Contract u = Parcel.createPayment(getApprovedUContract(ts), Do.listOf(ts.myKey), 1, false);
HashId opId = client.initiateVote(u.getPackedTransaction(), contract, "creator", Do.listOf(contract.getId()));
while (client.getPaidOperationProcessingState(opId).isProcessing()) {
Thread.sleep(100);
}
if (client.getState(u.getId()).state == ItemState.APPROVED) {
synchronized (ts.uContractLock) {
ts.uContract = u;
}
} else {
throw new IllegalArgumentException("wrong u");
}
ts.shutdown();
ts = prepareTestSpace();
ts.nodes.forEach(n -> n.config.setIsFreeRegistrationsAllowedFromYaml(true));
for (int j = 0; j < 18; j++) {
Client keyClient = new Client("test_node_config_v2", null, TestKeys.privateKey(j));
u = Parcel.createPayment(getApprovedUContract(ts), Do.listOf(ts.myKey), 1, false);
opId = keyClient.voteForContract(u.getPackedTransaction(), contract.getId(), contract.getId());
while (keyClient.getPaidOperationProcessingState(opId).isProcessing()) {
Thread.sleep(100);
}
if (keyClient.getState(u.getId()).state == ItemState.APPROVED) {
synchronized (ts.uContractLock) {
ts.uContract = u;
}
} else {
throw new IllegalArgumentException("wrong u");
}
}
assertEquals(client.register(contract.getPackedTransaction(), 100000).state, ItemState.DECLINED);
ts.shutdown();
}
use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.
the class VotingTest method persistentVoting.
@Test
public void persistentVoting() throws Exception {
TestSpace ts = prepareTestSpace();
Client client = new Client("test_node_config_v2", null, TestKeys.privateKey(1));
Contract contract = new Contract(TestKeys.privateKey(1));
QuorumVoteRole quorumVoteRole = new QuorumVoteRole("issuer", contract, "this.state.data.list", "90%");
contract.addRole(quorumVoteRole);
List<KeyAddress> addresses = new ArrayList<>();
for (int i = 0; i < 20; i++) {
addresses.add(TestKeys.publicKey(i).getLongAddress());
}
contract.getStateData().put("list", addresses);
contract.seal();
Contract u = Parcel.createPayment(getApprovedUContract(ts), Do.listOf(ts.myKey), 1, false);
HashId opId = client.initiateVote(u.getPackedTransaction(), contract, "creator", Do.listOf(contract.getId()));
while (client.getPaidOperationProcessingState(opId).isProcessing()) {
Thread.sleep(100);
}
if (client.getState(u.getId()).state == ItemState.APPROVED) {
synchronized (ts.uContractLock) {
ts.uContract = u;
}
} else {
throw new IllegalArgumentException("wrong u");
}
ts.shutdown();
ts = prepareTestSpace();
ts.nodes.forEach(n -> n.config.setIsFreeRegistrationsAllowedFromYaml(true));
for (int j = 0; j < 19; j++) {
Client keyClient = new Client("test_node_config_v2", null, TestKeys.privateKey(j));
u = Parcel.createPayment(getApprovedUContract(ts), Do.listOf(ts.myKey), 1, false);
opId = keyClient.voteForContract(u.getPackedTransaction(), contract.getId(), contract.getId());
while (keyClient.getPaidOperationProcessingState(opId).isProcessing()) {
Thread.sleep(100);
}
if (keyClient.getState(u.getId()).state == ItemState.APPROVED) {
synchronized (ts.uContractLock) {
ts.uContract = u;
}
} else {
throw new IllegalArgumentException("wrong u");
}
}
assertEquals(client.register(contract.getPackedTransaction(), 100000).state, ItemState.APPROVED);
ts.shutdown();
}
use of com.icodici.universa.node2.network.Client in project universa by UniversaBlockchain.
the class VotingTest method detachedContractsVotingAuthorityLevels.
@Test
public void detachedContractsVotingAuthorityLevels() throws Exception {
TestSpace ts = prepareTestSpace();
ts.nodes.forEach(n -> n.config.setIsFreeRegistrationsAllowedFromYaml(true));
Client client = new Client("test_node_config_v2", null, TestKeys.privateKey(1));
int N = 3;
int M = 3;
int K = 3;
Contract contract = new Contract(TestKeys.privateKey(TestKeys.binaryKeys.length - 1));
QuorumVoteRole quorumVoteRole = new QuorumVoteRole("issuer", contract, "refSupplied.state.data.list", "" + (N * M * K));
contract.addRole(quorumVoteRole);
Reference refSupplied = new Reference(contract);
refSupplied.name = "refSupplied";
refSupplied.type = Reference.TYPE_EXISTING_DEFINITION;
refSupplied.setConditions(Binder.of("all_of", Do.listOf("ref can_play ref2ndLevelAuth.state.roles.granted_auth")));
contract.addReference(refSupplied);
Reference ref2ndLevelAuth = new Reference(contract);
ref2ndLevelAuth.name = "ref2ndLevelAuth";
ref2ndLevelAuth.type = Reference.TYPE_EXISTING_DEFINITION;
ref2ndLevelAuth.setConditions(Binder.of("all_of", Do.listOf("ref can_play refRootAuth.state.roles.granted_auth")));
contract.addReference(ref2ndLevelAuth);
Reference refRootAuth = new Reference(contract);
refRootAuth.name = "refRootAuth";
refRootAuth.type = Reference.TYPE_EXISTING_DEFINITION;
refRootAuth.setConditions(Binder.of("all_of", Do.listOf("ref.issuer==this.definition.data.root_authority")));
contract.addReference(refRootAuth);
SimpleRole role = new SimpleRole("dummy", contract);
role.addRequiredReference("refSupplied", Role.RequiredMode.ALL_OF);
role.addRequiredReference("ref2ndLevelAuth", Role.RequiredMode.ALL_OF);
role.addRequiredReference("refRootAuth", Role.RequiredMode.ALL_OF);
contract.addRole(role);
final int BASE = 0;
contract.getDefinition().getData().put("root_authority", TestKeys.publicKey(BASE).getLongAddress().toString());
contract.seal();
Contract rootAuthorityContract = new Contract(TestKeys.privateKey(BASE));
List<Contract> scndLvlAuthContracts = new ArrayList<>();
rootAuthorityContract.setIssuerKeys(TestKeys.publicKey(BASE).getLongAddress());
ListRole grantedAuth = new ListRole("granted_auth", rootAuthorityContract);
grantedAuth.setMode(ListRole.Mode.ANY);
for (int i = 0; i < K; i++) {
SimpleRole simpleRole = new SimpleRole("@auth" + i, rootAuthorityContract, Do.listOf(TestKeys.publicKey(BASE + 1 + i).getLongAddress()));
grantedAuth.addRole(simpleRole);
Contract scndLvlAuth = new Contract(TestKeys.privateKey(BASE + 1 + i));
ListRole grantedAuth2nd = new ListRole("granted_auth", scndLvlAuth);
grantedAuth2nd.setMode(ListRole.Mode.ANY);
scndLvlAuth.addRole(grantedAuth2nd);
for (int j = 0; j < M; j++) {
simpleRole = new SimpleRole("@auth" + i, scndLvlAuth, Do.listOf(TestKeys.publicKey(BASE + 1 + K + i * K + j).getLongAddress()));
grantedAuth2nd.addRole(simpleRole);
}
scndLvlAuth.seal();
scndLvlAuthContracts.add(scndLvlAuth);
}
rootAuthorityContract.addRole(grantedAuth);
rootAuthorityContract.seal();
ItemResult ir = client.register(rootAuthorityContract.getPackedTransaction(), 100000);
assertEquals(ir.state, ItemState.APPROVED);
AtomicInteger readyCounter0 = new AtomicInteger();
AsyncEvent readyEvent0 = new AsyncEvent();
scndLvlAuthContracts.forEach(c -> {
Do.inParallel(() -> {
ItemResult ir2 = null;
try {
ir2 = client.register(c.getPackedTransaction(), 100000);
} catch (ClientError clientError) {
clientError.printStackTrace();
}
assertEquals(ir2.state, ItemState.APPROVED);
if (readyCounter0.incrementAndGet() == scndLvlAuthContracts.size()) {
readyEvent0.fire();
}
});
});
readyEvent0.await();
Contract u = Parcel.createPayment(getApprovedUContract(ts), Do.listOf(ts.myKey), 1, false);
HashId opId = client.initiateVote(u.getPackedTransaction(), contract, "creator", Do.listOf(contract.getId()));
while (client.getPaidOperationProcessingState(opId).isProcessing()) {
Thread.sleep(100);
}
if (client.getState(u.getId()).state == ItemState.APPROVED) {
synchronized (ts.uContractLock) {
ts.uContract = u;
}
} else {
throw new IllegalArgumentException("wrong u");
}
for (int i = 0; i < K; i++) {
for (int j = 0; j < M; j++) {
Contract supplied = new Contract(TestKeys.privateKey(BASE + 1 + K + i * K + j));
List<KeyAddress> kas = new ArrayList<>();
for (int k = 0; k < N; k++) {
kas.add(TestKeys.publicKey((i * K + j) * M + k).getLongAddress());
}
supplied.getStateData().put("list", kas);
supplied.seal();
ir = client.register(supplied.getPackedTransaction(), 100000);
assertEquals(ir.state, ItemState.APPROVED);
for (int k = 0; k < N; k++) {
Client c = new Client("test_node_config_v2", null, TestKeys.privateKey((i * K + j) * M + k));
u = Parcel.createPayment(getApprovedUContract(ts), Do.listOf(ts.myKey), 1, false);
opId = c.voteForContract(u.getPackedTransaction(), contract.getId(), contract.getId(), Do.listOf(rootAuthorityContract.getLastSealedBinary(), scndLvlAuthContracts.get(i).getLastSealedBinary(), supplied.getLastSealedBinary()));
while (c.getPaidOperationProcessingState(opId).isProcessing()) {
Thread.sleep(100);
}
if (c.getState(u.getId()).state == ItemState.APPROVED) {
synchronized (ts.uContractLock) {
ts.uContract = u;
}
} else {
throw new IllegalArgumentException("wrong u");
}
System.out.println("VOTE " + ((i * K + j) * M + k));
}
}
}
ir = client.register(contract.getPackedTransaction(), 100000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
ts.shutdown();
}
Aggregations