mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-13 17:46:39 +00:00

* [NOD-1123] Bubble bad-message errors up to the protocol level. * [NOD-1123] Implement Banning. * [NOD-1123] Properly use &stopped. * [NOD-1123] Ban by IP rather than IP and port. * [NOD-1123] Don't initiate connections to banned peers. * [NOD-1123] Fix infinite loop in checkOutgoingConnections. * [NOD-1123] Fix bannedAddresses key. * [NOD-1123] Rename onBadMessageHandler to onInvalidMessageHandler.
67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
package protocol
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/addrmgr"
|
|
"github.com/kaspanet/kaspad/blockdag"
|
|
"github.com/kaspanet/kaspad/config"
|
|
"github.com/kaspanet/kaspad/connmanager"
|
|
"github.com/kaspanet/kaspad/mempool"
|
|
"github.com/kaspanet/kaspad/netadapter"
|
|
"github.com/kaspanet/kaspad/protocol/flowcontext"
|
|
peerpkg "github.com/kaspanet/kaspad/protocol/peer"
|
|
"github.com/kaspanet/kaspad/util"
|
|
)
|
|
|
|
// Manager manages the p2p protocol
|
|
type Manager struct {
|
|
context *flowcontext.FlowContext
|
|
}
|
|
|
|
// NewManager creates a new instance of the p2p protocol manager
|
|
func NewManager(cfg *config.Config, dag *blockdag.BlockDAG,
|
|
addressManager *addrmgr.AddrManager, txPool *mempool.TxPool,
|
|
connectionManager *connmanager.ConnectionManager) (*Manager, error) {
|
|
|
|
netAdapter, err := netadapter.NewNetAdapter(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
manager := Manager{
|
|
context: flowcontext.New(cfg, dag, addressManager, txPool, netAdapter, connectionManager),
|
|
}
|
|
netAdapter.SetRouterInitializer(manager.routerInitializer)
|
|
return &manager, nil
|
|
}
|
|
|
|
// Start starts the p2p protocol
|
|
func (m *Manager) Start() error {
|
|
return m.context.NetAdapter().Start()
|
|
}
|
|
|
|
// Stop stops the p2p protocol
|
|
func (m *Manager) Stop() error {
|
|
return m.context.NetAdapter().Stop()
|
|
}
|
|
|
|
// Peers returns the currently active peers
|
|
func (m *Manager) Peers() []*peerpkg.Peer {
|
|
return m.context.Peers()
|
|
}
|
|
|
|
// IBDPeer returns the currently active IBD peer.
|
|
// Returns nil if we aren't currently in IBD
|
|
func (m *Manager) IBDPeer() *peerpkg.Peer {
|
|
return m.context.IBDPeer()
|
|
}
|
|
|
|
// AddTransaction adds transaction to the mempool and propagates it.
|
|
func (m *Manager) AddTransaction(tx *util.Tx) error {
|
|
return m.context.AddTransaction(tx)
|
|
}
|
|
|
|
// AddBlock adds the given block to the DAG and propagates it.
|
|
func (m *Manager) AddBlock(block *util.Block, flags blockdag.BehaviorFlags) error {
|
|
return m.context.AddBlock(block, flags)
|
|
}
|