github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/raft/peer.go (about)

     1  package raft
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net"
     7  
     8  	"github.com/bigzoro/my_simplechain/log"
     9  	"github.com/bigzoro/my_simplechain/p2p/enode"
    10  	"github.com/bigzoro/my_simplechain/p2p/enr"
    11  	"github.com/bigzoro/my_simplechain/rlp"
    12  )
    13  
    14  // Serializable information about a Peer. Sufficient to build `etcdRaft.Peer`
    15  // or `enode.Node`.
    16  // As NodeId is mainly used to derive the `ecdsa.pubkey` to build `enode.Node` it is kept as [64]byte instead of ID [32]byte used by `enode.Node`.
    17  type Address struct {
    18  	RaftId   uint16        `json:"raftId"`
    19  	NodeId   enode.EnodeID `json:"nodeId"`
    20  	Ip       net.IP        `json:"ip"`
    21  	P2pPort  enr.TCP       `json:"p2pPort"`
    22  	RaftPort enr.RaftPort  `json:"raftPort"`
    23  }
    24  
    25  func NewAddress(raftId uint16, raftPort int, node *enode.Node) *Address {
    26  	// derive 64 byte nodeID from 128 byte enodeID
    27  	id, err := enode.RaftHexID(node.EnodeID())
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  	return &Address{
    32  		RaftId:   raftId,
    33  		NodeId:   id,
    34  		Ip:       node.IP(),
    35  		P2pPort:  enr.TCP(node.TCP()),
    36  		RaftPort: enr.RaftPort(raftPort),
    37  	}
    38  }
    39  
    40  // A peer that we're connected to via both raft's http transport, and ethereum p2p
    41  type Peer struct {
    42  	Address *Address    // For raft transport
    43  	P2pNode *enode.Node // For ethereum transport
    44  }
    45  
    46  func (addr *Address) EncodeRLP(w io.Writer) error {
    47  	return rlp.Encode(w, []interface{}{addr.RaftId, addr.NodeId, addr.Ip, addr.P2pPort, addr.RaftPort})
    48  }
    49  
    50  func (addr *Address) DecodeRLP(s *rlp.Stream) error {
    51  	// These fields need to be public:
    52  	var temp struct {
    53  		RaftId   uint16
    54  		NodeId   enode.EnodeID
    55  		Ip       net.IP
    56  		P2pPort  enr.TCP
    57  		RaftPort enr.RaftPort
    58  	}
    59  
    60  	if err := s.Decode(&temp); err != nil {
    61  		return err
    62  	} else {
    63  		addr.RaftId, addr.NodeId, addr.Ip, addr.P2pPort, addr.RaftPort = temp.RaftId, temp.NodeId, temp.Ip, temp.P2pPort, temp.RaftPort
    64  		return nil
    65  	}
    66  }
    67  
    68  // RLP Address encoding, for transport over raft and storage in LevelDB.
    69  func (addr *Address) ToBytes() []byte {
    70  	size, r, err := rlp.EncodeToReader(addr)
    71  	if err != nil {
    72  		panic(fmt.Sprintf("error: failed to RLP-encode Address: %s", err.Error()))
    73  	}
    74  	var buffer = make([]byte, uint32(size))
    75  	r.Read(buffer)
    76  
    77  	return buffer
    78  }
    79  
    80  func BytesToAddress(bytes []byte) *Address {
    81  	var addr Address
    82  	if err := rlp.DecodeBytes(bytes, &addr); err != nil {
    83  		log.Error("failed to RLP-decode Address", "error", err)
    84  	}
    85  	return &addr
    86  }