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);
}
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;
}
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);
}
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;
}
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;
}
}
Aggregations