From ab59e3028bacbcb3ed84cf063bb98bdbe6a37c48 Mon Sep 17 00:00:00 2001 From: kuba-4chain <jakub.smolka@4chain.studio> Date: Wed, 3 Jul 2024 18:10:52 +0200 Subject: [PATCH] feat: improve naming and logging --- examples/simple/handler.go | 4 ++-- interface.go | 6 ++---- peer.go | 2 +- peer_handler_gen_mock.go | 20 ++++++++++---------- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/examples/simple/handler.go b/examples/simple/handler.go index a3ed469..b98c198 100644 --- a/examples/simple/handler.go +++ b/examples/simple/handler.go @@ -14,8 +14,8 @@ type SimplePeerHandler struct { logger *slog.Logger } -func (s *SimplePeerHandler) HandleTransactionsGet(msg []*wire.InvVect, peer p2p.PeerI) ([][]byte, error) { - s.logger.Info("Peer requested transactions", slog.Int("count", len(msg)), slog.String("peer", peer.String())) +func (s *SimplePeerHandler) HandleTransactionsGet(msgs []*wire.InvVect, peer p2p.PeerI) ([][]byte, error) { + s.logger.Info("Peer requested transactions", slog.Int("count", len(msgs)), slog.String("peer", peer.String())) // You should implement a store and return the transactions bytes here. return nil, fmt.Errorf("transactions not found") } diff --git a/interface.go b/interface.go index 505fa82..4221807 100644 --- a/interface.go +++ b/interface.go @@ -7,9 +7,7 @@ import ( "github.com/libsv/go-p2p/wire" ) -var ( - ErrPeerNetworkMismatch = fmt.Errorf("peer network mismatch") -) +var ErrPeerNetworkMismatch = fmt.Errorf("peer network mismatch") type PeerManagerI interface { AnnounceTransaction(txHash *chainhash.Hash, peers []PeerI) []PeerI @@ -36,7 +34,7 @@ type PeerI interface { } type PeerHandlerI interface { - HandleTransactionsGet(msg []*wire.InvVect, peer PeerI) ([][]byte, error) + HandleTransactionsGet(msgs []*wire.InvVect, peer PeerI) ([][]byte, error) HandleTransactionSent(msg *wire.MsgTx, peer PeerI) error HandleTransactionAnnouncement(msg *wire.InvVect, peer PeerI) error HandleTransactionRejection(rejMsg *wire.MsgReject, peer PeerI) error diff --git a/peer.go b/peer.go index 071feca..802a12e 100644 --- a/peer.go +++ b/peer.go @@ -571,7 +571,7 @@ func (p *Peer) handleGetDataMsg(dataMsg *wire.MsgGetData, logger *slog.Logger) { rawTxs, err := p.peerHandler.HandleTransactionsGet(txRequests, p) if err != nil { - logger.Warn("Unable to fetch txs from store", slog.String(errKey, err.Error())) + logger.Warn("Unable to fetch txs from store", slog.Int("count", len(txRequests)), slog.String(errKey, err.Error())) return } diff --git a/peer_handler_gen_mock.go b/peer_handler_gen_mock.go index fef17f9..1eaa06b 100644 --- a/peer_handler_gen_mock.go +++ b/peer_handler_gen_mock.go @@ -36,7 +36,7 @@ var _ PeerHandlerI = &PeerHandlerIMock{} // HandleTransactionSentFunc: func(msg *wire.MsgTx, peer PeerI) error { // panic("mock out the HandleTransactionSent method") // }, -// HandleTransactionsGetFunc: func(msg []*wire.InvVect, peer PeerI) ([][]byte, error) { +// HandleTransactionsGetFunc: func(msgs []*wire.InvVect, peer PeerI) ([][]byte, error) { // panic("mock out the HandleTransactionsGet method") // }, // } @@ -65,7 +65,7 @@ type PeerHandlerIMock struct { HandleTransactionSentFunc func(msg *wire.MsgTx, peer PeerI) error // HandleTransactionsGetFunc mocks the HandleTransactionsGet method. - HandleTransactionsGetFunc func(msg []*wire.InvVect, peer PeerI) ([][]byte, error) + HandleTransactionsGetFunc func(msgs []*wire.InvVect, peer PeerI) ([][]byte, error) // calls tracks calls to the methods. calls struct { @@ -113,8 +113,8 @@ type PeerHandlerIMock struct { } // HandleTransactionsGet holds details about calls to the HandleTransactionsGet method. HandleTransactionsGet []struct { - // Msg is the msg argument value. - Msg []*wire.InvVect + // Msgs is the msgs argument value. + Msgs []*wire.InvVect // Peer is the peer argument value. Peer PeerI } @@ -345,21 +345,21 @@ func (mock *PeerHandlerIMock) HandleTransactionSentCalls() []struct { } // HandleTransactionsGet calls HandleTransactionsGetFunc. -func (mock *PeerHandlerIMock) HandleTransactionsGet(msg []*wire.InvVect, peer PeerI) ([][]byte, error) { +func (mock *PeerHandlerIMock) HandleTransactionsGet(msgs []*wire.InvVect, peer PeerI) ([][]byte, error) { if mock.HandleTransactionsGetFunc == nil { panic("PeerHandlerIMock.HandleTransactionsGetFunc: method is nil but PeerHandlerI.HandleTransactionsGet was just called") } callInfo := struct { - Msg []*wire.InvVect + Msgs []*wire.InvVect Peer PeerI }{ - Msg: msg, + Msgs: msgs, Peer: peer, } mock.lockHandleTransactionsGet.Lock() mock.calls.HandleTransactionsGet = append(mock.calls.HandleTransactionsGet, callInfo) mock.lockHandleTransactionsGet.Unlock() - return mock.HandleTransactionsGetFunc(msg, peer) + return mock.HandleTransactionsGetFunc(msgs, peer) } // HandleTransactionsGetCalls gets all the calls that were made to HandleTransactionsGet. @@ -367,11 +367,11 @@ func (mock *PeerHandlerIMock) HandleTransactionsGet(msg []*wire.InvVect, peer Pe // // len(mockedPeerHandlerI.HandleTransactionsGetCalls()) func (mock *PeerHandlerIMock) HandleTransactionsGetCalls() []struct { - Msg []*wire.InvVect + Msgs []*wire.InvVect Peer PeerI } { var calls []struct { - Msg []*wire.InvVect + Msgs []*wire.InvVect Peer PeerI } mock.lockHandleTransactionsGet.RLock()