github.com/dominant-strategies/go-quai@v0.28.2/eth/peer.go (about)

     1  // Copyright 2015 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 eth
    18  
    19  import (
    20  	"math/big"
    21  	"sync"
    22  	"time"
    23  
    24  	"github.com/dominant-strategies/go-quai/eth/protocols/eth"
    25  )
    26  
    27  // ethPeerInfo represents a short summary of the `eth` sub-protocol metadata known
    28  // about a connected peer.
    29  type ethPeerInfo struct {
    30  	Version uint     `json:"version"` // Quai protocol version negotiated
    31  	Entropy *big.Int `json:"entropy"` // Head Entropy of the peer's blockchain
    32  	Head    string   `json:"head"`    // Hex hash of the peer's best owned block
    33  }
    34  
    35  // ethPeer is a wrapper around eth.Peer to maintain a few extra metadata.
    36  type ethPeer struct {
    37  	*eth.Peer
    38  
    39  	syncDrop *time.Timer  // Connection dropper if `eth` sync progress isn't validated in time
    40  	lock     sync.RWMutex // Mutex protecting the internal fields
    41  }
    42  
    43  // info gathers and returns some `eth` protocol metadata known about a peer.
    44  func (p *ethPeer) info() *ethPeerInfo {
    45  	hash, _, entropy, _ := p.Head()
    46  
    47  	return &ethPeerInfo{
    48  		Version: p.Version(),
    49  		Entropy: entropy,
    50  		Head:    hash.Hex(),
    51  	}
    52  }