Search in sources :

Example 16 with HashId

use of com.icodici.universa.HashId in project universa by UniversaBlockchain.

the class PostgresLedgerTest method saveOneRecordManyTimes.

// @Test
public void saveOneRecordManyTimes() throws Exception {
    HashId hashId = HashId.createRandom();
    StateRecord r = ledger.findOrCreate(hashId);
    class TestRunnable implements Runnable {

        @Override
        public void run() {
            ledger.getRecord(r.getId());
            r.setState(ItemState.APPROVED);
            r.save();
        }
    }
    List<Thread> threadsList = new ArrayList<>();
    List<TestRunnable> runnableList = new ArrayList<>();
    for (int j = 0; j < 700; j++) {
        TestRunnable runnableSingle = new TestRunnable();
        runnableList.add(runnableSingle);
        threadsList.add(new Thread(() -> {
            runnableSingle.run();
        }));
    }
    for (Thread th : threadsList) {
        th.start();
    }
}
Also used : HashId(com.icodici.universa.HashId) ArrayList(java.util.ArrayList)

Example 17 with HashId

use of com.icodici.universa.HashId in project universa by UniversaBlockchain.

the class PostgresLedgerTest method checkNegativeBytesInId.

@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);
}
Also used : HashId(com.icodici.universa.HashId) Test(org.junit.Test)

Example 18 with HashId

use of com.icodici.universa.HashId in project universa by UniversaBlockchain.

the class PostgresLedgerTest method createOutputLockRecord.

@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()));
}
Also used : HashId(com.icodici.universa.HashId) Test(org.junit.Test)

Example 19 with HashId

use of com.icodici.universa.HashId in project universa by UniversaBlockchain.

the class PostgresLedgerTest method ledgerCleanupTest.

@Test
public void ledgerCleanupTest() throws Exception {
    Contract contract = new Contract(TestKeys.privateKey(0));
    contract.seal();
    StateRecord r = ledger.findOrCreate(contract.getId());
    r.setExpiresAt(ZonedDateTime.now().minusSeconds(1));
    r.save();
    ledger.putItem(r, contract, Instant.now().plusSeconds(300));
    HashId hash1 = contract.getId();
    contract = new Contract(TestKeys.privateKey(0));
    contract.seal();
    r = ledger.findOrCreate(contract.getId());
    r.setExpiresAt(ZonedDateTime.now().plusMonths(1));
    r.save();
    ledger.putItem(r, contract, Instant.now().minusSeconds(1));
    HashId hash2 = contract.getId();
    ledger.cleanup();
    PreparedStatement st = ledger.getDb().statement("select count(*) from ledger where hash = ?", hash1.getDigest());
    try (ResultSet rs = st.executeQuery()) {
        assertTrue(rs.next());
        assertEquals(rs.getInt(1), 0);
    }
    st = ledger.getDb().statement("select count(*) from ledger where hash = ?", hash2.getDigest());
    try (ResultSet rs = st.executeQuery()) {
        assertTrue(rs.next());
        assertEquals(rs.getInt(1), 1);
    }
    st = ledger.getDb().statement("select count(*) from items where id in (select id from ledger where hash = ?)", hash2.getDigest());
    try (ResultSet rs = st.executeQuery()) {
        assertTrue(rs.next());
        assertEquals(rs.getInt(1), 0);
    }
}
Also used : HashId(com.icodici.universa.HashId) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 20 with HashId

use of com.icodici.universa.HashId in project universa by UniversaBlockchain.

the class SqliteLedgerTest method findOrCreateAndGet.

@Test
public void findOrCreateAndGet() throws Exception {
    // Atomic new record creation
    HashId id = HashId.createRandom();
    StateRecord r = ledger.findOrCreate(id);
    assertNotNull(r);
    assertEquals(id, r.getId());
    assertEquals(ItemState.PENDING, r.getState());
    assertAlmostSame(ZonedDateTime.now(), r.getCreatedAt());
    // returning existing record
    StateRecord r1 = ledger.findOrCreate(id);
    assertSameRecords(r, r1);
    StateRecord r2 = ledger.getRecord(id);
    assertSameRecords(r, r2);
    StateRecord r3 = ledger.getRecord(HashId.createRandom());
    assert (r3 == null);
}
Also used : HashId(com.icodici.universa.HashId) Test(org.junit.Test)

Aggregations

HashId (com.icodici.universa.HashId)30 Test (org.junit.Test)18 Contract (com.icodici.universa.contract.Contract)6 PrivateKey (com.icodici.crypto.PrivateKey)4 PublicKey (com.icodici.crypto.PublicKey)3 ZonedDateTime (java.time.ZonedDateTime)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 Binder (net.sergeych.tools.Binder)3 Approvable (com.icodici.universa.Approvable)2 Decimal (com.icodici.universa.Decimal)2 ItemResult (com.icodici.universa.node.ItemResult)2 ItemState (com.icodici.universa.node.ItemState)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 EncryptionError (com.icodici.crypto.EncryptionError)1 ErrorRecord (com.icodici.universa.ErrorRecord)1 Parcel (com.icodici.universa.contract.Parcel)1 com.icodici.universa.node2 (com.icodici.universa.node2)1