mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-22 09:29:38 +00:00
network: rework insertion into sorting in tryInitStateSync
Overall this'd be less operations and less allocations. Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
parent
49438798b5
commit
74acfe5288
1 changed files with 4 additions and 13 deletions
|
@ -1440,25 +1440,16 @@ func (s *Server) tryInitStateSync() {
|
|||
return
|
||||
}
|
||||
|
||||
var peersNumber int
|
||||
s.lock.RLock()
|
||||
heights := make([]uint32, 0)
|
||||
heights := make([]uint32, 0, len(s.peers))
|
||||
for p := range s.peers {
|
||||
if p.Handshaked() {
|
||||
peersNumber++
|
||||
peerLastBlock := p.LastBlockIndex()
|
||||
i := sort.Search(len(heights), func(i int) bool {
|
||||
return heights[i] >= peerLastBlock
|
||||
})
|
||||
heights = append(heights, peerLastBlock)
|
||||
if i != len(heights)-1 {
|
||||
copy(heights[i+1:], heights[i:])
|
||||
heights[i] = peerLastBlock
|
||||
}
|
||||
heights = append(heights, p.LastBlockIndex())
|
||||
}
|
||||
}
|
||||
s.lock.RUnlock()
|
||||
if peersNumber >= s.MinPeers && len(heights) > 0 {
|
||||
slices.Sort(heights)
|
||||
if len(heights) >= s.MinPeers && len(heights) > 0 {
|
||||
// choose the height of the median peer as the current chain's height
|
||||
h := heights[len(heights)/2]
|
||||
err := s.stateSync.Init(h)
|
||||
|
|
Loading…
Reference in a new issue