use of com.icodici.universa.HashId in project universa by UniversaBlockchain.
the class SqliteLedgerTest 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);
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);
}
use of com.icodici.universa.HashId in project universa by UniversaBlockchain.
the class SqliteLedgerTest method ledgerBenchmark.
// @Test
public void ledgerBenchmark() throws Exception {
ExecutorService es = Executors.newCachedThreadPool();
// ExecutorService es = Executors.newSingleThreadExecutor();
List<Future<?>> ff = new ArrayList<>();
long t = StopWatch.measure(true, () -> {
for (int n = 0; n < 4; n++) {
final int x = n;
ff.add(es.submit(() -> {
HashId[] ids = new HashId[10000];
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: " + (4.0 * 10000 * 1000 / t));
System.out.println("" + ledger.getDb().queryOne("SELECT count(*) from ledger"));
}
use of com.icodici.universa.HashId in project universa by UniversaBlockchain.
the class SqliteLedgerTest method checkNegatoveBytesInId.
@Test
public void checkNegatoveBytesInId() 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);
}
use of com.icodici.universa.HashId in project universa by UniversaBlockchain.
the class Client method getState.
public ItemResult getState(HashId itemId, Reporter reporter) throws ClientError {
final ExecutorService pool = Executors.newCachedThreadPool();
final List<ErrorRecord> errors = new ArrayList<>();
final AsyncEvent<Void> consensusFound = new AsyncEvent<>();
final int checkConsensus = getNodes().size() / 3;
final AtomicInteger nodesLeft = new AtomicInteger(nodes.size());
return protect(() -> {
final Map<ItemState, List<ItemResult>> states = new HashMap<>();
for (int i = 0; i < nodes.size(); i++) {
final int nn = i;
pool.submit(() -> {
for (int retry = 0; retry < 5; retry++) {
// reporter.verbose("trying "+reporter+" for node "+nn);
try {
Client c = getClient(nn);
ItemResult r = c.command("getState", "itemId", itemId).getOrThrow("itemResult");
r.meta.put("url", c.getNodeNumber());
synchronized (states) {
List<ItemResult> list = states.get(r.state);
if (list == null) {
list = new ArrayList();
states.put(r.state, list);
}
list.add(r);
if (r.errors.size() > 0)
reporter.warning("errors from " + c.getNodeNumber() + ": " + r.errors);
break;
}
} catch (IOException e) {
// reporter.warning("can't get answer from node " + nn + ", retry #" + retry + ": " + e);
}
}
// Now we should check the consensus
states.forEach((itemState, itemResults) -> {
if (itemResults.size() >= checkConsensus)
if (itemResults.size() >= checkConsensus) {
consensusFound.fire();
return;
}
});
if (nodesLeft.decrementAndGet() < 1)
consensusFound.fire();
});
}
consensusFound.await(5000);
pool.shutdownNow();
final ItemResult[] consensus = new ItemResult[1];
states.forEach((itemState, itemResults) -> {
if (itemResults.size() >= checkConsensus)
consensus[0] = itemResults.get(0);
});
if (consensus[0] != null)
reporter.message("State consensus found:" + consensus[0]);
else {
reporter.warning("no consensus found " + states.size());
}
if (states.size() > 1) {
states.entrySet().stream().sorted(Comparator.comparingInt(o -> o.getValue().size())).forEach(kv -> {
List<ItemResult> itemResults = kv.getValue();
reporter.message("" + kv.getKey() + ": " + itemResults.size() + ": " + itemResults.stream().map(x -> x.meta.getStringOrThrow("url")).collect(Collectors.toSet()));
});
}
return consensus[0];
});
}
use of com.icodici.universa.HashId in project universa by UniversaBlockchain.
the class ItemResyncNotification method readFrom.
@Override
protected void readFrom(Boss.Reader br) throws IOException {
super.readFrom(br);
Map<String, Object> packingMap = br.readMap();
for (String s : packingMap.keySet()) {
HashId hid = HashId.withDigest(s);
ItemState state = ItemState.values()[(int) packingMap.get(s)];
itemsToResync.put(hid, state);
}
}
Aggregations