Search in sources :

Example 56 with PrivateKey

use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method parallelContractNodeCheck.

@Ignore("parallel test")
@Test
public void parallelContractNodeCheck() throws Exception {
    // assertEquals("http://localhost:8080", main.myInfo.internalUrlString());
    // assertEquals("http://localhost:8080", main.myInfo.publicUrlString());
    PrivateKey myKey = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    // assertEquals(main.cache, main.node.getCache());
    // ItemCache c1 = main.cache;
    // ItemCache c2 = main.node.getCache();
    // Client client = new Client(myKey, main.myInfo, null);
    List<Contract> contractsForThreads = new ArrayList<>();
    int N = 100;
    int M = 2;
    float threshold = 1.2f;
    float ratio = 0;
    boolean createNewContracts = true;
    // assertTrue(singleContract.isOk());
    // ItemResult r = client.getState(singleContract.getId());
    // assertEquals(ItemState.UNDEFINED, r.state);
    // System.out.println(r);
    contractsForThreads = new ArrayList<>();
    for (int j = 0; j < M; j++) {
        Contract contract = new Contract(myKey);
        for (int k = 0; k < 10; k++) {
            Contract nc = new Contract(myKey);
            nc.seal();
            contract.addNewItems(nc);
        }
        contract.seal();
        assertTrue(contract.isOk());
        contractsForThreads.add(contract);
    // ItemResult r = client.getState(contract.getId());
    // assertEquals(ItemState.UNDEFINED, r.state);
    // System.out.println(r);
    }
    Contract singleContract = new Contract(myKey);
    for (int k = 0; k < 10; k++) {
        Contract nc = new Contract(myKey);
        nc.seal();
        singleContract.addNewItems(nc);
    }
    singleContract.seal();
    for (int i = 0; i < N; i++) {
        if (createNewContracts) {
            contractsForThreads = new ArrayList<>();
            for (int j = 0; j < M; j++) {
                Contract contract = new Contract(myKey);
                for (int k = 0; k < 10; k++) {
                    Contract nc = new Contract(myKey);
                    nc.seal();
                    contract.addNewItems(nc);
                }
                contract.seal();
                assertTrue(contract.isOk());
                contractsForThreads.add(contract);
            }
            singleContract = new Contract(myKey);
            for (int k = 0; k < 10; k++) {
                Contract nc = new Contract(myKey);
                nc.seal();
                singleContract.addNewItems(nc);
            }
            singleContract.seal();
        }
        long ts1;
        long ts2;
        Semaphore semaphore = new Semaphore(-(M - 1));
        ts1 = new Date().getTime();
        for (Contract c : contractsForThreads) {
            Thread thread = new Thread(() -> {
                long t = System.nanoTime();
                try {
                    List<StateRecord> lockedToCreate = new ArrayList<>();
                    List<StateRecord> lockedToRevoke = new ArrayList<>();
                    StateRecord record = node.getLedger().findOrCreate(c.getId());
                    c.check();
                    isNeedToResync(true, c);
                    checkSubItemsOf(c, record, lockedToCreate, lockedToRevoke);
                } catch (Quantiser.QuantiserException e) {
                    e.printStackTrace();
                }
                System.out.println("multi thread: time: " + ((System.nanoTime() - t) * 1e-9));
                semaphore.release();
            });
            thread.setName("Multi-thread register: " + c.getId().toString());
            thread.start();
        }
        semaphore.acquire();
        ts2 = new Date().getTime();
        long threadTime = ts2 - ts1;
        // 
        ts1 = new Date().getTime();
        Contract finalSingleContract = singleContract;
        Thread thread = new Thread(() -> {
            long t = System.nanoTime();
            try {
                List<StateRecord> lockedToCreate = new ArrayList<>();
                List<StateRecord> lockedToRevoke = new ArrayList<>();
                StateRecord record = node.getLedger().findOrCreate(finalSingleContract.getId());
                finalSingleContract.check();
                isNeedToResync(true, finalSingleContract);
                checkSubItemsOf(finalSingleContract, record, lockedToCreate, lockedToRevoke);
            } catch (Quantiser.QuantiserException e) {
                e.printStackTrace();
            }
            System.out.println("single thread: time: " + ((System.nanoTime() - t) * 1e-9));
            semaphore.release();
        });
        thread.setName("single-thread register: " + singleContract.getId().toString());
        thread.start();
        semaphore.acquire();
        ts2 = new Date().getTime();
        long singleTime = ts2 - ts1;
        System.out.println(threadTime * 1.0f / singleTime);
        ratio += threadTime * 1.0f / singleTime;
    }
    ratio /= N;
    System.out.println("average " + ratio);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Semaphore(java.util.concurrent.Semaphore) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 57 with PrivateKey

use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method registerParcelWithRealPayment.

@Test(timeout = 90000)
public void registerParcelWithRealPayment() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Contract stepaCoins = Contract.fromDslFile(ROOT_PATH + "stepaCoins.yml");
    stepaCoins.addSignerKey(stepaPrivateKeys.iterator().next());
    stepaCoins.seal();
    stepaCoins.check();
    stepaCoins.traceErrors();
    PrivateKey ownerKey = new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey"));
    Set<PublicKey> keys = new HashSet();
    keys.add(ownerKey.getPublicKey());
    Contract stepaTU = InnerContractsService.createFreshTU(100, keys, true);
    stepaTU.check();
    // stepaTU.setIsTU(true);
    stepaTU.traceErrors();
    node.registerItem(stepaTU);
    ItemResult itemResult = node.waitItem(stepaTU.getId(), 18000);
    assertEquals(ItemState.APPROVED, itemResult.state);
    Parcel parcel = ContractsService.createParcel(stepaCoins, stepaTU, 1, stepaPrivateKeys, false);
    parcel.getPayment().getContract().paymentCheck(config.getTransactionUnitsIssuerKey());
    parcel.getPayment().getContract().traceErrors();
    parcel.getPayload().getContract().check();
    parcel.getPayload().getContract().traceErrors();
    assertEquals(100 - 1, parcel.getPaymentContract().getStateData().getIntOrThrow("transaction_units"));
    assertEquals(10000, parcel.getPaymentContract().getStateData().getIntOrThrow("test_transaction_units"));
    assertTrue(parcel.getPaymentContract().isOk());
    assertFalse(parcel.getPaymentContract().isLimitedForTestnet());
    assertTrue(parcel.getPayloadContract().isOk());
    assertFalse(parcel.getPayloadContract().isLimitedForTestnet());
    System.out.println("Parcel: " + parcel.getId());
    System.out.println("Payment contract: " + parcel.getPaymentContract().getId() + " is TU: " + parcel.getPaymentContract().isTU(config.getTransactionUnitsIssuerKey(), config.getTUIssuerName()));
    System.out.println("Payload contract: " + parcel.getPayloadContract().getId() + " is TU: " + parcel.getPayloadContract().isTU(config.getTransactionUnitsIssuerKey(), config.getTUIssuerName()));
    // reuse payment for another contract
    Contract contract = new Contract(TestKeys.privateKey(12));
    contract.seal();
    Parcel parcel2 = new Parcel(contract.getTransactionPack(), parcel.getPayment());
    node.registerParcel(parcel);
    node.registerParcel(parcel2);
    node.waitParcel(parcel.getId(), 8000);
    node.waitParcel(parcel2.getId(), 8000);
    // check payment and payload contracts
    assertEquals(ItemState.APPROVED, node.waitItem(parcel.getPayment().getContract().getId(), 8000).state);
    assertEquals(ItemState.APPROVED, node.waitItem(parcel.getPayload().getContract().getId(), 8000).state);
    assertNotEquals(ItemState.APPROVED, node.waitItem(parcel2.getPayload().getContract().getId(), 8000).state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Example 58 with PrivateKey

use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method checkPayment_wrongIssuer.

@Test(timeout = 90000)
public void checkPayment_wrongIssuer() throws Exception {
    Contract payment = checkPayment_preparePaymentContract(checkPayment_preparePrivateKeys());
    PrivateKey manufactureFakePrivateKey = new PrivateKey(Do.read(ROOT_PATH + "keys/marty_mcfly.private.unikey"));
    SimpleRole issuerRole = new SimpleRole("issuer");
    KeyRecord kr = new KeyRecord(manufactureFakePrivateKey.getPublicKey());
    issuerRole.addKeyRecord(kr);
    payment.registerRole(issuerRole);
    boolean res = payment.paymentCheck(config.getTransactionUnitsIssuerKey());
    payment.traceErrors();
    assertFalse(res);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Test(org.junit.Test)

Example 59 with PrivateKey

use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method referenceForChangeOwnerWithCreateContract.

@Test
public void referenceForChangeOwnerWithCreateContract() throws Exception {
    // manager -
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    // issuer
    Set<PrivateKey> llcPrivateKeys = new HashSet<>();
    Set<PrivateKey> thirdPartyPrivateKeys = new HashSet<>();
    llcPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    thirdPartyPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/marty_mcfly.private.unikey")));
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Set<PublicKey> thirdPartyPublicKeys = new HashSet<>();
    for (PrivateKey pk : thirdPartyPrivateKeys) {
        thirdPartyPublicKeys.add(pk.getPublicKey());
    }
    Contract jobCertificate = new Contract(llcPrivateKeys.iterator().next());
    jobCertificate.setOwnerKeys(stepaPublicKeys);
    jobCertificate.getDefinition().getData().set("issuer", "Roga & Kopita");
    jobCertificate.getDefinition().getData().set("type", "chief accountant assignment");
    jobCertificate.seal();
    registerAndCheckApproved(jobCertificate);
    Contract llcProperty = ContractsService.createNotaryContract(llcPrivateKeys, stepaPublicKeys);
    List<String> listConditions = new ArrayList<>();
    listConditions.add("ref.definition.issuer == \"26RzRJDLqze3P5Z1AzpnucF75RLi1oa6jqBaDh8MJ3XmTaUoF8R\"");
    listConditions.add("ref.definition.data.issuer == \"Roga & Kopita\"");
    listConditions.add("ref.definition.data.type == \"chief accountant assignment\"");
    Reference reference = new Reference(llcProperty);
    reference.name = "certification_contract";
    reference.type = Reference.TYPE_EXISTING;
    Binder conditions = new Binder();
    conditions.set("all_of", listConditions);
    reference.setConditions(conditions);
    reference.addMatchingItem(jobCertificate);
    // llcProperty.getDefinition().getReferences().add(reference);
    llcProperty.addReference(reference);
    ListRole listRole = new ListRole("list_role");
    SimpleRole ownerRole = new SimpleRole("owner", stepaPrivateKeys);
    listRole.addRole(ownerRole);
    listRole.addRequiredReference("certification_contract", Role.RequiredMode.ALL_OF);
    llcProperty.getPermissions().remove("change_owner");
    llcProperty.getPermissions().remove("revoke");
    ChangeOwnerPermission changeOwnerPerm = new ChangeOwnerPermission(listRole);
    llcProperty.addPermission(changeOwnerPerm);
    RevokePermission revokePerm = new RevokePermission(listRole);
    llcProperty.addPermission(revokePerm);
    llcProperty.addSignerKey(llcPrivateKeys.iterator().next());
    llcProperty.seal();
    registerAndCheckApproved(llcProperty);
    Contract llcProperty2 = llcProperty.createRevision(stepaPrivateKeys);
    llcProperty2.setOwnerKeys(thirdPartyPublicKeys);
    llcProperty2.seal();
    llcProperty2.check();
    llcProperty2.traceErrors();
    assertFalse(llcProperty2.isOk());
    TransactionPack tp_before = llcProperty2.getTransactionPack();
    // tp_before.addReferencedItem(jobCertificate);
    byte[] data = tp_before.pack();
    TransactionPack tp_after = TransactionPack.unpack(data);
    registerAndCheckApproved(tp_after);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Binder(net.sergeych.tools.Binder) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ListRole(com.icodici.universa.contract.roles.ListRole) Test(org.junit.Test)

Example 60 with PrivateKey

use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.

the class BaseNetworkTest method shouldDeclineSplitAndJoinWithWrongAmount.

@Test(timeout = 90000)
public void shouldDeclineSplitAndJoinWithWrongAmount() throws Exception {
    if (node == null) {
        System.out.println("network not inited");
        return;
    }
    PrivateKey key = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    // 100
    Contract c = Contract.fromDslFile(ROOT_PATH + "coin100.yml");
    c.addSignerKey(key);
    assertTrue(c.check());
    c.seal();
    registerAndCheckApproved(c);
    assertEquals(100, c.getStateData().get("amount"));
    // split 100 - 30 = 70
    Contract c1 = ContractsService.createSplit(c, 30, "amount", new HashSet<PrivateKey>(Arrays.asList(key)));
    Contract c2 = c1.getNew().get(0);
    registerAndCheckApproved(c1);
    assertEquals("70", c1.getStateData().get("amount").toString());
    assertEquals("30", c2.getStateData().get("amount").toString());
    // wrong. send 500 out of 2 contracts (70 + 30)
    Contract c3 = c2.createRevision();
    c3.getStateData().set("amount", new Decimal(500));
    c3.addSignerKey(key);
    c3.addRevokingItems(c1);
    assertFalse(c3.check());
    c3.seal();
    registerAndCheckDeclined(c3);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Aggregations

PrivateKey (com.icodici.crypto.PrivateKey)168 Test (org.junit.Test)122 PublicKey (com.icodici.crypto.PublicKey)76 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)29 Contract (com.icodici.universa.contract.Contract)24 Binder (net.sergeych.tools.Binder)22 IOException (java.io.IOException)16 HashSet (java.util.HashSet)14 ListRole (com.icodici.universa.contract.roles.ListRole)12 SQLException (java.sql.SQLException)11 Ignore (org.junit.Ignore)9 File (java.io.File)8 KeyAddress (com.icodici.crypto.KeyAddress)7 KeyRecord (com.icodici.universa.contract.KeyRecord)7 Role (com.icodici.universa.contract.roles.Role)7 Semaphore (java.util.concurrent.Semaphore)7 RoleLink (com.icodici.universa.contract.roles.RoleLink)5 TimeoutException (java.util.concurrent.TimeoutException)5 Decimal (com.icodici.universa.Decimal)4 HashId (com.icodici.universa.HashId)4