Search in sources :

Example 6 with Db

use of com.icodici.db.Db in project universa by UniversaBlockchain.

the class MainTest method testPermanetApiGetBodyResync.

@Test
public void testPermanetApiGetBodyResync() throws Exception {
    // init network in permanet mode
    List<Main> mm = new ArrayList<>();
    for (int i = 0; i < 4; i++) mm.add(createMain("node" + (i + 1), "_permanet", false));
    Main main = mm.get(0);
    main.config.setIsFreeRegistrationsAllowedFromYaml(true);
    main.config.getKeysWhiteList().add(TestKeys.publicKey(20));
    Client client = new Client(TestKeys.privateKey(20), main.myInfo, null);
    assertTrue(main.config.isPermanetMode());
    for (int i = 1; i < 4; i++) assertTrue(mm.get(i).config.isPermanetMode());
    // try cancel permanet mode
    main.config.setPermanetMode(false);
    assertTrue(main.config.isPermanetMode());
    Set<PrivateKey> privateKeys = new HashSet<>(asList(TestKeys.privateKey(0)));
    Set<PublicKey> publicKeys = new HashSet<>();
    for (PrivateKey pk : privateKeys) {
        publicKeys.add(pk.getPublicKey());
    }
    Contract parcelContract = Contract.fromDslFile(ROOT_PATH + "LamborghiniOwnership.yml");
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey"));
    parcelContract.setOwnerKeys(TestKeys.privateKey(1).getPublicKey());
    parcelContract.addSignerKey(manufacturePrivateKey);
    parcelContract.seal();
    parcelContract.check();
    parcelContract.traceErrors();
    Parcel parcel = createParcelWithFreshU(client, parcelContract, privateKeys);
    client.registerParcelWithState(parcel.pack(), 8000);
    ItemResult itemResult = client.getState(parcel.getPayloadContract().getId());
    System.out.println("root : " + itemResult);
    assertEquals(ItemState.APPROVED, itemResult.state);
    Thread.sleep(2000);
    HashId origin = parcelContract.getId();
    // delete body from node db
    Db db = ((PostgresLedger) main.node.getLedger()).getDb();
    try (PreparedStatement statement = db.statement("delete from kept_items where origin = ?")) {
        statement.setBytes(1, origin.getDigest());
        db.updateWithStatement(statement);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    // check getKeepingItem for null
    assertNull(main.node.getLedger().getKeepingItem(parcelContract.getId()));
    // check getBody
    byte[] keeping = client.getBody(parcel.getPayloadContract().getId());
    assertNotNull(keeping);
    assertTrue(Arrays.equals(parcel.getPayloadContract().getPackedTransaction(), keeping));
    // create revision
    Contract revisionContract = parcelContract.createRevision(manufacturePrivateKey);
    revisionContract.setOwnerKeys(TestKeys.privateKey(2).getPublicKey());
    revisionContract.addSignerKey(TestKeys.privateKey(1));
    revisionContract.seal();
    revisionContract.check();
    revisionContract.traceErrors();
    parcel = createParcelWithFreshU(client, revisionContract, privateKeys);
    client.registerParcelWithState(parcel.pack(), 8000);
    itemResult = client.getState(parcel.getPayloadContract().getId());
    System.out.println("revision : " + itemResult);
    assertEquals(ItemState.APPROVED, itemResult.state);
    itemResult = client.getState(parcelContract.getId());
    System.out.println("root : " + itemResult);
    assertEquals(ItemState.REVOKED, itemResult.state);
    Thread.sleep(2000);
    // delete bodies from node db
    try (PreparedStatement statement = db.statement("delete from kept_items where origin = ?")) {
        statement.setBytes(1, origin.getDigest());
        db.updateWithStatement(statement);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    // check getKeepingItem for nulls
    assertNull(main.node.getLedger().getKeepingItem(parcelContract.getId()));
    assertNull(main.node.getLedger().getKeepingItem(revisionContract.getId()));
    // check getBody
    byte[] keeping_root = client.getBody(parcelContract.getId());
    byte[] keeping_revision = client.getBody(revisionContract.getId());
    assertNotNull(keeping_root);
    assertNotNull(keeping_revision);
    assertTrue(Arrays.equals(parcelContract.getPackedTransaction(), keeping_root));
    assertTrue(Arrays.equals(revisionContract.getPackedTransaction(), keeping_revision));
    Thread.sleep(2000);
    mm.forEach(x -> x.shutdown());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) PreparedStatement(java.sql.PreparedStatement) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) PooledDb(com.icodici.db.PooledDb) Db(com.icodici.db.Db) Test(org.junit.Test)

Aggregations

Db (com.icodici.db.Db)6 Test (org.junit.Test)6 PrivateKey (com.icodici.crypto.PrivateKey)5 PublicKey (com.icodici.crypto.PublicKey)5 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)5 PooledDb (com.icodici.db.PooledDb)2 HashId (com.icodici.universa.HashId)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Ignore (org.junit.Ignore)1