Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
8b240282
Unverified
Commit
8b240282
authored
May 29, 2020
by
Janoš Guljaš
Committed by
GitHub
May 29, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve list and get pinned chunks api endpoints and tests (#233)
parent
58bc639b
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
95 additions
and
60 deletions
+95
-60
export_test.go
pkg/debugapi/export_test.go
+6
-4
pin.go
pkg/debugapi/pin.go
+57
-6
pin_test.go
pkg/debugapi/pin_test.go
+24
-16
router.go
pkg/debugapi/router.go
+1
-1
jsonhttptest.go
pkg/jsonhttp/jsonhttptest/jsonhttptest.go
+0
-31
pin.go
pkg/localstore/pin.go
+5
-0
pin_test.go
pkg/localstore/pin_test.go
+1
-1
storer.go
pkg/storage/mock/storer.go
+1
-1
No files found.
pkg/debugapi/export_test.go
View file @
8b240282
...
...
@@ -5,8 +5,10 @@
package
debugapi
type
(
StatusResponse
=
statusResponse
PeerConnectResponse
=
peerConnectResponse
PeersResponse
=
peersResponse
AddressesResponse
=
addressesResponse
StatusResponse
=
statusResponse
PeerConnectResponse
=
peerConnectResponse
PeersResponse
=
peersResponse
AddressesResponse
=
addressesResponse
PinnedChunk
=
pinnedChunk
ListPinnedChunksResponse
=
listPinnedChunksResponse
)
pkg/debugapi/pin.go
View file @
8b240282
...
...
@@ -5,7 +5,7 @@
package
debugapi
import
(
"
context
"
"
errors
"
"net/http"
"github.com/ethersphere/bee/pkg/jsonhttp"
...
...
@@ -29,7 +29,7 @@ func (s *server) pinChunk(w http.ResponseWriter, r *http.Request) {
has
,
err
:=
s
.
Storer
.
Has
(
r
.
Context
(),
addr
)
if
err
!=
nil
{
s
.
Logger
.
Debugf
(
"debug api: pin chunk: localstore has: %v"
,
err
)
jsonhttp
.
BadRequest
(
w
,
err
)
jsonhttp
.
InternalServerError
(
w
,
err
)
return
}
...
...
@@ -60,7 +60,7 @@ func (s *server) unpinChunk(w http.ResponseWriter, r *http.Request) {
has
,
err
:=
s
.
Storer
.
Has
(
r
.
Context
(),
addr
)
if
err
!=
nil
{
s
.
Logger
.
Debugf
(
"debug api: pin chunk: localstore has: %v"
,
err
)
jsonhttp
.
BadRequest
(
w
,
err
)
jsonhttp
.
InternalServerError
(
w
,
err
)
return
}
...
...
@@ -85,13 +85,64 @@ func (s *server) unpinChunk(w http.ResponseWriter, r *http.Request) {
jsonhttp
.
OK
(
w
,
nil
)
}
type
pinnedChunk
struct
{
Address
swarm
.
Address
`json:"address"`
PinCounter
uint64
`json:"pinCounter"`
}
type
listPinnedChunksResponse
struct
{
Chunks
[]
pinnedChunk
`json:"chunks"`
}
// listPinnedChunks lists all the chunk address and pin counters that are currently pinned.
func
(
s
*
server
)
listPinnedChunks
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
pinnedChunks
,
err
:=
s
.
Storer
.
PinnedChunks
(
context
.
Background
(),
swarm
.
NewAddress
(
nil
))
pinnedChunks
,
err
:=
s
.
Storer
.
PinnedChunks
(
r
.
Context
(),
swarm
.
NewAddress
(
nil
))
if
err
!=
nil
{
s
.
Logger
.
Debugf
(
"debug-api: pin chunk: listing pinned chunks: %v"
,
err
)
jsonhttp
.
InternalServerError
(
w
,
err
)
return
}
chunks
:=
make
([]
pinnedChunk
,
len
(
pinnedChunks
))
for
i
,
c
:=
range
pinnedChunks
{
chunks
[
i
]
=
pinnedChunk
(
*
c
)
}
jsonhttp
.
OK
(
w
,
listPinnedChunksResponse
{
Chunks
:
chunks
,
})
}
func
(
s
*
server
)
getPinnedChunk
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
addr
,
err
:=
swarm
.
ParseHexAddress
(
mux
.
Vars
(
r
)[
"address"
])
if
err
!=
nil
{
s
.
Logger
.
Debugf
(
"debug api: pin chunk: parse chunk ddress: %v"
,
err
)
jsonhttp
.
BadRequest
(
w
,
"bad address"
)
return
}
has
,
err
:=
s
.
Storer
.
Has
(
r
.
Context
(),
addr
)
if
err
!=
nil
{
s
.
Logger
.
Debugf
(
"debug api: pin chunk: localstore has: %v"
,
err
)
jsonhttp
.
BadRequest
(
w
,
err
)
return
}
if
!
has
{
jsonhttp
.
NotFound
(
w
,
nil
)
return
}
pinCounter
,
err
:=
s
.
Storer
.
PinInfo
(
addr
)
if
err
!=
nil
{
s
.
Logger
.
Debugf
(
"debug-api: pin chunk: listing pinned chunks error: %v"
,
err
)
if
errors
.
Is
(
err
,
storage
.
ErrNotFound
)
{
jsonhttp
.
NotFound
(
w
,
nil
)
return
}
s
.
Logger
.
Debugf
(
"debug-api: pin chunk: listing pinned chunks: %v"
,
err
)
jsonhttp
.
InternalServerError
(
w
,
err
)
return
}
jsonhttp
.
OK
(
w
,
pinnedChunks
)
jsonhttp
.
OK
(
w
,
pinnedChunk
{
Address
:
addr
,
PinCounter
:
pinCounter
,
})
}
pkg/debugapi/pin_test.go
View file @
8b240282
...
...
@@ -9,6 +9,7 @@ import (
"net/http"
"testing"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/storage/mock"
...
...
@@ -78,9 +79,9 @@ func TestPinChunkHandler(t *testing.T) {
})
// Check is the chunk is pinned once
jsonhttptest
.
ResponseDirect
WithJson
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
Message
:
`{"Address":"aabbcc","PinCounter":1}`
,
Code
:
http
.
StatusOK
,
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
debugapi
.
PinnedChunk
{
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
)
,
PinCounter
:
1
,
})
})
...
...
@@ -93,9 +94,9 @@ func TestPinChunkHandler(t *testing.T) {
})
// Check is the chunk is pinned twice
jsonhttptest
.
ResponseDirect
WithJson
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
Message
:
`{"Address":"aabbcc","PinCounter":2}`
,
Code
:
http
.
StatusOK
,
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
debugapi
.
PinnedChunk
{
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
)
,
PinCounter
:
2
,
})
})
...
...
@@ -107,9 +108,9 @@ func TestPinChunkHandler(t *testing.T) {
})
// Check is the chunk is pinned once
jsonhttptest
.
ResponseDirect
WithJson
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
Message
:
`{"Address":"aabbcc","PinCounter":1}`
,
Code
:
http
.
StatusOK
,
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
debugapi
.
PinnedChunk
{
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
)
,
PinCounter
:
1
,
})
})
...
...
@@ -121,9 +122,9 @@ func TestPinChunkHandler(t *testing.T) {
})
// Check if the chunk is removed from the pinIndex
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
Status
InternalServerError
,
jsonhttp
.
StatusResponse
{
Message
:
"pin chunks: leveldb: not found"
,
Code
:
http
.
Status
InternalServerError
,
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
Status
NotFound
,
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusNotFound
)
,
Code
:
http
.
Status
NotFound
,
})
})
...
...
@@ -153,10 +154,17 @@ func TestPinChunkHandler(t *testing.T) {
Code
:
http
.
StatusOK
,
})
jsonhttptest
.
ResponseDirectWithJson
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin"
,
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
Message
:
`{"Address":"aabbcc","PinCounter":1},{"Address":"ddeeff","PinCounter":1}`
,
Code
:
http
.
StatusOK
,
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin"
,
nil
,
http
.
StatusOK
,
debugapi
.
ListPinnedChunksResponse
{
Chunks
:
[]
debugapi
.
PinnedChunk
{
{
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
),
PinCounter
:
1
,
},
{
Address
:
swarm
.
MustParseHexAddress
(
"ddeeff"
),
PinCounter
:
1
,
},
},
})
})
}
pkg/debugapi/router.go
View file @
8b240282
...
...
@@ -57,9 +57,9 @@ func (s *server) setupRouting() {
"GET"
:
http
.
HandlerFunc
(
s
.
hasChunkHandler
),
})
router
.
Handle
(
"/chunks-pin/{address}"
,
jsonhttp
.
MethodHandler
{
"GET"
:
http
.
HandlerFunc
(
s
.
getPinnedChunk
),
"POST"
:
http
.
HandlerFunc
(
s
.
pinChunk
),
"DELETE"
:
http
.
HandlerFunc
(
s
.
unpinChunk
),
"GET"
:
http
.
HandlerFunc
(
s
.
listPinnedChunks
),
})
router
.
Handle
(
"/chunks-pin"
,
jsonhttp
.
MethodHandler
{
"GET"
:
http
.
HandlerFunc
(
s
.
listPinnedChunks
),
...
...
pkg/jsonhttp/jsonhttptest/jsonhttptest.go
View file @
8b240282
...
...
@@ -11,8 +11,6 @@ import (
"io/ioutil"
"net/http"
"testing"
"github.com/ethersphere/bee/pkg/jsonhttp"
)
func
ResponseDirect
(
t
*
testing
.
T
,
client
*
http
.
Client
,
method
,
url
string
,
body
io
.
Reader
,
responseCode
int
,
response
interface
{})
{
...
...
@@ -37,35 +35,6 @@ func ResponseDirect(t *testing.T, client *http.Client, method, url string, body
}
}
// ResponseDirectWithJson checks for responses in json format. It is useful in cases where the response is json.
func
ResponseDirectWithJson
(
t
*
testing
.
T
,
client
*
http
.
Client
,
method
,
url
string
,
body
io
.
Reader
,
responseCode
int
,
response
interface
{})
{
t
.
Helper
()
resp
:=
request
(
t
,
client
,
method
,
url
,
body
,
responseCode
,
nil
)
defer
resp
.
Body
.
Close
()
got
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
got
=
bytes
.
TrimSpace
(
got
)
want
,
err
:=
json
.
Marshal
(
response
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
var
wantJson
jsonhttp
.
StatusResponse
err
=
json
.
Unmarshal
(
want
,
&
wantJson
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
wantString
:=
"["
+
wantJson
.
Message
+
"]"
if
wantString
!=
string
(
got
)
{
t
.
Errorf
(
"got response %s, want %s"
,
string
(
got
),
wantString
)
}
}
func
ResponseDirectWithHeaders
(
t
*
testing
.
T
,
client
*
http
.
Client
,
method
,
url
string
,
body
io
.
Reader
,
responseCode
int
,
response
interface
{},
headers
http
.
Header
)
{
t
.
Helper
()
...
...
pkg/localstore/pin.go
View file @
8b240282
...
...
@@ -7,11 +7,13 @@ package localstore
import
(
"bytes"
"context"
"errors"
"fmt"
"github.com/ethersphere/bee/pkg/shed"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/syndtr/goleveldb/leveldb"
)
const
(
...
...
@@ -60,6 +62,9 @@ func (db *DB) PinInfo(address swarm.Address) (uint64, error) {
}
out
,
err
:=
db
.
pinIndex
.
Get
(
it
)
if
err
!=
nil
{
if
errors
.
Is
(
err
,
leveldb
.
ErrNotFound
)
{
return
0
,
storage
.
ErrNotFound
}
return
0
,
err
}
return
out
.
PinCounter
,
nil
...
...
pkg/localstore/pin_test.go
View file @
8b240282
...
...
@@ -116,7 +116,7 @@ func TestPinInfo(t *testing.T) {
}
_
,
err
=
db
.
PinInfo
(
swarm
.
NewAddress
(
chunk
.
Address
()
.
Bytes
()))
if
err
!=
nil
{
if
!
errors
.
Is
(
err
,
leveldb
.
ErrNotFound
)
{
if
!
errors
.
Is
(
err
,
storage
.
ErrNotFound
)
{
t
.
Fatal
(
err
)
}
}
...
...
pkg/storage/mock/storer.go
View file @
8b240282
...
...
@@ -166,7 +166,7 @@ func (m *MockStorer) PinInfo(address swarm.Address) (uint64, error) {
return
m
.
pinnedCounter
[
i
],
nil
}
}
return
0
,
errors
.
New
(
"could not find address"
)
return
0
,
storage
.
ErrNotFound
}
func
(
m
*
MockStorer
)
Close
()
error
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment