Search in sources :

Example 26 with HashId

use of com.icodici.universa.HashId in project universa by UniversaBlockchain.

the class ItemNotificationTest method packUnpack.

@Test
public void packUnpack() throws Exception {
    NodeInfo ni = new NodeInfo(TestKeys.publicKey(0), 1, "test1", "localhost", 17101, 17102, 17104);
    HashId id1 = HashId.createRandom();
    ZonedDateTime now = ZonedDateTime.now().truncatedTo(ChronoUnit.SECONDS);
    ZonedDateTime expiresAt = now.plusDays(30);
    ItemResult ir1 = new ItemResult(ItemState.PENDING, false, now, expiresAt);
    ItemResult ir2 = new ItemResult(ItemState.REVOKED, true, now, expiresAt);
    ItemNotification n1 = new ItemNotification(ni, id1, ir1, true);
    ItemNotification n2 = new ItemNotification(ni, id1, ir1, false);
    ItemNotification n3 = new ItemNotification(ni, id1, ir1, true);
    byte[] packed = Notification.pack(asList(n1, n2, n3));
    List<Notification> l = Notification.unpack(ni, packed);
    assertEquals(3, l.size());
    ItemNotification n = (ItemNotification) l.get(0);
    assertEquals(n, n1);
    n = (ItemNotification) l.get(1);
    assertEquals(n, n2);
    n = (ItemNotification) l.get(2);
    assertEquals(n, n3);
}
Also used : HashId(com.icodici.universa.HashId) ItemResult(com.icodici.universa.node.ItemResult) ZonedDateTime(java.time.ZonedDateTime) Test(org.junit.Test)

Example 27 with HashId

use of com.icodici.universa.HashId in project universa by UniversaBlockchain.

the class ItemLockTest method lock.

/*
    Until the error in sqlite finalizers is fixed, call this test separately.
     */
@Test
@Ignore("in the sequence  causes SIGSEGV in java machine in sqlite module")
public void lock() throws Exception {
    for (int z = 0; z < 10; z++) {
        HashId id = HashId.createRandom();
        count = 0;
        ItemLock il = new ItemLock();
        il.synchronize(id, (__) -> count++);
        il.synchronize(id, (__) -> count++);
        il.synchronize(id, (__) -> count++);
        assertEquals(3, count);
        assertEquals(1, il.size());
        id = null;
        for (int i = 0; i < 10; i++) {
            System.gc();
            System.runFinalization();
            if (il.size() == 0)
                break;
            Thread.sleep(100);
        }
        assertEquals(0, il.size());
    }
}
Also used : HashId(com.icodici.universa.HashId) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 28 with HashId

use of com.icodici.universa.HashId in project universa by UniversaBlockchain.

the class SplitJoinPermissionTest method shouldNotSplitWithWrongParentSerialize.

@Test
public void shouldNotSplitWithWrongParentSerialize() throws Exception {
    Contract c = createCoin();
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(c, true);
    Contract c2 = c.split(1)[0];
    sealCheckTrace(c2, true);
    Binder sd2 = DefaultBiMapper.serialize(c2);
    Binder state = (Binder) sd2.get("state");
    assertNotNull(state);
    assertTrue(state.size() > 0);
    HashId parent = HashId.withDigest(Do.randomNegativeBytes(64));
    Binder parentB = DefaultBiMapper.serialize(parent);
    state.set("parent", parentB);
    Contract dc2 = DefaultBiMapper.deserialize(sd2);
    sealCheckTrace(dc2, false);
    state.remove("parent");
    Contract dc3 = DefaultBiMapper.deserialize(sd2);
    sealCheckTrace(dc3, false);
}
Also used : HashId(com.icodici.universa.HashId) Binder(net.sergeych.tools.Binder) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 29 with HashId

use of com.icodici.universa.HashId in project universa by UniversaBlockchain.

the class SplitJoinPermissionTest method shouldNotJoinWithWrongParent.

@Test
public void shouldNotJoinWithWrongParent() throws Exception {
    Contract c = createCoin();
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Contract c1 = c.splitValue(FIELD_NAME, new Decimal(1));
    Contract.ContractDev dev = c1.new ContractDev(c1);
    // Check after split.
    sealCheckTrace(c, true);
    // Set wrong parent
    HashId parent = HashId.withDigest(Do.randomNegativeBytes(64));
    HashId origin = HashId.withDigest(Do.randomNegativeBytes(64));
    dev.setParent(parent);
    dev.setOrigin(origin);
    c.getRevokingItems().add(dev.getContract());
    sealCheckTrace(c, false);
}
Also used : HashId(com.icodici.universa.HashId) Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 30 with HashId

use of com.icodici.universa.HashId in project universa by UniversaBlockchain.

the class ContractsService method signPresentedSwap.

/**
 * Second step of swap procedure. Calls from swapper2 part.
 *<br><br>
 * Swapper2 got swap contract from swapper1 and give it to service.
 * Service sign new contract where calling part was owner, store hashId of this contract.
 * Then add to reference of new contract, that will be own for calling part,
 * contract_id and point it to contract that was own for calling part.
 * Then sign second contract too.
 *<br><br>
 * Swap procedure consist from three steps:<br>
 * (1) prepare contracts with creating transactional section on the first swapper site, sign only one contract;<br>
 * (2) sign contracts on the second swapper site;<br>
 * (3) sign lost contracts on the first swapper site and finishing swap.
 *<br><br>
 * @param swapContract is being processing swap contract, got from swapper1
 * @param keys is own (belongs to swapper2) private keys
 * @return modified swapContract;
 * should be send back to partner (swapper1) and he should go to step (3) of the swap procedure.
 */
public static synchronized Contract signPresentedSwap(Contract swapContract, Set<PrivateKey> keys) {
    Set<PublicKey> publicKeys = new HashSet<>();
    for (PrivateKey pk : keys) {
        publicKeys.add(pk.getPublicKey());
    }
    List<Contract> swappingContracts = (List<Contract>) swapContract.getNew();
    // looking for contract that will be own and sign it
    HashMap<String, HashId> contractHashId = new HashMap<>();
    for (Contract c : swappingContracts) {
        boolean willBeMine = c.getOwner().isAllowedForKeys(publicKeys);
        if (willBeMine) {
            c.addSignatureToSeal(keys);
            contractHashId.put(c.getTransactional().getId(), c.getId());
        }
    }
    // looking for contract that was own, add to reference hash of above contract and sign it
    for (Contract c : swappingContracts) {
        boolean willBeNotMine = (!c.getOwner().isAllowedForKeys(publicKeys));
        if (willBeNotMine) {
            Set<KeyRecord> krs = new HashSet<>();
            for (PublicKey k : publicKeys) {
                krs.add(new KeyRecord(k));
            }
            c.setCreator(krs);
            if (c.getTransactional() != null && c.getTransactional().getReferences() != null) {
                for (Reference rm : c.getTransactional().getReferences()) {
                    rm.contract_id = contractHashId.get(rm.transactional_id);
                }
            } else {
                return swapContract;
            }
            c.seal();
            c.addSignatureToSeal(keys);
        }
    }
    swapContract.seal();
    return swapContract;
}
Also used : HashId(com.icodici.universa.HashId) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey)

Aggregations

HashId (com.icodici.universa.HashId)30 Test (org.junit.Test)18 Contract (com.icodici.universa.contract.Contract)6 PrivateKey (com.icodici.crypto.PrivateKey)4 PublicKey (com.icodici.crypto.PublicKey)3 ZonedDateTime (java.time.ZonedDateTime)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 Binder (net.sergeych.tools.Binder)3 Approvable (com.icodici.universa.Approvable)2 Decimal (com.icodici.universa.Decimal)2 ItemResult (com.icodici.universa.node.ItemResult)2 ItemState (com.icodici.universa.node.ItemState)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 EncryptionError (com.icodici.crypto.EncryptionError)1 ErrorRecord (com.icodici.universa.ErrorRecord)1 Parcel (com.icodici.universa.contract.Parcel)1 com.icodici.universa.node2 (com.icodici.universa.node2)1