Skip to content

Commit

Permalink
feat(logger): short stringer for loggers (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Oct 8, 2023
1 parent bcf737a commit 6a9a37f
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 50 deletions.
2 changes: 1 addition & 1 deletion consensus/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (s *commitState) decide() {
if err != nil {
s.logger.Error("committing block failed", "block", certBlock, "error", err)
} else {
s.logger.Info("block committed, schedule new height", "hash", certBlock.Hash().ShortString())
s.logger.Info("block committed, schedule new height", "hash", certBlock.Hash())
}

// Now we can announce the committed block and certificate
Expand Down
4 changes: 2 additions & 2 deletions consensus/cp_mainvote.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func (s *cpMainVoteState) detectByzantineProposal() {
if roundProposal != nil &&
roundProposal.Block().Hash() != *s.cpWeakValidity {
s.logger.Warn("double proposal detected",
"prepared", s.cpWeakValidity.ShortString(),
"roundProposal", roundProposal.Block().Hash().ShortString())
"prepared", s.cpWeakValidity,
"roundProposal", roundProposal.Block().Hash())

s.log.SetRoundProposal(s.round, nil)
s.queryProposal()
Expand Down
4 changes: 2 additions & 2 deletions consensus/precommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func (s *precommitState) decide() {
precommits := s.log.PrecommitVoteSet(s.round)
precommitQH := precommits.QuorumHash()
if precommitQH != nil {
s.logger.Debug("pre-commit has quorum", "hash", precommitQH.ShortString())
s.logger.Debug("pre-commit has quorum", "hash", precommitQH)

roundProposal := s.log.RoundProposal(s.round)
if roundProposal == nil {
// There is a consensus about a proposal that we don't have yet.
// Ask peers for this proposal.
s.logger.Info("query for a decided proposal", "hash", precommitQH.ShortString())
s.logger.Info("query for a decided proposal", "hash", precommitQH)
s.queryProposal()
} else {
// To ensure we have voted and won't be absent from the certificate
Expand Down
2 changes: 1 addition & 1 deletion consensus/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (s *prepareState) decide() {
prepares := s.log.PrepareVoteSet(s.round)
prepareQH := prepares.QuorumHash()
if prepareQH != nil {
s.logger.Debug("prepare has quorum", "hash", prepareQH.ShortString())
s.logger.Debug("prepare has quorum", "hash", prepareQH)
s.enterNewState(s.precommitState)
} else {
//
Expand Down
4 changes: 2 additions & 2 deletions consensus/propose.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func (s *proposeState) enter() {
func (s *proposeState) decide() {
proposer := s.proposer(s.round)
if proposer.Address() == s.valKey.Address() {
s.logger.Info("our turn to propose", "proposer", proposer.Address().ShortString())
s.logger.Info("our turn to propose", "proposer", proposer.Address())
s.createProposal(s.height, s.round)
} else {
s.logger.Debug("not our turn to propose", "proposer", proposer.Address().ShortString())
s.logger.Debug("not our turn to propose", "proposer", proposer.Address())
}

s.cpRound = 0
Expand Down
2 changes: 1 addition & 1 deletion network/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (g *gossipService) onReceiveMessage(m *lp2pps.Message) {
}

g.logger.Debug("receiving new gossip message",
"source", m.GetFrom().ShortString(), "from", m.ReceivedFrom.ShortString())
"source", m.GetFrom(), "from", m.ReceivedFrom)
event := &GossipMessage{
Source: m.GetFrom(),
From: m.ReceivedFrom,
Expand Down
6 changes: 3 additions & 3 deletions network/notifee.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ func (n *NotifeeService) Connected(lp2pn lp2pnetwork.Network, conn lp2pnetwork.C

protocols, _ := lp2pn.Peerstore().SupportsProtocols(peerID, n.protocolID)
if len(protocols) > 0 {
n.logger.Info("connected to peer", "pid", peerID.ShortString())
n.logger.Info("connected to peer", "pid", peerID)
n.eventChannel <- &ConnectEvent{PeerID: peerID}

return
}
}

n.logger.Info("this node doesn't support stream protocol", "pid", peerID.ShortString())
n.logger.Info("this node doesn't support stream protocol", "pid", peerID)
}()
}

func (n *NotifeeService) Disconnected(_ lp2pnetwork.Network, conn lp2pnetwork.Conn) {
peerID := conn.RemotePeer()
n.logger.Info("disconnected from peer", "pid", peerID.ShortString())
n.logger.Info("disconnected from peer", "pid", peerID)
n.eventChannel <- &DisconnectEvent{PeerID: peerID}
}

Expand Down
12 changes: 6 additions & 6 deletions network/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *streamService) Stop() {
func (s *streamService) handleStream(stream lp2pnetwork.Stream) {
from := stream.Conn().RemotePeer()

s.logger.Debug("receiving stream", "from", from.ShortString())
s.logger.Debug("receiving stream", "from", from)
event := &StreamMessage{
Source: from,
Reader: stream,
Expand All @@ -60,7 +60,7 @@ func (s *streamService) handleStream(stream lp2pnetwork.Stream) {
// If a direct connection can't be established, it attempts to connect via a relay node.
// Returns an error if the sending process fails.
func (s *streamService) SendRequest(msg []byte, pid lp2peer.ID) error {
s.logger.Trace("sending stream", "to", pid.ShortString())
s.logger.Trace("sending stream", "to", pid)
_, err := s.host.Peerstore().SupportsProtocols(pid, s.protocolID)
if err != nil {
return LibP2PError{Err: err}
Expand All @@ -70,7 +70,7 @@ func (s *streamService) SendRequest(msg []byte, pid lp2peer.ID) error {
stream, err := s.host.NewStream(
lp2pnetwork.WithNoDial(s.ctx, "should already have connection"), pid, s.protocolID)
if err != nil {
s.logger.Debug("unable to open direct stream", "pid", pid.ShortString(), "error", err)
s.logger.Debug("unable to open direct stream", "pid", pid, "error", err)
if len(s.relayAddrs) == 0 {
return err
}
Expand Down Expand Up @@ -100,17 +100,17 @@ func (s *streamService) SendRequest(msg []byte, pid lp2peer.ID) error {

if err := s.host.Connect(s.ctx, unreachableRelayInfo); err != nil {
// There is no relay connection to peer as well
s.logger.Warn("unable to connect to peer using relay", "pid", pid.ShortString(), "error", err)
s.logger.Warn("unable to connect to peer using relay", "pid", pid, "error", err)
return LibP2PError{Err: err}
}
s.logger.Debug("connected to peer using relay", "pid", pid.ShortString())
s.logger.Debug("connected to peer using relay", "pid", pid)

// Try to open a new stream to the target peer using the relay connection.
// The connection is marked as transient.
stream, err = s.host.NewStream(
lp2pnetwork.WithUseTransient(s.ctx, string(s.protocolID)), pid, s.protocolID)
if err != nil {
s.logger.Warn("unable to open relay stream", "pid", pid.ShortString(), "error", err)
s.logger.Warn("unable to open relay stream", "pid", pid, "error", err)
return LibP2PError{Err: err}
}
}
Expand Down
2 changes: 1 addition & 1 deletion state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func LoadOrNewState(

txPool.SetNewSandboxAndRecheck(st.concreteSandbox())

st.logger.Debug("last info", "committers", st.committee.Committers(), "state_root", st.stateRoot().ShortString())
st.logger.Debug("last info", "committers", st.committee.Committers(), "state_root", st.stateRoot())

return st, nil
}
Expand Down
2 changes: 1 addition & 1 deletion sync/firewall/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (f *Firewall) OpenGossipBundle(data []byte, source peer.ID, from peer.ID) *
if from != source {
f.peerSet.UpdateLastReceived(from)
if f.isPeerBanned(from) {
f.logger.Warn("firewall: from peer banned", "from", from.ShortString())
f.logger.Warn("firewall: from peer banned", "from", from)
f.closeConnection(from)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions sync/handler_blocks_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ func (handler *blocksRequestHandler) PrepareBundle(m message.Message) *bundle.Bu
func (handler *blocksRequestHandler) respond(msg *message.BlocksResponseMessage, to peer.ID) error {
if msg.ResponseCode == message.ResponseCodeRejected {
handler.logger.Error("rejecting block request message", "message", msg,
"to", to.ShortString(), "reason", msg.Reason)
"to", to, "reason", msg.Reason)
handler.network.CloseConnection(to)
} else {
handler.logger.Info("responding block request message", "message", msg,
"to", to.ShortString())
"to", to)
}

return handler.sendTo(msg, to)
Expand Down
4 changes: 2 additions & 2 deletions sync/handler_blocks_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (handler *blocksResponseHandler) ParseMessage(m message.Message, initiator
handler.logger.Trace("parsing BlocksResponse message", "message", msg)

if msg.IsRequestRejected() {
handler.logger.Warn("blocks request is rejected", "pid", initiator.ShortString(), "reason", msg.Reason)
handler.logger.Warn("blocks request is rejected", "pid", initiator, "reason", msg.Reason)
} else {
// TODO:
// It is good to check the latest height before adding blocks to the cache.
Expand Down Expand Up @@ -67,7 +67,7 @@ func (handler *blocksResponseHandler) updateSession(sessionID int, pid peer.ID,

if s.PeerID() != pid {
// TODO: test me
handler.logger.Warn("unknown peer", "session-id", sessionID, "pid", pid.ShortString())
handler.logger.Warn("unknown peer", "session-id", sessionID, "pid", pid)
return
}

Expand Down
6 changes: 3 additions & 3 deletions sync/handler_hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (handler *helloHandler) ParseMessage(m message.Message, initiator peer.ID)
}

handler.logger.Debug("updating peer info",
"pid", initiator.ShortString(),
"pid", initiator,
"moniker", msg.Moniker,
"services", msg.Services)

Expand All @@ -76,10 +76,10 @@ func (handler *helloHandler) acknowledge(msg *message.HelloAckMessage, to peer.I
handler.peerSet.UpdateStatus(to, peerset.StatusCodeBanned)

handler.logger.Warn("rejecting hello message", "message", msg,
"to", to.ShortString(), "reason", msg.Reason)
"to", to, "reason", msg.Reason)
} else {
handler.logger.Info("acknowledging hello message", "message", msg,
"to", to.ShortString())
"to", to)
}

return handler.sendTo(msg, to)
Expand Down
4 changes: 2 additions & 2 deletions sync/handler_hello_ack.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func (handler *helloAckHandler) ParseMessage(m message.Message, initiator peer.I

if msg.ResponseCode != message.ResponseCodeOK {
handler.logger.Warn("hello message rejected",
"from", initiator.ShortString(), "reason", msg.Reason)
"from", initiator, "reason", msg.Reason)

handler.network.CloseConnection(initiator)
return nil
}
handler.peerSet.UpdateStatus(initiator, peerset.StatusCodeKnown)
handler.logger.Debug("hello message acknowledged",
"from", initiator.ShortString())
"from", initiator)

return nil
}
Expand Down
16 changes: 8 additions & 8 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (sync *synchronizer) sayHello(to peer.ID) error {

sync.peerSet.UpdateStatus(to, peerset.StatusCodeConnected)

sync.logger.Info("sending Hello message", "to", to.ShortString())
sync.logger.Info("sending Hello message", "to", to)
return sync.sendTo(msg, to)
}

Expand Down Expand Up @@ -171,7 +171,7 @@ func (sync *synchronizer) receiveLoop() {
err := sync.processIncomingBundle(bdl)
if err != nil {
sync.logger.Warn("error on parsing a Gossip bundle",
"initiator", bdl.Initiator.ShortString(), "bundle", bdl, "error", err)
"initiator", bdl.Initiator, "bundle", bdl, "error", err)
sync.peerSet.IncreaseInvalidBundlesCounter(bdl.Initiator)
}

Expand All @@ -185,14 +185,14 @@ func (sync *synchronizer) receiveLoop() {
err := sync.processIncomingBundle(bdl)
if err != nil {
sync.logger.Warn("error on parsing a Stream bundle",
"initiator", bdl.Initiator.ShortString(), "bundle", bdl, "error", err)
"initiator", bdl.Initiator, "bundle", bdl, "error", err)
sync.peerSet.IncreaseInvalidBundlesCounter(bdl.Initiator)
}
case network.EventTypeConnect:
ce := e.(*network.ConnectEvent)
if err := sync.sayHello(ce.PeerID); err != nil {
sync.logger.Warn("sending Hello message failed",
"to", ce.PeerID.ShortString(), "error", err)
"to", ce.PeerID, "error", err)
}
case network.EventTypeDisconnect:
de := e.(*network.DisconnectEvent)
Expand All @@ -208,7 +208,7 @@ func (sync *synchronizer) processIncomingBundle(bdl *bundle.Bundle) error {
}

sync.logger.Info("received a bundle",
"initiator", bdl.Initiator.ShortString(), "bundle", bdl)
"initiator", bdl.Initiator, "bundle", bdl)
h := sync.handlers[bdl.Message.Type()]
if h == nil {
return errors.Errorf(errors.ErrInvalidMessage, "invalid message type: %v", bdl.Message.Type())
Expand Down Expand Up @@ -298,12 +298,12 @@ func (sync *synchronizer) sendTo(msg message.Message, to peer.ID) error {
err := sync.network.SendTo(data, to)
if err != nil {
sync.logger.Warn("error on sending bundle",
"bundle", bdl, "to", to.ShortString(), "error", err)
"bundle", bdl, "to", to, "error", err)

return err
}
sync.logger.Info("sending bundle to a peer",
"bundle", bdl, "to", to.ShortString())
"bundle", bdl, "to", to)
}
return nil
}
Expand Down Expand Up @@ -360,7 +360,7 @@ func (sync *synchronizer) downloadBlocks(from uint32, onlyNodeNetwork bool) {
}

count := LatestBlockInterval
sync.logger.Debug("sending download request", "from", from+1, "count", count, "pid", p.PeerID.ShortString())
sync.logger.Debug("sending download request", "from", from+1, "count", count, "pid", p.PeerID)
session := sync.peerSet.OpenSession(p.PeerID)
msg := message.NewBlocksRequestMessage(session.SessionID(), from+1, count)
err := sync.sendTo(msg, p.PeerID)
Expand Down
4 changes: 2 additions & 2 deletions types/tx/payload/bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func (p *BondPayload) Value() int64 {
func (p *BondPayload) BasicCheck() error {
if !p.From.IsAccountAddress() {
return BasicCheckError{
Reason: "sender is not an account address: " + p.From.ShortString(),
Reason: "sender is not an account address: " + p.From.String(),
}
}

if !p.To.IsValidatorAddress() {
return BasicCheckError{
Reason: "receiver is not a validator address: " + p.To.ShortString(),
Reason: "receiver is not a validator address: " + p.To.String(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions types/tx/payload/bond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestBondDecoding(t *testing.T) {
value: 0x200000,
readErr: nil,
basicErr: BasicCheckError{
Reason: "sender is not an account address: pc1pqgpsgpgx",
Reason: "sender is not an account address: pc1pqgpsgpgxquyqjzstpsxsurcszyfpx9q4vllmut",
},
},
{
Expand All @@ -110,7 +110,7 @@ func TestBondDecoding(t *testing.T) {
value: 0x200000,
readErr: nil,
basicErr: BasicCheckError{
Reason: "receiver is not a validator address: pc1zzgf3g9gk",
Reason: "receiver is not a validator address: pc1zzgf3g9gkzuvpjxsmrsw3u8eqyyfzxfp9yd9g68",
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion types/tx/payload/sortition.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (p *SortitionPayload) Value() int64 {
func (p *SortitionPayload) BasicCheck() error {
if !p.Validator.IsValidatorAddress() {
return BasicCheckError{
Reason: "address is not a validator address: " + p.Validator.ShortString(),
Reason: "address is not a validator address: " + p.Validator.String(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion types/tx/payload/sortition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestSortitionDecoding(t *testing.T) {
value: 0,
readErr: nil,
basicErr: BasicCheckError{
Reason: "address is not a validator address: pc1zqgpsgpgx",
Reason: "address is not a validator address: pc1zqgpsgpgxquyqjzstpsxsurcszyfpx9q4350xtk",
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions types/tx/payload/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func (p *TransferPayload) Value() int64 {
func (p *TransferPayload) BasicCheck() error {
if !p.From.IsAccountAddress() {
return BasicCheckError{
Reason: "sender is not an account address: " + p.From.ShortString(),
Reason: "sender is not an account address: " + p.From.String(),
}
}
if !p.To.IsAccountAddress() {
return BasicCheckError{
Reason: "receiver is not an account address: " + p.To.ShortString(),
Reason: "receiver is not an account address: " + p.To.String(),
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions types/tx/payload/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestTransferDecoding(t *testing.T) {
value: 0x200000,
readErr: nil,
basicErr: BasicCheckError{
Reason: "sender is not an account address: pc1pqgpsgpgx",
Reason: "sender is not an account address: pc1pqgpsgpgxquyqjzstpsxsurcszyfpx9q4vllmut",
},
},
{
Expand All @@ -89,7 +89,7 @@ func TestTransferDecoding(t *testing.T) {
value: 0x200000,
readErr: nil,
basicErr: BasicCheckError{
Reason: "receiver is not an account address: pc1pzgf3g9gk",
Reason: "receiver is not an account address: pc1pzgf3g9gkzuvpjxsmrsw3u8eqyyfzxfp9ex44d6",
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions types/tx/payload/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func (p *WithdrawPayload) Value() int64 {
func (p *WithdrawPayload) BasicCheck() error {
if !p.From.IsValidatorAddress() {
return BasicCheckError{
Reason: "sender is not a validator address: " + p.From.ShortString(),
Reason: "sender is not a validator address: " + p.From.String(),
}
}
if !p.To.IsAccountAddress() {
return BasicCheckError{
Reason: "receiver is not an account address: " + p.To.ShortString(),
Reason: "receiver is not an account address: " + p.To.String(),
}
}

Expand Down
Loading

0 comments on commit 6a9a37f

Please sign in to comment.