This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
/
entries.go
176 lines (141 loc) · 4.22 KB
/
entries.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (c) 2017-2020 The qitmeer developers
*/
package qnr
import (
"fmt"
"io"
"net"
"github.com/Qitmeer/qitmeer/common/encode/rlp"
)
// Entry is implemented by known node record entry types.
//
// To define a new entry that is to be included in a node record,
// create a Go type that satisfies this interface. The type should
// also implement rlp.Decoder if additional checks are needed on the value.
type Entry interface {
QNRKey() string
}
type generic struct {
key string
value interface{}
}
func (g generic) QNRKey() string { return g.key }
func (g generic) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, g.value)
}
func (g *generic) DecodeRLP(s *rlp.Stream) error {
return s.Decode(g.value)
}
// WithEntry wraps any value with a key name. It can be used to set and load arbitrary values
// in a record. The value v must be supported by rlp. To use WithEntry with Load, the value
// must be a pointer.
func WithEntry(k string, v interface{}) Entry {
return &generic{key: k, value: v}
}
// TCP is the "tcp" key, which holds the TCP port of the node.
type TCP uint16
func (v TCP) QNRKey() string { return "tcp" }
// UDP is the "udp" key, which holds the IPv6-specific UDP port of the node.
type TCP6 uint16
func (v TCP6) QNRKey() string { return "tcp6" }
// UDP is the "udp" key, which holds the UDP port of the node.
type UDP uint16
func (v UDP) QNRKey() string { return "udp" }
// UDP is the "udp" key, which holds the IPv6-specific UDP port of the node.
type UDP6 uint16
func (v UDP6) QNRKey() string { return "udp6" }
// ID is the "id" key, which holds the name of the identity scheme.
type ID string
const IDv4 = ID("v4") // the default identity scheme
func (v ID) QNRKey() string { return "id" }
// IP is either the "ip" or "ip6" key, depending on the value.
// Use this value to encode IP addresses that can be either v4 or v6.
// To load an address from a record use the IPv4 or IPv6 types.
type IP net.IP
func (v IP) QNRKey() string {
if net.IP(v).To4() == nil {
return "ip6"
}
return "ip"
}
// EncodeRLP implements rlp.Encoder.
func (v IP) EncodeRLP(w io.Writer) error {
if ip4 := net.IP(v).To4(); ip4 != nil {
return rlp.Encode(w, ip4)
}
if ip6 := net.IP(v).To16(); ip6 != nil {
return rlp.Encode(w, ip6)
}
return fmt.Errorf("invalid IP address: %v", net.IP(v))
}
// DecodeRLP implements rlp.Decoder.
func (v *IP) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode((*net.IP)(v)); err != nil {
return err
}
if len(*v) != 4 && len(*v) != 16 {
return fmt.Errorf("invalid IP address, want 4 or 16 bytes: %v", *v)
}
return nil
}
// IPv4 is the "ip" key, which holds the IP address of the node.
type IPv4 net.IP
func (v IPv4) QNRKey() string { return "ip" }
// EncodeRLP implements rlp.Encoder.
func (v IPv4) EncodeRLP(w io.Writer) error {
ip4 := net.IP(v).To4()
if ip4 == nil {
return fmt.Errorf("invalid IPv4 address: %v", net.IP(v))
}
return rlp.Encode(w, ip4)
}
// DecodeRLP implements rlp.Decoder.
func (v *IPv4) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode((*net.IP)(v)); err != nil {
return err
}
if len(*v) != 4 {
return fmt.Errorf("invalid IPv4 address, want 4 bytes: %v", *v)
}
return nil
}
// IPv6 is the "ip6" key, which holds the IP address of the node.
type IPv6 net.IP
func (v IPv6) QNRKey() string { return "ip6" }
// EncodeRLP implements rlp.Encoder.
func (v IPv6) EncodeRLP(w io.Writer) error {
ip6 := net.IP(v).To16()
if ip6 == nil {
return fmt.Errorf("invalid IPv6 address: %v", net.IP(v))
}
return rlp.Encode(w, ip6)
}
// DecodeRLP implements rlp.Decoder.
func (v *IPv6) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode((*net.IP)(v)); err != nil {
return err
}
if len(*v) != 16 {
return fmt.Errorf("invalid IPv6 address, want 16 bytes: %v", *v)
}
return nil
}
// KeyError is an error related to a key.
type KeyError struct {
Key string
Err error
}
// Error implements error.
func (err *KeyError) Error() string {
if err.Err == errNotFound {
return fmt.Sprintf("missing QNR key %q", err.Key)
}
return fmt.Sprintf("QNR key %q: %v", err.Key, err.Err)
}
// IsNotFound reports whether the given error means that a key/value pair is
// missing from a record.
func IsNotFound(err error) bool {
kerr, ok := err.(*KeyError)
return ok && kerr.Err == errNotFound
}