@Before public void setUp() throws Exception { // new File("testledger").delete(); // Class.forName("org.postgresql.Driver"); ledger = new PostgresLedger(CONNECTION_STRING); ledger.enableCache(false); }
@Test public void checkLockOwner() throws Exception { ledger.enableCache(true); StateRecord existing = ledger.findOrCreate(HashId.createRandom()); existing.approve(); StateRecord r = ledger.findOrCreate(HashId.createRandom()); StateRecord r1 = r.lockToRevoke(existing.getId()); existing.reload(); r.reload(); assertSameRecords(existing, r1); assertEquals(ItemState.LOCKED, existing.getState()); assertEquals(r.getRecordId(), existing.getLockedByRecordId()); StateRecord currentOwner = ledger.getLockOwnerOf(existing); System.out.println("existing: " + existing.getId()); System.out.println("locker: " + r.getId()); System.out.println("locked: " + r1.getId()); System.out.println("currentOwner: " + currentOwner.getId()); assertSameRecords(r, currentOwner); }
TestSpace testSpace = prepareTestSpace(TestKeys.privateKey(0)); testSpace.nodes.forEach(n -> n.config.setIsFreeRegistrationsAllowedFromYaml(true)); testSpace.nodes.forEach(m -> ((PostgresLedger)m.node.getLedger()).enableCache(false)); testSpace.nodes.get(testSpace.nodes.size()-1).shutdown();
@Test public void getStateWithNoLedgerCache() throws Exception { TestSpace testSpace = prepareTestSpace(TestKeys.privateKey(0)); testSpace.nodes.forEach(m -> m.config.setIsFreeRegistrationsAllowedFromYaml(true)); testSpace.nodes.forEach(m -> ((PostgresLedger)m.node.getLedger()).enableCache(false)); //SHUTDOWN LAST NODE testSpace.nodes.remove(testSpace.nodes.size()-1).shutdown(); Thread.sleep(4000); Contract rev1 = new Contract(TestKeys.privateKey(0)); rev1.getStateData().set("field1", 33); Permission permission = new ChangeNumberPermission(rev1.getOwner(), Binder.of("field_name", "field1")); rev1.addPermission(permission); rev1.seal(); ItemResult ir1 = testSpace.client.register(rev1.getPackedTransaction(), 5000); assertEquals(ItemState.APPROVED, ir1.state); Contract rev2 = rev1.createRevision(); rev2.getStateData().set("field1", 34); rev2.addSignerKey(TestKeys.privateKey(0)); rev2.seal(); ItemResult ir2 = testSpace.client.register(rev2.getPackedTransaction(), 5000); assertEquals(ItemState.APPROVED, ir2.state); ir1 = testSpace.client.register(rev1.getPackedTransaction(), 5000); assertEquals(ItemState.REVOKED, ir1.state); testSpace.nodes.forEach(m -> m.shutdown()); }
@Test public void checkNegativeBytesInId() throws Exception { HashId id = HashId.withDigest(Do.randomNegativeBytes(64)); StateRecord r1 = ledger.findOrCreate(id); r1.setState(ItemState.DECLINED); r1.save(); StateRecord r2 = ledger.getRecord(id); assertNotNull(r2); assertNotSame(r1, r2); assertEquals(r1.getState(), r2.getState()); ledger.enableCache(true); StateRecord r3 = ledger.getRecord(id); StateRecord r4 = ledger.getRecord(id); assertEquals(r3.toString(), r4.toString()); // why? assertSame(r3, r4); }
@Test public void createOutputLockRecord() throws Exception { ledger.enableCache(true); StateRecord owner = ledger.findOrCreate(HashId.createRandom()); StateRecord other = ledger.findOrCreate(HashId.createRandom()); HashId id = HashId.createRandom(); StateRecord r1 = owner.createOutputLockRecord(id); r1.reload(); assertEquals(id, r1.getId()); assertEquals(ItemState.LOCKED_FOR_CREATION, r1.getState()); assertEquals(owner.getRecordId(), r1.getLockedByRecordId()); StateRecord r2 = owner.createOutputLockRecord(id); assertSame(r2, r1); assertNull(owner.createOutputLockRecord(other.getId())); // And hacked low level operation must fail too assertNull(ledger.createOutputLockRecord(owner.getRecordId(), other.getId())); }
@Test public void lockForRevoking() throws Exception { ledger.enableCache(true); StateRecord existing = ledger.findOrCreate(HashId.createRandom()); existing.approve(); StateRecord existing2 = ledger.findOrCreate(HashId.createRandom()); existing2.approve(); StateRecord r = ledger.findOrCreate(HashId.createRandom()); StateRecord r1 = r.lockToRevoke(existing.getId()); existing.reload(); r.reload(); assertSameRecords(existing, r1); assertEquals(ItemState.LOCKED, existing.getState()); assertEquals(r.getRecordId(), existing.getLockedByRecordId()); // we lock again the same record, everything should be still ok: StateRecord r2 = r.lockToRevoke(existing.getId()); assertNotNull(r2); existing.reload(); r.reload(); assertSameRecords(existing, r1); assertSameRecords(existing, r2); assertSame(r1, r2); assertEquals(ItemState.LOCKED, existing.getState()); assertEquals(r.getRecordId(), existing.getLockedByRecordId()); StateRecord r3 = r.lockToRevoke(existing2.getId()); existing2.reload(); assertSameRecords(existing2, r3); assertEquals(ItemState.LOCKED, existing2.getState()); assertEquals(r.getRecordId(), existing2.getLockedByRecordId()); }