@Override public NImmutableEnvironment getEnvironment(long environmentId) { return protect(() -> { List<byte[]> smkv = getSmartContractForEnvironmentId(environmentId); HashId nContractHashId = HashId.withDigest(smkv.get(2)); Contract contract = NSmartContract.fromPackedTransaction(smkv.get(0)); Contract findNContract = contract.getTransactionPack().getSubItem(nContractHashId); contract = findNContract == null ? contract : findNContract; NSmartContract nSmartContract = (NSmartContract) contract; Binder kvBinder = Boss.unpack(smkv.get(1)); Collection<ContractSubscription> contractSubscriptions = getContractSubscriptions(environmentId); Collection<ContractStorage> contractStorages = getContractStorages(environmentId); FollowerService followerService = getFollowerService(environmentId); List<String> reducedNames = getReducedNames(environmentId); List<NameRecord> nameRecords = new ArrayList<>(); for (String reducedName : reducedNames) { NNameRecord nr = getNameRecord(reducedName); //nr.setId(nrModel.id); nameRecords.add(nr); } NImmutableEnvironment nImmutableEnvironment = new NImmutableEnvironment(nSmartContract, kvBinder, contractSubscriptions, contractStorages, nameRecords, followerService, this); nImmutableEnvironment.setId(environmentId); return nImmutableEnvironment; }); }
@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; }
/** * Get environment and follower contract by environment identifier. * * @param environmentId is environment subscription * * @return {@link Binder} with environment and follower contract * */ public Binder getFullEnvironment(long environmentId) { NImmutableEnvironment ime = getEnvironment(environmentId); ime.setNameCache(nameCache); NSmartContract contract = ime.getContract(); contract.setNodeInfoProvider(nodeInfoProvider); NMutableEnvironment me = ime.getMutable(); if (me == null) return Binder.EMPTY; return Binder.of("follower", contract, "environment", me); }
@Override public List<ErrorRecord> tryAllocate(Collection<String> reducedNamesToAllocate, Collection<HashId> originsToAllocate, Collection<String> addressesToAllocate) { List<String> namesErrors = isNamesAvailable(reducedNamesToAllocate); List<String> originsErrors = isOriginsAvailable(originsToAllocate); List<String> addressesErros = isAddressesAvailable(addressesToAllocate); boolean checkResult = namesErrors.isEmpty() && originsErrors.isEmpty() && addressesErros.isEmpty(); if (!checkResult) { if (namesErrors.isEmpty()) nameCache.unlockNameList(reducedNamesToAllocate); if (originsErrors.isEmpty()) nameCache.unlockOriginList(originsToAllocate); if (addressesErros.isEmpty()) nameCache.unlockAddressList(addressesToAllocate); } List<ErrorRecord> res = new ArrayList<>(); for (String s : namesErrors) res.add(new ErrorRecord(Errors.FAILED_CHECK, "names", "name '"+s+"' is not available")); for (String s : originsErrors) res.add(new ErrorRecord(Errors.FAILED_CHECK, "origins", "origin '"+s+"' is not available")); for (String s : addressesErros) res.add(new ErrorRecord(Errors.FAILED_CHECK, "addresses", "address '"+s+"' is not available")); return res; }
private Set<HashId> saveEnvironment_getConflicts(NImmutableEnvironment environment) { HashId ownSmartContractId = environment.getContract().getId(); for (NameRecord nameRecord : environment.nameRecords()) { namesToCheck.add(nameRecord.getNameReduced()); for (NameRecordEntry nameRecordEntry : nameRecord.getEntries()) {
for (Long envId : envs) { NImmutableEnvironment environment = node.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected expiresAt: " + calculateExpires); System.out.println("found expiresAt: " + foundCss.expiresAt()); for(Long envId : envs) { NImmutableEnvironment environment = node.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected: " + calculateExpires); System.out.println("found: " + foundCss.expiresAt()); FollowerService fs = environment.getFollowerService(); System.out.println("expected expiresAt: " + calculateExpires); System.out.println("found expiresAt: " + fs.expiresAt()); for (Long envId : envs) { NImmutableEnvironment environment = networkNode.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected: " + calculateExpires); System.out.println("found: " + foundCss.expiresAt()); for(Long envId : envs) { NImmutableEnvironment environment = networkNode.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected: " + calculateExpires); System.out.println("found: " + foundCss.expiresAt()); FollowerService fs = environment.getFollowerService();
for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("days " + days); System.out.println("hours " + hours); for (ContractStorage foundStorage : environment.storages()) { System.out.println("expected " + calculateExpires); System.out.println("found " + foundStorage.expiresAt()); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("days " + days); System.out.println("hours " + hours); for (ContractStorage foundStorage : environment.storages()) { System.out.println("expected " + calculateExpires); System.out.println("found " + foundStorage.expiresAt()); for (Long envId : envs) { NImmutableEnvironment environment = networkNode.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected " + calculateExpires); System.out.println("found " + foundCss.expiresAt()); for (ContractStorage foundStorage : environment.storages()) { System.out.println("expected " + calculateExpires); System.out.println("found " + foundStorage.expiresAt()); for (Long envId : envs) { NImmutableEnvironment environment = node.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) {
synchronized (callbackService) { NImmutableEnvironment ime = getEnvironment(environmentId); ime.setNameCache(nameCache); NSmartContract contract = ime.getContract(); contract.setNodeInfoProvider(nodeInfoProvider); NMutableEnvironment me = ime.getMutable(); for (ContractSubscription sub : ime.subscriptions()) { if ((lookingId != null) && (sub.getContractId() != null) && (lookingId.equals(sub.getContractId()))) { ContractSubscription subscription = sub;
env.subscriptions().iterator().next().getContractId().getDigest()); ResultSet rs = ps.executeQuery(); assertTrue(rs.next()); env.subscriptions().iterator().next().getContractId().getDigest()); rs = ps.executeQuery(); assertTrue(rs.next());
ime.setNameCache(nameCache); NMutableEnvironment me = ime.getMutable();
NImmutableEnvironment ime = new NImmutableEnvironment(newSlotContract, networkNode.getLedger()); ime.setNameCache(new NameCache(Duration.ofMinutes(1))); NImmutableEnvironment ime = new NImmutableEnvironment(newSlotContract, node.getLedger()); ime.setNameCache(new NameCache(Duration.ofMinutes(1)));
ime.setNameCache(nameCache);
NImmutableEnvironment environment = new NImmutableEnvironment( smartContract,kvStore,Do.listOf(sub),Do.listOf(storage),Do.listOf(nnr2),followerService,null); assertEquals(environment.get("test",null),"test1");
@Override public FollowerService getFollowerService() { return getFollowerService(false); }
NImmutableEnvironment environment = new NImmutableEnvironment(rev1,new Binder(),Do.listOf(),Do.listOf(),Do.listOf(),null,null); ledger.saveEnvironment(environment); assertNotNull(ledger.getEnvironment(rev1.getId()));
final void cleanUp() { // we should avoid creating an object for each check: Instant now = Instant.now(); environmetsExpiration.keySet().forEach(envId -> { if(environmetsExpiration.get(envId).isBefore(now)) { environmetsExpiration.remove(envId); environemtsByContract.remove(environemtsById.remove(envId).getContract().getId()); } }); }
public void remove(HashId id) {; NImmutableEnvironment env = environemtsByContract.remove(id); if(env != null) { long envId = env.getId(); environemtsById.remove(envId); environmetsExpiration.remove(envId); } } }
for(Long envId : envs) { NImmutableEnvironment environment = node.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected: " + calculateExpires); System.out.println("found: " + foundCss.expiresAt()); FollowerService fs = environment.getFollowerService(); System.out.println("expected expiresAt: " + calculateExpires); System.out.println("found expiresAt: " + fs.expiresAt()); for(Long envId : envs) { NImmutableEnvironment environment = node.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected: " + calculateExpires2); System.out.println("found: " + foundCss.expiresAt()); FollowerService fs = environment.getFollowerService(); System.out.println("expected expiresAt: " + calculateExpires2); System.out.println("found expiresAt: " + fs.expiresAt()); for(Long envId : envs) { NImmutableEnvironment environment = networkNode.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected: " + calculateExpires); System.out.println("found: " + foundCss.expiresAt()); FollowerService fs = environment.getFollowerService(); System.out.println("expected expiresAt: " + calculateExpires); System.out.println("found expiresAt: " + fs.expiresAt());
for(Long envId : envs) { NImmutableEnvironment environment = node.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected " + calculateExpires); System.out.println("found " + foundCss.expiresAt()); for (ContractStorage foundStorage : environment.storages()) { System.out.println("expected " + calculateExpires); System.out.println("found " + foundStorage.expiresAt()); if (!envs.contains(envId)) { NImmutableEnvironment environment = node.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected " + calculateExpires); System.out.println("found " + foundCss.expiresAt()); for (ContractStorage foundStorage : environment.storages()) { System.out.println("expected " + calculateExpires); System.out.println("found " + foundStorage.expiresAt());
for(Long envId : envs) { NImmutableEnvironment environment = node.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected: " + calculateExpires); System.out.println("found: " + foundCss.expiresAt()); for(Long envId : envs) { NImmutableEnvironment environment = networkNode.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected: " + calculateExpires); System.out.println("found: " + foundCss.expiresAt()); for(Long envId : envs) { NImmutableEnvironment environment = node.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected: " + calculateExpires2); System.out.println("found: " + foundCss.expiresAt()); for(Long envId : envs) { NImmutableEnvironment environment = networkNode.getLedger().getEnvironment(envId); for (ContractSubscription foundCss : environment.subscriptions()) { System.out.println("expected: " + calculateExpires2); System.out.println("found: " + foundCss.expiresAt());