github.com/hyperion-hyn/go-ethereum@v2.4.0+incompatible/p2p/enr/entries.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package enr
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"net"
    23  
    24  	"github.com/ethereum/go-ethereum/rlp"
    25  )
    26  
    27  // Entry is implemented by known node record entry types.
    28  //
    29  // To define a new entry that is to be included in a node record,
    30  // create a Go type that satisfies this interface. The type should
    31  // also implement rlp.Decoder if additional checks are needed on the value.
    32  type Entry interface {
    33  	ENRKey() string
    34  }
    35  
    36  type generic struct {
    37  	key   string
    38  	value interface{}
    39  }
    40  
    41  func (g generic) ENRKey() string { return g.key }
    42  
    43  func (g generic) EncodeRLP(w io.Writer) error {
    44  	return rlp.Encode(w, g.value)
    45  }
    46  
    47  func (g *generic) DecodeRLP(s *rlp.Stream) error {
    48  	return s.Decode(g.value)
    49  }
    50  
    51  // WithEntry wraps any value with a key name. It can be used to set and load arbitrary values
    52  // in a record. The value v must be supported by rlp. To use WithEntry with Load, the value
    53  // must be a pointer.
    54  func WithEntry(k string, v interface{}) Entry {
    55  	return &generic{key: k, value: v}
    56  }
    57  
    58  // TCP is the "tcp" key, which holds the TCP port of the node.
    59  type TCP uint16
    60  
    61  func (v TCP) ENRKey() string { return "tcp" }
    62  
    63  // UDP is the "udp" key, which holds the UDP port of the node.
    64  type UDP uint16
    65  
    66  func (v UDP) ENRKey() string { return "udp" }
    67  
    68  // ID is the "id" key, which holds the name of the identity scheme.
    69  type ID string
    70  
    71  const IDv4 = ID("v4") // the default identity scheme
    72  
    73  func (v ID) ENRKey() string { return "id" }
    74  
    75  // IP is the "ip" key, which holds the IP address of the node.
    76  type IP net.IP
    77  
    78  func (v IP) ENRKey() string { return "ip" }
    79  
    80  // RaftPort is the "raftport" key, which holds the raftport of the node
    81  type RaftPort uint16
    82  
    83  func (v RaftPort) ENRKey() string { return "raftport" }
    84  
    85  type Hostname string
    86  
    87  func (v Hostname) ENRKey() string { return "hostname" }
    88  
    89  // EncodeRLP implements rlp.Encoder.
    90  func (v IP) EncodeRLP(w io.Writer) error {
    91  	if ip4 := net.IP(v).To4(); ip4 != nil {
    92  		return rlp.Encode(w, ip4)
    93  	}
    94  	return rlp.Encode(w, net.IP(v))
    95  }
    96  
    97  // DecodeRLP implements rlp.Decoder.
    98  func (v *IP) DecodeRLP(s *rlp.Stream) error {
    99  	if err := s.Decode((*net.IP)(v)); err != nil {
   100  		return err
   101  	}
   102  	if len(*v) != 4 && len(*v) != 16 {
   103  		return fmt.Errorf("invalid IP address, want 4 or 16 bytes: %v", *v)
   104  	}
   105  	return nil
   106  }
   107  
   108  // KeyError is an error related to a key.
   109  type KeyError struct {
   110  	Key string
   111  	Err error
   112  }
   113  
   114  // Error implements error.
   115  func (err *KeyError) Error() string {
   116  	if err.Err == errNotFound {
   117  		return fmt.Sprintf("missing ENR key %q", err.Key)
   118  	}
   119  	return fmt.Sprintf("ENR key %q: %v", err.Key, err.Err)
   120  }
   121  
   122  // IsNotFound reports whether the given error means that a key/value pair is
   123  // missing from a record.
   124  func IsNotFound(err error) bool {
   125  	kerr, ok := err.(*KeyError)
   126  	return ok && kerr.Err == errNotFound
   127  }