Search in sources :

Example 6 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class MapSerializer method toMap.

/**
 * Serialize some object to Map, using aliases if any.
 *
 * @param object
 *         what to serialize
 * @param includeClass
 *         if true, will add "__class" key with object's class name or alias.
 *
 * @return serialized content.
 *
 * @throws net.sergeych.map_serializer.MapSerializer.CantProcessException
 */
public Binder toMap(Object object, boolean includeClass) throws CantProcessException {
    Binder map = new Binder();
    Class<?> cls = object.getClass();
    for (Field f : cls.getDeclaredFields()) {
        int mods = f.getModifiers();
        if (!Modifier.isTransient(mods)) {
            try {
                f.setAccessible(true);
                SerialName sn = f.getAnnotation(SerialName.class);
                map.put(sn == null ? camelToSnakeCase(f.getName()) : sn.name(), f.get(object));
            } catch (Exception e) {
                logicError(e);
            }
        }
        if (includeClass)
            map.put("__class", className(cls));
    }
    return map;
}
Also used : Binder(net.sergeych.tools.Binder) Field(java.lang.reflect.Field) IOException(java.io.IOException)

Example 7 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class StructureDescriptor method unpack.

/**
 * Unpack packed binary data staring from a given offset
 *
 * @param data
 * @param fromOffset
 *w
 * @return Binder with map of unpacked fields
 */
public Binder unpack(byte[] data, int fromOffset) {
    Binder result = new Binder();
    fields.forEach((key, fd) -> result.put(key, fd.unpack(data, fromOffset)));
    return result;
}
Also used : Binder(net.sergeych.tools.Binder)

Example 8 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class MainTest method createNetConfigContract.

private Contract createNetConfigContract(List<NodeInfo> netConfig, PrivateKey issuerKey) throws IOException {
    Contract contract = new Contract();
    contract.setIssuerKeys(issuerKey.getPublicKey());
    contract.registerRole(new RoleLink("creator", "issuer"));
    ListRole listRole = new ListRole("owner");
    for (NodeInfo ni : netConfig) {
        SimpleRole role = new SimpleRole(ni.getName());
        contract.registerRole(role);
        role.addKeyRecord(new KeyRecord(ni.getPublicKey()));
        listRole.addRole(role);
    }
    listRole.setQuorum(netConfig.size() - 1);
    contract.registerRole(listRole);
    RoleLink ownerLink = new RoleLink("ownerlink", "owner");
    ChangeOwnerPermission changeOwnerPermission = new ChangeOwnerPermission(ownerLink);
    HashMap<String, Object> fieldsMap = new HashMap<>();
    fieldsMap.put("net_config", null);
    Binder modifyDataParams = Binder.of("fields", fieldsMap);
    ModifyDataPermission modifyDataPermission = new ModifyDataPermission(ownerLink, modifyDataParams);
    contract.addPermission(changeOwnerPermission);
    contract.addPermission(modifyDataPermission);
    contract.setExpiresAt(ZonedDateTime.now().plusYears(40));
    contract.getStateData().set("net_config", netConfig);
    contract.addSignerKey(issuerKey);
    contract.seal();
    return contract;
}
Also used : RoleLink(com.icodici.universa.contract.roles.RoleLink) ModifyDataPermission(com.icodici.universa.contract.permissions.ModifyDataPermission) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Binder(net.sergeych.tools.Binder) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) ListRole(com.icodici.universa.contract.roles.ListRole)

Example 9 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class Node2LocalNetworkTest method initTestSet.

private static void initTestSet(int posCons, int negCons) throws Exception {
    System.out.println("init test set... ");
    nodesMap_s = new HashMap<>();
    networks_s = new ArrayList<>();
    config_s = new Config();
    config_s.setPositiveConsensus(posCons);
    config_s.setNegativeConsensus(negCons);
    config_s.setResyncBreakConsensus(2);
    config_s.setTransactionUnitsIssuerKeyData(Bytes.fromHex("1E 08 1C 01 00 01 C4 00 01 B9 C7 CB 1B BA 3C 30 80 D0 8B 29 54 95 61 41 39 9E C6 BB 15 56 78 B8 72 DC 97 58 9F 83 8E A0 B7 98 9E BB A9 1D 45 A1 6F 27 2F 61 E0 26 78 D4 9D A9 C2 2F 29 CB B6 F7 9F 97 60 F3 03 ED 5C 58 27 27 63 3B D3 32 B5 82 6A FB 54 EA 26 14 E9 17 B6 4C 5D 60 F7 49 FB E3 2F 26 52 16 04 A6 5E 6E 78 D1 78 85 4D CD 7B 71 EB 2B FE 31 39 E9 E0 24 4F 58 3A 1D AE 1B DA 41 CA 8C 42 2B 19 35 4B 11 2E 45 02 AD AA A2 55 45 33 39 A9 FD D1 F3 1F FA FE 54 4C 2E EE F1 75 C9 B4 1A 27 5C E9 C0 42 4D 08 AD 3E A2 88 99 A3 A2 9F 70 9E 93 A3 DF 1C 75 E0 19 AB 1F E0 82 4D FF 24 DA 5D B4 22 A0 3C A7 79 61 41 FD B7 02 5C F9 74 6F 2C FE 9A DD 36 44 98 A2 37 67 15 28 E9 81 AC 40 CE EF 05 AA 9E 36 8F 56 DA 97 10 E4 10 6A 32 46 16 D0 3B 6F EF 80 41 F3 CC DA 14 74 D1 BF 63 AC 28 E0 F1 04 69 63 F7"));
    config_s.getKeysWhiteList().add(config_s.getTransactionUnitsIssuerKey());
    // config_s.setPollTime(Duration.ofMillis(2500));
    // config_s.setConsensusReceivedCheckTime(Duration.ofMillis(2500));
    // config_s.setResyncTime(Duration.ofMillis(2500));
    Properties properties = new Properties();
    File file = new File(CONFIG_2_PATH + "config/config.yaml");
    Yaml yaml = new Yaml();
    Binder settings = new Binder();
    if (file.exists())
        settings = Binder.from(yaml.load(new FileReader(file)));
    // properties.setProperty("database", settings.getStringOrThrow("database"));
    /* test loading onfig should be in other place
        NetConfig ncNet = new NetConfig(CONFIG_2_PATH+"config/nodes");
        List<NodeConsumer> netNodes = ncNet.toList();
        */
    nc_s = new NetConfig();
    for (int i = 0; i < NODES; i++) {
        int offset = 7100 + 10 * i;
        NodeInfo info = new NodeInfo(getNodeKey(i).getPublicKey(), i, "testnode_" + i, "localhost", offset + 3, offset, offset + 2);
        nc_s.addNode(info);
    }
    for (int i = 0; i < NODES; i++) {
        NodeInfo info = nc_s.getInfo(i);
        System.out.println("set up node " + info);
        TestLocalNetwork ln = new TestLocalNetwork(nc_s, info, getNodeKey(i));
        ln.setNodes(nodesMap_s);
        // ledger = new SqliteLedger("jdbc:sqlite:testledger" + "_t" + i);
        Ledger ledger = new PostgresLedger(PostgresLedgerTest.CONNECTION_STRING + "_t" + i, properties);
        Node n = new Node(config_s, info, ledger, ln);
        nodesMap_s.put(info, n);
        networks_s.add(ln);
        if (i == 0) {
            ledger_s = ledger;
            network_s = ln;
        }
    }
    node_s = nodesMap_s.values().iterator().next();
    nodes_s = new ArrayList<>();
    for (int i = 0; i < NODES; i++) {
        nodes_s.add(nodesMap_s.get(nc_s.getInfo(i)));
    }
// System.out.println("waiting for sanitation... ");
// for(Node n : nodes_s) {
// if(n.isSanitating())
// n.sanitationFinished.await();
// }
// System.out.println("sanitation finished!");
}
Also used : Yaml(org.yaml.snakeyaml.Yaml) Binder(net.sergeych.tools.Binder) FileReader(java.io.FileReader) File(java.io.File)

Example 10 with Binder

use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.

the class ContractTest method serializeToBinder.

@Test
public void serializeToBinder() throws Exception {
    Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
    Binder b = BossBiMapper.serialize(c);
    Contract c1 = DefaultBiMapper.deserialize(b);
    // System.out.println(yaml.dump(b));
    // System.out.println(yaml.dump(c1.serializeToBinder()));
    assertProperSimpleRootContract(c1);
    Contract c2 = c.copy();
    assertProperSimpleRootContract(c2);
}
Also used : Binder(net.sergeych.tools.Binder) Test(org.junit.Test)

Aggregations

Binder (net.sergeych.tools.Binder)136 Test (org.junit.Test)67 PrivateKey (com.icodici.crypto.PrivateKey)21 Contract (com.icodici.universa.contract.Contract)14 PublicKey (com.icodici.crypto.PublicKey)13 IOException (java.io.IOException)11 KeyRecord (com.icodici.universa.contract.KeyRecord)10 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)10 Yaml (org.yaml.snakeyaml.Yaml)9 Bytes (net.sergeych.utils.Bytes)8 Decimal (com.icodici.universa.Decimal)7 ListRole (com.icodici.universa.contract.roles.ListRole)6 NonNull (org.checkerframework.checker.nullness.qual.NonNull)5 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 XStream (com.thoughtworks.xstream.XStream)4 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)4 List (java.util.List)4 RoleLink (com.icodici.universa.contract.roles.RoleLink)3 ItemResult (com.icodici.universa.node.ItemResult)3