/** * Register a paying parcel. * * @param contract must be a sealed binary. */ public static Parcel prepareForRegisterPayingParcel(Contract contract, Contract u, int amount, int amountStorage, Set<PrivateKey> uKeys, boolean withTestPayment) throws IOException { List<ErrorRecord> errors = contract.getErrors(); if (errors.size() > 0) { report("contract has errors and can't be submitted for registration"); report("contract id: " + contract.getId().toBase64String()); addErrors(errors); } else { Set<PrivateKey> keys = new HashSet<>(keysMapContract().values()); if (keys != null && keys.size() > 0) contract.addSignerKeys(keys); Parcel parcel = ContractsService.createPayingParcel(contract.getTransactionPack(), u, amount, amountStorage, uKeys, withTestPayment); return parcel; } return null; }
/** * Register a specified contract. * * @param contract must be a sealed binary. */ public static Parcel prepareForRegisterContract(Contract contract, Contract u, int amount, Set<PrivateKey> uKeys, boolean withTestPayment) throws IOException { List<ErrorRecord> errors = contract.getErrors(); if (errors.size() > 0) { report("contract has errors and can't be submitted for registration"); report("contract id: " + contract.getId().toBase64String()); addErrors(errors); } else { Parcel parcel = ContractsService.createParcel(contract, u, amount, uKeys, withTestPayment); return parcel; } return null; }
/** * Save specified parcel to file. * * @param parcel - parcel to save. * @param fileName - name of file to save to. * */ public static boolean saveParcel(Parcel parcel, String fileName) throws IOException { if (fileName == null) { fileName = "Universa_" + DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss").format(parcel.getPayloadContract().getCreatedAt()) + ".uniparcel"; } byte[] data = parcel.pack(); String newFileName = FileTool.writeFileContentsWithRenaming(fileName, data); report("Parcel is saved to: " + newFileName); report("Parcel size: " + data.length); try { if (parcel.getPaymentContract().check() && parcel.getPayloadContract().check()) { report("Parcel has no errors"); } else { addErrors(parcel.getPaymentContract().getErrors()); addErrors(parcel.getPayloadContract().getErrors()); } } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", parcel.toString(), e.getMessage()); } return (newFileName!=null); }
report("Sealed contract has no errors"); } else addErrors(contract.getErrors()); } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", contract.toString(), e.getMessage());
@Test public void badRevoke() throws Exception { Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml"); c.addSignerKeyFromFile(rootPath+"_xer0yfe2nn1xthc.private.unikey"); c.seal(); PrivateKey issuer = TestKeys.privateKey(2); Contract tc = c.createRevocation(issuer); // c can't be revoked with this key! boolean result = tc.check(); assertFalse(result); assertEquals(1, tc.getErrors().size()); assertEquals(Errors.FORBIDDEN, tc.getErrors().get(0).getError()); }
/** * Check bytes is contract. And if bytes is, check contract for errors. Print errors if found. * * @param data - data to check. * * @return true if bytes is Contract and Contract is valid. */ private static Boolean checkBytesIsValidContract(byte[] data) { try { Contract contract = new Contract(data); if (!contract.isOk()) { reporter.message("The capsule is not sealed"); contract.getErrors().forEach(e -> reporter.error(e.getError().toString(), e.getObjectName(), e.getMessage())); } checkContract(contract); } catch (RuntimeException e) { addError(Errors.BAD_VALUE.name(), "byte[] data", e.getMessage()); return false; } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", "", e.toString()); } catch (IOException e) { addError(Errors.BAD_VALUE.name(), "byte[] data", e.getMessage()); return false; } return true; }
@Test public void checkCreatingRootContract() throws Exception { Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml"); c.seal(); boolean ok = c.check(); assertFalse(ok); List<ErrorRecord> errors = c.getErrors(); // It is just ok but not signed assertEquals(2, errors.size()); assertEquals(errors.get(0).getError(), Errors.NOT_SIGNED); c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey"); c.getErrors().clear(); c.seal(); ok = c.check(); if (errors.isEmpty()) { assertTrue(ok); assertTrue(c.isOk()); } else { for (ErrorRecord e : errors) { System.out.println(e); fail("errors in contract"); } } assertTrue(c.check()); Files.write(Paths.get(rootPath + "simple_root_contract.unc"), c.seal()); Yaml yaml = new Yaml(); Files.write(Paths.get(rootPath + "simple_root_contract.raw.yaml"), yaml.dump(DefaultBiMapper.serialize(c)).getBytes() ); }
/** * Register a specified contract. * * @param contract must be a sealed binary. * @param waitTime - wait time for responce. * @param fromPackedTransaction - register contract with Contract.getPackedTransaction() */ @Deprecated public static void registerContract(Contract contract, int waitTime, Boolean fromPackedTransaction) throws IOException { // checkContract(contract); List<ErrorRecord> errors = contract.getErrors(); if (errors.size() > 0) { report("contract has errors and can't be submitted for registration"); report("contract id: " + contract.getId().toBase64String()); addErrors(errors); } else { // contract.seal(); ItemResult r; if (fromPackedTransaction) { r = getClientNetwork().register(contract.getPackedTransaction(), waitTime); } else { r = getClientNetwork().register(contract.getLastSealedBinary(), waitTime); } report("submitted with result:"); report(r.toString()); } }
((Contract)revokingItem).getErrors().clear();
@Test public void dupesWrongTest() throws Exception { PrivateKey key = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey")); Set<PrivateKey> keys = new HashSet<>(); keys.add(key); Contract c_1 = Contract.fromDslFile(rootPath + "coin100.yml"); c_1.addSignerKey(key); c_1.seal(); assertTrue(c_1.check()); c_1.traceErrors(); Contract c_2_1 = ContractsService.createSplit(c_1, new BigDecimal("20"), "amount", keys); Contract c_2_2 = c_2_1.getNew().get(0); if(c_2_2 != null) { Contract c_2_3 = c_2_2.copy(); c_2_3.addSignerKey(key); c_2_3.seal(); c_2_1.addNewItems(c_2_3); } assertEquals(2, c_2_1.getNewItems().size()); c_2_1.check(); c_2_1.traceErrors(); assertFalse(c_2_1.isOk()); // should be BAD_VALUE duplicated revision id assertEquals(2, c_2_1.getErrors().size()); }
c1.check(); c1.traceErrors(); assertEquals(1, c1.getErrors().size()); for(ErrorRecord error : c1.getErrors()) { assertThat(error.getError(), anyOf(equalTo(Errors.FORBIDDEN), equalTo(Errors.FAILED_CHECK))); c2.check(); c2.traceErrors(); assertEquals(1, c2.getErrors().size()); for(ErrorRecord error : c1.getErrors()) { assertThat(error.getError(), anyOf(equalTo(Errors.FORBIDDEN), equalTo(Errors.FAILED_CHECK)));
c1.check(); c1.traceErrors(); assertEquals(1, c1.getErrors().size()); for(ErrorRecord error : c1.getErrors()) { assertThat(error.getError(), anyOf(equalTo(Errors.FORBIDDEN), equalTo(Errors.FAILED_CHECK))); c2.check(); c2.traceErrors(); assertEquals(1, c2.getErrors().size()); for(ErrorRecord error : c1.getErrors()) { assertThat(error.getError(), anyOf(equalTo(Errors.FORBIDDEN), equalTo(Errors.FAILED_CHECK)));
@Test public void checkSealingRootContract() throws Exception { Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml"); c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey"); byte[] sealed = c.seal(); assertTrue(c.check()); // Bytes.dump(sealed); // System.out.println(sealed.length); Contract c2 = new Contract(sealed); assertProperSimpleRootContract(c2); boolean ok = c2.check(); List<ErrorRecord> errors = c2.getErrors(); if (errors.isEmpty()) { assertTrue(ok); assertTrue(c.isOk()); } else { for (ErrorRecord e : errors) { System.out.println(e); fail("errors in contract"); } } assertTrue(c.check()); }
@Test public void issuerTest() throws Exception { KeyAddress keyAddress1 = TestKeys.privateKey(0).getPublicKey().getShortAddress(); KeyAddress keyAddress2 = TestKeys.privateKey(1).getPublicKey().getShortAddress(); Contract contract = new Contract(TestKeys.privateKey(2)); contract.setCreatorKeys(keyAddress1); contract.setIssuerKeys(keyAddress2); contract.addSignerKey(TestKeys.privateKey(0)); contract.seal(); assertFalse(contract.check()); contract.setIssuerKeys(keyAddress1); contract.getErrors().clear(); contract.seal(); assertTrue(contract.check()); }
if (ItemState.APPROVED != itemResult.state) fail("Wrong state on repetition " + i + ": " + itemResult + ", " + itemResult.errors + " \r\ncontract_errors: " + contract.getErrors());
@Test public void testJoinSum() throws Exception { Contract c = createCoin(); c.addSignerKeyFromFile(PRIVATE_KEY_PATH); Binder d = c.getStateData(); int a = 1000000; assertEquals(a, d.getIntOrThrow(FIELD_NAME)); c.seal(); c.check(); c.traceErrors(); assertTrue(c.check()); // bad split: no changes Contract c1 = c.createRevision(ownerKey2); sealCheckTrace(c1, false); // Good split Contract c2 = c1.splitValue(FIELD_NAME, new Decimal(500)); assertEquals(a - 500, c1.getStateData().getIntOrThrow(FIELD_NAME)); assertEquals(500, c2.getStateData().getIntOrThrow(FIELD_NAME)); c1.getErrors().clear(); sealCheckTrace(c1, true); Contract c3 = c1.createRevision(ownerKey2); c3.getRevokingItems().add(c2); c3.getStateData().set(FIELD_NAME, new Decimal(a)); sealCheckTrace(c3, true); }
assertEquals(500, c2.getStateData().getIntOrThrow(FIELD_NAME)); c1.getErrors().clear(); sealCheckTrace(c1, true);
changed.getErrors().clear(); changed.getStateData().set("value", "1"); changed.seal();