Search in sources :

Example 1 with NSmartContract

use of com.icodici.universa.contract.services.NSmartContract in project universa by UniversaBlockchain.

the class SmartContractTest method goodSmartContractFromDSL.

@Test
public void goodSmartContractFromDSL() throws Exception {
    Contract smartContract = NSmartContract.fromDslFile(rootPath + "NotarySmartDSLTemplate.yml");
    smartContract.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    smartContract.seal();
    smartContract.check();
    smartContract.traceErrors();
    assertTrue(smartContract.isOk());
    assertEquals(NSmartContract.SmartContractType.N_SMART_CONTRACT.name(), smartContract.getDefinition().getExtendedType());
    assertEquals(NSmartContract.SmartContractType.N_SMART_CONTRACT.name(), smartContract.get("definition.extended_type"));
    assertTrue(smartContract instanceof NSmartContract);
    assertTrue(smartContract instanceof NContract);
}
Also used : NSmartContract(com.icodici.universa.contract.services.NSmartContract) NContract(com.icodici.universa.contract.services.NContract) NContract(com.icodici.universa.contract.services.NContract) NSmartContract(com.icodici.universa.contract.services.NSmartContract) Test(org.junit.Test)

Example 2 with NSmartContract

use of com.icodici.universa.contract.services.NSmartContract in project universa by UniversaBlockchain.

the class BaseMainTest method registerWithMinimumKeys.

protected ItemResult registerWithMinimumKeys(Contract contract, Collection<PrivateKey> signatures, TestSpace ts, int payingParcelAmount) throws Exception {
    for (PrivateKey key : signatures) {
        HashSet<PrivateKey> currentKeys = new HashSet<>(signatures);
        currentKeys.remove(key);
        NSmartContract.NodeInfoProvider nodeInfoProvider = null;
        if (contract instanceof NSmartContract) {
            nodeInfoProvider = ((NSmartContract) contract).getNodeInfoProvider();
        }
        Contract c = Contract.fromPackedTransaction(contract.getPackedTransaction());
        if (c instanceof NSmartContract) {
            ((NSmartContract) c).setNodeInfoProvider(nodeInfoProvider);
        }
        c.getKeysToSignWith().addAll(currentKeys);
        c.seal();
        Contract u = getApprovedUContract(ts);
        Parcel parcel;
        if (payingParcelAmount > 0) {
            parcel = ContractsService.createPayingParcel(c.getTransactionPack(), u, 1, payingParcelAmount, new HashSet<PrivateKey>(Do.listOf(ts.myKey)), false);
        } else {
            parcel = ContractsService.createParcel(c, u, 1, new HashSet<PrivateKey>(Do.listOf(ts.myKey)));
        }
        assertEquals(ts.client.registerParcelWithState(parcel.pack(), 8000).state, ItemState.DECLINED);
        synchronized (ts.uContractLock) {
            ts.uContract = parcel.getPaymentContract();
        }
        Thread.sleep(500);
    }
    HashSet<PrivateKey> currentKeys = new HashSet<>(signatures);
    contract.getKeysToSignWith().clear();
    contract.getKeysToSignWith().addAll(currentKeys);
    contract.seal();
    contract.getKeysToSignWith().clear();
    Contract u = getApprovedUContract(ts);
    Parcel parcel;
    if (payingParcelAmount > 0) {
        parcel = Parcel.of(contract, u, new HashSet<>(Do.listOf(ts.myKey)), payingParcelAmount);
    } else {
        parcel = Parcel.of(contract, u, new HashSet<>(Do.listOf(ts.myKey)));
    }
    ItemResult ir = ts.client.registerParcelWithState(parcel.pack(), 80000);
    if (ir.state != ItemState.APPROVED)
        System.out.println(ir);
    assertEquals(ir.state, ItemState.APPROVED);
    synchronized (ts.uContractLock) {
        if (payingParcelAmount > 0) {
            ts.uContract = (Contract) parcel.getPayloadContract().getNewItems().stream().filter(c -> c.isU(ts.node.config.getUIssuerKeys(), ts.node.config.getUIssuerName())).findFirst().get();
        } else {
            ts.uContract = parcel.getPaymentContract();
        }
    }
    return ir;
}
Also used : NSmartContract(com.icodici.universa.contract.services.NSmartContract) PrivateKey(com.icodici.crypto.PrivateKey) Parcel(com.icodici.universa.contract.Parcel) Contract(com.icodici.universa.contract.Contract) NSmartContract(com.icodici.universa.contract.services.NSmartContract)

Example 3 with NSmartContract

use of com.icodici.universa.contract.services.NSmartContract in project universa by UniversaBlockchain.

the class Node method getFullEnvironment.

/**
 * Get environment and follower contract by environment identifier.
 *
 * @param environmentId is environment subscription
 *
 * @return {@link Binder} with environment and follower contract
 */
public Binder getFullEnvironment(long environmentId) {
    NImmutableEnvironment ime = getEnvironment(environmentId);
    ime.setNameCache(nameCache);
    NSmartContract contract = ime.getContract();
    contract.setNodeInfoProvider(nodeInfoProvider);
    NMutableEnvironment me = ime.getMutable();
    if (me == null)
        return Binder.EMPTY;
    return Binder.of("follower", contract, "environment", me);
}
Also used : NSmartContract(com.icodici.universa.contract.services.NSmartContract) NMutableEnvironment(com.icodici.universa.contract.services.NMutableEnvironment) NImmutableEnvironment(com.icodici.universa.contract.services.NImmutableEnvironment)

Example 4 with NSmartContract

use of com.icodici.universa.contract.services.NSmartContract in project universa by UniversaBlockchain.

the class Contract method fromSealedBinary.

public static Contract fromSealedBinary(byte[] sealedBinary, TransactionPack tp) throws IOException {
    Binder data = Boss.unpack(sealedBinary);
    if (!data.getStringOrThrow("type").equals("unicapsule"))
        throw new UnicapsuleExpectedException("wrong object type, unicapsule required");
    byte[] contractBytes = data.getBinaryOrThrow("data");
    // This must be explained. By default, Boss.load will apply contract transformation in place
    // as it is registered BiSerializable type, and we want to avoid it. Therefore, we decode boss
    // data without BiSerializer and then do it by hand calling deserialize:
    Binder payload = Boss.load(contractBytes, null);
    // contract can be extended type - we need know about it before
    String extendedType = payload.getBinder("contract").getBinder("definition").getString("extended_type", null);
    NSmartContract.SmartContractType scType = null;
    if (extendedType != null) {
        try {
            scType = NSmartContract.SmartContractType.valueOf(extendedType);
        } catch (IllegalArgumentException e) {
        }
    }
    Contract result;
    // and if extended type of contract is allowed - create extended contrac, otherwise create simple contract
    if (scType != null) {
        switch(scType) {
            case N_SMART_CONTRACT:
                result = new NSmartContract(data, tp);
                break;
            case SLOT1:
                result = new SlotContract(data, tp);
                break;
            case UNS1:
                result = new UnsContract(data, tp);
                break;
            case UNS2:
                result = new UnsContract(data, tp);
                break;
            case FOLLOWER1:
                result = new FollowerContract(data, tp);
                break;
            default:
                // unknwon extended type. create simple contract
                // TODO: should we throw?
                result = new Contract(data, tp);
        }
    } else {
        result = new Contract(data, tp);
    }
    result.sealedBinary = sealedBinary;
    return result;
}
Also used : Binder(net.sergeych.tools.Binder) NSmartContract(com.icodici.universa.contract.services.NSmartContract) FollowerContract(com.icodici.universa.contract.services.FollowerContract) UnsContract(com.icodici.universa.contract.services.UnsContract) NSmartContract(com.icodici.universa.contract.services.NSmartContract) UnsContract(com.icodici.universa.contract.services.UnsContract) FollowerContract(com.icodici.universa.contract.services.FollowerContract) SlotContract(com.icodici.universa.contract.services.SlotContract) SlotContract(com.icodici.universa.contract.services.SlotContract)

Example 5 with NSmartContract

use of com.icodici.universa.contract.services.NSmartContract in project universa by UniversaBlockchain.

the class TransactionPack method serialize.

@Override
public Binder serialize(BiSerializer serializer) {
    synchronized (this) {
        Binder of = Binder.of("contract", contract.getLastSealedBinary(), "subItems", serializer.serialize(subItems.values().stream().map(x -> x.getLastSealedBinary()).collect(Collectors.toList())));
        if (referencedItems.size() > 0) {
            of.set("referencedItems", serializer.serialize(referencedItems.keySet().stream().filter(id -> !subItems.containsKey(id) && !id.equals(contract.getId())).map(x -> referencedItems.get(x).getLastSealedBinary()).collect(Collectors.toList())));
        }
        if (taggedItems.size() > 0) {
            Binder tagsBinder = new Binder();
            // do not serialize tags with reserved prefix. these are only added by runtime at node side
            taggedItems.forEach((k, v) -> {
                if (!k.startsWith(TAG_PREFIX_RESERVED))
                    tagsBinder.put(k, serializer.serialize(v.getId()));
            });
            of.set("tags", tagsBinder);
        }
        if (keysForPack.size() > 0) {
            of.set("keys", serializer.serialize(keysForPack.stream().map(x -> x.pack()).collect(Collectors.toList())));
        }
        if (contract instanceof NSmartContract) {
            of.set("extended_type", ((NSmartContract) contract).getExtendedType());
        }
        return of;
    }
}
Also used : HashIdentifiable(com.icodici.universa.HashIdentifiable) PrivateKey(com.icodici.crypto.PrivateKey) java.util(java.util) Predicate(java.util.function.Predicate) IOException(java.io.IOException) Quantiser(com.icodici.universa.node2.Quantiser) Binder(net.sergeych.tools.Binder) Bytes(net.sergeych.utils.Bytes) Collectors(java.util.stream.Collectors) PublicKey(com.icodici.crypto.PublicKey) HashId(com.icodici.universa.HashId) NSmartContract(com.icodici.universa.contract.services.NSmartContract) net.sergeych.biserializer(net.sergeych.biserializer) Boss(net.sergeych.boss.Boss) Binder(net.sergeych.tools.Binder) NSmartContract(com.icodici.universa.contract.services.NSmartContract)

Aggregations

NSmartContract (com.icodici.universa.contract.services.NSmartContract)10 PrivateKey (com.icodici.crypto.PrivateKey)6 NContract (com.icodici.universa.contract.services.NContract)6 Test (org.junit.Test)6 Binder (net.sergeych.tools.Binder)4 PublicKey (com.icodici.crypto.PublicKey)1 HashId (com.icodici.universa.HashId)1 HashIdentifiable (com.icodici.universa.HashIdentifiable)1 Contract (com.icodici.universa.contract.Contract)1 Parcel (com.icodici.universa.contract.Parcel)1 FollowerContract (com.icodici.universa.contract.services.FollowerContract)1 NImmutableEnvironment (com.icodici.universa.contract.services.NImmutableEnvironment)1 NMutableEnvironment (com.icodici.universa.contract.services.NMutableEnvironment)1 SlotContract (com.icodici.universa.contract.services.SlotContract)1 UnsContract (com.icodici.universa.contract.services.UnsContract)1 Quantiser (com.icodici.universa.node2.Quantiser)1 IOException (java.io.IOException)1 java.util (java.util)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1