case 0: ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of(Reference.conditionsModeType.all_of.name(),asList("ref.issuer=="+keys100.get(random.nextInt(keys100.size())).getPublicKey().getShortAddress()))); break; case 1: ref.type = Reference.TYPE_EXISTING_DEFINITION; ref.setConditions(Binder.of(Reference.conditionsModeType.all_of.name(),asList("ref.owner=="+keys100.get(random.nextInt(keys100.size())).getPublicKey().getLongAddress()))); break; case 2: ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList( "ref.state.data.some_value=="+contracts100.get(random.nextInt(contracts100.size())).getStateData().getStringOrThrow("some_value") ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.some_value<=1000"))); break;
conditionsList.add(REFERENCE_CONDITION_PREFIX+origin.toBase64String()); conditionsList.add(REFERENCE_CONDITION2); Binder conditions = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList); ref.setConditions(conditions); conditionsList2.add(REFERENCE_CONDITION_PREFIX+origin2.toBase64String()); conditionsList2.add(REFERENCE_CONDITION2); Binder conditions2 = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList2); ref2.setConditions(conditions2); conditionsList3.add(REFERENCE_CONDITION_PREFIX+origin3.toBase64String()); conditionsList3.add(REFERENCE_CONDITION2); Binder conditions3 = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList3); ref3.setConditions(conditions3); conditionsList4.add(REFERENCE_CONDITION_PREFIX+origin4.toBase64String()); conditionsList4.add(REFERENCE_CONDITION2); Binder conditions4 = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList4); ref4.setConditions(conditions4); conditionsList5.add(REFERENCE_CONDITION_PREFIX+origin5.toBase64String()); conditionsList5.add(REFERENCE_CONDITION2); Binder conditions5 = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList5); ref5.setConditions(conditions5);
conditionsList.add(REFERENCE_CONDITION_PREFIX+origin.toBase64String()); conditionsList.add(REFERENCE_CONDITION2); Binder conditions = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList); ref.setConditions(conditions); conditionsList2.add(REFERENCE_CONDITION_PREFIX+origin2.toBase64String()); conditionsList2.add(REFERENCE_CONDITION2); Binder conditions2 = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList2); ref2.setConditions(conditions2);
return true; if (conditions.containsKey(all_of.name())) List<Object> condList = conditions.getList(all_of.name(), null); if (condList == null) throw new IllegalArgumentException("Expected all_of conditions"); else if (conditions.containsKey(any_of.name())) List<Object> condList = conditions.getList(any_of.name(), null); if (condList == null) throw new IllegalArgumentException("Expected any_of conditions");
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))
conditionsList.add(REFERENCE_CONDITION_PREFIX+contract4.getId().toBase64String()); conditionsList.add(REFERENCE_CONDITION2); Binder conditions = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList); reference.setConditions(conditions);
private boolean isInherited(Binder conditions, Reference ref, Contract refContract, Collection<Contract> contracts, int iteration) { if ((conditions == null) || (conditions.size() == 0)) return false; List<Object> condList = null; if (conditions.containsKey(all_of.name())) { condList = conditions.getList(all_of.name(), null); if (condList == null) throw new IllegalArgumentException("Expected all_of conditions"); } else if (conditions.containsKey(any_of.name())) { condList = conditions.getList(any_of.name(), null); if (condList == null) throw new IllegalArgumentException("Expected any_of conditions"); } else if (conditions.containsKey("operator")) return isInheritedParsed(conditions, ref, refContract, contracts, iteration); else throw new IllegalArgumentException("Expected all_of or any_of"); if (condList != null) for (Object item: condList) if (item.getClass().getName().endsWith("String")) { if (isInherited((String) item, ref, refContract, contracts, iteration)) return true; } else if (isInherited((Binder) item, ref, refContract, contracts, iteration)) return true; return false; }
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; }
@Test public void refMissingFieldConstantForEquals() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("another_val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val==false", "ref.state.data.ival==0", "false==ref.state.data.val", "0==ref.state.data.ival") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void refMissingFieldDateTimeForEquals() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("another_val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val!=ref.definition.created_at", "this.definition.created_at!=ref.state.data.val") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void refMissingFieldHashIdForEquals() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("another_val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val!=ref.id", "this.id!=ref.state.data.val") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void refMissingField() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("another_val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val>-100") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void refLessOrEquals() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val<10") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void refMissingFieldRoleForEquals() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("another_val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val!=ref.issuer", "this.issuer!=ref.state.data.val") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
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); }
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))); }