github.com/amazechain/amc@v0.1.3/common/peer_set.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package common
    18  
    19  import (
    20  	"github.com/holiman/uint256"
    21  	"github.com/libp2p/go-libp2p/core/peer"
    22  	"time"
    23  )
    24  
    25  type Peer struct {
    26  	IPeer
    27  	CurrentHeight *uint256.Int
    28  	AddTimer      time.Time
    29  }
    30  
    31  type PeerSet []Peer
    32  type PeerMap map[peer.ID]Peer
    33  
    34  func (pm PeerMap) ToSlice() PeerSet {
    35  	peerSet := PeerSet{}
    36  	for k, _ := range pm {
    37  		peerSet = append(peerSet, pm[k])
    38  	}
    39  
    40  	return peerSet
    41  }
    42  
    43  func (ps PeerSet) Len() int {
    44  	return len(ps)
    45  }
    46  
    47  func (ps PeerSet) Less(i, j int) bool {
    48  	if ps[i].CurrentHeight.Cmp(ps[j].CurrentHeight) == 1 {
    49  		return true
    50  	}
    51  	return false
    52  }
    53  
    54  func (ps PeerSet) Swap(i, j int) {
    55  	ps[i], ps[j] = ps[j], ps[i]
    56  }