use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class MainTest method udpDisruptionTest.
@Test
public void udpDisruptionTest() throws Exception {
List<Main> mm = new ArrayList<>();
final int NODE_COUNT = 4;
final int PORT_BASE = 12000;
for (int i = 0; i < NODE_COUNT; i++) {
mm.add(createMain("node" + (i + 1), false));
}
class TestRunnable implements Runnable {
int finalI;
int finalJ;
boolean alive = true;
@Override
public void run() {
try {
NodeInfo source = mm.get(finalI).myInfo;
NodeInfo destination = mm.get(finalJ).myInfo;
DatagramSocket socket = new DatagramSocket(PORT_BASE + finalI * NODE_COUNT + finalJ);
while (alive) {
sendHello(source, destination, mm.get(finalI).network.getUDPAdapter(), socket);
}
} catch (Exception e) {
System.out.println("runnable exception: " + e.toString());
}
}
}
List<Thread> threadsList = new ArrayList<>();
List<TestRunnable> runnableList = new ArrayList<>();
for (int i = 0; i < NODE_COUNT; i++) {
for (int j = 0; j < NODE_COUNT; j++) {
if (j == i)
continue;
final int finalI = i;
final int finalJ = j;
TestRunnable runnableSingle = new TestRunnable();
runnableList.add(runnableSingle);
threadsList.add(new Thread(() -> {
runnableSingle.finalI = finalI;
runnableSingle.finalJ = finalJ;
runnableSingle.run();
}));
}
}
for (Thread th : threadsList) {
th.start();
}
Thread.sleep(1000);
PrivateKey myKey = TestKeys.privateKey(0);
Client client = new Client(myKey, mm.get(0).myInfo, null);
Contract contract = new Contract(myKey);
contract.seal();
Parcel parcel = createParcelWithFreshTU(client, contract, Do.listOf(myKey));
client.registerParcel(parcel.pack(), 60000);
ItemResult rr;
while (true) {
rr = client.getState(contract.getId());
if (!rr.state.isPending())
break;
}
assertEquals(rr.state, ItemState.APPROVED);
for (TestRunnable tr : runnableList) {
tr.alive = false;
}
for (Thread th : threadsList) {
th.interrupt();
}
mm.forEach(x -> x.shutdown());
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class MainTest method dbSanitationTest.
@Test
public void dbSanitationTest() throws Exception {
final int NODE_COUNT = 4;
PrivateKey myKey = TestKeys.privateKey(NODE_COUNT);
List<String> dbUrls = new ArrayList<>();
dbUrls.add("jdbc:postgresql://localhost:5432/universa_node_t1");
dbUrls.add("jdbc:postgresql://localhost:5432/universa_node_t2");
dbUrls.add("jdbc:postgresql://localhost:5432/universa_node_t3");
dbUrls.add("jdbc:postgresql://localhost:5432/universa_node_t4");
List<Ledger> ledgers = new ArrayList<>();
dbUrls.stream().forEach(url -> {
try {
clearLedger(url);
PostgresLedger ledger = new PostgresLedger(url);
ledgers.add(ledger);
} catch (Exception e) {
e.printStackTrace();
}
});
Random random = new Random(123);
List<Contract> origins = new ArrayList<>();
List<Contract> newRevisions = new ArrayList<>();
List<Contract> newContracts = new ArrayList<>();
final int N = 100;
for (int i = 0; i < N; i++) {
Contract origin = new Contract(myKey);
origin.seal();
origins.add(origin);
Contract newRevision = origin.createRevision(myKey);
if (i < N / 2) {
// ACCEPTED
newRevision.setOwnerKeys(TestKeys.privateKey(NODE_COUNT + 1).getPublicKey());
} else {
// DECLINED
// State is equal
}
Contract newContract = new Contract(myKey);
newRevision.addNewItems(newContract);
newRevision.seal();
newContracts.add(newContract);
newRevisions.add(newRevision);
int unfinishedNodesCount = random.nextInt(2) + 1;
Set<Integer> unfinishedNodesNumbers = new HashSet<>();
while (unfinishedNodesCount > unfinishedNodesNumbers.size()) {
unfinishedNodesNumbers.add(random.nextInt(NODE_COUNT) + 1);
}
System.out.println("item# " + newRevision.getId().toBase64String().substring(0, 6) + " nodes " + unfinishedNodesNumbers.toString());
int finalI = i;
for (int j = 0; j < NODE_COUNT; j++) {
boolean finished = !unfinishedNodesNumbers.contains(j + 1);
Ledger ledger = ledgers.get(j);
StateRecord originRecord = ledger.findOrCreate(origin.getId());
originRecord.setExpiresAt(origin.getExpiresAt());
originRecord.setCreatedAt(origin.getCreatedAt());
StateRecord newRevisionRecord = ledger.findOrCreate(newRevision.getId());
newRevisionRecord.setExpiresAt(newRevision.getExpiresAt());
newRevisionRecord.setCreatedAt(newRevision.getCreatedAt());
StateRecord newContractRecord = ledger.findOrCreate(newContract.getId());
newContractRecord.setExpiresAt(newContract.getExpiresAt());
newContractRecord.setCreatedAt(newContract.getCreatedAt());
if (finished) {
if (finalI < N / 2) {
originRecord.setState(ItemState.REVOKED);
newContractRecord.setState(ItemState.APPROVED);
newRevisionRecord.setState(ItemState.APPROVED);
} else {
originRecord.setState(ItemState.APPROVED);
newContractRecord.setState(ItemState.UNDEFINED);
newRevisionRecord.setState(ItemState.DECLINED);
}
} else {
originRecord.setState(ItemState.LOCKED);
originRecord.setLockedByRecordId(newRevisionRecord.getRecordId());
newContractRecord.setState(ItemState.LOCKED_FOR_CREATION);
newContractRecord.setLockedByRecordId(newRevisionRecord.getRecordId());
newRevisionRecord.setState(finalI < N / 2 ? ItemState.PENDING_POSITIVE : ItemState.PENDING_NEGATIVE);
}
originRecord.save();
ledger.putItem(originRecord, origin, Instant.now().plusSeconds(3600 * 24));
newRevisionRecord.save();
ledger.putItem(newRevisionRecord, newRevision, Instant.now().plusSeconds(3600 * 24));
if (newContractRecord.getState() == ItemState.UNDEFINED) {
newContractRecord.destroy();
} else {
newContractRecord.save();
}
}
}
ledgers.stream().forEach(ledger -> ledger.close());
ledgers.clear();
List<Main> mm = new ArrayList<>();
List<Client> clients = new ArrayList<>();
for (int i = 0; i < NODE_COUNT; i++) {
Main m = createMain("node" + (i + 1), false);
mm.add(m);
Client client = new Client(TestKeys.privateKey(i), m.myInfo, null);
clients.add(client);
}
while (true) {
try {
for (int i = 0; i < NODE_COUNT; i++) {
clients.get(i).getState(newRevisions.get(0));
}
break;
} catch (ClientError e) {
Thread.sleep(1000);
mm.stream().forEach(m -> System.out.println("node#" + m.myInfo.getNumber() + " is " + (m.node.isSanitating() ? "" : "not ") + "sanitating"));
}
}
Contract contract = new Contract(TestKeys.privateKey(3));
contract.seal();
ItemResult ir = clients.get(0).register(contract.getPackedTransaction(), 10000);
ir.errors.toString();
for (int i = 0; i < N; i++) {
ItemResult rr = clients.get(i % NODE_COUNT).getState(newRevisions.get(i).getId());
ItemState targetState = i < N / 2 ? ItemState.APPROVED : ItemState.DECLINED;
assertEquals(rr.state, targetState);
}
Thread.sleep(1000);
mm.stream().forEach(m -> m.shutdown());
Thread.sleep(1000);
dbUrls.stream().forEach(url -> {
try {
PostgresLedger ledger = new PostgresLedger(url);
assertTrue(ledger.findUnfinished().isEmpty());
ledger.close();
} catch (Exception e) {
e.printStackTrace();
}
});
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class MainTest method createNetConfigContract.
private Contract createNetConfigContract(Contract contract, List<NodeInfo> netConfig, Collection<PrivateKey> currentConfigKeys) throws IOException {
contract = contract.createRevision();
ListRole listRole = new ListRole("owner");
for (NodeInfo ni : netConfig) {
SimpleRole role = new SimpleRole(ni.getName());
contract.registerRole(role);
role.addKeyRecord(new KeyRecord(ni.getPublicKey()));
listRole.addRole(role);
}
listRole.setQuorum(netConfig.size() - 1);
contract.registerRole(listRole);
contract.getStateData().set("net_config", netConfig);
List<KeyRecord> creatorKeys = new ArrayList<>();
for (PrivateKey key : currentConfigKeys) {
creatorKeys.add(new KeyRecord(key.getPublicKey()));
contract.addSignerKey(key);
}
contract.setCreator(creatorKeys);
contract.seal();
return contract;
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class MainTest method checkRestartUDP.
@Test
public void checkRestartUDP() throws Exception {
List<Main> mm = new ArrayList<>();
for (int i = 0; i < 3; i++) {
mm.add(createMain("node" + (i + 1), false));
}
Main main = mm.get(0);
PrivateKey myKey = TestKeys.privateKey(3);
Client client = null;
try {
client = new Client(myKey, main.myInfo, null);
} catch (Exception e) {
System.out.println("prepareClient exception: " + e.toString());
}
Contract testContract = new Contract(myKey);
for (int i = 0; i < 10; i++) {
Contract nc = new Contract(myKey);
testContract.addNewItems(nc);
}
testContract.seal();
assertTrue(testContract.isOk());
Parcel parcel = createParcelWithFreshTU(client, testContract, Do.listOf(myKey));
client.registerParcel(parcel.pack());
System.out.println(">> before restart state: " + client.getState(parcel.getPayloadContract().getId()));
System.out.println(">> before restart state: " + client.getState(parcel.getPayloadContract().getNew().get(0).getId()));
main.restartUDPAdapter();
ItemResult itemResult = client.getState(parcel.getPayloadContract().getId());
ItemResult itemResult2 = client.getState(parcel.getPayloadContract().getNew().get(0).getId());
System.out.println(">> after restart state: " + itemResult + " and new " + itemResult2);
while (itemResult.state.isPending()) {
Thread.currentThread().sleep(100);
itemResult = client.getState(parcel.getPayloadContract().getId());
System.out.println(">> wait result: " + itemResult);
}
itemResult2 = client.getState(parcel.getPayloadContract().getNew().get(0).getId());
assertEquals(ItemState.APPROVED, itemResult.state);
assertEquals(ItemState.APPROVED, itemResult2.state);
mm.forEach(x -> x.shutdown());
}
use of com.icodici.crypto.PrivateKey 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);
}
}
Aggregations