Search in sources :

Example 6 with Node

use of org.tron.common.overlay.discover.Node in project java-tron by tronprotocol.

the class Manager method getHomeNode.

public Node getHomeNode() {
    final Args args = Args.getInstance();
    Set<Node> nodes = this.peersStore.get("home".getBytes());
    if (nodes.size() > 0) {
        return nodes.stream().findFirst().get();
    } else {
        Node node = new Node(new ECKey().getNodeId(), args.getNodeExternalIp(), args.getNodeListenPort());
        nodes.add(node);
        this.peersStore.put("home".getBytes(), nodes);
        return node;
    }
}
Also used : Args(org.tron.core.config.args.Args) Node(org.tron.common.overlay.discover.Node) ECKey(org.tron.common.crypto.ECKey)

Example 7 with Node

use of org.tron.common.overlay.discover.Node in project java-tron by tronprotocol.

the class PeersStore method get.

@Override
public Set<Node> get(byte[] key) {
    Set<Node> nodes = new HashSet<>();
    byte[] value = dbSource.getData(key);
    if (value != null) {
        StringTokenizer st = new StringTokenizer(new String(value), "||");
        while (st.hasMoreElements()) {
            nodes.add(new Node(st.nextToken()));
        }
    }
    return nodes;
}
Also used : StringTokenizer(java.util.StringTokenizer) Node(org.tron.common.overlay.discover.Node) HashSet(java.util.HashSet)

Example 8 with Node

use of org.tron.common.overlay.discover.Node in project java-tron by tronprotocol.

the class Args method nodeActive.

private static List<Node> nodeActive(final com.typesafe.config.Config config) {
    if (!config.hasPath("node.active")) {
        return Collections.EMPTY_LIST;
    }
    List<Node> ret = new ArrayList<>();
    List<? extends ConfigObject> list = config.getObjectList("node.active");
    for (ConfigObject configObject : list) {
        Node n;
        if (configObject.get("url") != null) {
            String url = configObject.toConfig().getString("url");
            n = new Node(url.startsWith("enode://") ? url : "enode://" + url);
        } else if (configObject.get("ip") != null) {
            String ip = configObject.toConfig().getString("ip");
            int port = configObject.toConfig().getInt("port");
            byte[] nodeId;
            if (configObject.toConfig().hasPath("nodeId")) {
                nodeId = Hex.decode(configObject.toConfig().getString("nodeId").trim());
                if (nodeId.length != 64) {
                    throw new RuntimeException("Invalid config nodeId '" + nodeId + "' at " + configObject);
                }
            } else {
                if (configObject.toConfig().hasPath("nodeName")) {
                    String nodeName = configObject.toConfig().getString("nodeName").trim();
                    // FIXME should be keccak-512 here ?
                    nodeId = ECKey.fromPrivate(sha3(nodeName.getBytes())).getNodeId();
                } else {
                    throw new RuntimeException("Either nodeId or nodeName should be specified: " + configObject);
                }
            }
            n = new Node(nodeId, ip, port);
        } else {
            throw new RuntimeException("Unexpected element within 'peer.active' config list: " + configObject);
        }
        ret.add(n);
    }
    return ret;
}
Also used : Node(org.tron.common.overlay.discover.Node) ArrayList(java.util.ArrayList) ConfigObject(com.typesafe.config.ConfigObject)

Aggregations

Node (org.tron.common.overlay.discover.Node)8 Endpoint (org.tron.protos.Discover.Endpoint)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ConfigObject (com.typesafe.config.ConfigObject)1 StringTokenizer (java.util.StringTokenizer)1 ECKey (org.tron.common.crypto.ECKey)1 NodeHandler (org.tron.common.overlay.discover.NodeHandler)1 Args (org.tron.core.config.args.Args)1 PeerConnection (org.tron.core.net.peer.PeerConnection)1