@Ignore @Test public void dbSanitationTest() throws Exception { final int NODE_COUNT = 4; PrivateKey myKey = TestKeys.privateKey(NODE_COUNT); List<String> dbUrls = new ArrayList<>(); dbUrls.add("jdbc:postgresql: dbUrls.add("jdbc:postgresql: dbUrls.add("jdbc:postgresql: dbUrls.add("jdbc:postgresql: List<Ledger> ledgers = new ArrayList<>(); dbUrls.stream().forEach(url -> { try { PostgresLedger ledger = new PostgresLedger(url); ledgers.add(ledger); } catch (Exception e) { e.printStackTrace(); } }); Random random = new Random(123); List<Contract> origins = new ArrayList<>(); List<Contract> newRevisions = new ArrayList<>(); List<Contract> newContracts = new ArrayList<>(); final int N = 100; for (int i = 0; i < N; i++) { Contract origin = new Contract(myKey); origin.seal(); origins.add(origin); Contract newRevision = origin.createRevision(myKey); if (i < N / 2) { newRevision.setOwnerKeys(TestKeys.privateKey(NODE_COUNT + 1).getPublicKey()); } else { } Contract newContract = new Contract(myKey); newRevision.addNewItems(newContract); newRevision.seal(); newContracts.add(newContract); newRevisions.add(newRevision); int unfinishedNodesCount = random.nextInt(2) + 1; Set<Integer> unfinishedNodesNumbers = new HashSet<>(); while (unfinishedNodesCount > unfinishedNodesNumbers.size()) { unfinishedNodesNumbers.add(random.nextInt(NODE_COUNT) + 1); } System.out.println("item# " + newRevision.getId().toBase64String().substring(0, 6) + " nodes " + unfinishedNodesNumbers.toString()); int finalI = i; for (int j = 0; j < NODE_COUNT; j++) { boolean finished = !unfinishedNodesNumbers.contains(j + 1); Ledger ledger = ledgers.get(j); StateRecord originRecord = ledger.findOrCreate(origin.getId()); originRecord.setExpiresAt(origin.getExpiresAt()); originRecord.setCreatedAt(origin.getCreatedAt()); StateRecord newRevisionRecord = ledger.findOrCreate(newRevision.getId()); newRevisionRecord.setExpiresAt(newRevision.getExpiresAt()); newRevisionRecord.setCreatedAt(newRevision.getCreatedAt()); StateRecord newContractRecord = ledger.findOrCreate(newContract.getId()); newContractRecord.setExpiresAt(newContract.getExpiresAt()); newContractRecord.setCreatedAt(newContract.getCreatedAt()); if (finished) { if (finalI < N / 2) { originRecord.setState(ItemState.REVOKED); newContractRecord.setState(ItemState.APPROVED); newRevisionRecord.setState(ItemState.APPROVED); } else { originRecord.setState(ItemState.APPROVED); newContractRecord.setState(ItemState.UNDEFINED); newRevisionRecord.setState(ItemState.DECLINED); } } else { originRecord.setState(ItemState.LOCKED); originRecord.setLockedByRecordId(newRevisionRecord.getRecordId()); newContractRecord.setState(ItemState.LOCKED_FOR_CREATION); newContractRecord.setLockedByRecordId(newRevisionRecord.getRecordId()); newRevisionRecord.setState(finalI < N / 2 ? ItemState.PENDING_POSITIVE : ItemState.PENDING_NEGATIVE); } originRecord.save(); ledger.putItem(originRecord, origin, Instant.now().plusSeconds(3600 * 24)); newRevisionRecord.save(); ledger.putItem(newRevisionRecord, newRevision, Instant.now().plusSeconds(3600 * 24)); if (newContractRecord.getState() == ItemState.UNDEFINED) { newContractRecord.destroy(); } else { newContractRecord.save(); } } } ledgers.stream().forEach(ledger -> ledger.close()); ledgers.clear(); List<Main> mm = new ArrayList<>(); List<Client> clients = new ArrayList<>(); for (int i = 0; i < NODE_COUNT; i++) { Main m = createMain("node" + (i + 1), false); m.config.setIsFreeRegistrationsAllowedFromYaml(true); mm.add(m); Client client = new Client(TestKeys.privateKey(i), m.myInfo, null); clients.add(client); } while (true) { try { for (int i = 0; i < NODE_COUNT; i++) { clients.get(i).getState(newRevisions.get(0)); } break; } catch (ClientError e) { Thread.sleep(1000); mm.stream().forEach(m -> System.out.println("node#" + m.myInfo.getNumber() + " is " + (m.node.isSanitating() ? "" : "not ") + "sanitating")); } } Contract contract = new Contract(TestKeys.privateKey(3)); contract.seal(); ItemResult ir = clients.get(0).register(contract.getPackedTransaction(), 10000); ir.errors.toString(); for (int i = 0; i < N; i++) { ItemResult rr = clients.get(i % NODE_COUNT).getState(newRevisions.get(i).getId()); ItemState targetState = i < N / 2 ? ItemState.APPROVED : ItemState.DECLINED; assertEquals(rr.state, targetState); } Thread.sleep(1000); mm.stream().forEach(m -> m.shutdown()); Thread.sleep(1000); dbUrls.stream().forEach(url -> { try { PostgresLedger ledger = new PostgresLedger(url); assertTrue(ledger.findUnfinished().isEmpty()); ledger.close(); } catch (Exception e) { e.printStackTrace(); } }); }
public void shutdown() { try { if (hookThread != null) Runtime.getRuntime().removeShutdownHook(hookThread); ledger.close(); log("shutting down"); network.shutdown(); clientHTTPServer.shutdown(); } catch (Exception e) { } synchronized (parser) { parser.notifyAll(); } try { logger.close(); } catch (Exception e) { e.printStackTrace(); } }
Main { public void shutdown() { try { if (hookThread != null) Runtime.getRuntime().removeShutdownHook(hookThread); ledger.close(); log("shutting down"); network.shutdown(); clientHTTPServer.shutdown(); } catch (Exception e) { } synchronized (parser) { parser.notifyAll(); } try { logger.close(); } catch (Exception e) { e.printStackTrace(); } } }
Main { public void shutdown() { try { if (hookThread != null) Runtime.getRuntime().removeShutdownHook(hookThread); ledger.close(); log("shutting down"); network.shutdown(); clientHTTPServer.shutdown(); } catch (Exception e) { } synchronized (parser) { parser.notifyAll(); } try { logger.close(); } catch (Exception e) { e.printStackTrace(); } } Main(String[] args); }
Main { public void shutdown() { try { if (hookThread != null) Runtime.getRuntime().removeShutdownHook(hookThread); ledger.close(); log("shutting down"); network.shutdown(); clientHTTPServer.shutdown(); } catch (Exception e) { } synchronized (parser) { parser.notifyAll(); } try { logger.close(); } catch (Exception e) { e.printStackTrace(); } } Main(String[] args); static void main(String[] args); void waitReady(); void shutdown(); void setVerboseLevel(int level); void setUDPVerboseLevel(int level); void restartUDPAdapter(); PublicKey getNodePublicKey(); }
Main { public void shutdown() { try { if (hookThread != null) Runtime.getRuntime().removeShutdownHook(hookThread); ledger.close(); log("shutting down"); network.shutdown(); clientHTTPServer.shutdown(); } catch (Exception e) { } synchronized (parser) { parser.notifyAll(); } try { logger.close(); } catch (Exception e) { e.printStackTrace(); } } Main(String[] args); static void main(String[] args); void waitReady(); void shutdown(); void setVerboseLevel(int level); void setUDPVerboseLevel(int level); void restartUDPAdapter(); PublicKey getNodePublicKey(); static final String NODE_VERSION; final Reporter reporter; final BufferedLogger logger; public NetConfig netConfig; public NetworkV2 network; final Config config; public NodeInfo myInfo; public Node node; public ItemCache cache; public ParcelCache parcelCache; public PaidOperationCache paidOperationCache; }