Search in sources :

Example 1 with BasicHttpClient

use of com.icodici.universa.node2.network.BasicHttpClient in project universa by UniversaBlockchain.

the class Research2Test method researchParcel.

// @Test
// public void httpSessionsConcurrency() throws Exception {
// PrivateKey serverKey = new PrivateKey(2048);
// BufferedLogger logger = new BufferedLogger(100);
// BasicHttpServer server = new BasicHttpServer(serverKey, 8887, 64, logger);
// server.addSecureEndpoint("testEndpoint", (params, session) -> {
// //System.out.println("server: on testEndpoint");
// return new Binder("ans", "some_value");
// });
// 
// ThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(64);
// int CLIENTS = 100;
// List<PrivateKey> clientKeys = new CopyOnWriteArrayList<>();
// List<BasicHttpClient> clients = new CopyOnWriteArrayList<>();
// CountDownLatch latch0 = new CountDownLatch(CLIENTS);
// for (int i = 0; i < CLIENTS; ++i) {
// pool.execute(() -> {
// try {
// //PrivateKey clientKey = new PrivateKey(2048);
// PrivateKey clientKey = TestKeys.privateKey(8);
// clientKeys.add(clientKey);
// BasicHttpClient client = new BasicHttpClient("http://localhost:8887");
// BasicHttpClientSession session = null;
// client.start(clientKey, serverKey.getPublicKey(), null);
// clients.add(client);
// latch0.countDown();
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
// }
// latch0.await();
// System.out.println("clients.size: " + clients.size());
// int N = 1000;
// CountDownLatch latch = new CountDownLatch(N);
// for (int i = 0; i < N; ++i) {
// int fi = i;
// pool.execute(() -> {
// try {
// BasicHttpClient client = clients.get(fi % CLIENTS);
// client.command("testEndpoint", new Binder("prm", "some_parameter"));
// latch.countDown();
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
// }
// while (latch.getCount() > 0) {
// Thread.sleep(1000);
// System.out.println("latch: " + latch.getCount());
// }
// latch.await();
// }
@Ignore
@Test
public void researchParcel() 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);
    // wait for sanitation
    // Thread.sleep(9000);
    System.out.println("\n\n\n\n\n\n\n\n === start ===\n");
    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);
    assertEquals(ItemState.APPROVED, itemResult.state);
    Contract u2 = uContract.createRevision(new HashSet<>(Arrays.asList(myKey)));
    u2.getStateData().set("transaction_units", "8100");
    u2.seal();
    itemResult = client.register(u2.getPackedTransaction(), 5000);
    System.out.println("register u2: " + itemResult);
    System.out.println("uContract state: " + client.getState(uContract.getId()));
    mm.forEach(m -> m.config.setIsFreeRegistrationsAllowedFromYaml(false));
    Contract contractPayload = new Contract(myKey);
    contractPayload.seal();
    itemResult = client.register(uContract.getPackedTransaction(), 5000);
    System.out.println("register contractPayload (no parcel): " + itemResult);
    assertEquals(ItemState.UNDEFINED, itemResult.state);
    Parcel parcel = ContractsService.createParcel(contractPayload, u2, 100, new HashSet<>(Arrays.asList(myKey)));
    itemResult = client.registerParcelWithState(parcel.pack(), 5000);
    System.out.println("register parcel: " + itemResult);
    assertEquals(ItemState.APPROVED, itemResult.state);
    System.out.println("payment state: " + client.getState(parcel.getPaymentContract().getId()));
    System.out.println("payload state: " + client.getState(parcel.getPayloadContract().getId()));
    System.out.println("uContract state: " + client.getState(uContract.getId()));
    System.out.println("u2 state: " + client.getState(u2.getId()));
    System.out.println("\n === done ===\n\n\n\n\n\n\n\n\n");
    mm.forEach(x -> x.shutdown());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) ItemResult(com.icodici.universa.node.ItemResult) Client(com.icodici.universa.node2.network.Client) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

PrivateKey (com.icodici.crypto.PrivateKey)1 ItemResult (com.icodici.universa.node.ItemResult)1 Client (com.icodici.universa.node2.network.Client)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1