if ((((rm.transactional_id != null && neighbour.transactional != null && rm.transactional_id.equals(neighbour.transactional.id)) || (rm.contract_id != null && rm.contract_id.equals(neighbour.id))) && checkOneReference(rm, neighbour)) || (rm.getConditions().size() > 0)) // new format of reference with conditions, transactional_id - optional if (rm.isMatchingWith(neighbour, neighbours)) { rm.addMatchingItem(neighbour); rm_check = true; break; if(rm.isMatchingWith(neighbour,neighbours)) { rm.addMatchingItem(neighbour); rm_check = rm.isValid();
Reference customerRef = new Reference(); customerRef.name = "return_payment_to_customer"; customerRef.type = Reference.TYPE_TRANSACTIONAL; Binder customerConditions = new Binder(); customerConditions.set("all_of", listCustomerConditions); customerRef.setConditions(customerConditions); Reference executorRef = new Reference(); executorRef.name = "send_payment_to_executor"; executorRef.type = Reference.TYPE_TRANSACTIONAL; Binder executorConditions = new Binder(); executorConditions.set("all_of", listExecutorConditions); executorRef.setConditions(executorConditions);
/** * Adds required references to role. The references is added to the set corresponding * to the specified mode of combining references ({@link RequiredMode}). * * @param references is collection of added references * @param requiredMode is mode of combining references */ public void addAllRequiredReferences(Collection<Reference> references, RequiredMode requiredMode) { (requiredMode == ALL_OF ? requiredAllReferences : requiredAnyReferences).addAll(references.stream().map(reference -> reference.getName()).collect(Collectors.toSet())); }
private void addOriginReference(HashId origin) { Reference ref = new Reference(this); ref.type = Reference.TYPE_EXISTING_STATE; ref.setName(origin.toString()); List<Object> conditionsList = new ArrayList<>(); conditionsList.add(REFERENCE_CONDITION_PREFIX+origin.toBase64String()); Binder conditions = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList); ref.setConditions(conditions); if(originContracts.containsKey(origin)) ref.addMatchingItem(originContracts.get(origin)); addReference(ref); }
/** * Add to base {@link Contract} reference and referenced contract. * When the returned {@link Contract} is unpacking referenced contract verifies * the compliance with the conditions of reference in base contract. * <br><br> * Also reference to base contract may be added by {@link Contract#addReference(Reference)}. * <br><br> * * @param baseContract is base contract fro adding reference. * @param refContract is referenced contract (which must satisfy the conditions of the reference). * @param refName is name of reference. * @param refType is type of reference (section, may be {@link Reference#TYPE_TRANSACTIONAL}, {@link Reference#TYPE_EXISTING_DEFINITION}, or {@link Reference#TYPE_EXISTING_STATE}). * @param conditions is conditions of the reference. * @return ready {@link Contract} */ public synchronized static Contract addReferenceToContract( Contract baseContract, Contract refContract, String refName, int refType, Binder conditions) { Reference reference = new Reference(baseContract); reference.name = refName; reference.type = refType; reference.setConditions(conditions); reference.addMatchingItem(refContract); baseContract.addReference(reference); baseContract.seal(); return baseContract; }
static public Reference fromDslBinder(Binder ref, Contract contract) { String name = ref.getString("name"); String comment = ref.getString("comment", null); Binder where = null; try { where = ref.getBinderOrThrow("where"); } catch (Exception e) { // Insert simple condition to binder with key all_of List<String> simpleConditions = ref.getList("where", null); if (simpleConditions != null) where = new Binder(all_of.name(), simpleConditions); } Reference reference = new Reference(contract); if (name == null) throw new IllegalArgumentException("Expected reference name"); reference.setName(name); reference.setComment(comment); if (where != null) reference.setConditions(where); return reference; }
Reference newReference = new Reference(newContract); newReference.setName("ref1"); ArrayList<String> newConditions = new ArrayList<>(); newConditions.add("ref.id=="+referencedContract.getId().toBase64String()); newReference.setConditions(Binder.of("all_of",newConditions)); newContract.addReference(newReference); newContract.getIssuer().addRequiredReference(newReference.getName(), Role.RequiredMode.ALL_OF); newContract.seal(); Reference rootReference = new Reference(transactionRoot); rootReference.setName("ref2"); ArrayList<String> rootConditions = new ArrayList<>(); rootConditions.add("ref.id=="+referencedContract.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.seal();
Reference newContractRef = new Reference(newContract); newContractRef.setName("ref1"); ArrayList<String> revokeCondtitions = new ArrayList<>(); revokeCondtitions.add("ref.id=="+referencedContract.getId().toBase64String()); newContractRef.setConditions(Binder.of("all_of",revokeCondtitions)); newContract.addReference(newContractRef); newContract.getIssuer().addRequiredReference("ref1", Role.RequiredMode.ALL_OF); Reference rootReference = new Reference(transactionRoot); rootReference.setName("ref1"); ArrayList<String> rootConditions = new ArrayList<>(); rootConditions.add("ref.id=="+referencedContract2.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.getIssuer().addRequiredReference("ref1", Role.RequiredMode.ALL_OF);
Binder conditions = contract1.getReferences().get("ref_string").getConditions(); List<Object> condList = conditions.getList(all_of.name(), null); " MexOc3/kPEEdfjH/GcJU0Mw6DgoY8QgfaNwXcFbBUvf3TwZ5Mysf21OLHH13g8gzREm+h8c=\"==ref.definition.issuer"); condList.add("\"1:25\"==this.state.branchId"); contract1.getReferences().get("ref_string").setConditions(conditions); conditions = contract1.getReferences().get("ref_time").getConditions(); condList = conditions.getList(all_of.name(), null); condList.add("\"2086-03-22 11:35:37\"!=now"); contract1.getReferences().get("ref_time").setConditions(conditions);
/** * Check serialization and deserialization contract with references * @throws Exception */ @Test public void checkReferenceSerialization() throws Exception { Contract contract = Contract.fromDslFile(rootPath + "simple_root_contract_with_references.yml"); assertEquals(contract.findReferenceByName("ref_cont").getComment(), "ref_comment"); contract.findReferenceByName("ref_cont3").setComment("ref_comment3"); contract.seal(); Binder b = BossBiMapper.serialize(contract); Contract desContract = DefaultBiMapper.deserialize(b); assertEquals(desContract.findReferenceByName("ref_cont").getComment(), "ref_comment"); assertEquals(desContract.findReferenceByName("ref_cont3").getComment(), "ref_comment3"); assertNull(desContract.findReferenceByName("ref_cont2").getComment()); for (Reference ref: contract.getReferences().values()) { Reference desRef = desContract.findReferenceByName(ref.getName()); assertTrue(desRef != null); assertEquals(ref.getConditions(), desRef.getConditions()); } }
public void addReferencedItems(Approvable... items) { Stream.of(items).forEach(i -> { Reference refModel = new Reference(); refModel.name = i.getId().toBase64String(); refModel.type = Reference.TYPE_EXISTING_DEFINITION; refModel.addMatchingItem(i); references.put(refModel.name, refModel); }); }
ArrayList conditions = ref.getConditions().getArray(Reference.conditionsModeType.all_of.name()); conditions.forEach(condition -> { HashId o = null; } else { Reference reference = getReferences().values().stream().filter(ref -> ref.getConditions().getArray(Reference.conditionsModeType.all_of.name()).stream().anyMatch(cond -> isOriginCondition(cond, origin))).collect(Collectors.toList()).get(0); if(reference.matchingItems.isEmpty() && originContracts.containsKey(origin)) reference.addMatchingItem(originContracts.get(origin));
public void setConditions(ScriptObjectMirror conditions) { Object paramsMap = JSApiHelpers.jo2Object(conditions); if (paramsMap instanceof HashMap) { Binder paramsBinder = new Binder(); ((HashMap)paramsMap).forEach((k, v) -> paramsBinder.set(k.toString(), v)); this.reference.setConditions(paramsBinder); } }
Reference desRef = checkingContract.findReferenceByName(ref.getName()); assertTrue(desRef != null); assertEquals(ref.getConditions(), desRef.getConditions());
public JSApiReference createReference(String type) { Reference reference = new Reference(); switch (type) { case "TRANSACTIONAL": reference.type = Reference.TYPE_TRANSACTIONAL; break; case "EXISTING_DEFINITION": reference.type = Reference.TYPE_EXISTING_DEFINITION; break; case "EXISTING_STATE": reference.type = Reference.TYPE_EXISTING_STATE; break; default: throw new IllegalArgumentException("JSApiReferenceBuilder.createReference error: wrong reference type '"+type+"'"); } return new JSApiReference(reference); }
private boolean isOriginReferenceExists(HashId origin) { return getReferences().values().stream().anyMatch(ref -> ref.getConditions().getArray(Reference.conditionsModeType.all_of.name()).stream().anyMatch(cond -> isOriginCondition(cond, origin))); }
LinkedHashMap<String, Binder> insideHashMap = (LinkedHashMap<String, Binder>) item; Binder insideBinder = new Binder(insideHashMap); parsed = assemblyConditions(insideBinder); } else if (((Binder) item).containsKey("operator")) cond = assemblyCondition((Binder) item); else parsed = assemblyConditions((Binder) item);
/** * Export the conditions from the reference as strings * @return strings conditions reference */ public Binder exportConditions() { return assemblyConditions(conditions); }
@Test public void revokeDifferentNameValidRefRoleNotRegistered() throws Exception { Contract referencedContract = new Contract(key1); referencedContract.seal(); Contract revokingContract = new Contract(key2); Reference revokeReference = new Reference(revokingContract); revokeReference.setName("ref1"); ArrayList<String> revokeCondtitions = new ArrayList<>(); revokeCondtitions.add("ref.id=="+referencedContract.getId().toBase64String()); revokeReference.setConditions(Binder.of("all_of",revokeCondtitions)); revokingContract.addReference(revokeReference); SimpleRole role = new SimpleRole("@revoke"); role.addRequiredReference(revokeReference.getName(), Role.RequiredMode.ALL_OF); RevokePermission permission = new RevokePermission(role); revokingContract.addPermission(permission); revokingContract.seal(); Contract transactionRoot = new Contract(key3); transactionRoot.addRevokingItems(revokingContract); Reference rootReference = new Reference(transactionRoot); rootReference.setName("ref2"); ArrayList<String> rootConditions = new ArrayList<>(); rootConditions.add("ref.id=="+referencedContract.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.seal(); transactionRoot.getTransactionPack().addReferencedItem(referencedContract); transactionRoot = Contract.fromPackedTransaction(transactionRoot.getPackedTransaction()); transactionRoot.check(); assertTrue(transactionRoot.isOk()); }