Search in sources :

Example 11 with HashId

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

the class SqliteLedgerTest method createOutputLockRecord.

@Test
public void createOutputLockRecord() throws Exception {
    ledger.enableCache(true);
    StateRecord owner = ledger.findOrCreate(HashId.createRandom());
    StateRecord other = ledger.findOrCreate(HashId.createRandom());
    HashId id = HashId.createRandom();
    StateRecord r1 = owner.createOutputLockRecord(id);
    r1.reload();
    assertEquals(id, r1.getId());
    assertEquals(ItemState.LOCKED_FOR_CREATION, r1.getState());
    assertEquals(owner.getRecordId(), r1.getLockedByRecordId());
    StateRecord r2 = owner.createOutputLockRecord(id);
    assertSame(r2, r1);
    assertNull(owner.createOutputLockRecord(other.getId()));
    // And hacked low level operation must fail too
    assertNull(ledger.createOutputLockRecord(owner.getRecordId(), other.getId()));
}
Also used : HashId(com.icodici.universa.HashId) Test(org.junit.Test)

Example 12 with HashId

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

the class TransactionPack method deserialize.

@Override
public void deserialize(Binder data, BiDeserializer deserializer) throws IOException {
    synchronized (this) {
        // It is local quantiser that should throw exception
        // if limit is got while deserializing TransactionPack.
        Quantiser quantiser = new Quantiser();
        quantiser.reset(Contract.getTestQuantaLimit());
        List<Object> keysList = deserializer.deserializeCollection(data.getList("keys", new ArrayList<>()));
        keysForPack = new HashSet<>();
        if (keysList != null) {
            for (Object x : keysList) {
                if (x instanceof Bytes)
                    x = ((Bytes) x).toArray();
                if (x instanceof byte[]) {
                    keysForPack.add(new PublicKey((byte[]) x));
                } else {
                    throw new IllegalArgumentException("unsupported key object: " + x.getClass().getName());
                }
            }
        }
        List<Bytes> foreignReferenceBytesList = deserializer.deserializeCollection(data.getList("referencedItems", new ArrayList<>()));
        if (foreignReferenceBytesList != null) {
            for (Bytes b : foreignReferenceBytesList) {
                Contract frc = new Contract(b.toArray(), this);
                quantiser.addWorkCostFrom(frc.getQuantiser());
                referencedItems.put(frc.getId(), frc);
            }
        }
        List<Bytes> subItemsBytesList = deserializer.deserializeCollection(data.getListOrThrow("subItems"));
        HashMap<ContractDependencies, Bytes> allContractsTrees = new HashMap<>();
        List<HashId> allContractsHids = new ArrayList<>();
        ArrayList<Bytes> sortedSubItemsBytesList = new ArrayList<>();
        if (subItemsBytesList != null) {
            // First of all extract contracts dependencies from subItems
            for (Bytes b : subItemsBytesList) {
                ContractDependencies ct = new ContractDependencies(b.toArray());
                allContractsTrees.put(ct, b);
                allContractsHids.add(ct.id);
            }
            // and add items to subItems on the each level of tree's hierarchy
            do {
                // first add contract from ends of trees, means without own subitems
                sortedSubItemsBytesList = new ArrayList<>();
                List<ContractDependencies> removingContractDependencies = new ArrayList<>();
                for (ContractDependencies ct : allContractsTrees.keySet()) {
                    if (ct.dependencies.size() == 0) {
                        sortedSubItemsBytesList.add(allContractsTrees.get(ct));
                        removingContractDependencies.add(ct);
                    }
                }
                // remove found items from tree's list
                for (ContractDependencies ct : removingContractDependencies) {
                    allContractsTrees.remove(ct);
                }
                // then add contract with already exist subitems in the subItems or will never find in the tree
                removingContractDependencies = new ArrayList<>();
                for (ContractDependencies ct : allContractsTrees.keySet()) {
                    boolean allDependenciesSafe = true;
                    for (HashId hid : ct.dependencies) {
                        if (!subItems.containsKey(hid) && allContractsHids.contains(hid)) {
                            allDependenciesSafe = false;
                        }
                    }
                    if (allDependenciesSafe) {
                        sortedSubItemsBytesList.add(allContractsTrees.get(ct));
                        removingContractDependencies.add(ct);
                    }
                }
                // remove found items from tree's list
                for (ContractDependencies ct : removingContractDependencies) {
                    allContractsTrees.remove(ct);
                }
                // add found binaries on the hierarchy level to subItems
                for (int i = 0; i < sortedSubItemsBytesList.size(); i++) {
                    Contract c = new Contract(sortedSubItemsBytesList.get(i).toArray(), this);
                    quantiser.addWorkCostFrom(c.getQuantiser());
                    subItems.put(c.getId(), c);
                }
            // then repeat until we can find hierarchy
            } while (sortedSubItemsBytesList.size() != 0);
            // finally add not found binaries on the hierarchy levels to subItems
            for (Bytes b : allContractsTrees.values()) {
                Contract c = new Contract(b.toArray(), this);
                quantiser.addWorkCostFrom(c.getQuantiser());
                subItems.put(c.getId(), c);
            }
        }
        byte[] bb = data.getBinaryOrThrow("contract");
        contract = new Contract(bb, this);
        quantiser.addWorkCostFrom(contract.getQuantiser());
    }
}
Also used : HashId(com.icodici.universa.HashId) PublicKey(com.icodici.crypto.PublicKey) Quantiser(com.icodici.universa.node2.Quantiser) Bytes(net.sergeych.utils.Bytes)

Example 13 with HashId

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

the class ParcelCache method idsCheck.

public void idsCheck(HashId itemId) {
    for (HashId x : records.keySet()) {
        System.out.println(" checking " + itemId + " eq " + x + ": " + itemId.equals(x) + " / " + x.equals(itemId));
        System.out.println(" codes: " + itemId.hashCode() + " / " + x.hashCode());
        System.out.println(" digest check: " + Base64.encodeString(itemId.getDigest()));
        System.out.println(" digest data : " + Base64.encodeString(x.getDigest()));
    }
}
Also used : HashId(com.icodici.universa.HashId)

Example 14 with HashId

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

the class ItemCache method idsCheck.

public void idsCheck(HashId itemId) {
    for (HashId x : records.keySet()) {
        System.out.println(" checking " + itemId + " eq " + x + ": " + itemId.equals(x) + " / " + x.equals(itemId));
        System.out.println(" codes: " + itemId.hashCode() + " / " + x.hashCode());
        System.out.println(" digest check: " + Base64.encodeString(itemId.getDigest()));
        System.out.println(" digest data : " + Base64.encodeString(x.getDigest()));
    }
}
Also used : HashId(com.icodici.universa.HashId)

Example 15 with HashId

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

the class ItemResyncNotification method writeTo.

@Override
protected void writeTo(Boss.Writer bw) throws IOException {
    super.writeTo(bw);
    Map<String, Object> packingMap = new HashMap<>();
    for (HashId hid : itemsToResync.keySet()) {
        packingMap.put(hid.toBase64String(), itemsToResync.get(hid).ordinal());
    }
    bw.writeObject(packingMap);
}
Also used : HashId(com.icodici.universa.HashId) HashMap(java.util.HashMap)

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