Search in sources :

Example 21 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class SplitJoinPermissionTest method shouldNotSplitWithNegativeCount.

@Test
public void shouldNotSplitWithNegativeCount() throws Exception {
    Contract c = createCoin();
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(c, true);
    try {
        c.split(-1);
        fail("Expected exception to be thrown.");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().equalsIgnoreCase("split: count should be > 0"));
    }
    try {
        c.split(0);
        fail("Expected exception to be thrown.");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().equalsIgnoreCase("split: count should be > 0"));
    }
}
Also used : Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 22 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class SplitJoinPermissionTest method testProperSum.

@Test
public void testProperSum() throws Exception {
    Contract c = createCoin();
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Binder d = c.getStateData();
    int a = 1000000;
    assertEquals(a, d.getIntOrThrow(FIELD_NAME));
    sealCheckTrace(c, true);
    // bad split: no changes
    Contract c1 = c.createRevision(ownerKey2);
    sealCheckTrace(c1, false);
    // Good split
    Contract c2 = c1.splitValue(FIELD_NAME, new Decimal(500));
    assertEquals(a - 500, c1.getStateData().getIntOrThrow(FIELD_NAME));
    assertEquals(500, c2.getStateData().getIntOrThrow(FIELD_NAME));
    c1.getErrors().clear();
    sealCheckTrace(c1, true);
    // and it should be the same after seriazling to the transaction pack
    TransactionPack tp = new TransactionPack(c1);
    // tp.trace();
    TransactionPack tp2 = TransactionPack.unpack(new TransactionPack(c1).pack());
    // tp2.trace();
    Contract restored = tp2.getContract();
    restored.check();
    restored.traceErrors();
    assertTrue(restored.isOk());
// TODO: check that value can't be just changed
// TODO: check that the sum must be equal
// TODO: check children have different branches
// TODO: check smae branch spoofing
}
Also used : Binder(net.sergeych.tools.Binder) Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 23 with Contract

use of com.icodici.universa.contract.Contract 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);
}
Also used : HashId(com.icodici.universa.HashId) Binder(net.sergeych.tools.Binder) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 24 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class SplitJoinPermissionTest method shouldSplitJoinHasEnoughSumRevoking.

@Test
public void shouldSplitJoinHasEnoughSumRevoking() throws Exception {
    // 2 coins: 1st v: 50 (r: 50 and 50), 2nd v: 50 (r: 50 and 50)
    Contract root = createCoinWithAmount("200", FIELD_NAME);
    Contract c1 = root.splitValue(FIELD_NAME, new Decimal(100));
    sealCheckTrace(c1, true);
    // c1 split 50 50
    c1 = c1.createRevision(ownerKey2);
    c1.seal();
    Contract c50_1 = c1.splitValue(FIELD_NAME, new Decimal(50));
    sealCheckTrace(c50_1, true);
    // good join
    Contract finalC = c50_1.createRevision(ownerKey2);
    finalC.seal();
    finalC.getStateData().set(FIELD_NAME, new Decimal(100));
    finalC.addRevokingItems(c50_1);
    finalC.addRevokingItems(c1);
    sealCheckTrace(finalC, true);
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 25 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class ResearchTest method registerSimpleContract.

@Test
public void registerSimpleContract() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Contract stepaCoins = Contract.fromDslFile(ROOT_PATH + "stepaCoins.yml");
    stepaCoins.addSignerKey(stepaPrivateKeys.iterator().next());
    stepaCoins.seal();
    System.out.println("nodeClient.register(stepaCoins)...");
    ItemResult itemResult = nodeClient.register(stepaCoins.getLastSealedBinary(), 5000);
    System.out.println("nodeClient.register(stepaCoins)... done! itemResult: " + itemResult.state);
    itemResult = nodeClient.getState(stepaCoins.getId());
    System.out.println("nodeClient.getState(stepaCoins): " + itemResult.state);
    assertEquals(ItemState.APPROVED, itemResult.state);
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Contract(com.icodici.universa.contract.Contract) HashSet(java.util.HashSet)

Aggregations

Contract (com.icodici.universa.contract.Contract)131 Test (org.junit.Test)67 Decimal (com.icodici.universa.Decimal)31 PrivateKey (com.icodici.crypto.PrivateKey)24 File (java.io.File)16 AsyncEvent (net.sergeych.tools.AsyncEvent)16 TimeoutException (java.util.concurrent.TimeoutException)14 Binder (net.sergeych.tools.Binder)14 KeyRecord (com.icodici.universa.contract.KeyRecord)13 HashSet (java.util.HashSet)9 Parcel (com.icodici.universa.contract.Parcel)8 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)7 Quantiser (com.icodici.universa.node2.Quantiser)7 PublicKey (com.icodici.crypto.PublicKey)6 TransactionPack (com.icodici.universa.contract.TransactionPack)6 IOException (java.io.IOException)6 BackingStoreException (java.util.prefs.BackingStoreException)6 OptionException (joptsimple.OptionException)6 HashId (com.icodici.universa.HashId)5 Arrays.asList (java.util.Arrays.asList)5