From 7a831a9a044bee4bc214d6e5ef67403336bdfef7 Mon Sep 17 00:00:00 2001 From: Hendrik Hofstadt Date: Fri, 13 Mar 2020 14:31:34 +0100 Subject: [PATCH] Sanitize non-utf8 spec IDs --- cmd/chainlink_exporter/aggregator.go | 6 +++--- cmd/chainlink_exporter/monitor.go | 26 ++++++++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cmd/chainlink_exporter/aggregator.go b/cmd/chainlink_exporter/aggregator.go index f67e875..9633aaf 100644 --- a/cmd/chainlink_exporter/aggregator.go +++ b/cmd/chainlink_exporter/aggregator.go @@ -79,7 +79,7 @@ func (a *AggregatorMonitor) HandleNewBlock(height uint64) { if delta > 15 { zap.L().Info("job fulfillment slot missed", zap.Uint64("height", n.Raw.BlockNumber), zap.String("requester", n.Requester.String()), zap.Binary("request_id", n.RequestId[:]), - zap.ByteString("spec_id", n.SpecId[:])) + zap.String("spec_id", sanitizeSpecID(n.SpecId))) delete(a.pendingJobs, reqID) a.monitor.HandleMiss(n) } @@ -93,7 +93,7 @@ func (a *AggregatorMonitor) handleRequest(res *abi.OracleOracleRequest) { if _, exists := a.seenRequestIDs[requestIDString]; exists { zap.L().Info("request dropped; already seen same reqID", zap.Uint64("height", res.Raw.BlockNumber), zap.String("requester", res.Requester.String()), zap.Binary("request_id", res.RequestId[:]), - zap.ByteString("spec_id", res.SpecId[:]), zap.Uint64("request_height", res.Raw.BlockNumber)) + zap.String("spec_id", sanitizeSpecID(res.SpecId)), zap.Uint64("request_height", res.Raw.BlockNumber)) return } else { a.seenRequestIDs[requestIDString] = true @@ -109,7 +109,7 @@ func (a *AggregatorMonitor) handleFulfillment(res *abi.AggregatorChainlinkFulfil if job, ok := a.pendingJobs[hex.EncodeToString(res.Id[:])]; ok { zap.L().Info("job fulfilled", zap.Uint64("height", res.Raw.BlockNumber), zap.String("requester", job.Requester.String()), zap.Binary("request_id", job.RequestId[:]), - zap.ByteString("spec_id", job.SpecId[:]), zap.Uint64("request_height", job.Raw.BlockNumber)) + zap.String("spec_id", sanitizeSpecID(job.SpecId)), zap.Uint64("request_height", job.Raw.BlockNumber)) delete(a.pendingJobs, hex.EncodeToString(res.Id[:])) a.monitor.HandleFulfillment(res, job) diff --git a/cmd/chainlink_exporter/monitor.go b/cmd/chainlink_exporter/monitor.go index 8be2915..7fbac27 100644 --- a/cmd/chainlink_exporter/monitor.go +++ b/cmd/chainlink_exporter/monitor.go @@ -3,6 +3,7 @@ package main import ( "chainlink_exporter/abi" "context" + "encoding/hex" "fmt" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" @@ -15,6 +16,7 @@ import ( "math/big" "sync" "time" + "unicode/utf8" ) const ( @@ -291,7 +293,7 @@ func (m *Monitor) requestRoutine() { func (m *Monitor) handleRequest(req *abi.OracleOracleRequest) error { logger := zap.L().With(zap.Uint64("height", req.Raw.BlockNumber), zap.String("requester", req.Requester.String()), zap.Binary("request_id", req.RequestId[:]), - zap.ByteString("spec_id", req.SpecId[:])) + zap.String("spec_id", sanitizeSpecID(req.SpecId))) logger.Info("received request") if old := m.lastReqTime.Load(); old < req.Raw.BlockNumber { @@ -334,13 +336,25 @@ func (m *Monitor) HandleFulfillment(res *abi.AggregatorChainlinkFulfilled, req * } deltaBlocks := res.Raw.BlockNumber - req.Raw.BlockNumber - m.responseTimeHistogram.WithLabelValues(string(req.SpecId[:])).Observe(float64(deltaBlocks)) - m.fulfillmentCounter.WithLabelValues(string(req.SpecId[:]), req.Requester.String()).Inc() - m.revenueCounter.WithLabelValues(string(req.SpecId[:]), req.Requester.String(), "fulfilled").Add(float64(req.Payment.Uint64()) / params.Ether) + sanitizedSpecID := sanitizeSpecID(req.SpecId) + m.responseTimeHistogram.WithLabelValues(sanitizedSpecID).Observe(float64(deltaBlocks)) + + m.fulfillmentCounter.WithLabelValues(sanitizedSpecID, req.Requester.String()).Inc() + m.revenueCounter.WithLabelValues(sanitizedSpecID, req.Requester.String(), "fulfilled").Add(float64(req.Payment.Uint64()) / params.Ether) } func (m *Monitor) HandleMiss(req *abi.OracleOracleRequest) { - m.missCounter.WithLabelValues(string(req.SpecId[:]), req.Requester.String()).Inc() - m.revenueCounter.WithLabelValues(string(req.SpecId[:]), req.Requester.String(), "missed").Add(float64(req.Payment.Uint64()) / params.Ether) + sanitizedSpecID := sanitizeSpecID(req.SpecId) + + m.missCounter.WithLabelValues(sanitizedSpecID, req.Requester.String()).Inc() + m.revenueCounter.WithLabelValues(sanitizedSpecID, req.Requester.String(), "missed").Add(float64(req.Payment.Uint64()) / params.Ether) +} + +func sanitizeSpecID(specID [32]byte) string { + if !utf8.Valid(specID[:]) { + return hex.EncodeToString(specID[:]) + } else { + return string(specID[:]) + } }