Search in sources :

Example 31 with Decimal

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

the class ContractTestBase method createCoinWithAmount.

protected Contract createCoinWithAmount(String amount, String fieldName) throws Exception {
    Contract contract = createCoin();
    contract.getStateData().set(fieldName, new Decimal(amount));
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contract, true);
    return contract;
}
Also used : Decimal(com.icodici.universa.Decimal)

Example 32 with Decimal

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

the class ContractsServiceTest method goodShare.

@Test
public void goodShare() throws Exception {
    Set<PrivateKey> martyPrivateKeys = new HashSet<>();
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    Set<PublicKey> martyPublicKeys = new HashSet<>();
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    martyPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/marty_mcfly.private.unikey")));
    stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
    for (PrivateKey pk : stepaPrivateKeys) stepaPublicKeys.add(pk.getPublicKey());
    for (PrivateKey pk : martyPrivateKeys) martyPublicKeys.add(pk.getPublicKey());
    Contract notaryContract = ContractsService.createShareContract(martyPrivateKeys, stepaPublicKeys, "100");
    notaryContract.check();
    notaryContract.traceErrors();
    assertTrue(notaryContract.isOk());
    assertTrue(notaryContract.getOwner().isAllowedForKeys(stepaPublicKeys));
    assertTrue(notaryContract.getIssuer().isAllowedForKeys(martyPrivateKeys));
    assertTrue(notaryContract.getCreator().isAllowedForKeys(martyPrivateKeys));
    assertFalse(notaryContract.getOwner().isAllowedForKeys(martyPrivateKeys));
    assertFalse(notaryContract.getIssuer().isAllowedForKeys(stepaPublicKeys));
    assertFalse(notaryContract.getCreator().isAllowedForKeys(stepaPublicKeys));
    assertTrue(notaryContract.getExpiresAt().isAfter(ZonedDateTime.now().plusMonths(3)));
    assertTrue(notaryContract.getCreatedAt().isBefore(ZonedDateTime.now()));
    assertEquals(InnerContractsService.getDecimalField(notaryContract, "amount"), new Decimal(100));
    assertEquals(notaryContract.getPermissions().get("split_join").size(), 1);
    Binder splitJoinParams = notaryContract.getPermissions().get("split_join").iterator().next().getParams();
    assertEquals(splitJoinParams.get("min_value"), 1);
    assertEquals(splitJoinParams.get("min_unit"), 1);
    assertEquals(splitJoinParams.get("field_name"), "amount");
    assertTrue(splitJoinParams.get("join_match_fields") instanceof List);
    assertEquals(((List) splitJoinParams.get("join_match_fields")).get(0), "state.origin");
    assertTrue(notaryContract.isPermitted("revoke", stepaPublicKeys));
    assertTrue(notaryContract.isPermitted("revoke", martyPublicKeys));
    assertTrue(notaryContract.isPermitted("change_owner", stepaPublicKeys));
    assertFalse(notaryContract.isPermitted("change_owner", martyPublicKeys));
    assertTrue(notaryContract.isPermitted("split_join", stepaPublicKeys));
    assertFalse(notaryContract.isPermitted("split_join", martyPublicKeys));
}
Also used : Binder(net.sergeych.tools.Binder) PrivateKey(com.icodici.crypto.PrivateKey) Decimal(com.icodici.universa.Decimal) PublicKey(com.icodici.crypto.PublicKey) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 33 with Decimal

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

the class SplitJoinPermissionTest method shouldSplitWithChangedOwnerAndNewValueSerialize.

@Test
public void shouldSplitWithChangedOwnerAndNewValueSerialize() throws Exception {
    int defaultValue = 1000000;
    int valueForSplit = 85;
    Contract c = createCoin();
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Binder d = c.getStateData();
    sealCheckTrace(c, true);
    Contract c2 = c.splitValue(FIELD_NAME, new Decimal(valueForSplit));
    c2.addSignerKey(ownerKey2);
    Binder d2 = c2.getStateData();
    sealCheckTrace(c2, true);
    assertEquals(defaultValue - valueForSplit, d.getIntOrThrow(FIELD_NAME));
    assertEquals(valueForSplit, d2.getIntOrThrow(FIELD_NAME));
}
Also used : Binder(net.sergeych.tools.Binder) Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 34 with Decimal

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

the class SplitJoinPermissionTest method shouldNotJoinWithWrongParent.

@Test
public void shouldNotJoinWithWrongParent() throws Exception {
    Contract c = createCoin();
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Contract c1 = c.splitValue(FIELD_NAME, new Decimal(1));
    Contract.ContractDev dev = c1.new ContractDev(c1);
    // Check after split.
    sealCheckTrace(c, true);
    // Set wrong parent
    HashId parent = HashId.withDigest(Do.randomNegativeBytes(64));
    HashId origin = HashId.withDigest(Do.randomNegativeBytes(64));
    dev.setParent(parent);
    dev.setOrigin(origin);
    c.getRevokingItems().add(dev.getContract());
    sealCheckTrace(c, false);
}
Also used : HashId(com.icodici.universa.HashId) Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 35 with Decimal

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

the class SplitJoinPermissionTest method splitJoinHasNotEnoughSumRevoking.

@Test
public void splitJoinHasNotEnoughSumRevoking() throws Exception {
    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);
    // set wrong revoking with the same amount
    root = root.createRevision(ownerKey2);
    root.seal();
    // c1 split 45 5
    c1 = c50_1.createRevision(ownerKey2);
    c1.seal();
    Contract c5 = c1.splitValue(FIELD_NAME, new Decimal(5));
    sealCheckTrace(c5, true);
    // coin amount: 200 (revoking: 100 and 50 and 5)
    root.getStateData().set(FIELD_NAME, new Decimal(200));
    root.addRevokingItems(c50_1);
    root.addRevokingItems(c5);
    sealCheckTrace(root, false);
}
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