Skip to content

Commit

Permalink
fix failed count; improved logging (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
decentralgabe authored Jun 18, 2024
1 parent b51c873 commit e299563
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
18 changes: 9 additions & 9 deletions impl/pkg/server/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func (r *DHTRouter) GetRecord(c *gin.Context) {
// make sure the key is valid
key, err := util.Z32Decode(*id)
if err != nil {
LoggingRespondErrWithMsg(c, err, "invalid record id", http.StatusInternalServerError)
LoggingRespondErrWithMsg(c, err, fmt.Sprintf("invalid record id: %s", *id), http.StatusInternalServerError)
return
}
if len(key) != ed25519.PublicKeySize {
LoggingRespondErrMsg(c, "invalid z32 encoded ed25519 public key", http.StatusBadRequest)
LoggingRespondErrMsg(c, fmt.Sprintf("invalid z32 encoded ed25519 public key: %s", *id), http.StatusBadRequest)
return
}

Expand All @@ -67,11 +67,11 @@ func (r *DHTRouter) GetRecord(c *gin.Context) {
LoggingRespondErrMsg(c, fmt.Sprintf("too many requests for bad key %s", *id), http.StatusTooManyRequests)
return
}
LoggingRespondErrWithMsg(c, err, "failed to get dht record", http.StatusInternalServerError)
LoggingRespondErrWithMsg(c, err, fmt.Sprintf("failed to get dht record: %s", *id), http.StatusInternalServerError)
return
}
if resp == nil {
LoggingRespondErrMsg(c, "dht record not found", http.StatusNotFound)
LoggingRespondErrMsg(c, fmt.Sprintf("dht record not found: %s", *id), http.StatusNotFound)
return
}

Expand Down Expand Up @@ -106,24 +106,24 @@ func (r *DHTRouter) PutRecord(c *gin.Context) {
}
key, err := util.Z32Decode(*id)
if err != nil {
LoggingRespondErrWithMsg(c, err, "invalid record id", http.StatusInternalServerError)
LoggingRespondErrWithMsg(c, err, fmt.Sprintf("invalid record id: %s", *id), http.StatusInternalServerError)
return
}
if len(key) != ed25519.PublicKeySize {
LoggingRespondErrMsg(c, "invalid z32 encoded ed25519 public key", http.StatusBadRequest)
LoggingRespondErrMsg(c, fmt.Sprintf("invalid z32 encoded ed25519 public key: %s", *id), http.StatusBadRequest)
return
}

body, err := io.ReadAll(c.Request.Body)
if err != nil {
LoggingRespondErrWithMsg(c, err, "failed to read body", http.StatusInternalServerError)
LoggingRespondErrWithMsg(c, err, fmt.Sprintf("failed to read body for id: %s", *id), http.StatusInternalServerError)
return
}
defer c.Request.Body.Close()

// 64 byte signature and 8 byte sequence number
if len(body) <= 72 {
LoggingRespondErrMsg(c, "invalid request body", http.StatusBadRequest)
LoggingRespondErrMsg(c, fmt.Sprintf("invalid request body for id: %s", *id), http.StatusBadRequest)
return
}

Expand All @@ -138,7 +138,7 @@ func (r *DHTRouter) PutRecord(c *gin.Context) {
}

if err = r.service.PublishDHT(ctx, *id, *request); err != nil {
LoggingRespondErrWithMsg(c, err, "failed to publish dht record", http.StatusInternalServerError)
LoggingRespondErrWithMsg(c, err, fmt.Sprintf("failed to publish dht record: %s", *id), http.StatusInternalServerError)
return
}

Expand Down
4 changes: 1 addition & 3 deletions impl/pkg/storage/db/bolt/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,7 @@ func (b *Bolt) WriteFailedRecord(ctx context.Context, id string) error {
var count int32 = 1
v := bucket.Get([]byte(id))
if v != nil {
if err = json.Unmarshal(v, &count); err != nil {
return err
}
count = int32(binary.LittleEndian.Uint32(v))
count++
}

Expand Down

0 comments on commit e299563

Please sign in to comment.