refactor: dont use pointers with interfaces

This commit is contained in:
Derrick Hammer 2024-01-07 06:35:41 -05:00
parent 52b7426a7a
commit 2e9b07c6bd
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
12 changed files with 46 additions and 27 deletions

View File

@ -14,8 +14,8 @@ type P2PService interface {
Stop() error
Init() error
ConnectToNode(connectionUris []*url.URL, retried bool) error
OnNewPeer(peer *net.Peer, verifyId bool) error
OnNewPeerListen(peer *net.Peer, verifyId bool)
OnNewPeer(peer net.Peer, verifyId bool) error
OnNewPeerListen(peer net.Peer, verifyId bool)
ReadNodeScore(nodeId *encoding.NodeId) (NodeVotes, error)
GetNodeScore(nodeId *encoding.NodeId) (float64, error)
SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId, error)

View File

@ -51,7 +51,7 @@ func CreateTransportSocket(peerType string, uri *url.URL) (interface{}, error) {
return &t, err
}
func CreateTransportPeer(peerType string, options *TransportPeerConfig) (*Peer, error) {
func CreateTransportPeer(peerType string, options *TransportPeerConfig) (Peer, error) {
factory, ok := transports.Load(peerType)
if !ok {
return nil, errors.New("no factory registered for type: " + peerType)
@ -59,5 +59,5 @@ func CreateTransportPeer(peerType string, options *TransportPeerConfig) (*Peer,
t, err := factory.(PeerFactory).NewPeer(options)
return &t, err
return t, err
}

View File

@ -6,6 +6,10 @@ import (
"net/url"
)
var (
_ Peer = (*BasePeer)(nil)
)
// EventCallback type for the callback function
type EventCallback func(event []byte) error
@ -43,6 +47,22 @@ type BasePeer struct {
id *encoding.NodeId
}
func (b *BasePeer) SendMessage(message []byte) error {
panic("must implement in child class")
}
func (b *BasePeer) RenderLocationURI() string {
panic("must implement in child class")
}
func (b *BasePeer) ListenForMessages(callback EventCallback, options ListenerOptions) {
panic("must implement in child class")
}
func (b *BasePeer) End() error {
panic("must implement in child class")
}
func (b *BasePeer) Challenge() []byte {
return b.challenge
}

View File

@ -7,7 +7,7 @@ import (
)
type IncomingMessage interface {
HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error
HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error
SetIncomingMessage(msg IncomingMessage)
msgpack.CustomDecoder
}

View File

@ -41,7 +41,7 @@ func (i *IncomingMessageImpl) ToMessage() (message []byte, err error) {
return msgpack.Marshal(i)
}
func (i *IncomingMessageImpl) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
func (i *IncomingMessageImpl) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
panic("child class should implement this method")
}

View File

@ -56,7 +56,7 @@ func (m HandshakeOpen) EncodeMsgpack(enc *msgpack.Encoder) error {
return nil
}
func (m *HandshakeOpen) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
func (m *HandshakeOpen) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
return nil
}

View File

@ -48,7 +48,7 @@ func (h *HashQuery) DecodeMessage(dec *msgpack.Decoder) error {
return nil
}
func (h *HashQuery) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
func (h *HashQuery) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
mapLocations, err := node.GetCachedStorageLocations(h.hash, h.kinds)
if err != nil {
log.Printf("Error getting cached storage locations: %v", err)
@ -79,7 +79,7 @@ func (h *HashQuery) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId
entry, exists := mapLocations[sortedNodeId]
if exists {
err := (*peer).SendMessage(entry.ProviderMessage())
err := peer.SendMessage(entry.ProviderMessage())
if err != nil {
return err
}
@ -94,13 +94,13 @@ func (h *HashQuery) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId
peers = peersVal.(*hashset.Set)
if exists := peers.Contains((*peer).Id()); !exists {
peers.Add((*peer).Id())
if exists := peers.Contains(peer.Id()); !exists {
peers.Add(peer.Id())
}
for _, val := range node.Services().P2P().Peers().Values() {
peerVal := val.(net.Peer)
if !peerVal.Id().Equals((*peer).Id()) {
if !peerVal.Id().Equals(peer.Id()) {
err := peerVal.SendMessage(h.IncomingMessageImpl.Original())
if err != nil {
node.Logger().Error("Failed to send message", zap.Error(err))

View File

@ -60,7 +60,7 @@ func (a *AnnouncePeers) DecodeMessage(dec *msgpack.Decoder) error {
return nil
}
func (a AnnouncePeers) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
func (a AnnouncePeers) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
if len(a.connectionUris) > 0 {
firstUrl := a.connectionUris[0]
uri := new(url.URL)

View File

@ -31,21 +31,21 @@ func NewHandshakeDone() *HandshakeDone {
return &HandshakeDone{challenge: nil, networkId: "", supportedFeatures: -1}
}
func (h HandshakeDone) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
func (h HandshakeDone) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
if !node.IsStarted() {
err := (*peer).End()
err := peer.End()
if err != nil {
return nil
}
return nil
}
if !bytes.Equal((*peer).Challenge(), h.challenge) {
if !bytes.Equal(peer.Challenge(), h.challenge) {
return errors.New("Invalid challenge")
}
/*
if !verifyId {
(*peer).SetId(h)
peer.SetId(h)
} else {
if !peer.ID.Equals(pId) {
return errInvalidChallenge

View File

@ -54,7 +54,7 @@ func NewSignedMessage() *SignedMessage {
return &SignedMessage{}
}
func (s *SignedMessage) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
func (s *SignedMessage) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
var payload signedMessagePayoad
err := msgpack.Unmarshal(s.message, &payload)

View File

@ -41,7 +41,7 @@ func (s *StorageLocation) DecodeMessage(dec *msgpack.Decoder) error {
return nil
}
func (s *StorageLocation) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
func (s *StorageLocation) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
hash := encoding.NewMultihash(s.raw[1:34]) // Replace NewMultihash with appropriate function
fmt.Println("Hash:", hash)

View File

@ -194,15 +194,14 @@ func (p *P2PImpl) ConnectToNode(connectionUris []*url.URL, retried bool) error {
return err
}
(*peer).SetId(id)
peer.SetId(id)
return p.OnNewPeer(peer, true)
}
func (p *P2PImpl) OnNewPeer(peer *net.Peer, verifyId bool) error {
func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error {
challenge := protocol.GenerateChallenge()
pd := *peer
pd.SetChallenge(challenge)
peer.SetChallenge(challenge)
p.OnNewPeerListen(peer, verifyId)
@ -212,15 +211,15 @@ func (p *P2PImpl) OnNewPeer(peer *net.Peer, verifyId bool) error {
return err
}
err = pd.SendMessage(handshakeOpenMsg)
err = peer.SendMessage(handshakeOpenMsg)
if err != nil {
return err
}
return nil
}
func (p *P2PImpl) OnNewPeerListen(peer *net.Peer, verifyId bool) {
func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {
onDone := net.CloseCallback(func() {
peerId, err := (*peer).Id().ToString()
peerId, err := peer.Id().ToString()
if err != nil {
p.logger.Error("failed to get peer id", zap.Error(err))
return
@ -236,7 +235,7 @@ func (p *P2PImpl) OnNewPeerListen(peer *net.Peer, verifyId bool) {
p.logger.Error("peer error", zap.Any("args", args))
})
(*peer).ListenForMessages(func(message []byte) error {
peer.ListenForMessages(func(message []byte) error {
imsg := base.NewIncomingMessageUnknown()
err := msgpack.Unmarshal(message, imsg)