SlotContract slotContract = new SlotContract(); slotContract.setNodeInfoProvider(nodeInfoProvider); slotContract.setApiLevel(3); Contract.Definition cd = slotContract.getDefinition(); cd.setExpiresAt(slotContract.getCreatedAt().plusMonths(60)); slotContract.registerRole(issuerRole); slotContract.createRole("issuer", issuerRole); slotContract.createRole("creator", issuerRole); slotContract.registerRole(ownerRole); slotContract.createRole("owner", ownerRole); slotContract.addPermission(changeOwnerPerm); slotContract.addPermission(revokePerm1); slotContract.addPermission(revokePerm2); slotContract.addSlotSpecific(); slotContract.seal(); slotContract.addSignatureToSeal(issuerKeys);
addError(Errors.FAILED_CHECK, "Environment should be not null"); return checkResult; checkResult = getExtendedType().equals(SmartContractType.SLOT1.name()); if(!checkResult) { addError(Errors.FAILED_CHECK, "definition.extended_type", "illegal value, should be " + SmartContractType.SLOT1.name() + " instead " + getExtendedType()); return checkResult; checkResult = getTrackingContract() != null; if(!checkResult) { addError(Errors.FAILED_CHECK, "Tracking contract is missed"); return checkResult; if(getTrackingContract() != null) { checkResult = getTrackingContract().getOrigin().equals(tc.getOrigin()); if (!checkResult) { addError(Errors.FAILED_CHECK, "Slot-contract should store only contracts with same origin"); return checkResult;
@Override public void onContractSubscriptionEvent(ContractSubscription.Event event) { if(event instanceof ContractSubscription.ApprovedEvent) { MutableEnvironment me = event.getEnvironment(); // recreate subscription: Contract newStoredItem = ((ContractSubscription.ApprovedEvent)event).getNewRevision(); putTrackingContract(newStoredItem); saveTrackingContractsToState(); // and save new updateSubscriptions(me); } else if(event instanceof ContractSubscription.RevokedEvent) { } }
@Override public boolean beforeUpdate(ImmutableEnvironment c) { boolean checkResult = false; // recalculate storing info without saving to state to get valid storing data calculatePrepaidKilobytesForDays(false); // check that payment was not hacked checkResult = prepaidKilobytesForDays == getStateData().getInt(PREPAID_KD_FIELD_NAME, 0); if(!checkResult) { addError(Errors.FAILED_CHECK, "Wrong [state.data." + PREPAID_KD_FIELD_NAME + "] value. " + "Should be sum of early paid U and paid U by current revision."); return checkResult; } if(getTrackingContract() != null) { // check for that last revision of tracking contract has same owner as creator of slot checkResult = getTrackingContract().getOwner().isAllowedForKeys(getEffectiveKeys()); if (!checkResult) { addError(Errors.FAILED_CHECK, "Slot-contract signing keys must has allowed keys for owner of tracking contract"); return checkResult; } } // and call common slot check checkResult = additionallySlotCheck(c); return checkResult; }
calculatePrepaidKilobytesForDays(false); int paidU = getPaidU(); if(paidU == 0) { if(getPaidU(true) > 0) { addError(Errors.FAILED_CHECK, "Test payment is not allowed for storing contracts in slots"); } else if(paidU < getMinPayment()) { addError(Errors.FAILED_CHECK, "Payment for slot contract is below minimum level of " + getMinPayment() + "U"); checkResult = false; addError(Errors.FAILED_CHECK, "Slot contract hasn't valid payment"); return checkResult; checkResult = prepaidKilobytesForDays == getStateData().getInt(PREPAID_KD_FIELD_NAME, 0); if(!checkResult) { addError(Errors.FAILED_CHECK, "Wrong [state.data." + PREPAID_KD_FIELD_NAME + "] value. " + "Should be sum of early paid U and paid U by current revision."); return checkResult; if(getTrackingContract() != null) { checkResult = getTrackingContract().getOwner().isAllowedForKeys(getEffectiveKeys()); if (!checkResult) { addError(Errors.FAILED_CHECK, "Slot-contract signing keys must has allowed keys for owner of tracking contract"); return checkResult; checkResult = additionallySlotCheck(c);
slotContract.putTrackingContract(simpleContract); Parcel payingParcel = ContractsService.createPayingParcel(slotContract.getTransactionPack(), paymentContract, 1, 100, stepaPrivateKeys, false); slotContract.check(); slotContract.traceErrors(); assertTrue(slotContract.isOk()); assertEquals(NSmartContract.SmartContractType.SLOT1.name(), slotContract.getDefinition().getExtendedType()); assertEquals(NSmartContract.SmartContractType.SLOT1.name(), slotContract.get("definition.extended_type")); assertEquals(100 * config.getServiceRate(NSmartContract.SmartContractType.SLOT1.name()).doubleValue(), slotContract.getPrepaidKilobytesDays().doubleValue(), 0.01); slotContract.traceErrors(); assertEquals(ItemState.REVOKED, node.waitItem(payingParcel.getPayment().getContract().getId(), 8000).state); assertEquals(ItemState.APPROVED, node.waitItem(payingParcel.getPayload().getContract().getId(), 8000).state); assertEquals(ItemState.APPROVED, node.waitItem(slotContract.getNew().get(0).getId(), 8000).state); ItemResult itemResult = node.waitItem(slotContract.getId(), 8000); assertEquals("ok", itemResult.extraDataBinder.getBinder("onCreatedResult").getString("status", null)); assertEquals(simpleContract.getId(), slotContract.getTrackingContract().getId()); assertEquals(simpleContract.getId(), ((SlotContract) payingParcel.getPayload().getContract()).getTrackingContract().getId()); assertNotNull(node.getLedger().getEnvironment(slotContract.getId())); slotContract2.putTrackingContract(simpleContract); payingParcel = ContractsService.createPayingParcel(slotContract2.getTransactionPack(), paymentContract, 1, 100, stepaPrivateKeys, false);
slotContract.setKeepRevisions(5); slotContract.putTrackingContract(simpleContract); slotContract.seal(); TransactionPack tp_before = slotContract.getTransactionPack(); TransactionPack tp_after = TransactionPack.unpack(tp_before.pack()); assertEquals(((SlotContract)tp_after.getContract()).getKeepRevisions(), 5); Parcel payingParcel = ContractsService.createPayingParcel(slotContract.getTransactionPack(), paymentContract, 1, 100, stepaPrivateKeys, false); slotContract.check(); slotContract.traceErrors(); assertTrue(slotContract.isOk()); assertEquals(ItemState.APPROVED, node.waitItem(slotContract.getNew().get(0).getId(), 8000).state); SlotContract refilledSlotContract = (SlotContract) slotContract.createRevision(keysSlotRevisions); refilledSlotContract.putTrackingContract(simpleContract2); refilledSlotContract.setNodeInfoProvider(nodeInfoProvider); refilledSlotContract.seal(); assertEquals(refilledSlotContract.getKeepRevisions(), 5); tp_before = refilledSlotContract.getTransactionPack(); tp_after = TransactionPack.unpack(tp_before.pack()); assertEquals(((SlotContract)tp_after.getContract()).getKeepRevisions(), 5);
Contract smartContract = new SlotContract(key); ((SlotContract)smartContract).putTrackingContract(simpleContract); ((SlotContract)smartContract).setNodeInfoProvider(nodeInfoProvider); smartContract.addNewItems(paymentDecreased); smartContract.seal(); assertTrue(((ModifyDataPermission)mdp.iterator().next()).getFields().containsKey("action")); assertEquals(simpleContract.getId(), ((SlotContract) smartContract).getTrackingContract().getId()); assertEquals(simpleContract.getId(), TransactionPack.unpack(((SlotContract) smartContract).getPackedTrackingContract()).getContract().getId());
slotContract.setNodeInfoProvider(nodeInfoProvider); slotContract.putTrackingContract(simpleContract); Parcel parcel = ContractsService.createPayingParcel(slotContract.getTransactionPack(), stepaU, 1, 100, new HashSet<>(Arrays.asList(TestKeys.privateKey(1))), false); Binder slotInfo = client.querySlotInfo(slotContract.getId()); System.out.println("slot info is null: " + (slotInfo == null)); assertNull(slotInfo); byte[] simpleContractBytes = client.queryContract(slotContract.getId(), null, simpleContract.getId()); System.out.println("simpleContractBytes (by contractId): " + simpleContractBytes); assertEquals(false, Arrays.equals(simpleContract.getPackedTransaction(), simpleContractBytes)); simpleContractBytes = client.queryContract(slotContract.getId(), simpleContract.getOrigin(), null); System.out.println("simpleContractBytes (by originId): " + simpleContractBytes); assertEquals(false, Arrays.equals(simpleContract.getPackedTransaction(), simpleContractBytes)); itemResult = client.getState(slotContract.getId()); System.out.println("slot : " + itemResult); assertEquals(ItemState.APPROVED, itemResult.state); slotInfo = client.querySlotInfo(slotContract.getId()); System.out.println("slot info size: " + slotInfo.size()); assertNotNull(slotInfo); simpleContractBytes = client.queryContract(slotContract.getId(), null, simpleContract.getId()); System.out.println("simpleContractBytes (by contractId) length: " + simpleContractBytes.length); assertEquals(true, Arrays.equals(simpleContract.getPackedTransaction(), simpleContractBytes));
Contract smartContract = new SlotContract(key); ((SlotContract)smartContract).putTrackingContract(simpleContract); ((SlotContract)smartContract).setNodeInfoProvider(nodeInfoProvider); ((SlotContract)smartContract).setKeepRevisions(2); smartContract.addNewItems(paymentDecreased); smartContract.seal(); assertEquals(1, ((SlotContract)smartContract).getTrackingContracts().size()); assertEquals(simpleContract.getId(), ((SlotContract) smartContract).getTrackingContract().getId()); assertEquals(simpleContract.getId(), TransactionPack.unpack(((SlotContract) smartContract).getPackedTrackingContract()).getContract().getId()); ((SlotContract)smartContract).putTrackingContract(simpleContract2); smartContract.seal(); smartContract.check(); assertEquals(2, ((SlotContract)smartContract).getTrackingContracts().size()); assertEquals(simpleContract2.getId(), ((SlotContract) smartContract).getTrackingContract().getId()); assertEquals(simpleContract2.getId(), TransactionPack.unpack(((SlotContract) smartContract).getPackedTrackingContract()).getContract().getId()); ((SlotContract)smartContract).putTrackingContract(simpleContract3); smartContract.seal(); smartContract.check(); assertEquals(2, ((SlotContract)smartContract).getTrackingContracts().size()); assertEquals(simpleContract3.getId(), ((SlotContract) smartContract).getTrackingContract().getId()); assertEquals(simpleContract3.getId(), TransactionPack.unpack(((SlotContract) smartContract).getPackedTrackingContract()).getContract().getId());
slotContract.putTrackingContract(simpleContract); slotContract.seal(); slotContract.check(); slotContract.traceErrors(); assertTrue(slotContract.isOk()); Parcel payingParcel = ContractsService.createPayingParcel(baseContract.getTransactionPack(), paymentContract, 1, nodeInfoProvider.getMinPayment(slotContract.getExtendedType()) - 1, stepaPrivateKeys, false); for (Contract c: baseContract.getNew()) if (!c.equals(slotContract)) { baseContract.getNewItems().remove(c); slotContract.addNewItems(c); slotContract.seal(); baseContract.seal(); break;
Contract smartContract = SlotContract.fromDslFile(rootPath + "SlotDSLTemplate.yml"); smartContract.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey"); ((SlotContract)smartContract).putTrackingContract(simpleContract); ((SlotContract)smartContract).setNodeInfoProvider(nodeInfoProvider); smartContract.addNewItems(paymentDecreased); smartContract.seal(); assertEquals(NSmartContract.SmartContractType.SLOT1.name(), smartContract.get("definition.extended_type")); assertEquals(2, ((SlotContract) smartContract).getKeepRevisions()); assertTrue(((ModifyDataPermission)mdp.iterator().next()).getFields().containsKey("action")); assertEquals(simpleContract.getId(), ((SlotContract) smartContract).getTrackingContract().getId()); assertEquals(simpleContract.getId(), TransactionPack.unpack(((SlotContract) smartContract).getPackedTrackingContract()).getContract().getId());
slotContract.putTrackingContract(trackingContract); slotContract.seal(); paymentContract = getApprovedUContract(); parcel = ContractsService.createPayingParcel(slotContract.getTransactionPack(), paymentContract, 1, 100, stepaPrivateKeys, false); node.registerParcel(parcel); synchronized (uContractLock) {uContract = parcel.getPayloadContract().getNew().get(0);} assertEquals(ItemState.APPROVED, node.waitItem(slotContract.getNew().get(0).getId(), 8000).state); NImmutableEnvironment env = n.getLedger().getEnvironment(slotContract.getId()); assertNotNull(n.getLedger().getSmartContractById(slotContract.getId())); n.getLedger().removeEnvironment(slotContract.getId()); assertNull(n.getLedger().getSmartContractById(slotContract.getId()));
if(getDefinition().getExtendedType() == null || !getDefinition().getExtendedType().equals(SmartContractType.SLOT1.name())) getDefinition().setExtendedType(SmartContractType.SLOT1.name()); registerRole(ownerLink); HashMap<String, Object> fieldsMap = new HashMap<>(); fieldsMap.put("action", null); Binder modifyDataParams = Binder.of("fields", fieldsMap); ModifyDataPermission modifyDataPermission = new ModifyDataPermission(ownerLink, modifyDataParams); addPermission(modifyDataPermission);
slotContract.putTrackingContract(simpleContract); Parcel parcel = ContractsService.createPayingParcel(slotContract.getTransactionPack(), paymentContract, 1, 170, stepaPrivateKeys, false); uContract = slotContract.getNew().get(0);
/** * Method creates {@link SlotContract} contract from dsl file where contract is described. * @param fileName is path to dsl file with yaml structure of data for contract. * @return created and ready {@link SlotContract} contract. * @throws IOException if something went wrong */ public static SlotContract fromDslFile(String fileName) throws IOException { Yaml yaml = new Yaml(); try (FileReader r = new FileReader(fileName)) { Binder binder = Binder.from(DefaultBiMapper.deserialize((Map) yaml.load(r))); return new SlotContract().initializeWithDsl(binder); } }
slotContract.putTrackingContract(simpleContract); Parcel parcel = ContractsService.createPayingParcel(slotContract.getTransactionPack(), contractU, 1, 170, stepaPrivateKeys, false);
contract = new SlotContract(bb, this); break;
} else if (origin_id != null) { HashId originHashId = HashId.withDigest(origin_id); List<byte[]> storedRevisions = node.getLedger().getContractsInStorageByOrigin(slotContract.getId(), originHashId); if (storedRevisions.size() == 1) { res.set("contract", storedRevisions.get(0));
slotContract.putTrackingContract(simpleContract); Parcel payingParcel = ContractsService.createPayingParcel(slotContract.getTransactionPack(), paymentContract, 1, 100, stepaPrivateKeys, false); slotContract.check(); slotContract.traceErrors(); assertTrue(slotContract.isOk()); assertEquals(NSmartContract.SmartContractType.SLOT1.name(), slotContract.getDefinition().getExtendedType()); assertEquals(NSmartContract.SmartContractType.SLOT1.name(), slotContract.get("definition.extended_type")); assertEquals(100 * config.getServiceRate(NSmartContract.SmartContractType.SLOT1.name()).doubleValue(), slotContract.getPrepaidKilobytesDays().doubleValue(), 0.01); slotContract.traceErrors(); assertEquals(ItemState.REVOKED, node.waitItem(payingParcel.getPayment().getContract().getId(), 8000).state); assertEquals(ItemState.APPROVED, node.waitItem(payingParcel.getPayload().getContract().getId(), 8000).state); assertEquals(ItemState.APPROVED, node.waitItem(slotContract.getNew().get(0).getId(), 8000).state); ItemResult itemResult = node.waitItem(slotContract.getId(), 8000); assertEquals("ok", itemResult.extraDataBinder.getBinder("onCreatedResult").getString("status", null)); assertEquals(simpleContract.getId(), slotContract.getTrackingContract().getId()); assertEquals(simpleContract.getId(), ((SlotContract) payingParcel.getPayload().getContract()).getTrackingContract().getId()); assertNotNull(networkNode.getLedger().getEnvironment(slotContract.getId())); assertNotNull(node.getLedger().getEnvironment(slotContract.getId())); SlotContract refilledSlotContract = (SlotContract) slotContract.createRevision(key); refilledSlotContract.setNodeInfoProvider(nodeInfoProvider);