Search in sources :

Example 6 with Decimal

use of com.icodici.universa.Decimal in project universa by UniversaBlockchain.

the class SplitJoinPermissionTest method shouldNotSplitWithAnotherIssuerSerialize.

@Test
public void shouldNotSplitWithAnotherIssuerSerialize() throws Exception {
    Contract c = createCoin();
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(c, true);
    Contract c2 = c.splitValue(FIELD_NAME, new Decimal(50));
    c2.setIssuerKeys(ownerKey1.getPublicKey());
    sealCheckTrace(c2, false);
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 7 with Decimal

use of com.icodici.universa.Decimal in project universa by UniversaBlockchain.

the class SplitJoinPermissionTest method shouldNotJoinWithWrongAmount.

@Test
public void shouldNotJoinWithWrongAmount() throws Exception {
    int amount = 1000000;
    int v = 1;
    Contract c = createCoin();
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(c, true);
    // Split with 1
    Contract c2 = c.splitValue(FIELD_NAME, new Decimal(v));
    assertEquals(amount - v, c.getStateData().getIntOrThrow(FIELD_NAME));
    assertEquals(v, c2.getStateData().getIntOrThrow(FIELD_NAME));
    sealCheckTrace(c2, true);
    Contract c3 = c.createRevision(ownerKey2);
    c3.getRevokingItems().add(c2);
    // Trying to hack the join and get bigger amount
    c3.getStateData().set(FIELD_NAME, new Decimal(v + 1));
    assertEquals(amount - v, c.getStateData().getIntOrThrow(FIELD_NAME));
    assertEquals(v + 1, c3.getStateData().getIntOrThrow(FIELD_NAME));
    sealCheckTrace(c3, false);
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 8 with Decimal

use of com.icodici.universa.Decimal 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 9 with Decimal

use of com.icodici.universa.Decimal 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 10 with Decimal

use of com.icodici.universa.Decimal in project universa by UniversaBlockchain.

the class WalletTest method shouldSplitAndMergeOne.

@Test
public void shouldSplitAndMergeOne() throws Exception {
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(100, 200));
    List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount);
    // want to send 250
    Wallet wallet = wallets.get(0);
    Contract output = wallet.buildContractWithValue("amount", new Decimal(250));
    output.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(output, true);
    Contract contract = wallet.getContracts().get(0);
    sealCheckTrace(contract, true);
    assertEquals("50", contract.getState().getData().get("amount"));
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Aggregations

Decimal (com.icodici.universa.Decimal)41 Contract (com.icodici.universa.contract.Contract)31 Test (org.junit.Test)30 Binder (net.sergeych.tools.Binder)7 TransactionPack (com.icodici.universa.contract.TransactionPack)5 PrivateKey (com.icodici.crypto.PrivateKey)4 Approvable (com.icodici.universa.Approvable)3 PublicKey (com.icodici.crypto.PublicKey)2 HashId (com.icodici.universa.HashId)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Permission (com.icodici.universa.contract.permissions.Permission)1 SplitJoinPermission (com.icodici.universa.contract.permissions.SplitJoinPermission)1 Quantiser (com.icodici.universa.node2.Quantiser)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1