From 5cfaa00195ab522167e1deb40e80af82f881c733 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Tue, 27 Feb 2024 10:40:40 +0000 Subject: [PATCH] chore_: Add script for extracting logs --- _assets/scripts/README.md | 13 +++++ _assets/scripts/extract_logs.go | 90 +++++++++++++++++++++++++++++++ protocol/common/crypto.go | 8 +++ protocol/common/message_sender.go | 28 ++++++++-- 4 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 _assets/scripts/README.md create mode 100644 _assets/scripts/extract_logs.go diff --git a/_assets/scripts/README.md b/_assets/scripts/README.md new file mode 100644 index 000000000..d0df81dc0 --- /dev/null +++ b/_assets/scripts/README.md @@ -0,0 +1,13 @@ +# Scripts + +# extract_logs.go + +This script analyzes geth.log files in a specific format and extracts information related to "sent-message" actions. It then prints relevant details such as timestamp, recipients, message ID, message type, and hashes to the console. + +## Usage + +```bash +go run extract_logs.go +``` + +It will output in tab separated values (TSV) diff --git a/_assets/scripts/extract_logs.go b/_assets/scripts/extract_logs.go new file mode 100644 index 000000000..03f2d0ffd --- /dev/null +++ b/_assets/scripts/extract_logs.go @@ -0,0 +1,90 @@ +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "os" + "regexp" + "strings" + "time" +) + +var logPattern = regexp.MustCompile(`(?P[\d-]+T[\d:]+\.\d+Z).*(?Psent-message:.*)\s+(?P{.*})`) + +func main() { + if len(os.Args) != 2 { + fmt.Println("Usage: go run script.go ") + os.Exit(1) + } + + filename := os.Args[1] + file, err := os.Open(filename) + if err != nil { + fmt.Printf("Error opening file: %s\n", err) + os.Exit(1) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + + fmt.Printf("Timestamp\tMessageType\tContentType\tMessageID\tHashes\tRecipients\n") + for scanner.Scan() { + line := scanner.Text() + + // Check if the line contains "sent-message" + if strings.Contains(line, "sent-message") { + match := logPattern.FindStringSubmatch(line) + + // Ensure the match is not nil and has expected groups + if match != nil && len(match) > 3 { + logTime, _ := time.Parse(time.RFC3339Nano, matchMap("timestamp", match)) + logData := matchMap("logData", match) + + var data map[string]interface{} + if err := json.Unmarshal([]byte(logData), &data); err == nil { + recipients := arrayToString(data["recipient"]) + messageID := fmt.Sprintf("%v", data["messageID"]) + messageType := fmt.Sprintf("%v", data["messageType"]) + contentType := fmt.Sprintf("%v", data["contentType"]) + hashes := arrayToString(data["hashes"]) + + // Print the required information + fmt.Printf("%s\t%s\t%s\t%s\t%s\t%s\t\n", + logTime.Format(time.RFC3339Nano), messageType, contentType, messageID, hashes, recipients) + } + } else { + fmt.Printf("Warning: Line does not match expected format: %s\n", line) + } + } + } + + if err := scanner.Err(); err != nil { + fmt.Printf("Error reading file: %s\n", err) + } +} + +// Helper function to convert an array to a string +func arrayToString(arr interface{}) string { + if arr != nil { + switch v := arr.(type) { + case []interface{}: + var result []string + for _, item := range v { + result = append(result, fmt.Sprintf("%v", item)) + } + return strings.Join(result, ", ") + } + } + return "" +} + +// Helper function to get the value of a named capture group from regex match +func matchMap(key string, matches []string) string { + for i, name := range logPattern.SubexpNames() { + if name == key { + return matches[i] + } + } + return "" +} diff --git a/protocol/common/crypto.go b/protocol/common/crypto.go index 86c770277..ec2eadfb6 100644 --- a/protocol/common/crypto.go +++ b/protocol/common/crypto.go @@ -85,6 +85,14 @@ func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool { return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0 } +func PubkeysToHex(keys []*ecdsa.PublicKey) []string { + var result []string + for _, k := range keys { + result = append(result, PubkeyToHex(k)) + } + return result +} + func PubkeyToHex(key *ecdsa.PublicKey) string { return types.EncodeHex(crypto.FromECDSAPub(key)) } diff --git a/protocol/common/message_sender.go b/protocol/common/message_sender.go index 3d6a0feb8..219d6c6f4 100644 --- a/protocol/common/message_sender.go +++ b/protocol/common/message_sender.go @@ -384,7 +384,12 @@ func (s *MessageSender) sendCommunity( } } - s.logger.Debug("sent community message ", zap.String("messageID", messageID.String()), zap.Strings("hashes", types.EncodeHexes(hashes))) + s.logger.Debug("sent-message: community ", + zap.Strings("recipient", PubkeysToHex(rawMessage.Recipients)), + zap.String("messageID", messageID.String()), + zap.String("messageType", "community"), + zap.Any("contentType", rawMessage.MessageType), + zap.Strings("hashes", types.EncodeHexes(hashes))) s.transport.Track(messageID, hashes, newMessages) return messageID, nil @@ -470,7 +475,12 @@ func (s *MessageSender) sendPrivate( return nil, errors.Wrap(err, "failed to send a message spec") } - s.logger.Debug("sent private message skipProtocolLayer", zap.String("messageID", messageID.String()), zap.Strings("hashes", types.EncodeHexes(hashes))) + s.logger.Debug("sent-message: private skipProtocolLayer", + zap.Strings("recipient", PubkeysToHex(rawMessage.Recipients)), + zap.String("messageID", messageID.String()), + zap.String("messageType", "private"), + zap.Any("contentType", rawMessage.MessageType), + zap.Strings("hashes", types.EncodeHexes(hashes))) s.transport.Track(messageID, hashes, newMessages) } else { @@ -485,7 +495,12 @@ func (s *MessageSender) sendPrivate( return nil, errors.Wrap(err, "failed to send a message spec") } - s.logger.Debug("sent private message without datasync", zap.String("messageID", messageID.String()), zap.Strings("hashes", types.EncodeHexes(hashes))) + s.logger.Debug("sent-message: private without datasync", + zap.Strings("recipient", PubkeysToHex(rawMessage.Recipients)), + zap.String("messageID", messageID.String()), + zap.Any("contentType", rawMessage.MessageType), + zap.String("messageType", "private"), + zap.Strings("hashes", types.EncodeHexes(hashes))) s.transport.Track(messageID, hashes, newMessages) } @@ -737,7 +752,12 @@ func (s *MessageSender) SendPublic( s.notifyOnSentMessage(sentMessage) - s.logger.Debug("sent public message", zap.String("messageID", messageID.String()), zap.Strings("hashes", types.EncodeHexes(hashes))) + s.logger.Debug("sent-message: public message", + zap.Strings("recipient", PubkeysToHex(rawMessage.Recipients)), + zap.String("messageID", messageID.String()), + zap.Any("contentType", rawMessage.MessageType), + zap.String("messageType", "public"), + zap.Strings("hashes", types.EncodeHexes(hashes))) s.transport.Track(messageID, hashes, newMessages) return messageID, nil