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);
}
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);
}
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;
}
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"));
}
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;
}
}
}
Aggregations