Search in sources :

Example 6 with ErrorRecord

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

the class ClientHTTPServer method itemResultOfError.

private ItemResult itemResultOfError(Errors error, String object, String message) {
    Binder binder = new Binder();
    binder.put("state", ItemState.UNDEFINED.name());
    binder.put("haveCopy", false);
    binder.put("createdAt", new Date());
    binder.put("expiresAt", new Date());
    ArrayList<ErrorRecord> errorRecords = new ArrayList<>();
    errorRecords.add(new ErrorRecord(error, object, message));
    binder.put("errors", errorRecords);
    return new ItemResult(binder);
}
Also used : Binder(net.sergeych.tools.Binder) ItemResult(com.icodici.universa.node.ItemResult) ArrayList(java.util.ArrayList) Date(java.util.Date) ErrorRecord(com.icodici.universa.ErrorRecord)

Example 7 with ErrorRecord

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

the class ContractTest method checkCreatingRootContract.

@Test
public void checkCreatingRootContract() throws Exception {
    Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    boolean ok = c.check();
    assertFalse(ok);
    List<ErrorRecord> errors = c.getErrors();
    // It is just ok but not signed
    assertEquals(1, errors.size());
    assertEquals(errors.get(0).getError(), Errors.NOT_SIGNED);
    c.addSignerKeyFromFile(rootPath + "_xer0yfe2nn1xthc.private.unikey");
    c.getErrors().clear();
    ok = c.check();
    if (errors.isEmpty()) {
        assertTrue(ok);
        assertTrue(c.isOk());
    } else {
        for (ErrorRecord e : errors) {
            System.out.println(e);
            fail("errors in contract");
        }
    }
    assertTrue(c.check());
    Files.write(Paths.get(rootPath + "simple_root_contract.unc"), c.seal());
    Yaml yaml = new Yaml();
    Files.write(Paths.get(rootPath + "simple_root_contract.raw.yaml"), yaml.dump(DefaultBiMapper.serialize(c)).getBytes());
}
Also used : Yaml(org.yaml.snakeyaml.Yaml) ErrorRecord(com.icodici.universa.ErrorRecord) Test(org.junit.Test)

Example 8 with ErrorRecord

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

the class PermissionsTest method changeOwnerWithReference.

@Test
public void changeOwnerWithReference() throws Exception {
    Set<PrivateKey> stepaPrivateKeys = new HashSet<>();
    stepaPrivateKeys.add(new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")));
    Set<PublicKey> stepaPublicKeys = new HashSet<>();
    for (PrivateKey pk : stepaPrivateKeys) {
        stepaPublicKeys.add(pk.getPublicKey());
    }
    Set<String> references = new HashSet<>();
    references.add("certification_contract");
    Contract c = Contract.fromDslFile(rootPath + "NotaryWithReferenceDSLTemplate.yml");
    c.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    Role r = c.getPermissions().getFirst("change_owner").getRole();
    assertThat(r, is(instanceOf(ListRole.class)));
    assertFalse(r.isAllowedFor(stepaPublicKeys, null));
    assertTrue(r.isAllowedFor(stepaPublicKeys, references));
    System.out.println("Owner now :" + c.getOwner());
    System.out.println("change owner permission :" + c.getPermissions().get("change_owner"));
    c.seal();
    c.check();
    c.traceErrors();
    assertTrue(c.isOk());
    assertEquals(c, (c.getPermissions().getFirst("change_owner").getRole()).getContract());
    // Bad contract change: owner has no right to change owner ;)
    Contract c1 = c.createRevision(TestKeys.privateKey(0));
    c1.setOwnerKey(ownerKey2);
    assertNotEquals(c.getOwner(), c1.getOwner());
    c1.seal();
    c1.check();
    c1.traceErrors();
    assertEquals(1, c1.getErrors().size());
    ErrorRecord error = c1.getErrors().get(0);
    assertEquals(Errors.FORBIDDEN, error.getError());
    // bad contract change: good key but bad reference
    Contract c2 = c.createRevision(stepaPrivateKeys);
    c2.setOwnerKey(ownerKey3);
    assertEquals(c2, c2.getPermissions().getFirst("change_owner").getRole().getContract());
    assertNotEquals(c.getOwner(), c2.getOwner());
    c2.seal();
    c2.check();
    c2.traceErrors();
    assertEquals(1, c2.getErrors().size());
    error = c2.getErrors().get(0);
    assertEquals(Errors.FORBIDDEN, error.getError());
    // good contract change: creator is an owner
    Contract c3 = c.createRevision(stepaPrivateKeys);
    c3.setOwnerKey(ownerKey3);
    Reference ref = new Reference();
    ref.name = "certification_contract";
    ref.type = Reference.TYPE_EXISTING;
    ref.addMatchingItem(new Contract());
    c3.getReferences().put(ref.name, ref);
    assertEquals(c3, c3.getPermissions().getFirst("change_owner").getRole().getContract());
    assertNotEquals(c.getOwner(), c3.getOwner());
    sealCheckTrace(c3, true);
}
Also used : ListRole(com.icodici.universa.contract.roles.ListRole) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) Role(com.icodici.universa.contract.roles.Role) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) HashSet(java.util.HashSet) ErrorRecord(com.icodici.universa.ErrorRecord) Test(org.junit.Test)

Aggregations

ErrorRecord (com.icodici.universa.ErrorRecord)8 Test (org.junit.Test)5 PrivateKey (com.icodici.crypto.PrivateKey)3 PublicKey (com.icodici.crypto.PublicKey)3 ListRole (com.icodici.universa.contract.roles.ListRole)3 Role (com.icodici.universa.contract.roles.Role)3 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)3 HashSet (java.util.HashSet)3 Binder (net.sergeych.tools.Binder)3 ItemResult (com.icodici.universa.node.ItemResult)2 IOException (java.io.IOException)2 EncryptionError (com.icodici.crypto.EncryptionError)1 Approvable (com.icodici.universa.Approvable)1 HashId (com.icodici.universa.HashId)1 Contract (com.icodici.universa.contract.Contract)1 Parcel (com.icodici.universa.contract.Parcel)1 ItemState (com.icodici.universa.node.ItemState)1 com.icodici.universa.node2 (com.icodici.universa.node2)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1