Contract createComplexConctract(PrivateKey key,int subcontracts) { Contract root = new Contract(key); for(int i = 0; i < subcontracts; i++) { Contract c = new Contract(key); c.getKeysToSignWith().clear(); root.addNewItems(c); } root.seal(); return root; }
/** * Create a batch contract, which registers all the included contracts, possibily referencing each other, * in the single transaction, saving time and reducing U cost. Note that if any of the batched contracts * fails, the whole batch is rejected. * * @param contracts to register all in one batch. Shuld be prepared and sealed. * @param keys to sign batch with. * @return batch contract that includes all contracts as new items. */ public static Contract createBatch(Collection<PrivateKey> keys, Contract... contracts) { Contract batch = new Contract(); batch.setIssuerKeys(keys); batch.registerRole(new RoleLink("creator","issuer")); batch.registerRole(new RoleLink("owner","issuer")); batch.setExpiresAt(ZonedDateTime.now().plusDays(3)); for(Contract c : contracts) { batch.addNewItems(c); } batch.addSignerKeys(keys); batch.seal(); return batch; }
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; } }
static Contract createComplexConctract(PrivateKey key, int subcontracts, int n) { Contract root = createSimpleContract(key); for(int i = 0; i < subcontracts; i++) { Contract c = createSimpleContract(key); c.setExpiresAt(c.getExpiresAt().plusSeconds(i+1+(subcontracts+1)*n)); c.seal(); root.addNewItems(c); } root.setExpiresAt(root.getExpiresAt().plusSeconds((subcontracts+1)*n)); root.addSignerKey(key); root.seal(); return root; }
@Test public void checkTransactionPack() throws Exception { Contract r = new Contract(ownerKey1); r.seal(); Contract c = r.createRevision(ownerKey1); Contract n = c.split(1)[0]; n.seal(); c.seal(); c.addNewItems(n); String path = rootPath + "/testtranspack.unicon"; // path = "/Users/sergeych/dev/!/e7810197-d148-4936-866b-44daae182e83.transaction"; c.seal(); Files.deleteIfExists(Paths.get(path)); CLIMain.saveContract(c, path, true, true); // try (FileOutputStream fs = new FileOutputStream(path)) { // fs.write(c.getPackedTransaction()); // fs.close(); // } callMain("--check", path, "-v"); System.out.println(output); }
@Test public void checkWhiteListKey() throws Exception { List<Main> mm = new ArrayList<>(); for (int i = 0; i < 3; i++) { mm.add(createMain("node" + (i + 1), false)); } Main main = mm.get(0); PrivateKey myKey = TestKeys.privateKey(3); Client client = null; try { client = new Client(myKey, main.myInfo, null); } catch (Exception e) { System.out.println("prepareClient exception: " + e.toString()); } Contract testContract = new Contract(myKey); for (int i = 0; i < 10; i++) { Contract nc = new Contract(myKey); nc.seal(); testContract.addNewItems(nc); } testContract.seal(); assertTrue(testContract.isOk()); Parcel parcel = createParcelWithFreshU(client, testContract,Do.listOf(myKey)); ItemResult itemResult = client.registerParcelWithState(parcel.pack(), 15000); System.out.println(">> state: " + itemResult); assertEquals (ItemState.APPROVED, itemResult.state); mm.forEach(x -> x.shutdown()); }
@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()); }
@Ignore @Test public void testNewContractSeal() throws Exception { testSomeWork(() -> { for (int i = 0; i < 10; ++i) { PrivateKey myKey = null; try { myKey = TestKeys.privateKey(3); } catch (Exception e) { } Contract testContract = new Contract(myKey); for (int iContract = 0; iContract < 10; ++iContract) { Contract nc = new Contract(myKey); nc.seal(); testContract.addNewItems(nc); } testContract.seal(); } }); }
@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 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 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 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 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); }
@Ignore @Test public void testContractCheck() throws Exception { PrivateKey key = TestKeys.privateKey(3); testSomeWork(() -> { try { Contract c = new Contract(key); for (int k = 0; k < 500; k++) { Contract nc = new Contract(key); nc.seal(); c.addNewItems(nc); } c.seal(); c.check(); } catch (Quantiser.QuantiserException e) { e.printStackTrace(); } }); }
@Test public void checkTestnetNewItemExpirationDateCriteria() throws Exception { PrivateKey key = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")); Contract newItem = Contract.fromDslFile(rootPath + "LamborghiniTestDrive.yml"); newItem.addSignerKey(key); sealCheckTrace(newItem, true); newItem.setExpiresAt(ZonedDateTime.now().plusMonths(13)); Contract contract = Contract.fromDslFile(rootPath + "LamborghiniTestDrive.yml"); contract.addSignerKey(key); contract.setExpiresAt(ZonedDateTime.now().plusMonths(1)); contract.addNewItems(newItem); sealCheckTrace(contract, true); assertFalse(contract.isSuitableForTestnet()); // now set contract limited for testnet contract.setLimitedForTestnet(true); sealCheckTrace(contract, false); assertFalse(contract.isSuitableForTestnet()); }
@Test public void checkTestnetCostUCriteria() throws Exception { PrivateKey key = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")); Contract contract = Contract.fromDslFile(rootPath + "LamborghiniTestDrive.yml"); contract.setExpiresAt(ZonedDateTime.now().plusMonths(1)); contract.addSignerKey(key); for (int i = 0; i < 100; i++) { Contract newItem = Contract.fromDslFile(rootPath + "LamborghiniTestDrive.yml"); newItem.setExpiresAt(ZonedDateTime.now().plusMonths(1)); newItem.addSignerKey(key); sealCheckTrace(newItem, true); contract.addNewItems(newItem); } sealCheckTrace(contract, true); System.out.println("Processing cost is " + contract.getProcessedCostU()); assertTrue(contract.getProcessedCostU() > Config.maxCostUInTestMode); assertFalse(contract.isSuitableForTestnet()); // now set contract limited for testnet contract.setLimitedForTestnet(true); sealCheckTrace(contract, false); assertFalse(contract.isSuitableForTestnet()); }
@Test public void references() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); String js = ""; js += "print('references');"; js += "var ref = jsApi.getReferenceBuilder().createReference('EXISTING_STATE');"; js += "ref.setConditions({'all_of':['ref.issuer=="+TestKeys.publicKey(1).getShortAddress().toString()+"']});"; js += "jsApi.getCurrentContract().addReference(ref);"; contract.getState().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters()); contract.seal(); contract = Contract.fromPackedTransaction(contract.getPackedTransaction()); Contract batchContract = new Contract(TestKeys.privateKey(3)); batchContract.addNewItems(contract); batchContract.seal(); assertTrue(batchContract.check()); contract.execJS(new JSApiExecOptions(), js.getBytes()); contract.seal(); batchContract.seal(); assertFalse(batchContract.check()); }
transactionRoot.addNewItems(newContract); Reference rootReference = new Reference(transactionRoot); rootReference.setName("ref2");
smartContract.addNewItems(paymentDecreased); smartContract.seal(); smartContract.check();