Search in sources :

Example 11 with Parcel

use of com.icodici.universa.contract.Parcel in project universa by UniversaBlockchain.

the class NetworkV2 method getParcel.

@Override
public Parcel getParcel(HashId itemId, NodeInfo nodeInfo, Duration maxTimeout) throws InterruptedException {
    try {
        // URL url = new URL("http://localhost:8080/contracts/" + itemId.toBase64String());
        URL url = new URL(nodeInfo.publicUrlString() + "/parcels/" + itemId.toBase64String());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent", "Universa JAVA API Client");
        connection.setRequestProperty("Connection", "close");
        connection.setRequestMethod("GET");
        if (200 != connection.getResponseCode())
            return null;
        byte[] data = Do.read(connection.getInputStream());
        Parcel parcel = Parcel.unpack(data);
        // Contract c = Contract.fromPackedTransaction(data);
        return parcel;
    } catch (Exception e) {
        report(getLabel(), "download failure. from: " + nodeInfo.getNumber() + " by: " + myInfo.getNumber() + " reason: " + e);
        return null;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Parcel(com.icodici.universa.contract.Parcel) URL(java.net.URL) IOException(java.io.IOException)

Example 12 with Parcel

use of com.icodici.universa.contract.Parcel in project universa by UniversaBlockchain.

the class Node2LocalNetworkTest method shouldNotResyncWithFalseComplexState.

@Test
public void shouldNotResyncWithFalseComplexState() throws Exception {
    // LogPrinter.showDebug(true);
    // Test should broke condition to resync:
    // complex contract should has no errors itself
    ItemState definedState = ItemState.APPROVED;
    ItemState undefinedState = ItemState.UNDEFINED;
    AsyncEvent ae = new AsyncEvent();
    List<Contract> subContracts = new ArrayList<>();
    RunnableWithException<ItemState> addContract = (ItemState state) -> {
        Contract c = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
        c.addSignerKeyFromFile(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey");
        assertTrue(c.check());
        c.seal();
        addToAllLedgers(c, state);
        subContracts.add(c);
    };
    int wantedSubContracts = 5;
    int knownSubContractsToResync = config.getKnownSubContractsToResync();
    System.out.println("knownSubContractsToResync: " + knownSubContractsToResync);
    int numDefinedSubContracts = Math.min(wantedSubContracts, knownSubContractsToResync);
    System.out.println("add " + numDefinedSubContracts + " defined subcontracts (with state=" + definedState + ")");
    for (int i = 0; i < numDefinedSubContracts; ++i) addContract.run(definedState);
    int numUndefinedSubContracts = Math.max(0, wantedSubContracts - subContracts.size());
    System.out.println("add " + numUndefinedSubContracts + " " + undefinedState + " subcontract");
    for (int i = 0; i < numUndefinedSubContracts; ++i) addContract.run(undefinedState);
    for (int i = 0; i < subContracts.size(); i++) {
        ItemResult r = node.checkItem(subContracts.get(i).getId());
        System.out.println("Contract: " + i + " state: " + r.state);
    }
    Contract contract = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    contract.addSignerKey(new PrivateKey(2048));
    for (int i = 0; i < subContracts.size(); i++) {
        contract.addRevokingItems(subContracts.get(i));
    }
    contract.seal();
    contract.check();
    contract.traceErrors();
    assertFalse(contract.check());
    // LogPrinter.showDebug(true);
    Parcel parcel = registerWithNewParcel(contract);
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            ItemResult r = node.checkItem(parcel.getPayloadContract().getId());
            System.out.println("Complex contract state: " + r.state);
            if (r.state == ItemState.DECLINED)
                ae.fire();
        }
    }, 0, 500);
    try {
        ae.await(25000);
    } catch (TimeoutException e) {
        System.out.println("time is up");
    }
    timer.cancel();
    for (TestLocalNetwork ln : networks_s) {
        ln.setUDPAdapterTestMode(DatagramAdapter.TestModes.NONE);
        ln.setUDPAdapterVerboseLevel(DatagramAdapter.VerboseLevel.NOTHING);
    }
    node.waitParcel(parcel.getId(), 18000);
    ItemResult r = node.waitItem(parcel.getPayloadContract().getId(), 3000);
    assertEquals(ItemState.DECLINED, r.state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Parcel(com.icodici.universa.contract.Parcel) AsyncEvent(net.sergeych.tools.AsyncEvent) Contract(com.icodici.universa.contract.Contract) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Parcel (com.icodici.universa.contract.Parcel)12 Contract (com.icodici.universa.contract.Contract)8 AsyncEvent (net.sergeych.tools.AsyncEvent)5 PrivateKey (com.icodici.crypto.PrivateKey)3 TimeoutException (java.util.concurrent.TimeoutException)3 ItemResult (com.icodici.universa.node.ItemResult)2 IOException (java.io.IOException)2 Quantiser (com.icodici.universa.node2.Quantiser)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Instant (java.time.Instant)1