mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-1130] Delete rpcadapters.go. * [NOD-1130] Delete p2p. Move rpc to top level. * [NOD-1130] Remove DAGParams from rpcserverConfig. * [NOD-1130] Remove rpcserverPeer, rpcserverConnManager, rpcserverSyncManager, and rpcserverConfig. * [NOD-1130] Remove wallet RPC commands. * [NOD-1130] Remove wallet RPC commands. * [NOD-1130] Remove connmgr and peer. * [NOD-1130] Move rpcmodel into rpc. * [NOD-1130] Implement ConnectionCount. * [NOD-1130] Remove ping and node RPC commands. * [NOD-1130] Dummify handleGetNetTotals. * [NOD-1130] Add NetConnection to Peer. * [NOD-1130] Fix merge errors. * [NOD-1130] Implement Peers. * [NOD-1130] Fix HandleGetConnectedPeerInfo. * [NOD-1130] Fix SendRawTransaction. * [NOD-1130] Rename addManualNode to connect and removeManualNode to disconnect. * [NOD-1130] Add a stub for AddBlock. * [NOD-1130] Fix tests. * [NOD-1130] Replace half-baked contents of RemoveConnection with a stub. * [NOD-1130] Fix merge errors. * [NOD-1130] Make golint happy. * [NOD-1130] Get rid of something weird. * [NOD-1130] Rename minerClient back to client. * [NOD-1130] Add a few fields to GetConnectedPeerInfoResult. * [NOD-1130] Rename oneTry to isPermanent. * [NOD-1130] Implement ConnectionCount in NetAdapter. * [NOD-1130] Move RawMempoolVerbose out of mempool. * [NOD-1130] Move isSynced into the mining package. * [NOD-1130] Fix a compilation error. * [NOD-1130] Make golint happy. * [NOD-1130] Fix merge errors.
83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package flowcontext
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/blockdag"
|
|
peerpkg "github.com/kaspanet/kaspad/protocol/peer"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// StartIBDIfRequired selects a peer and starts IBD against it
|
|
// if required
|
|
func (f *FlowContext) StartIBDIfRequired() {
|
|
f.startIBDMutex.Lock()
|
|
defer f.startIBDMutex.Unlock()
|
|
|
|
if f.IsInIBD() {
|
|
return
|
|
}
|
|
|
|
peer := f.selectPeerForIBD(f.dag)
|
|
if peer == nil {
|
|
f.requestSelectedTipsIfRequired()
|
|
return
|
|
}
|
|
|
|
atomic.StoreUint32(&f.isInIBD, 1)
|
|
f.ibdPeer = peer
|
|
peer.StartIBD()
|
|
}
|
|
|
|
// IsInIBD is true if IBD is currently running
|
|
func (f *FlowContext) IsInIBD() bool {
|
|
return atomic.LoadUint32(&f.isInIBD) != 0
|
|
}
|
|
|
|
// selectPeerForIBD returns the first peer whose selected tip
|
|
// hash is not in our DAG
|
|
func (f *FlowContext) selectPeerForIBD(dag *blockdag.BlockDAG) *peerpkg.Peer {
|
|
for _, peer := range f.peers {
|
|
peerSelectedTipHash := peer.SelectedTipHash()
|
|
if !dag.IsInDAG(peerSelectedTipHash) {
|
|
return peer
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *FlowContext) requestSelectedTipsIfRequired() {
|
|
if f.isDAGTimeCurrent() {
|
|
return
|
|
}
|
|
f.requestSelectedTips()
|
|
}
|
|
|
|
func (f *FlowContext) isDAGTimeCurrent() bool {
|
|
const minDurationToRequestSelectedTips = time.Minute
|
|
return f.dag.Now().Sub(f.dag.SelectedTipHeader().Timestamp) > minDurationToRequestSelectedTips
|
|
}
|
|
|
|
func (f *FlowContext) requestSelectedTips() {
|
|
for _, peer := range f.peers {
|
|
peer.RequestSelectedTipIfRequired()
|
|
}
|
|
}
|
|
|
|
// FinishIBD finishes the current IBD flow and starts a new one if required.
|
|
func (f *FlowContext) FinishIBD() {
|
|
f.ibdPeer = nil
|
|
|
|
atomic.StoreUint32(&f.isInIBD, 0)
|
|
|
|
f.StartIBDIfRequired()
|
|
}
|
|
|
|
// IBDPeer returns the currently active IBD peer.
|
|
// Returns nil if we aren't currently in IBD
|
|
func (f *FlowContext) IBDPeer() *peerpkg.Peer {
|
|
if !f.IsInIBD() {
|
|
return nil
|
|
}
|
|
return f.ibdPeer
|
|
}
|