Search in sources :

Example 6 with HashId

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

the class HashIdTest method init.

@Before
public void init() {
    idA = new HashId(new byte[] { 1, 2, 3 });
    idB = new HashId(new byte[] { 1, 2, 4 });
    idA1 = new HashId(new byte[] { 1, 2, 3 });
}
Also used : HashId(com.icodici.universa.HashId) Before(org.junit.Before)

Example 7 with HashId

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

the class PostgresLedgerTest method create.

@Test
public void create() throws Exception {
    // System.out.println("" + ledger.getDb().getIntParam("version"));
    HashId id = HashId.createRandom();
    assertNull(ledger.getRecord(id));
    StateRecord r = ledger.findOrCreate(id);
    System.out.println(r);
    System.out.println(ledger.countRecords());
}
Also used : HashId(com.icodici.universa.HashId) Test(org.junit.Test)

Example 8 with HashId

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

the class PostgresLedgerTest 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)

Example 9 with HashId

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

the class PostgresLedgerTest method ledgerBenchmark.

// @Test
public void ledgerBenchmark() throws Exception {
    ExecutorService es = Executors.newCachedThreadPool();
    List<Future<?>> ff = new ArrayList<>();
    int nMax = 32;
    int nIds = 4000;
    long t = StopWatch.measure(true, () -> {
        for (int n = 0; n < nMax; n++) {
            final int x = n;
            ff.add(es.submit(() -> {
                HashId[] ids = new HashId[nIds];
                for (int i = 0; i < ids.length; i++) ids[i] = HashId.createRandom();
                System.out.println(x);
                StopWatch.measure(true, () -> {
                    for (HashId i : ids) {
                        try {
                            ledger.findOrCreate(i);
                        } catch (Exception e) {
                            e.printStackTrace();
                            fail(e.getMessage());
                        }
                    }
                });
                System.out.println("end-" + x);
                return null;
            }));
        }
        ff.forEach(f -> {
            try {
                f.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
        System.out.println("total");
    });
    System.out.println("TPS: " + (nMax * nIds * 1000 / t));
    System.out.println("" + ledger.getDb().queryOne("SELECT count(*) from ledger"));
}
Also used : HashId(com.icodici.universa.HashId) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with HashId

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

the class PostgresLedgerTest method recordExpiration.

@Test
public void recordExpiration() throws Exception {
    // todo: expired can't be get - it should be dropped by the database
    HashId hashId = HashId.createRandom();
    StateRecord r = ledger.findOrCreate(hashId);
    assertNotNull(r.getExpiresAt());
    assert (r.getExpiresAt().isAfter(ZonedDateTime.now()));
    long recordId = r.getRecordId();
    ZonedDateTime inFuture = ZonedDateTime.now().plusHours(2);
    r.setExpiresAt(inFuture);
    StateRecord r1 = ledger.getRecord(hashId);
    assertNotEquals(r1.getExpiresAt(), inFuture);
    r.save();
    r1 = ledger.getRecord(hashId);
    assertAlmostSame(r.getExpiresAt(), r1.getExpiresAt());
    r.setExpiresAt(ZonedDateTime.now().minusHours(1));
    r.save();
    r1 = ledger.getRecord(hashId);
    assertNull(r1);
}
Also used : HashId(com.icodici.universa.HashId) ZonedDateTime(java.time.ZonedDateTime) 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