-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pinned reference integrity check API (#4573)
- Loading branch information
Showing
8 changed files
with
277 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2023 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
storer "github.com/ethersphere/bee/pkg/storer" | ||
"github.com/ethersphere/bee/pkg/swarm" | ||
) | ||
|
||
type PinIntegrityResponse struct { | ||
Ref swarm.Address `json:"ref"` | ||
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{ | ||
Ref: v.Ref, | ||
Total: v.Total, | ||
Missing: v.Missing, | ||
Invalid: v.Invalid, | ||
} | ||
if err := enc.Encode(resp); err != nil { | ||
break | ||
} | ||
flusher.Flush() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2022 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package api_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" | ||
"github.com/ethersphere/bee/pkg/log" | ||
storage "github.com/ethersphere/bee/pkg/storage" | ||
"github.com/ethersphere/bee/pkg/storage/inmemstore" | ||
storer "github.com/ethersphere/bee/pkg/storer" | ||
) | ||
|
||
const pinRef = "620fcd78c7ce54da2d1b7cc2274a02e190cbe8fecbc3bd244690ab6517ce8f39" | ||
|
||
func TestIntegrityHandler(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("ok", func(t *testing.T) { | ||
t.Parallel() | ||
testServer, _, _, _ := newTestServer(t, testServerOptions{ | ||
DebugAPI: true, | ||
PinIntegrity: &mockPinIntegrity{ | ||
Store: inmemstore.New(), | ||
tester: t, | ||
}, | ||
}) | ||
|
||
endp := "/check/pin?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{ | ||
DebugAPI: true, | ||
PinIntegrity: &mockPinIntegrity{ | ||
Store: inmemstore.New(), | ||
tester: t, | ||
}, | ||
}) | ||
|
||
endp := "/check/pin?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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.