Search in sources :

Example 1 with TransactionPack

use of com.icodici.universa.contract.TransactionPack 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 2 with TransactionPack

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

the class WalletTest method shouldTransferAmountFromCoupleContracts.

@Test
public void shouldTransferAmountFromCoupleContracts() throws Exception {
    Decimal valueToSend = new Decimal(70);
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(50, 45));
    List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount);
    assertEquals(1, wallets.size());
    assertEquals(2, wallets.get(0).getContracts().size());
    // gonna send 70 but I have 2 contracts (50, 45)
    Wallet wallet = wallets.get(0);
    Contract contract = wallet.buildContractWithValue(FIELD_NAME, valueToSend);
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    assertEquals(1, wallet.getContracts().size());
    sealCheckTrace(contract, true);
    TransactionPack tp = contract.getTransactionPack();
    Contract contractToSend = new Contract(contract.seal(), tp);
    contractToSend.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contractToSend, true);
    assertEquals(valueToSend.intValue(), contractToSend.getStateData().getIntOrThrow(FIELD_NAME));
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 3 with TransactionPack

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

the class WalletTest method shouldTransferSumOf7.

@Test
public void shouldTransferSumOf7() throws Exception {
    Decimal valueToSend = new Decimal(280);
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60));
    List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount);
    assertEquals(1, wallets.size());
    assertEquals(12, wallets.get(0).getContracts().size());
    Wallet wallet = wallets.get(0);
    Contract contract = wallet.buildContractWithValue(FIELD_NAME, valueToSend);
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contract, true);
    assertEquals(6, wallet.getContracts().size());
    TransactionPack tp = contract.getTransactionPack();
    Contract contractToSend = new Contract(contract.seal(), tp);
    contractToSend.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contractToSend, true);
    assertEquals(valueToSend.intValue(), contractToSend.getStateData().getIntOrThrow(FIELD_NAME));
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 4 with TransactionPack

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

the class WalletTest method shouldTransferTheSameValue.

@Test
public void shouldTransferTheSameValue() throws Exception {
    Decimal valueToSend = new Decimal(5);
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(5));
    List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount);
    assertEquals(1, wallets.size());
    assertEquals(1, wallets.get(0).getContracts().size());
    Wallet wallet = wallets.get(0);
    Contract contract = wallet.buildContractWithValue(FIELD_NAME, valueToSend);
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contract, true);
    assertEquals(0, wallet.getContracts().size());
    TransactionPack tp = contract.getTransactionPack();
    Contract contractToSend = new Contract(contract.seal(), tp);
    contractToSend.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contractToSend, true);
    assertEquals(valueToSend.intValue(), contractToSend.getStateData().getIntOrThrow(FIELD_NAME));
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 5 with TransactionPack

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

the class CLIMain method checkFile.

private static void checkFile(File f) {
    try {
        TransactionPack tp = TransactionPack.unpack(Do.read(f), true);
        if (tp.isReconstructed()) {
            report("file " + f + " is a single contract");
        } else {
            report("file " + f + " is a transaction pack");
        }
        System.out.println();
        checkContract(tp.getContract());
    } catch (Quantiser.QuantiserException e) {
        addError("QUANTIZER_COST_LIMIT", f.getPath(), e.toString());
    } catch (IOException e) {
        addError("READ_ERROR", f.getPath(), e.toString());
    } catch (Exception e) {
        addError("UNKNOWN_ERROR", f.getPath(), e.toString());
    }
}
Also used : Quantiser(com.icodici.universa.node2.Quantiser) BackingStoreException(java.util.prefs.BackingStoreException) OptionException(joptsimple.OptionException) TransactionPack(com.icodici.universa.contract.TransactionPack)

Aggregations

TransactionPack (com.icodici.universa.contract.TransactionPack)8 Contract (com.icodici.universa.contract.Contract)6 Test (org.junit.Test)6 Decimal (com.icodici.universa.Decimal)5 KeyAddress (com.icodici.crypto.KeyAddress)1 PrivateKey (com.icodici.crypto.PrivateKey)1 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)1 Quantiser (com.icodici.universa.node2.Quantiser)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 BackingStoreException (java.util.prefs.BackingStoreException)1 OptionException (joptsimple.OptionException)1 Binder (net.sergeych.tools.Binder)1