public NMutableEnvironment getMutable() { return new NMutableEnvironment(this); }
@Override public @NonNull NameRecord createNameRecord(@NonNull UnsName unsName, @NonNull ZonedDateTime expiresAt) { NNameRecord nr = new NNameRecord(unsName,expiresAt); nr.setEnvironmentId(this.getId()); nameRecordsToAdd.add(nr); return nr; }
public NMutableEnvironment(NImmutableEnvironment ime) { super(ime.contract, ime.kvStore, ime.subscriptionsSet, ime.storagesSet, ime.nameRecordsSet, ime.followerService, ime.ledger); setNameCache(ime.nameCache); setId(ime.getId()); immutable = ime; }
private void complete(Node node) { synchronized (node.getCallbackService()) { // full environment Binder fullEnvironment = node.getFullEnvironment(environmentId); FollowerContract follower = (FollowerContract) fullEnvironment.get("follower"); NMutableEnvironment environment = (NMutableEnvironment) fullEnvironment.get("environment"); // complete event follower.onContractSubscriptionEvent(new ContractSubscription.CompletedEvent() { @Override public MutableEnvironment getEnvironment() { return environment; } }); environment.save(); } }
@Override public Set<HashId> saveEnvironment(NImmutableEnvironment environment) { Set<HashId> conflicts = saveEnvironment_getConflicts(environment); if (conflicts.size() == 0) { NSmartContract nsc = environment.getContract(); removeEnvironment(nsc.getId()); long envId = saveEnvironmentToStorage(nsc.getExtendedType(), nsc.getId(), Boss.pack(environment.getMutable().getKVStore()), nsc.getPackedTransaction()); for (NameRecord nr : environment.nameRecords()) { NNameRecord nnr = (NNameRecord)nr; nnr.setEnvironmentId(envId); addNameRecord(nnr); } for (ContractSubscription css : environment.subscriptions()) saveSubscriptionInStorage(css.getHashId(), css.isChainSubscription(), css.expiresAt(), envId); for (ContractStorage cst : environment.storages()) saveContractInStorage(cst.getContract().getId(), cst.getPackedContract(), cst.expiresAt(), cst.getContract().getOrigin(), envId); FollowerService fs = environment.getFollowerService(); if (fs != null) saveFollowerEnvironment(envId, fs.expiresAt(), fs.mutedAt(), fs.getCallbacksSpent(), fs.getStartedCallbacks()); } return conflicts; }
private void fail(Node node) { synchronized (node.getCallbackService()) { // full environment Binder fullEnvironment = node.getFullEnvironment(environmentId); FollowerContract follower = (FollowerContract) fullEnvironment.get("follower"); NMutableEnvironment environment = (NMutableEnvironment) fullEnvironment.get("environment"); // fail event follower.onContractSubscriptionEvent(new ContractSubscription.FailedEvent() { @Override public MutableEnvironment getEnvironment() { return environment; } }); environment.save(); } }
private void spent(Node node) { synchronized (node.getCallbackService()) { // full environment Binder fullEnvironment = node.getFullEnvironment(environmentId); FollowerContract follower = (FollowerContract) fullEnvironment.get("follower"); NMutableEnvironment environment = (NMutableEnvironment) fullEnvironment.get("environment"); // fail event follower.onContractSubscriptionEvent(new ContractSubscription.SpentEvent() { @Override public MutableEnvironment getEnvironment() { return environment; } }); environment.save(); } }
/** * Runs callback processor for one callback. Adds callback record to ledger, runs callback processing thread and * checks and obtains deferred callback notifications. * * @param updatingItem is new revision of following contract * @param contract is follower contract * @param me is environment */ public void startCallbackProcessor(Contract updatingItem, ItemState state, NSmartContract contract, NMutableEnvironment me) { // initialize callback processor CallbackProcessor callback = new CallbackProcessor(updatingItem, state, contract, me.getId(), this); // add callback record to DB CallbackRecord.addCallbackRecordToLedger(callback.getId(), me.getId(), config, network.getNodesCount(), ledger); // run callback processor int startDelay = callback.getDelay(); int repeatDelay = (int) config.getFollowerCallbackDelay().toMillis() * (network.getNodesCount() + 2); callback.setExecutor(executorService.scheduleWithFixedDelay(() -> callback.call(), startDelay, repeatDelay, TimeUnit.MILLISECONDS)); synchronized (callbackProcessors) { callbackProcessors.put(callback.getId(), callback); node.report(DatagramAdapter.VerboseLevel.DETAILED, "notifyFollowerSubscribers: put callback ", callback.getId().toBase64String()); CallbackNotification deferredNotification = deferredCallbackNotifications.get(callback.getId()); if (deferredNotification != null) { // do deferred notification callback.obtainNotification(deferredNotification); deferredCallbackNotifications.remove(callback.getId()); node.report(DatagramAdapter.VerboseLevel.DETAILED, "notifyFollowerSubscribers: remove deferred notification for callback ", callback.getId().toBase64String()); } } }
private void complete() { synchronized (callbackService) { // full environment Binder fullEnvironment = node.getFullEnvironment(environmentId); NSmartContract follower = (NSmartContract) fullEnvironment.get("follower"); NMutableEnvironment environment = (NMutableEnvironment) fullEnvironment.get("environment"); callbackProcessors.remove(id); node.report(DatagramAdapter.VerboseLevel.DETAILED, "CallbackProcessor.complete: Removed callback ", id.toBase64String()); follower.onContractSubscriptionEvent(new ContractSubscription.CompletedEvent() { @Override public MutableEnvironment getEnvironment() { return environment; } }); environment.save(); } // save new callback state in DB record ledger.updateFollowerCallbackState(id, FollowerCallbackState.COMPLETED); node.report(DatagramAdapter.VerboseLevel.BASE, "Completed callback ", id.toBase64String()); stop(); }
public void save() { ledger.updateEnvironment(getId(),contract.getExtendedType(),contract.getId(), Boss.pack(kvStore),contract.getPackedTransaction()); long subId = ledger.saveSubscriptionInStorage(sub.getHashId(), sub.isChainSubscription(), sub.expiresAt(), getId()); sub.setId(subId); storage.expiresAt(), storage.getContract().getOrigin(), getId()); storage.setId(storageId);
private void fail() { synchronized (callbackService) { // full environment Binder fullEnvironment = node.getFullEnvironment(environmentId); NSmartContract follower = (NSmartContract) fullEnvironment.get("follower"); NMutableEnvironment environment = (NMutableEnvironment) fullEnvironment.get("environment"); callbackProcessors.remove(id); node.report(DatagramAdapter.VerboseLevel.DETAILED, "CallbackProcessor.fail: Removed callback ", id.toBase64String()); follower.onContractSubscriptionEvent(new ContractSubscription.FailedEvent() { @Override public MutableEnvironment getEnvironment() { return environment; } }); environment.save(); } // save new callback state in DB record ledger.updateFollowerCallbackState(id, FollowerCallbackState.EXPIRED); node.report(DatagramAdapter.VerboseLevel.BASE, "Failed callback ", id.toBase64String()); stop(); }