Skip to content

Commit 31a8b77

Browse files
committed
Minor improvements in logging
1 parent 15bb9ec commit 31a8b77

File tree

6 files changed

+80
-58
lines changed

6 files changed

+80
-58
lines changed

cmd/tss/tss_http.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,36 +129,36 @@ func (t *HTTPServer) keySignHandler(w http.ResponseWriter, r *http.Request) {
129129
w.WriteHeader(http.StatusMethodNotAllowed)
130130
return
131131
}
132+
132133
defer func() {
133134
if err := r.Body.Close(); nil != err {
134135
t.logger.Error().Err(err).Msg("fail to close request body")
135136
}
136137
}()
137-
t.logger.Info().Msg("receive key sign request")
138138

139139
var keySignReq keysign.Request
140-
decoder := json.NewDecoder(r.Body)
141-
if err := decoder.Decode(&keySignReq); nil != err {
140+
141+
if err := json.NewDecoder(r.Body).Decode(&keySignReq); nil != err {
142142
t.logger.Error().Err(err).Msg("fail to decode key sign request")
143143
w.WriteHeader(http.StatusBadRequest)
144144
return
145145
}
146-
t.logger.Info().Any("request", keySignReq).Msg("received key sign request")
146+
147147
signResp, err := t.tssServer.KeySign(keySignReq)
148148
if err != nil {
149149
t.logger.Error().Err(err).Msg("fail to key sign")
150150
w.WriteHeader(http.StatusInternalServerError)
151151
return
152152
}
153153

154-
jsonResult, err := json.MarshalIndent(signResp, "", " ")
154+
jsonResult, err := json.MarshalIndent(signResp, "", " ")
155155
if err != nil {
156156
t.logger.Error().Err(err).Msg("fail to marshal response to json message")
157157
w.WriteHeader(http.StatusInternalServerError)
158158
return
159159
}
160-
_, err = w.Write(jsonResult)
161-
if err != nil {
160+
161+
if _, err = w.Write(jsonResult); err != nil {
162162
t.logger.Error().Err(err).Msg("fail to write response")
163163
}
164164
}

keysign/ecdsa/tss_keysign.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func (tKeySign *TssKeySign) SignMessage(
107107
tKeySign.logger.Info().Msg("we are not in this rounds key sign")
108108
return nil, nil
109109
}
110+
110111
threshold, err := conversion.GetThreshold(len(localStateItem.ParticipantKeys))
111112
if err != nil {
112113
return nil, errors.New("fail to get threshold")
@@ -147,11 +148,15 @@ func (tKeySign *TssKeySign) SignMessage(
147148

148149
blameMgr := tKeySign.tssCommonStruct.GetBlameMgr()
149150
partyIDMap := conversion.SetupPartyIDMap(partiesID)
150-
err1 := conversion.SetupIDMaps(partyIDMap, tKeySign.tssCommonStruct.PartyIDtoP2PID)
151-
err2 := conversion.SetupIDMaps(partyIDMap, blameMgr.PartyIDtoP2PID)
152-
if err1 != nil || err2 != nil {
153-
tKeySign.logger.Error().Err(err).Msg("error in creating mapping between partyID and P2P ID")
154-
return nil, err
151+
152+
err = conversion.SetupIDMaps(partyIDMap, tKeySign.tssCommonStruct.PartyIDtoP2PID)
153+
if err != nil {
154+
return nil, fmt.Errorf("fail to setup id maps #1: %w", err)
155+
}
156+
157+
err = conversion.SetupIDMaps(partyIDMap, blameMgr.PartyIDtoP2PID)
158+
if err != nil {
159+
return nil, fmt.Errorf("fail to setup id maps #2: %w", err)
155160
}
156161

157162
tKeySign.tssCommonStruct.SetPartyInfo(&common.PartyInfo{
@@ -198,7 +203,7 @@ func (tKeySign *TssKeySign) SignMessage(
198203
a := new(big.Int).SetBytes(results[i].M)
199204
b := new(big.Int).SetBytes(results[j].M)
200205

201-
return a.Cmp(b) != -1
206+
return a.Cmp(b) >= 0
202207
})
203208

204209
return results, nil

keysign/eddsa/tss_keysign.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (tKeySign *KeySign) SignMessage(
205205
a := new(big.Int).SetBytes(results[i].M)
206206
b := new(big.Int).SetBytes(results[j].M)
207207

208-
return a.Cmp(b) != -1
208+
return a.Cmp(b) >= 0
209209
})
210210

211211
return results, nil

keysign/request.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package keysign
22

3+
import "github.com/rs/zerolog"
4+
35
// Request request to sign a message
46
type Request struct {
57
PoolPubKey string `json:"pool_pub_key"` // pub key of the pool that we would like to send this message from
@@ -18,3 +20,8 @@ func NewRequest(pk string, msgs []string, blockHeight int64, signers []string, v
1820
Version: version,
1921
}
2022
}
23+
24+
func (r *Request) MarshalZerologObject(e *zerolog.Event) {
25+
e.Strs("request.messages", r.Messages)
26+
e.Int64("request.block_height", r.BlockHeight)
27+
}

p2p/communication.go

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -162,56 +162,63 @@ func (c *Communication) writeToStream(pID peer.ID, msg []byte, msgID string) err
162162

163163
func (c *Communication) readFromStream(stream network.Stream) {
164164
peerID := stream.Conn().RemotePeer().String()
165-
c.logger.Debug().Msgf("reading from stream of peer: %s", peerID)
165+
c.logger.Debug().Str(logs.Peer, peerID).Msg("Reading from peer's stream")
166166

167167
const timeout = 10 * time.Second
168168

169-
select {
170-
case <-c.stopChan:
169+
payload, err := ReadStreamWithBuffer(stream)
170+
if err != nil {
171+
c.logger.Error().Err(err).Str(logs.Peer, peerID).Msg("fail to read from stream")
172+
c.streamMgr.AddStream("UNKNOWN", stream)
173+
return
174+
}
175+
176+
var wrappedMsg messages.WrappedMessage
177+
if err := json.Unmarshal(payload, &wrappedMsg); nil != err {
178+
c.logger.Error().Err(err).Msg("fail to unmarshal wrapped message bytes")
179+
c.streamMgr.AddStream("UNKNOWN", stream)
171180
return
172-
default:
173-
dataBuf, err := ReadStreamWithBuffer(stream)
174-
if err != nil {
175-
c.logger.Error().Err(err).Str(logs.Peer, peerID).Msg("fail to read from stream")
176-
c.streamMgr.AddStream("UNKNOWN", stream)
177-
return
178-
}
179-
var wrappedMsg messages.WrappedMessage
180-
if err := json.Unmarshal(dataBuf, &wrappedMsg); nil != err {
181-
c.logger.Error().Err(err).Msg("fail to unmarshal wrapped message bytes")
182-
c.streamMgr.AddStream("UNKNOWN", stream)
183-
return
184-
}
185-
c.logger.Debug().Msgf(">>>>>>>[%s] %s", wrappedMsg.MessageType, string(wrappedMsg.Payload))
186-
channel := c.getSubscriber(wrappedMsg.MessageType, wrappedMsg.MsgID)
187-
if nil == channel {
188-
c.logger.Debug().Msgf("no MsgID %s found for this message", wrappedMsg.MsgID)
189-
c.logger.Debug().Msgf("no MsgID %s found for this message", wrappedMsg.MessageType)
190-
_ = stream.Reset()
191-
return
192-
}
193-
c.streamMgr.AddStream(wrappedMsg.MsgID, stream)
194-
select {
195-
case <-time.After(timeout):
196-
c.logger.Warn().
197-
Str(logs.MsgID, wrappedMsg.MsgID).
198-
Str(logs.Peer, peerID).
199-
Str("protocol", string(stream.Protocol())).
200-
Str("message_type", wrappedMsg.MessageType.String()).
201-
Float64("timeout", timeout.Seconds()).
202-
Msg("Timeout to send message to channel")
203-
case channel <- &Message{
204-
PeerID: stream.Conn().RemotePeer(),
205-
Payload: dataBuf}:
206-
}
181+
}
182+
183+
channel := c.getSubscriber(wrappedMsg.MessageType, wrappedMsg.MsgID)
184+
if nil == channel {
185+
c.logger.Debug().Msgf("no MsgID %s found for this message", wrappedMsg.MsgID)
186+
c.logger.Debug().Msgf("no MsgID %s found for this message", wrappedMsg.MessageType)
187+
_ = stream.Reset()
188+
return
189+
}
190+
191+
c.streamMgr.AddStream(wrappedMsg.MsgID, stream)
192+
193+
msg := &Message{
194+
PeerID: stream.Conn().RemotePeer(),
195+
Payload: payload,
196+
}
197+
198+
select {
199+
case channel <- msg:
200+
// all good, message sent
201+
case <-time.After(timeout):
202+
// Note that we aren't logging payload itself
203+
// as it might contain sensitive information
204+
c.logger.Warn().
205+
Str(logs.MsgID, wrappedMsg.MsgID).
206+
Str(logs.Peer, peerID).
207+
Str("protocol", string(stream.Protocol())).
208+
Str("message_type", wrappedMsg.MessageType.String()).
209+
Int("message_payload_bytes", len(wrappedMsg.Payload)).
210+
Float64("timeout", timeout.Seconds()).
211+
Msg("readFromStream: timeout to send message to channel")
207212
}
208213
}
209214

210215
func (c *Communication) handleStream(stream network.Stream) {
211-
peerID := stream.Conn().RemotePeer().String()
212-
c.logger.Debug().Msgf("handle stream from peer: %s", peerID)
213-
// we will read from that stream
214-
c.readFromStream(stream)
216+
select {
217+
case <-c.stopChan:
218+
return
219+
default:
220+
c.readFromStream(stream)
221+
}
215222
}
216223

217224
func (c *Communication) bootStrapConnectivityCheck() error {

tss/keysign.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (t *Server) generateSignature(
203203
// the statistic of keygen only care about Tss it self, even if the following http response aborts,
204204
// it still counted as a successful keygen as the Tss model runs successfully.
205205
if err != nil {
206-
t.logger.Error().Err(err).Msg("err in keysign")
206+
t.logger.Error().Err(err).Msg("SignMessage failed")
207207
sigChan <- "signature generated"
208208
t.broadcastKeysignFailure(msgID, allPeersID)
209209
blameNodes := *blameMgr.GetBlame()
@@ -237,7 +237,10 @@ func (t *Server) KeySign(req keysign.Request) (keysign.Response, error) {
237237
return emptyResp, err
238238
}
239239

240-
t.logger.Info().Str(logs.MsgID, msgID).Any("request", req).Msg("Keysign request")
240+
t.logger.Info().
241+
Str(logs.MsgID, msgID).
242+
Object("request", &req).
243+
Msg("Keysign request")
241244

242245
var keysignInstance keysign.TssKeySign
243246
var algo common.Algo

0 commit comments

Comments
 (0)