aboutsummaryrefslogtreecommitdiffstats
path: root/mobile/discover.go
blob: 9b221a874eb6c0c1f9913ae6abf49d78ff096209 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// Contains all the wrappers from the accounts package to support client side enode
// management on mobile platforms.

package geth

import (
    "errors"

    "github.com/ethereum/go-ethereum/cmd/utils"
    "github.com/ethereum/go-ethereum/p2p/discover"
)

// MainnetBootnodes returns the enode URLs of the P2P bootstrap nodes running
// on the main network.
//
// Note, this needs to be a method to prevent gomobile generating a setter for it.
func MainnetBootnodes() *Enodes {
    nodes := &Enodes{nodes: make([]*discover.Node, len(utils.MainnetBootnodes))}
    for i, node := range utils.MainnetBootnodes {
        nodes.nodes[i] = node
    }
    return nodes
}

// TestnetBootnodes returns the enode URLs of the P2P bootstrap nodes running
// on the test network.
//
// Note, this needs to be a method to prevent gomobile generating a setter for it.
func TestnetBootnodes() *Enodes {
    nodes := &Enodes{nodes: make([]*discover.Node, len(utils.TestnetBootnodes))}
    for i, node := range utils.TestnetBootnodes {
        nodes.nodes[i] = node
    }
    return nodes
}

// Enode represents a host on the network.
type Enode struct {
    node *discover.Node
}

// NewEnode parses a node designator.
//
// There are two basic forms of node designators
//   - incomplete nodes, which only have the public key (node ID)
//   - complete nodes, which contain the public key and IP/Port information
//
// For incomplete nodes, the designator must look like one of these
//
//    enode://<hex node id>
//    <hex node id>
//
// For complete nodes, the node ID is encoded in the username portion
// of the URL, separated from the host by an @ sign. The hostname can
// only be given as an IP address, DNS domain names are not allowed.
// The port in the host name section is the TCP listening port. If the
// TCP and UDP (discovery) ports differ, the UDP port is specified as
// query parameter "discport".
//
// In the following example, the node URL describes
// a node with IP address 10.3.58.6, TCP listening port 30303
// and UDP discovery port 30301.
//
//    enode://<hex node id>@10.3.58.6:30303?discport=30301
func NewEnode(rawurl string) (*Enode, error) {
    node, err := discover.ParseNode(rawurl)
    if err != nil {
        return nil, err
    }
    return &Enode{node}, nil
}

// Enodes represents a slice of accounts.
type Enodes struct{ nodes []*discover.Node }

// NewEnodes creates a slice of uninitialized enodes.
func NewEnodes(size int) *Enodes {
    return &Enodes{
        nodes: make([]*discover.Node, size),
    }
}

// NewEnodesEmpty creates an empty slice of Enode values.
func NewEnodesEmpty() *Enodes {
    return NewEnodes(0)
}

// Size returns the number of enodes in the slice.
func (e *Enodes) Size() int {
    return len(e.nodes)
}

// Get returns the enode at the given index from the slice.
func (e *Enodes) Get(index int) (*Enode, error) {
    if index < 0 || index >= len(e.nodes) {
        return nil, errors.New("index out of bounds")
    }
    return &Enode{e.nodes[index]}, nil
}

// Set sets the enode at the given index in the slice.
func (e *Enodes) Set(index int, enode *Enode) error {
    if index < 0 || index >= len(e.nodes) {
        return errors.New("index out of bounds")
    }
    e.nodes[index] = enode.node
    return nil
}

// Append adds a new enode element to the end of the slice.
func (e *Enodes) Append(enode *Enode) {
    e.nodes = append(e.nodes, enode.node)
}