/** * Method add found contracts in the new items and revoking items to {@link TransactionPack#subItems} and do it * again for each new item. * Also method add to {@link TransactionPack#referencedItems} referenced contracts from given. * @param c - given contract to extract from. */ protected synchronized void extractAllSubItemsAndReferenced(Contract c) { for (Contract r : c.getRevoking()) { putSubItem(r); for (Contract ref : r.getReferenced()) { addReferencedItem(ref); } } for (Contract n : c.getNew()) { putSubItem(n); extractAllSubItemsAndReferenced(n); } for (Contract ref : c.getReferenced()) { addReferencedItem(ref); } }
/** * Main .unicon read routine. Load any .unicon version and construct a linked Contract with counterparts (new and * revoking items if present) and corresponding {@link TransactionPack} instance to pack it to store or send to * approval. * <p> * The supported file variants are: * <p> * - v2 legacy unicon. Is loaded with packed counterparts if any. Only for compatibility, avoid using it. * <p> * - v3 compacted unicon. Is loaded without counterparts, should be added later if need with {@link * #addNewItems(Contract...)} and {@link #addRevokingItems(Contract...)}. This is a good way to keep the long * contract chain. * <p> * - packed {@link TransactionPack}. This is a preferred way to keep current contract state. * <p> * To pack and write corresponding .unicon file use {@link #getPackedTransaction()}. * * @param packedItem some packed from of the universa contract * @return unpacked {@link Contract} * @throws IOException if the packedItem is broken */ public static Contract fromPackedTransaction(@NonNull byte[] packedItem) throws IOException { TransactionPack tp = TransactionPack.unpack(packedItem); return tp.getContract(); }
@Test public void packAndUnpackWithKeys() throws Exception { TransactionPack tp = new TransactionPack(); tp.setContract(c); // tp.addKeys(publicKey); checkPackWithKeys(tp); assertSame(tp,c.getTransactionPack()); byte[] packedTp = tp.pack(); TransactionPack tp1 = TransactionPack.unpack(packedTp); checkPackWithKeys(tp1); }
/** * Add contract that already includes all its subItems, referenced items and keys. It will be added as a contract * per transaction, while its subItems will be added to subItems if not already included and refrenced items and keys too. * <p> * This is extremely important that the contract is properly sealed as well as its possibly new items, revoking * items and referenced items have binary image attached. <b>Do not ever seal the approved contract</b>: * it will break it's id and cancel the approval blockchain, so the new state will not be approved. * If it was done by mistake, reload the packed contract to continue. * * @param c is a contract to append to the list of transactions. */ public void setContract(Contract c) { if (contract != null) throw new IllegalArgumentException("the contract is already added"); contract = c; packedBinary = null; extractAllSubItemsAndReferenced(c); c.setTransactionPack(this); for (PrivateKey key : c.getKeysToSignWith()) addKeys(key.getPublicKey()); }
tp_before.addReferencedItem(jobCertificate); byte[] data = tp_before.pack(); TransactionPack tp_after = TransactionPack.unpack(data); tp_before.addReferencedItem(jobCertificate); data = tp_before.pack(); tp_after = TransactionPack.unpack(data);
tp_before.addReferencedItem(jobCertificate); tp_after = TransactionPack.unpack(tp_before.pack()); tp_before.getReferencedItems().clear(); tp_before.addReferencedItem(jobCertificate); tp_after = TransactionPack.unpack(tp_before.pack()); tp_before.getReferencedItems().clear(); tp_before.addReferencedItem(jobCertificate); tp_after = TransactionPack.unpack(tp_before.pack()); tp_before.getReferencedItems().clear(); tp_before.addReferencedItem(jobCertificate); tp_after = TransactionPack.unpack(tp_before.pack()); tp_before.getReferencedItems().clear(); tp_after = TransactionPack.unpack(tp_before.pack()); tp_before.getReferencedItems().clear(); tp_before.addReferencedItem(jobCertificate); tp_after = TransactionPack.unpack(tp_before.pack()); tp_before.getReferencedItems().clear(); tp_before.addReferencedItem(jobCertificate);
/** * Imitate of sending contract from one part of swappers to another. * * Method packs sending contracts with main swap contract (can be blank - doesn't matter) into TransactionPack. * Then restore from packed binary main swap contract, contracts sending with. * * @param mainContract * @return * @throws Exception */ public synchronized Contract imitateSendingTransactionToPartner(Contract mainContract) throws Exception { TransactionPack tp_before = mainContract.getTransactionPack(); byte[] data = tp_before.pack(); // here we "send" data and "got" it TransactionPack tp_after = TransactionPack.unpack(data); Contract gotMainContract = tp_after.getContract(); return gotMainContract; }
contract3.seal(); TransactionPack tp = new TransactionPack(); tp.setContract(contract1); tp.addSubItem(contract1); tp.addReferencedItem(contract1); tp.addSubItem(contract2); tp.addReferencedItem(contract2); tp.addSubItem(contract3); tp.addReferencedItem(contract3);
referencedItems.forEach(c -> tp.addReferencedItem(c));
public Contract getPayloadContract() { if (payload != null) return payload.getContract(); return null; }
assertEquals(ItemState.REVOKED, node.waitItem(payingParcel.getPayment().getContract().getId(), 8000).state); assertEquals(ItemState.APPROVED, node.waitItem(payingParcel.getPayload().getContract().getId(), 8000).state); assertEquals(ItemState.APPROVED, node.waitItem(uns.getNew().get(0).getId(), 8000).state); tp_before.addReferencedItem(referencesContract); TransactionPack tp_after = TransactionPack.unpack(tp_before.pack()); refilledUnsContract = (UnsContract) tp_after.getContract(); refilledUnsContract.setNodeInfoProvider(nodeInfoProvider); refilledUnsContract.addSignerKey(manufacturePrivateKeys.iterator().next()); assertEquals(ItemState.REVOKED, node.waitItem(payingParcel.getPayment().getContract().getId(), 8000).state); assertEquals(ItemState.APPROVED, node.waitItem(payingParcel.getPayload().getContract().getId(), 8000).state); assertEquals(ItemState.APPROVED, node.waitItem(refilledUnsContract.getNew().get(0).getId(), 8000).state);
public void checkSimplePack(TransactionPack tp) { assertEquals(3, tp.getSubItems().size()); assertEquals(c.getId(), tp.getContract().getId()); Set<HashId> rids = c.getRevokingItems().stream().map(x->x.getId()).collect(Collectors.toSet()); Set<HashId> nids = c.getNewItems().stream().map(x->x.getId()).collect(Collectors.toSet()); assertTrue(rids.contains(r0.getId())); assertTrue(nids.contains(n0.getId())); assertTrue(nids.contains(n1.getId())); }
private static void checkFile(File f) { try { TransactionPack tp = TransactionPack.unpack(Do.read(f), true); if (tp.isReconstructed()) { report("file " + f + " is a single contract"); } else { report("file " + f + " is a transaction pack"); } System.out.println(); checkContract(tp.getContract()); } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", f.getPath(), e.toString()); } catch (IOException e) { addError("READ_ERROR", f.getPath(), e.toString()); } catch (Exception e) { addError("UNKNOWN_ERROR", f.getPath(), e.toString()); } }
@Test public void serializeNew() throws Exception { TransactionPack tp = new TransactionPack(); tp.setContract(c); checkSimplePack(tp); }
/** * Unpack either old contract binary (all included), or newer transaction pack. Could be used to load old contracts * to perform a transaction. * * @param packOrContractBytes binary that was packed by {@link TransactionPack#pack()} * * @return transaction, either unpacked or reconstructed from the self-contained v2 contract * @throws IOException if something went wrong */ public static TransactionPack unpack(byte[] packOrContractBytes) throws IOException { return unpack(packOrContractBytes, true); }
/** * Get transaction pack of the contract * @return transaction pack of the contract */ public synchronized TransactionPack getTransactionPack() { if (transactionPack == null) transactionPack = new TransactionPack(this); return transactionPack; }
contractsTree = new HashMap(getTransactionPack().getSubItems()); contractsTree.putAll(getTransactionPack().getReferencedItems()); contractsTree.put(getId(),this); setEffectiveKeys(null);
contractImported.setKeysToSignWith(signKeys); byte[] sealedContract = contractImported.sealAsV2(); TransactionPack tp = new TransactionPack(); tp.addKeys(creatorPrivateKey.getPublicKey()); tp.addKeys(TestKeys.privateKey(0).getPublicKey()); Contract restoredContract = new Contract(sealedContract, tp);
Contract c = new Contract(((Bytes) packed).toArray(), pack); revokingItems.add(c); pack.addSubItem(c); Contract c = new Contract(((Bytes) packed).toArray(), pack); newItems.add(c); pack.addSubItem(c); Contract r = pack.getSubItem(hid); if (r != null) { revokingItems.add(r); Contract n = pack.getSubItem(hid); if (n != null) { newItems.add(n);