use of com.icodici.universa.HashId in project universa by UniversaBlockchain.
the class MainTest method localNetwork2.
@Test
public void localNetwork2() throws Exception {
List<Main> mm = new ArrayList<>();
for (int i = 0; i < 4; i++) mm.add(createMain("node" + (i + 1), false));
Main main = mm.get(0);
assertEquals("http://localhost:8080", main.myInfo.internalUrlString());
assertEquals("http://localhost:8080", main.myInfo.publicUrlString());
PrivateKey myKey = new PrivateKey(Do.read("./src/test_contracts/keys/tu_key.private.unikey"));
// Client client = new Client(myKey, main.myInfo, null);
final long CONTRACTS_PER_THREAD = 60;
final long THREADS_COUNT = 4;
class TestRunnable implements Runnable {
public int threadNum = 0;
List<Contract> contractList = new ArrayList<>();
Map<HashId, Contract> contractHashesMap = new ConcurrentHashMap<>();
Client client = null;
public void prepareClient() {
try {
client = new Client(myKey, main.myInfo, null);
} catch (Exception e) {
System.out.println("prepareClient exception: " + e.toString());
}
}
public void prepareContracts() throws Exception {
contractList = new ArrayList<>();
for (int iContract = 0; iContract < CONTRACTS_PER_THREAD; ++iContract) {
Contract testContract = new Contract(myKey);
for (int i = 0; i < 10; i++) {
Contract nc = new Contract(myKey);
nc.seal();
testContract.addNewItems(nc);
}
testContract.seal();
assertTrue(testContract.isOk());
contractList.add(testContract);
contractHashesMap.put(testContract.getId(), testContract);
}
}
private void sendContractsToRegister() throws Exception {
for (int i = 0; i < contractList.size(); ++i) {
Contract contract = contractList.get(i);
client.register(contract.getPackedTransaction());
}
}
private void waitForContracts() throws Exception {
while (contractHashesMap.size() > 0) {
Thread.currentThread().sleep(300);
for (HashId id : contractHashesMap.keySet()) {
ItemResult itemResult = client.getState(id);
if (!itemResult.state.isPending())
contractHashesMap.remove(id);
else
break;
}
}
}
@Override
public void run() {
try {
sendContractsToRegister();
waitForContracts();
} catch (Exception e) {
System.out.println("runnable exception: " + e.toString());
}
}
}
System.out.println("singlethread test prepare...");
TestRunnable runnableSingle = new TestRunnable();
Thread threadSingle = new Thread(() -> {
runnableSingle.threadNum = 0;
runnableSingle.run();
});
runnableSingle.prepareClient();
runnableSingle.prepareContracts();
System.out.println("singlethread test start...");
long t1 = new Date().getTime();
threadSingle.start();
threadSingle.join();
long t2 = new Date().getTime();
long dt = t2 - t1;
long singleThreadTime = dt;
System.out.println("singlethread test done!");
System.out.println("multithread test prepare...");
List<Thread> threadsList = new ArrayList<>();
List<Thread> threadsPrepareList = new ArrayList<>();
List<TestRunnable> runnableList = new ArrayList<>();
for (int iThread = 0; iThread < THREADS_COUNT; ++iThread) {
TestRunnable runnableMultithread = new TestRunnable();
final int threadNum = iThread + 1;
Thread threadMultiThread = new Thread(() -> {
runnableMultithread.threadNum = threadNum;
runnableMultithread.run();
});
Thread threadPrepareMultiThread = new Thread(() -> {
try {
runnableMultithread.prepareContracts();
} catch (Exception e) {
System.out.println("prepare exception: " + e.toString());
}
});
runnableMultithread.prepareClient();
threadsList.add(threadMultiThread);
threadsPrepareList.add(threadPrepareMultiThread);
runnableList.add(runnableMultithread);
}
for (Thread thread : threadsPrepareList) thread.start();
for (Thread thread : threadsPrepareList) thread.join();
Thread.sleep(500);
System.out.println("multithread test start...");
t1 = new Date().getTime();
for (Thread thread : threadsList) thread.start();
for (Thread thread : threadsList) thread.join();
t2 = new Date().getTime();
dt = t2 - t1;
long multiThreadTime = dt;
System.out.println("multithread test done!");
Double tpsSingleThread = (double) CONTRACTS_PER_THREAD / (double) singleThreadTime * 1000.0;
Double tpsMultiThread = (double) CONTRACTS_PER_THREAD * (double) THREADS_COUNT / (double) multiThreadTime * 1000.0;
Double boostRate = tpsMultiThread / tpsSingleThread;
System.out.println("\n === total ===");
System.out.println("singleThread: " + (CONTRACTS_PER_THREAD) + " for " + singleThreadTime + "ms, tps=" + String.format("%.2f", tpsSingleThread));
System.out.println("multiThread(N=" + THREADS_COUNT + "): " + (CONTRACTS_PER_THREAD * THREADS_COUNT) + " for " + multiThreadTime + "ms, tps=" + String.format("%.2f", tpsMultiThread));
System.out.println("boostRate: " + String.format("%.2f", boostRate));
System.out.println("\n");
mm.forEach(x -> x.shutdown());
}
use of com.icodici.universa.HashId in project universa by UniversaBlockchain.
the class Node2EmulatedNetworkTest method unexpectedStrangeCaseWithConcurrent.
// @Test
public void unexpectedStrangeCaseWithConcurrent() throws Exception {
String FIELD_NAME = "amount";
PrivateKey ownerKey2 = TestKeys.privateKey(1);
String PRIVATE_KEY = "_xer0yfe2nn1xthc.private.unikey";
Contract root = Contract.fromDslFile("./src/test_contracts/coin.yml");
root.getStateData().set(FIELD_NAME, new Decimal(200));
root.addSignerKeyFromFile("./src/test_contracts/" + PRIVATE_KEY);
root.setOwnerKey(ownerKey2);
root.seal();
assertTrue(root.check());
Contract c1 = root.splitValue(FIELD_NAME, new Decimal(100));
c1.seal();
assertTrue(root.check());
assertTrue(c1.isOk());
// c1 split 50 50
c1 = c1.createRevision(ownerKey2);
c1.seal();
Contract c50_1 = c1.splitValue(FIELD_NAME, new Decimal(50));
c50_1.seal();
assertTrue(c50_1.isOk());
// good join
Contract finalC = c50_1.createRevision(ownerKey2);
finalC.addSignerKeyFromFile(Config.tuKeyPath);
finalC.seal();
finalC.getStateData().set(FIELD_NAME, new Decimal(100));
finalC.addRevokingItems(c50_1);
finalC.addRevokingItems(c1);
for (int j = 0; j < 500; j++) {
HashId id;
StateRecord orCreate;
int p = 0;
for (Approvable c : finalC.getRevokingItems()) {
id = c.getId();
for (int i = 0; i < nodes.size(); i++) {
if (i == nodes.size() - 1 && p == 1)
break;
Node nodeS = nodes.get(i);
orCreate = nodeS.getLedger().findOrCreate(id);
orCreate.setState(ItemState.APPROVED).save();
}
++p;
}
destroyFromAllNodesExistingNew(finalC);
destroyCurrentFromAllNodesIfExists(finalC);
node.registerItem(finalC);
ItemResult itemResult = node.waitItem(finalC.getId(), 1500);
System.out.println(itemResult.state);
// if (ItemState.APPROVED != itemResult.state)
// System.out.println("\r\nWrong state on repetition " + j + ": " + itemResult + ", " + itemResult.errors +
// " \r\ncontract_errors: " + finalC.getErrors());
// else
// System.out.println("\r\nGood. repetition: " + j + " ledger:" + node.getLedger().toString());
// fail("Wrong state on repetition " + j + ": " + itemResult + ", " + itemResult.errors +
// " \r\ncontract_errors: " + finalC.getErrors());
// assertEquals(ItemState.APPROVED, itemResult.state);
}
}
use of com.icodici.universa.HashId in project universa by UniversaBlockchain.
the class QuantiserTest method fullSum.
@Test
public void fullSum() throws Exception {
int wantedSum = Quantiser.QuantiserProcesses.PRICE_APPLICABLE_PERM.getCost() + Quantiser.QuantiserProcesses.PRICE_CHECK_2048_SIG.getCost() + Quantiser.QuantiserProcesses.PRICE_CHECK_4096_SIG.getCost() + Quantiser.QuantiserProcesses.PRICE_CHECK_REFERENCED_VERSION.getCost() + Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION.getCost() + Quantiser.QuantiserProcesses.PRICE_REVOKE_VERSION.getCost() + Quantiser.QuantiserProcesses.PRICE_SPLITJOIN_PERM.getCost();
try {
byte[] hashBytes = new byte[128];
new Random().nextBytes(hashBytes);
HashId hashId = new HashId(hashBytes);
Quantiser q = new Quantiser();
q.reset(wantedSum);
q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_APPLICABLE_PERM);
q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_2048_SIG);
q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_4096_SIG);
q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_REFERENCED_VERSION);
q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION);
q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REVOKE_VERSION);
q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_SPLITJOIN_PERM);
assertEquals(wantedSum, q.getQuantaSum());
} catch (Quantiser.QuantiserException e) {
fail();
}
}
use of com.icodici.universa.HashId in project universa by UniversaBlockchain.
the class SplitJoinPermissionTest method shouldNotSplitWithWrongOriginSerialize.
@Test
public void shouldNotSplitWithWrongOriginSerialize() throws Exception {
Contract c = createCoin();
c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
sealCheckTrace(c, true);
Contract c2 = c.split(1)[0];
sealCheckTrace(c2, true);
Binder sd2 = DefaultBiMapper.serialize(c2);
Binder state = (Binder) sd2.get("state");
assertNotNull(state);
assertTrue(state.size() > 0);
HashId origin = HashId.withDigest(Do.randomNegativeBytes(64));
Binder originB = DefaultBiMapper.serialize(origin);
state.set("origin", originB);
Contract dc2 = DefaultBiMapper.deserialize(sd2);
sealCheckTrace(dc2, false);
state.remove("origin");
Contract dc3 = DefaultBiMapper.deserialize(sd2);
sealCheckTrace(dc3, false);
}
use of com.icodici.universa.HashId in project universa by UniversaBlockchain.
the class HashIdTest method testToString.
@Test
public void testToString() throws Exception {
byte[] data = "hello world!".getBytes();
HashId hid = new HashId(data);
assertArrayEquals(hid.getDigest(), Base64u.decodeCompactString(hid.toBase64String()));
}
Aggregations