/** * Create contract importing its parameters with passed .yaml file. No signatures are added automatically. It is required to add signatures before check. * @param fileName path to file containing Yaml representation of contract. */ public static Contract 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 Contract().initializeWithDsl(binder); } }
/** * Construct contract from sealed binary stored in given file name * @param contractFileName file name to get binary from * @return extracted contract * @throws IOException */ public static Contract fromSealedFile(String contractFileName) throws IOException { return new Contract(Do.read(contractFileName), new TransactionPack()); }
static Contract createSimpleContract(PrivateKey key) { Contract result = new Contract(); // default expiration date result.setExpiresAt(ZonedDateTime.now().plusDays(90)); // issuer role is a key for a new contract result.setIssuerKeys(key.getPublicKey().getShortAddress()); // issuer is owner, link roles result.registerRole(new RoleLink("owner", "issuer")); result.registerRole(new RoleLink("creator", "issuer")); return result; }
@Test public void testFiles() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); contract.seal(); String newFilename = FileTool.writeFileContentsWithRenaming(System.getProperty("java.io.tmpdir")+"/testFile_2.file", contract.getPackedTransaction()); System.out.println("write done, new filename=" + newFilename); }
public TestContracts invoke() throws EncryptionError, Quantiser.QuantiserException { r0 = new Contract(TestKeys.privateKey(0)); r0.seal(); c = r0.createRevision(TestKeys.privateKey(0)); n0 = new Contract(TestKeys.privateKey(0)); n1 = new Contract(TestKeys.privateKey(0)); c.addNewItems(n0); c.addNewItems(n1); c.seal(); return this; } }
@Test public void packedContractNotContainsOtherItems() throws Exception { // if we seal and load a contract without a pack, it should have empty // containers for new and revoking items - these should be fill separately. Contract c2 = new Contract(c.seal()); assertEquals(0, c2.getRevokingItems().size()); assertEquals(0, c2.getNewItems().size()); }
@Test public void registerSimpleContractWhite() throws Exception { Contract whiteContract = new Contract(TestKeys.privateKey(0)); whiteContract.seal(); System.out.println("whiteClient.register(whiteContract)..."); ItemResult itemResult = whiteClient.register(whiteContract.getPackedTransaction(), 5000); System.out.println("whiteClient.register(whiteContract)... done! itemResult: " + itemResult.state); itemResult = whiteClient.getState(whiteContract.getId()); System.out.println("whiteClient.getState(whiteContract): " + itemResult.state); assertEquals(ItemState.APPROVED, itemResult.state); }
@Test public void registerSimpleContractNormal() throws Exception { Contract simpleContract = new Contract(TestKeys.privateKey(0)); simpleContract.seal(); registerAndCheckApproved(simpleContract); }
@Test public void jsInContract_execZeroParams() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); String js = ""; js += "print('jsApiParams.length: ' + jsApiParams.length);"; js += "result = jsApiParams.length;"; contract.getDefinition().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters()); contract.seal(); assertEquals(0, contract.execJS(js.getBytes())); }
@Test public void shouldPerformRoleWithAnyMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); c.registerRole(s1); c.registerRole(s2); ListRole roleList = new ListRole("listAnyMode", ListRole.Mode.ANY, Do.listOf(s1, s2)); assertTrue(roleList.isAllowedForKeys(new HashSet<>(Do.listOf(keys.get(1).getPublicKey())))); }
@Test public void createFromSealedWithRealContract() throws Exception { String fileName = "./src/test_contracts/subscription.yml"; Contract c = Contract.fromDslFile(fileName); c.addSignerKeyFromFile(PRIVATE_KEY_PATH); sealCheckTrace(c, true); // Contract from seal byte[] seal = c.seal(); Contract sealedContract = new Contract(seal); sealedContract.addSignerKeyFromFile(PRIVATE_KEY_PATH); sealCheckTrace(sealedContract, true); }
@Test public void createFromSealedWithRealContractData() throws Exception { String fileName = "./src/test_contracts/subscription_with_data.yml"; Contract c = Contract.fromDslFile(fileName); c.addSignerKeyFromFile(PRIVATE_KEY_PATH); sealCheckTrace(c, true); // Contract from seal byte[] seal = c.seal(); Contract sealedContract = new Contract(seal); sealedContract.addSignerKeyFromFile(PRIVATE_KEY_PATH); sealCheckTrace(sealedContract, true); }
@Test public void detectsCirculars() throws Exception { Contract c = new Contract(); RoleLink r1 = new RoleLink("bar", "foo"); RoleLink r2 = new RoleLink("foo", "bar"); c.registerRole(r1); c.registerRole(r2); assertNull(r2.resolve()); }
@Test public void transactionalData() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); String testValue = HashId.createRandom().toBase64String(); contract.getTransactionalData().set("test_value", testValue); contract.seal(); byte[] packedData = contract.getPackedTransaction(); Contract unpackedContract = Contract.fromPackedTransaction(packedData); System.out.println("unpackedContract.transactional.data.test_value: " + unpackedContract.getTransactionalData().getStringOrThrow("test_value")); assertEquals(testValue, unpackedContract.getTransactionalData().getStringOrThrow("test_value")); }
@Test public void resolve() throws Exception { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); c.registerRole(s1); RoleLink r1 = new RoleLink("lover", "owner"); c.registerRole(r1); RoleLink r2 = r1.linkAs("mucker"); assertSame(s1, s1.resolve()); assertSame(s1, r1.resolve()); assertSame(s1, r2.resolve()); }
@Test(timeout = 15000) public void resyncApproved() throws Exception { Contract c = new Contract(TestKeys.privateKey(0)); c.seal(); addToAllLedgers(c, ItemState.APPROVED); node.getLedger().getRecord(c.getId()).destroy(); assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state); node.resync(c.getId()); assertEquals(ItemState.APPROVED, node.waitItem(c.getId(), 15000).state); }
@Test(timeout = 15000) public void resyncRevoked() throws Exception { Contract c = new Contract(TestKeys.privateKey(0)); c.seal(); addToAllLedgers(c, ItemState.REVOKED); node.getLedger().getRecord(c.getId()).destroy(); assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state); node.resync(c.getId()); assertEquals(ItemState.REVOKED, node.waitItem(c.getId(), 13000).state); }
@Test(timeout = 15000) public void resyncDeclined() throws Exception { Contract c = new Contract(TestKeys.privateKey(0)); c.seal(); addToAllLedgers(c, ItemState.DECLINED); node.getLedger().getRecord(c.getId()).destroy(); assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state); node.resync(c.getId()); assertEquals(ItemState.DECLINED, node.waitItem(c.getId(), 12000).state); }
@Test(timeout = 15000) public void resyncOther() throws Exception { Contract c = new Contract(TestKeys.privateKey(0)); c.seal(); addToAllLedgers(c, ItemState.PENDING_POSITIVE); node.getLedger().getRecord(c.getId()).destroy(); assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state); node.resync(c.getId()); assertEquals(ItemState.PENDING, node.checkItem(c.getId()).state); assertEquals(ItemState.UNDEFINED, node.waitItem(c.getId(), 12000).state); }