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); } }
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; }
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); ArrayList<String> rootConditions = new ArrayList<>(); rootConditions.add("ref.id=="+referencedContract.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.seal();
goodReference.name = "good"; goodReference.type = Reference.TYPE_EXISTING_STATE; goodReference.setConditions(goodConditions); contract.addReference(goodReference); badReference.name = "bad"; badReference.type = Reference.TYPE_EXISTING_STATE; badReference.setConditions(badConditions); contract.addReference(badReference);
@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()); }
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"); ArrayList<String> rootConditions = new ArrayList<>(); rootConditions.add("ref.id=="+referencedContract.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.seal();
conditionsList.add(REFERENCE_CONDITION2); Binder conditions = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList); reference.setConditions(conditions);
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); 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);
ref.name = "ref_can_play"; ref.type = Reference.TYPE_EXISTING_DEFINITION; ref.setConditions(conditions); contract1.addReference(ref);
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"); ArrayList<String> rootConditions = new ArrayList<>(); rootConditions.add("ref.id=="+referencedContract2.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.seal();
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"); 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);
@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 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 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 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); }
@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 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); }
goodReference.name = "good"; goodReference.type = Reference.TYPE_EXISTING_STATE; goodReference.setConditions(goodConditions); contract.addReference(goodReference);