Search in sources :

Example 36 with Decimal

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

the class SplitJoinPermissionTest method testJoinSum.

@Test
public void testJoinSum() throws Exception {
    Contract c = createCoin();
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Binder d = c.getStateData();
    int a = 1000000;
    assertEquals(a, d.getIntOrThrow(FIELD_NAME));
    c.seal();
    c.check();
    c.traceErrors();
    assertTrue(c.check());
    // 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);
    Contract c3 = c1.createRevision(ownerKey2);
    c3.getRevokingItems().add(c2);
    c3.getStateData().set(FIELD_NAME, new Decimal(a));
    sealCheckTrace(c3, true);
}
Also used : Binder(net.sergeych.tools.Binder) Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 37 with Decimal

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

the class SplitJoinPermissionTest method cheatCreateValue.

@Test
public void cheatCreateValue() throws Exception {
    Contract c = createCoin();
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Binder d = c.getStateData();
    int a = 1000000;
    assertEquals(a, d.getIntOrThrow(FIELD_NAME));
    c.seal();
    assertTrue(c.check());
    Contract c1 = c.createRevision(ownerKey2);
    // Good split but wrong amount
    Contract c2 = c1.splitValue(FIELD_NAME, new Decimal(500));
    assertEquals(a - 500, c1.getStateData().getIntOrThrow(FIELD_NAME));
    assertEquals(500, c2.getStateData().getIntOrThrow(FIELD_NAME));
    c2.getStateData().set(FIELD_NAME, "500.00000001");
    sealCheckTrace(c1, false);
}
Also used : Binder(net.sergeych.tools.Binder) Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 38 with Decimal

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

the class ContractsService method createSplit.

/**
 * Implementing split procedure for token-type contracts.
 * <br><br>
 * Service create new revision of given contract, split it to a pair of contracts with split amount.
 * <br><br>
 * Given contract should have splitjoin permission for given keys.
 * <br><br>
 * @param c is contract should split be
 * @param amount is value that should be split from given contract
 * @param fieldName is name of field that should be split
 * @param keys is keys from owner of c
 * @param andSetCreator if true set owners as creator in both contarcts
 * @return working contract that should be register in the Universa to finish procedure.
 */
public static synchronized Contract createSplit(Contract c, long amount, String fieldName, Set<PrivateKey> keys, boolean andSetCreator) {
    Contract splitFrom = c.createRevision();
    Contract splitTo = splitFrom.splitValue(fieldName, new Decimal(amount));
    for (PrivateKey key : keys) {
        splitTo.addSignerKey(key);
    }
    if (andSetCreator) {
        splitTo.createRole("creator", splitTo.getRole("owner"));
        splitFrom.createRole("creator", splitFrom.getRole("owner"));
    }
    splitTo.seal();
    splitFrom.seal();
    return splitFrom;
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) Decimal(com.icodici.universa.Decimal)

Example 39 with Decimal

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

the class SplitJoinPermission method initFromParams.

protected void initFromParams() {
    fieldName = params.getStringOrThrow("field_name");
    minValue = new Decimal(params.getString("min_value", "0"));
    minUnit = new Decimal(params.getString("min_unit", "1e-9"));
    mergeFields = params.getList("join_match_fields", asList("state.origin"));
}
Also used : Decimal(com.icodici.universa.Decimal)

Example 40 with Decimal

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

the class SplitJoinPermission method checkChanges.

/**
 * Check and remove changes that this permission allow. Note that it does not add errors itself, to allow using
 * several such permission, from which some may allow the change, and some may not. If a check will add error,
 * though, it will prevent subsequent permission objects to allow the change.
 *
 * @param contract     source (valid) contract
 * @param changed is contract for checking
 * @param stateChanges map of changes, see {@link Delta} for details
 */
@Override
public void checkChanges(Contract contract, Contract changed, Map<String, Delta> stateChanges) {
    MapDelta<String, Binder, Binder> dataChanges = (MapDelta<String, Binder, Binder>) stateChanges.get("data");
    if (dataChanges == null)
        return;
    Delta delta = dataChanges.getChange(fieldName);
    if (delta != null) {
        if (!(delta instanceof ChangedItem))
            return;
        try {
            Decimal oldValue = new Decimal(delta.oldValue().toString());
            Decimal newValue = new Decimal(delta.newValue().toString());
            int cmp = oldValue.compareTo(newValue);
            if (cmp > 0)
                checkSplit(changed, dataChanges, oldValue, newValue);
            else if (cmp < 0)
                checkMerge(changed, dataChanges, newValue);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
}
Also used : Binder(net.sergeych.tools.Binder) Decimal(com.icodici.universa.Decimal) Delta(net.sergeych.diff.Delta) MapDelta(net.sergeych.diff.MapDelta) ChangedItem(net.sergeych.diff.ChangedItem) MapDelta(net.sergeych.diff.MapDelta)

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