default StateRecord getLockOwnerOf(HashId itemId) { return getLockOwnerOf(getRecord(itemId)); }
public ResyncingItem(HashId hid, StateRecord record) { resyncingState = ResyncingItemProcessingState.WAIT_FOR_VOTES; this.hashId = hid; this.record = record; StateRecord recordWas = ledger.getRecord(hid); if (recordWas != null) { stateWas = recordWas.getState(); } else { stateWas = ItemState.UNDEFINED; } resyncNodes.put(ItemState.APPROVED, new HashSet<>()); resyncNodes.put(ItemState.REVOKED, new HashSet<>()); resyncNodes.put(ItemState.DECLINED, new HashSet<>()); resyncNodes.put(ItemState.UNDEFINED, new HashSet<>()); }
/** * Shortcut method: check that record exists and its state returns {@link ItemState#isApproved()}}. Check it to * ensure its meaning. * * @param id is {@link HashId} for checking item * @return true if it is. */ default boolean isApproved(HashId id) { StateRecord r = getRecord(id); return r != null && r.getState().isApproved(); }
/** * Shortcut method: check that record exists and its state returns {@link ItemState#isConsensusFound()}}. Check it to * ensure its meaning. * * @param id is {@link HashId} for checking item * @return true if it is. */ default boolean isConsensusFound(HashId id) { StateRecord r = getRecord(id); return r != null && r.getState().isConsensusFound(); }
/** * Create a record, locked for approval, e.g. item that will be creared (and approved) by current item if it will * get the positive consensus. Throws {@link IllegalStateException} if this record is not in a proper state to * perform this operation, e.g. not is in {@link ItemState#PENDING} - what means, the local check is not yet * finished. * <p> * Note that items that are already locally checked ({@link ItemState#PENDING_NEGATIVE} or {@link * ItemState#PENDING_POSITIVE} can not create any output locks. * * @param id id of the new item to be locked for approval * @return the record of the new item locked for creatoin pn success, null it such item already exists and not * locked for apporoval by us. */ public StateRecord createOutputLockRecord(HashId id) { checkLedgerExists(); checkHaveRecordId(); if (state != ItemState.PENDING) throw new IllegalStateException("wrong state to createOutputLockRecord: " + state); StateRecord newRecord = ledger.getRecord(id); if (newRecord != null) { // if it is not locked for approval - failure if (newRecord.state != ItemState.LOCKED_FOR_CREATION) return null; // it it is locked by us, ok return newRecord.lockedByRecordId == recordId ? newRecord : null; } newRecord = ledger.createOutputLockRecord(recordId, id); return newRecord; }
public void startResync() { report(getLabel(), ()->"ResyncProcessor.startResync(itemId="+itemId+")", DatagramAdapter.VerboseLevel.BASE); resyncExpiresAt = Instant.now().plus(config.getMaxResyncTime()); executorService.schedule(()->resyncEnded(), config.getMaxResyncTime().getSeconds(), TimeUnit.SECONDS); resyncingItem = new ResyncingItem(itemId, ledger.getRecord(itemId)); resyncingItem.finishEvent.addConsumer((ri)->onFinishResync(ri)); List<Integer> periodsMillis = config.getResyncTime(); obtainedAnswersFromNodes.clear(); voteItself(); resyncer = new RunnableWithDynamicPeriod(() -> pulseResync(), periodsMillis, executorService); resyncer.run(); }
protected synchronized void destroyCurrentFromAllNodesIfExists(Contract finalC) { for (Node nodeS : nodesMap.values()) { StateRecord r = nodeS.getLedger().getRecord(finalC.getId()); if (r != null) { r.destroy(); } } }
protected synchronized void destroyFromAllNodesExistingNew(Contract c50_1) { StateRecord orCreate; for (Approvable c : c50_1.getNewItems()) { for (Node nodeS : nodesMap.values()) { orCreate = nodeS.getLedger().getRecord(c.getId()); if (orCreate != null) orCreate.destroy(); } } }
addToAllLedgers(c, ItemState.APPROVED); node.getLedger().getRecord(c.getId()).destroy(); assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state);
addToAllLedgers(c, ItemState.DECLINED); node.getLedger().getRecord(c.getId()).destroy(); assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state);
addToAllLedgers(c, ItemState.DECLINED); node.getLedger().getRecord(c.getId()).destroy(); assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state);
addToAllLedgers(c, ItemState.PENDING_POSITIVE); node.getLedger().getRecord(c.getId()).destroy(); assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state); node.resync(c.getId());
addToAllLedgers(c, ItemState.PENDING_POSITIVE); node.getLedger().getRecord(c.getId()).destroy(); assertEquals(ItemState.UNDEFINED, node.checkItem(c.getId()).state); node.resync(c.getId());
node.getLedger().getRecord(contract.getId()).destroy(); assertEquals(ItemState.UNDEFINED, node.checkItem(contract.getId()).state);
private Binder getBody(Binder params, Session session) throws IOException { checkNode(session, true); Binder res = new Binder(); if (!node.getConfig().isPermanetMode()) return res; HashId itemId = (HashId) params.get("itemId"); byte[] body = node.getLedger().getKeepingItem(itemId); if (body != null) { res.put("packedContract", body); return res; } node.resync(itemId); ItemResult itemResult = node.checkItem(itemId); if (itemResult == ItemResult.UNDEFINED) return res; Approvable item = node.getKeepingItemFromNetwork(itemId); if (item == null) return res; if ((item instanceof Contract) && (item.getId().equals(itemId)) && (HashId.of(((Contract) item).getLastSealedBinary()).equals(itemId))) { StateRecord record = node.getLedger().getRecord(itemId); node.getLedger().putKeepingItem(record, item); body = ((Contract) item).getPackedTransaction(); res.put("packedContract", body); } return res; }
@Test(timeout = 15000) public void resyncWithTimeout() throws Exception { Contract c = new Contract(TestKeys.privateKey(0)); c.seal(); addToAllLedgers(c, ItemState.APPROVED); Duration wasDuration = config.getMaxResyncTime(); config.setMaxResyncTime(Duration.ofMillis(2000)); for (int i = 0; i < NODES/2; i++) { ((TestEmulatedNetwork)network).switchOffNodeTestMode(nodes.get(NODES-i-1)); } 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(), 15000).state); config.setMaxResyncTime(wasDuration); ((TestEmulatedNetwork)network).switchOnAllNodesTestMode(); }
@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); }