Skip to content

Commit

Permalink
fix(pin_integrity): changed route and added openapi (ethersphere#4616)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Mar 20, 2024
1 parent 47c612a commit ee8fa15
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 138 deletions.
24 changes: 24 additions & 0 deletions openapi/Swarm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,30 @@ paths:
default:
description: Default response

"/pins/check":
get:
summary: Validate pinned chunks integerity
tags:
- Pinning
parameters:
- in: query
name: ref
schema:
$ref: "SwarmCommon.yaml#/components/schemas/SwarmOnlyReference"
required: false
description: The number of items to skip before starting to collect the result set.
responses:
"200":
description: List of checked root hash references
content:
application/json:
schema:
$ref: "SwarmCommon.yaml#/components/schemas/PinCheckResponse"
"500":
$ref: "SwarmCommon.yaml#/components/responses/500"
default:
description: Default response

"/pss/send/{topic}/{targets}":
post:
summary: Send to recipient or target with Postal Service for Swarm
Expand Down
17 changes: 15 additions & 2 deletions openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
openapi: 3.0.3
info:
version: 3.2.6
version: 3.2.7
title: Common Data Types
description: |
\*****bzzz*****
Expand Down Expand Up @@ -602,15 +602,28 @@ components:
- $ref: "#/components/schemas/SwarmAddress"
- $ref: "#/components/schemas/SwarmEncryptedReference"

PinCheckResponse:
type: object
properties:
reference:
$ref: "#/components/schemas/SwarmOnlyReference"
total:
type: integer
missing:
type: integer
invalid:
type: integer

SwarmOnlyReferencesList:
type: object
properties:
references:
reference:
type: array
nullable: false
items:
$ref: "#/components/schemas/SwarmOnlyReference"


SwarmReference:
oneOf:
- $ref: "#/components/schemas/SwarmAddress"
Expand Down
2 changes: 1 addition & 1 deletion openapi/SwarmDebug.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
openapi: 3.0.3
info:
version: 4.1.0
version: 4.1.1
title: Bee Debug API
description: "A list of the currently provided debug interfaces to interact with the bee node"

Expand Down
62 changes: 0 additions & 62 deletions pkg/api/integritycheck.go

This file was deleted.

67 changes: 0 additions & 67 deletions pkg/api/integritycheck_test.go

This file was deleted.

52 changes: 52 additions & 0 deletions pkg/api/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package api

import (
"encoding/json"
"errors"
"net/http"
"sync"

"github.com/ethersphere/bee/v2/pkg/jsonhttp"
"github.com/ethersphere/bee/v2/pkg/storage"
storer "github.com/ethersphere/bee/v2/pkg/storer"
"github.com/ethersphere/bee/v2/pkg/swarm"
"github.com/ethersphere/bee/v2/pkg/traversal"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -199,3 +201,53 @@ func (s *Service) listPinnedRootHashes(w http.ResponseWriter, r *http.Request) {
References: pinned,
})
}

type PinIntegrityResponse struct {
Reference swarm.Address `json:"reference"`
Total int `json:"total"`
Missing int `json:"missing"`
Invalid int `json:"invalid"`
}

func (s *Service) pinIntegrityHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("get_pin_integrity").Build()

querie := struct {
Ref swarm.Address `map:"ref"`
}{}

if response := s.mapStructure(r.URL.Query(), &querie); response != nil {
response("invalid query params", logger, w)
return
}

out := make(chan storer.PinStat)

go s.pinIntegrity.Check(r.Context(), logger, querie.Ref.String(), out)

flusher, ok := w.(http.Flusher)
if !ok {
http.NotFound(w, r)
return
}

w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
flusher.Flush()

enc := json.NewEncoder(w)

for v := range out {
resp := PinIntegrityResponse{
Reference: v.Ref,
Total: v.Total,
Missing: v.Missing,
Invalid: v.Invalid,
}
if err := enc.Encode(resp); err != nil {
break
}
flusher.Flush()
}
}
54 changes: 54 additions & 0 deletions pkg/api/pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
package api_test

import (
"context"
"net/http"
"strings"
"testing"

"github.com/ethersphere/bee/v2/pkg/api"
"github.com/ethersphere/bee/v2/pkg/jsonhttp"
"github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/v2/pkg/log"
mockpost "github.com/ethersphere/bee/v2/pkg/postage/mock"
storage "github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storage/inmemstore"
storer "github.com/ethersphere/bee/v2/pkg/storer"
mockstorer "github.com/ethersphere/bee/v2/pkg/storer/mock"
"github.com/ethersphere/bee/v2/pkg/swarm"
)
Expand Down Expand Up @@ -185,3 +190,52 @@ func TestPinHandlersInvalidInputs(t *testing.T) {
}
}
}

const pinRef = "620fcd78c7ce54da2d1b7cc2274a02e190cbe8fecbc3bd244690ab6517ce8f39"

func TestIntegrityHandler(t *testing.T) {

t.Parallel()

t.Run("ok", func(t *testing.T) {
t.Parallel()
testServer, _, _, _ := newTestServer(t, testServerOptions{
PinIntegrity: &mockPinIntegrity{
Store: inmemstore.New(),
tester: t,
},
})

endp := "/pins/check?ref=" + pinRef

// When probe is not set health endpoint should indicate that node is not healthy
jsonhttptest.Request(t, testServer, http.MethodGet, endp, http.StatusOK, jsonhttptest.WithExpectedResponse(nil))
})

t.Run("wrong hash format", func(t *testing.T) {
t.Parallel()
testServer, _, _, _ := newTestServer(t, testServerOptions{
PinIntegrity: &mockPinIntegrity{
Store: inmemstore.New(),
tester: t,
},
})

endp := "/pins/check?ref=0xbadhash"

// When probe is not set health endpoint should indicate that node is not healthy
jsonhttptest.Request(t, testServer, http.MethodGet, endp, http.StatusBadRequest, jsonhttptest.WithExpectedResponse(nil))
})
}

type mockPinIntegrity struct {
tester *testing.T
Store storage.Store
}

func (p *mockPinIntegrity) Check(ctx context.Context, logger log.Logger, pin string, out chan storer.PinStat) {
if pin != pinRef {
p.tester.Fatal("bad pin", pin)
}
close(out)
}
12 changes: 6 additions & 6 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ func (s *Service) mountAPI() {
})),
)

handle("/pins/check", web.ChainHandlers(
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.pinIntegrityHandler),
}),
))

handle("/pins/{reference}", web.ChainHandlers(
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getPinnedRootHash),
Expand Down Expand Up @@ -608,10 +614,4 @@ func (s *Service) mountBusinessDebug(restricted bool) {
"GET": http.HandlerFunc(s.rchash),
}),
))

handle("/check/pin", web.ChainHandlers(
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.pinIntegrityHandler),
}),
))
}

0 comments on commit ee8fa15

Please sign in to comment.