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
5ada5bcd
Unverified
Commit
5ada5bcd
authored
Aug 05, 2020
by
metacertain
Committed by
GitHub
Aug 05, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Accounting debug api (#461)
Co-authored-by:
Janos Guljas
<
janos@resenje.org
>
parent
2226acb4
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
441 additions
and
48 deletions
+441
-48
accounting.go
pkg/accounting/accounting.go
+69
-6
accounting.go
pkg/accounting/mock/accounting.go
+113
-22
validator.go
pkg/content/validator.go
+2
-2
validator_test.go
pkg/content/validator_test.go
+2
-2
balances.go
pkg/debugapi/balances.go
+74
-0
balances_test.go
pkg/debugapi/balances_test.go
+144
-0
debugapi.go
pkg/debugapi/debugapi.go
+2
-0
debugapi_test.go
pkg/debugapi/debugapi_test.go
+14
-10
export_test.go
pkg/debugapi/export_test.go
+8
-0
router.go
pkg/debugapi/router.go
+6
-0
node.go
pkg/node/node.go
+4
-3
validator.go
pkg/soc/validator.go
+1
-1
validator_test.go
pkg/soc/validator_test.go
+2
-2
No files found.
pkg/accounting/accounting.go
View file @
5ada5bcd
...
@@ -7,6 +7,7 @@ package accounting
...
@@ -7,6 +7,7 @@ package accounting
import
(
import
(
"errors"
"errors"
"fmt"
"fmt"
"strings"
"sync"
"sync"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/logging"
...
@@ -15,7 +16,10 @@ import (
...
@@ -15,7 +16,10 @@ import (
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/swarm"
)
)
var
_
Interface
=
(
*
Accounting
)(
nil
)
var
(
_
Interface
=
(
*
Accounting
)(
nil
)
balancesPrefix
string
=
"balance_"
)
// Interface is the main interface for Accounting
// Interface is the main interface for Accounting
type
Interface
interface
{
type
Interface
interface
{
...
@@ -31,6 +35,8 @@ type Interface interface {
...
@@ -31,6 +35,8 @@ type Interface interface {
Debit
(
peer
swarm
.
Address
,
price
uint64
)
error
Debit
(
peer
swarm
.
Address
,
price
uint64
)
error
// Balance returns the current balance for the given peer
// Balance returns the current balance for the given peer
Balance
(
peer
swarm
.
Address
)
(
int64
,
error
)
Balance
(
peer
swarm
.
Address
)
(
int64
,
error
)
// Balances returns balances for all known peers
Balances
()
(
map
[
string
]
int64
,
error
)
}
}
// PeerBalance holds all relevant accounting information for one peer
// PeerBalance holds all relevant accounting information for one peer
...
@@ -129,7 +135,7 @@ func (a *Accounting) Credit(peer swarm.Address, price uint64) error {
...
@@ -129,7 +135,7 @@ func (a *Accounting) Credit(peer swarm.Address, price uint64) error {
a
.
logger
.
Tracef
(
"crediting peer %v with price %d, new balance is %d"
,
peer
,
price
,
nextBalance
)
a
.
logger
.
Tracef
(
"crediting peer %v with price %d, new balance is %d"
,
peer
,
price
,
nextBalance
)
err
=
a
.
store
.
Put
(
b
alanceKey
(
peer
),
nextBalance
)
err
=
a
.
store
.
Put
(
peerB
alanceKey
(
peer
),
nextBalance
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -159,7 +165,7 @@ func (a *Accounting) Debit(peer swarm.Address, price uint64) error {
...
@@ -159,7 +165,7 @@ func (a *Accounting) Debit(peer swarm.Address, price uint64) error {
a
.
logger
.
Tracef
(
"debiting peer %v with price %d, new balance is %d"
,
peer
,
price
,
nextBalance
)
a
.
logger
.
Tracef
(
"debiting peer %v with price %d, new balance is %d"
,
peer
,
price
,
nextBalance
)
err
=
a
.
store
.
Put
(
b
alanceKey
(
peer
),
nextBalance
)
err
=
a
.
store
.
Put
(
peerB
alanceKey
(
peer
),
nextBalance
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -188,8 +194,8 @@ func (a *Accounting) Balance(peer swarm.Address) (int64, error) {
...
@@ -188,8 +194,8 @@ func (a *Accounting) Balance(peer swarm.Address) (int64, error) {
}
}
// get the balance storage key for the given peer
// get the balance storage key for the given peer
func
b
alanceKey
(
peer
swarm
.
Address
)
string
{
func
peerB
alanceKey
(
peer
swarm
.
Address
)
string
{
return
fmt
.
Sprintf
(
"
balance_%s"
,
peer
.
String
())
return
fmt
.
Sprintf
(
"
%s%s"
,
balancesPrefix
,
peer
.
String
())
}
}
// getPeerBalance gets the PeerBalance for a given peer
// getPeerBalance gets the PeerBalance for a given peer
...
@@ -203,7 +209,7 @@ func (a *Accounting) getPeerBalance(peer swarm.Address) (*PeerBalance, error) {
...
@@ -203,7 +209,7 @@ func (a *Accounting) getPeerBalance(peer swarm.Address) (*PeerBalance, error) {
if
!
ok
{
if
!
ok
{
// balance not yet in memory, load from state store
// balance not yet in memory, load from state store
var
balance
int64
var
balance
int64
err
:=
a
.
store
.
Get
(
b
alanceKey
(
peer
),
&
balance
)
err
:=
a
.
store
.
Get
(
peerB
alanceKey
(
peer
),
&
balance
)
if
err
==
nil
{
if
err
==
nil
{
peerBalance
=
&
PeerBalance
{
peerBalance
=
&
PeerBalance
{
balance
:
balance
,
balance
:
balance
,
...
@@ -226,6 +232,63 @@ func (a *Accounting) getPeerBalance(peer swarm.Address) (*PeerBalance, error) {
...
@@ -226,6 +232,63 @@ func (a *Accounting) getPeerBalance(peer swarm.Address) (*PeerBalance, error) {
return
peerBalance
,
nil
return
peerBalance
,
nil
}
}
// Balances gets balances for all peers, first from memory, than completing from store
func
(
a
*
Accounting
)
Balances
()
(
map
[
string
]
int64
,
error
)
{
peersBalances
:=
make
(
map
[
string
]
int64
)
// get peer balances from store first as it may be outdated
// compared to the in memory map
if
err
:=
a
.
balancesFromStore
(
peersBalances
);
err
!=
nil
{
return
nil
,
err
}
a
.
balancesMu
.
Lock
()
for
peer
,
balance
:=
range
a
.
balances
{
peersBalances
[
peer
]
=
balance
.
balance
}
a
.
balancesMu
.
Unlock
()
return
peersBalances
,
nil
}
// Get balances from store for keys (peers) that do not already exist in argument map.
// Used to get all balances not loaded in memory at the time the Balances() function is called.
func
(
a
*
Accounting
)
balancesFromStore
(
s
map
[
string
]
int64
)
error
{
return
a
.
store
.
Iterate
(
balancesPrefix
,
func
(
key
,
val
[]
byte
)
(
stop
bool
,
err
error
)
{
addr
,
err
:=
balanceKeyPeer
(
key
)
if
err
!=
nil
{
return
false
,
fmt
.
Errorf
(
"parse address from key: %s: %v"
,
string
(
key
),
err
)
}
if
_
,
ok
:=
s
[
addr
.
String
()];
!
ok
{
var
storevalue
int64
err
=
a
.
store
.
Get
(
peerBalanceKey
(
addr
),
&
storevalue
)
if
err
!=
nil
{
return
false
,
fmt
.
Errorf
(
"get peer %s balance: %v"
,
addr
.
String
(),
err
)
}
s
[
addr
.
String
()]
=
storevalue
}
return
false
,
nil
})
}
// get the embedded peer from the balance storage key
func
balanceKeyPeer
(
key
[]
byte
)
(
swarm
.
Address
,
error
)
{
k
:=
string
(
key
)
split
:=
strings
.
SplitAfter
(
k
,
balancesPrefix
)
if
len
(
split
)
!=
2
{
return
swarm
.
ZeroAddress
,
errors
.
New
(
"no peer in key"
)
}
addr
,
err
:=
swarm
.
ParseHexAddress
(
split
[
1
])
if
err
!=
nil
{
return
swarm
.
ZeroAddress
,
err
}
return
addr
,
nil
}
func
(
pb
*
PeerBalance
)
freeBalance
()
int64
{
func
(
pb
*
PeerBalance
)
freeBalance
()
int64
{
return
pb
.
balance
-
int64
(
pb
.
reserved
)
return
pb
.
balance
-
int64
(
pb
.
reserved
)
}
}
pkg/accounting/mock/accounting.go
View file @
5ada5bcd
...
@@ -5,45 +5,136 @@
...
@@ -5,45 +5,136 @@
package
mock
package
mock
import
(
import
(
"github.com/ethersphere/bee/pkg/swarm"
"sync"
"sync"
"github.com/ethersphere/bee/pkg/accounting"
"github.com/ethersphere/bee/pkg/swarm"
)
)
type
MockAccounting
struct
{
// Service is the mock Accounting service.
lock
sync
.
Mutex
type
Service
struct
{
balances
map
[
string
]
int64
lock
sync
.
Mutex
balances
map
[
string
]
int64
reserveFunc
func
(
peer
swarm
.
Address
,
price
uint64
)
error
releaseFunc
func
(
peer
swarm
.
Address
,
price
uint64
)
creditFunc
func
(
peer
swarm
.
Address
,
price
uint64
)
error
debitFunc
func
(
peer
swarm
.
Address
,
price
uint64
)
error
balanceFunc
func
(
swarm
.
Address
)
(
int64
,
error
)
balancesFunc
func
()
(
map
[
string
]
int64
,
error
)
}
}
func
(
ma
*
MockAccounting
)
Reserve
(
peer
swarm
.
Address
,
price
uint64
)
error
{
// WithReserveFunc sets the mock Reserve function
return
nil
func
WithReserveFunc
(
f
func
(
peer
swarm
.
Address
,
price
uint64
)
error
)
Option
{
return
optionFunc
(
func
(
s
*
Service
)
{
s
.
reserveFunc
=
f
})
}
// WithReleaseFunc sets the mock Release function
func
WithReleaseFunc
(
f
func
(
peer
swarm
.
Address
,
price
uint64
))
Option
{
return
optionFunc
(
func
(
s
*
Service
)
{
s
.
releaseFunc
=
f
})
}
// WithCreditFunc sets the mock Credit function
func
WithCreditFunc
(
f
func
(
peer
swarm
.
Address
,
price
uint64
)
error
)
Option
{
return
optionFunc
(
func
(
s
*
Service
)
{
s
.
creditFunc
=
f
})
}
// WithDebitFunc sets the mock Debit function
func
WithDebitFunc
(
f
func
(
peer
swarm
.
Address
,
price
uint64
)
error
)
Option
{
return
optionFunc
(
func
(
s
*
Service
)
{
s
.
debitFunc
=
f
})
}
// WithBalanceFunc sets the mock Balance function
func
WithBalanceFunc
(
f
func
(
swarm
.
Address
)
(
int64
,
error
))
Option
{
return
optionFunc
(
func
(
s
*
Service
)
{
s
.
balanceFunc
=
f
})
}
// WithBalancesFunc sets the mock Balances function
func
WithBalancesFunc
(
f
func
()
(
map
[
string
]
int64
,
error
))
Option
{
return
optionFunc
(
func
(
s
*
Service
)
{
s
.
balancesFunc
=
f
})
}
}
func
(
ma
*
MockAccounting
)
Release
(
peer
swarm
.
Address
,
price
uint64
)
{
// NewAccounting creates the mock accounting implementation
func
NewAccounting
(
opts
...
Option
)
accounting
.
Interface
{
mock
:=
new
(
Service
)
mock
.
balances
=
make
(
map
[
string
]
int64
)
for
_
,
o
:=
range
opts
{
o
.
apply
(
mock
)
}
return
mock
}
// Reserve is the mock function wrapper that calls the set implementation
func
(
s
*
Service
)
Reserve
(
peer
swarm
.
Address
,
price
uint64
)
error
{
if
s
.
reserveFunc
!=
nil
{
return
s
.
reserveFunc
(
peer
,
price
)
}
return
nil
}
// Release is the mock function wrapper that calls the set implementation
func
(
s
*
Service
)
Release
(
peer
swarm
.
Address
,
price
uint64
)
{
if
s
.
releaseFunc
!=
nil
{
s
.
releaseFunc
(
peer
,
price
)
}
}
}
func
(
ma
*
MockAccounting
)
Credit
(
peer
swarm
.
Address
,
price
uint64
)
error
{
// Credit is the mock function wrapper that calls the set implementation
ma
.
lock
.
Lock
()
func
(
s
*
Service
)
Credit
(
peer
swarm
.
Address
,
price
uint64
)
error
{
defer
ma
.
lock
.
Unlock
()
if
s
.
creditFunc
!=
nil
{
ma
.
balances
[
peer
.
String
()]
-=
int64
(
price
)
return
s
.
creditFunc
(
peer
,
price
)
}
s
.
lock
.
Lock
()
defer
s
.
lock
.
Unlock
()
s
.
balances
[
peer
.
String
()]
-=
int64
(
price
)
return
nil
return
nil
}
}
func
(
ma
*
MockAccounting
)
Debit
(
peer
swarm
.
Address
,
price
uint64
)
error
{
// Debit is the mock function wrapper that calls the set implementation
ma
.
lock
.
Lock
()
func
(
s
*
Service
)
Debit
(
peer
swarm
.
Address
,
price
uint64
)
error
{
defer
ma
.
lock
.
Unlock
()
if
s
.
debitFunc
!=
nil
{
ma
.
balances
[
peer
.
String
()]
+=
int64
(
price
)
return
s
.
debitFunc
(
peer
,
price
)
}
s
.
lock
.
Lock
()
defer
s
.
lock
.
Unlock
()
s
.
balances
[
peer
.
String
()]
+=
int64
(
price
)
return
nil
return
nil
}
}
func
(
ma
*
MockAccounting
)
Balance
(
peer
swarm
.
Address
)
(
int64
,
error
)
{
// Balance is the mock function wrapper that calls the set implementation
ma
.
lock
.
Lock
()
func
(
s
*
Service
)
Balance
(
peer
swarm
.
Address
)
(
int64
,
error
)
{
defer
ma
.
lock
.
Unlock
()
if
s
.
balanceFunc
!=
nil
{
return
ma
.
balances
[
peer
.
String
()],
nil
return
s
.
balanceFunc
(
peer
)
}
s
.
lock
.
Lock
()
defer
s
.
lock
.
Unlock
()
return
s
.
balances
[
peer
.
String
()],
nil
}
}
func
NewAccounting
()
*
MockAccounting
{
// Balances is the mock function wrapper that calls the set implementation
return
&
MockAccounting
{
func
(
s
*
Service
)
Balances
()
(
map
[
string
]
int64
,
error
)
{
balances
:
make
(
map
[
string
]
int64
),
if
s
.
balancesFunc
!=
nil
{
return
s
.
balancesFunc
()
}
}
return
s
.
balances
,
nil
}
}
// Option is the option passed to the mock accounting service
type
Option
interface
{
apply
(
*
Service
)
}
type
optionFunc
func
(
*
Service
)
func
(
f
optionFunc
)
apply
(
r
*
Service
)
{
f
(
r
)
}
pkg/content/validator.go
View file @
5ada5bcd
...
@@ -10,12 +10,12 @@ import (
...
@@ -10,12 +10,12 @@ import (
var
_
swarm
.
Validator
=
(
*
Validator
)(
nil
)
var
_
swarm
.
Validator
=
(
*
Validator
)(
nil
)
//
ContentAddress
Validator validates that the address of a given chunk
// Validator validates that the address of a given chunk
// is the content address of its contents.
// is the content address of its contents.
type
Validator
struct
{
type
Validator
struct
{
}
}
// New
ContentAddressValidator constructs a new ContentAddress
Validator
// New
Validator constructs a new
Validator
func
NewValidator
()
swarm
.
Validator
{
func
NewValidator
()
swarm
.
Validator
{
return
&
Validator
{}
return
&
Validator
{}
}
}
...
...
pkg/content/validator_test.go
View file @
5ada5bcd
...
@@ -11,9 +11,9 @@ import (
...
@@ -11,9 +11,9 @@ import (
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/swarm"
)
)
// Test
ContentAddress
Validator checks that the validator evaluates correctly
// TestValidator checks that the validator evaluates correctly
// on valid and invalid input
// on valid and invalid input
func
Test
ContentAddress
Validator
(
t
*
testing
.
T
)
{
func
TestValidator
(
t
*
testing
.
T
)
{
// instantiate validator
// instantiate validator
validator
:=
content
.
NewValidator
()
validator
:=
content
.
NewValidator
()
...
...
pkg/debugapi/balances.go
0 → 100644
View file @
5ada5bcd
// Copyright 2020 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
debugapi
import
(
"net/http"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/gorilla/mux"
)
var
(
errCantBalances
=
"Cannot get balances"
errCantBalance
=
"Cannot get balance"
errInvaliAddress
=
"Invalid address"
)
type
balanceResponse
struct
{
Peer
string
`json:"peer"`
Balance
int64
`json:"balance"`
}
type
balancesResponse
struct
{
Balances
[]
balanceResponse
`json:"balances"`
}
func
(
s
*
server
)
balancesHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
balances
,
err
:=
s
.
Accounting
.
Balances
()
if
err
!=
nil
{
jsonhttp
.
InternalServerError
(
w
,
errCantBalances
)
s
.
Logger
.
Debugf
(
"debug api: balances: %v"
,
err
)
s
.
Logger
.
Error
(
"debug api: can not get balances"
)
return
}
balResponses
:=
make
([]
balanceResponse
,
len
(
balances
))
i
:=
0
for
k
:=
range
balances
{
balResponses
[
i
]
=
balanceResponse
{
Peer
:
k
,
Balance
:
balances
[
k
],
}
i
++
}
jsonhttp
.
OK
(
w
,
balancesResponse
{
Balances
:
balResponses
})
}
func
(
s
*
server
)
peerBalanceHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
addr
:=
mux
.
Vars
(
r
)[
"peer"
]
peer
,
err
:=
swarm
.
ParseHexAddress
(
addr
)
if
err
!=
nil
{
s
.
Logger
.
Debugf
(
"debug api: balances peer: invalid peer address %s: %v"
,
addr
,
err
)
s
.
Logger
.
Error
(
"debug api: balances peer: invalid peer address %s"
,
addr
)
jsonhttp
.
NotFound
(
w
,
errInvaliAddress
)
return
}
balance
,
err
:=
s
.
Accounting
.
Balance
(
peer
)
if
err
!=
nil
{
s
.
Logger
.
Debugf
(
"debug api: balances peer: get peer %s balance: %v"
,
peer
.
String
(),
err
)
s
.
Logger
.
Errorf
(
"debug api: balances peer: can't get peer %s balance"
,
peer
.
String
())
jsonhttp
.
InternalServerError
(
w
,
errCantBalance
)
return
}
jsonhttp
.
OK
(
w
,
balanceResponse
{
Peer
:
peer
.
String
(),
Balance
:
balance
,
})
}
pkg/debugapi/balances_test.go
0 → 100644
View file @
5ada5bcd
// Copyright 2020 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
debugapi_test
import
(
"errors"
"net/http"
"reflect"
"testing"
"github.com/ethersphere/bee/pkg/accounting/mock"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/swarm"
)
func
TestBalances
(
t
*
testing
.
T
)
{
balancesFunc
:=
func
()
(
ret
map
[
string
]
int64
,
err
error
)
{
ret
=
make
(
map
[
string
]
int64
)
ret
[
"DEAD"
]
=
1000000000000000000
ret
[
"BEEF"
]
=
-
100000000000000000
ret
[
"PARTY"
]
=
0
return
ret
,
err
}
testServer
:=
newTestServer
(
t
,
testServerOptions
{
AccountingOpts
:
[]
mock
.
Option
{
mock
.
WithBalancesFunc
(
balancesFunc
)},
})
expected
:=
&
debugapi
.
BalancesResponse
{
[]
debugapi
.
BalanceResponse
{
{
Peer
:
"DEAD"
,
Balance
:
1000000000000000000
,
},
{
Peer
:
"BEEF"
,
Balance
:
-
100000000000000000
,
},
{
Peer
:
"PARTY"
,
Balance
:
0
,
},
},
}
// We expect a list of items unordered by peer:
var
got
*
debugapi
.
BalancesResponse
jsonhttptest
.
ResponseUnmarshal
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances"
,
nil
,
http
.
StatusOK
,
&
got
)
if
!
equalBalances
(
got
,
expected
)
{
t
.
Errorf
(
"got balances: %v, expected: %v"
,
got
,
expected
)
}
}
func
TestBalancesError
(
t
*
testing
.
T
)
{
wantErr
:=
errors
.
New
(
"ASDF"
)
balancesFunc
:=
func
()
(
ret
map
[
string
]
int64
,
err
error
)
{
return
nil
,
wantErr
}
testServer
:=
newTestServer
(
t
,
testServerOptions
{
AccountingOpts
:
[]
mock
.
Option
{
mock
.
WithBalancesFunc
(
balancesFunc
)},
})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances"
,
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
Message
:
debugapi
.
ErrCantBalances
,
Code
:
http
.
StatusInternalServerError
,
})
}
func
TestBalancesPeers
(
t
*
testing
.
T
)
{
peer
:=
"bff2c89e85e78c38bd89fca1acc996afb876c21bf5a8482ad798ce15f1c223fa"
balanceFunc
:=
func
(
swarm
.
Address
)
(
int64
,
error
)
{
return
1000000000000000000
,
nil
}
testServer
:=
newTestServer
(
t
,
testServerOptions
{
AccountingOpts
:
[]
mock
.
Option
{
mock
.
WithBalanceFunc
(
balanceFunc
)},
})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances/"
+
peer
,
nil
,
http
.
StatusOK
,
debugapi
.
BalanceResponse
{
Peer
:
peer
,
Balance
:
1000000000000000000
,
})
}
func
TestBalancesPeersError
(
t
*
testing
.
T
)
{
peer
:=
"bff2c89e85e78c38bd89fca1acc996afb876c21bf5a8482ad798ce15f1c223fa"
wantErr
:=
errors
.
New
(
"Error"
)
balanceFunc
:=
func
(
swarm
.
Address
)
(
int64
,
error
)
{
return
0
,
wantErr
}
testServer
:=
newTestServer
(
t
,
testServerOptions
{
AccountingOpts
:
[]
mock
.
Option
{
mock
.
WithBalanceFunc
(
balanceFunc
)},
})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances/"
+
peer
,
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
Message
:
debugapi
.
ErrCantBalance
,
Code
:
http
.
StatusInternalServerError
,
})
}
func
TestBalancesInvalidAddress
(
t
*
testing
.
T
)
{
peer
:=
"bad peer address"
testServer
:=
newTestServer
(
t
,
testServerOptions
{})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances/"
+
peer
,
nil
,
http
.
StatusNotFound
,
jsonhttp
.
StatusResponse
{
Message
:
debugapi
.
ErrInvaliAddress
,
Code
:
http
.
StatusNotFound
,
})
}
func
equalBalances
(
a
,
b
*
debugapi
.
BalancesResponse
)
bool
{
var
state
bool
for
akeys
:=
range
a
.
Balances
{
state
=
false
for
bkeys
:=
range
b
.
Balances
{
if
reflect
.
DeepEqual
(
a
.
Balances
[
akeys
],
b
.
Balances
[
bkeys
])
{
state
=
true
}
}
if
!
state
{
return
false
}
}
for
bkeys
:=
range
b
.
Balances
{
state
=
false
for
akeys
:=
range
a
.
Balances
{
if
reflect
.
DeepEqual
(
a
.
Balances
[
akeys
],
b
.
Balances
[
bkeys
])
{
state
=
true
}
}
if
!
state
{
return
false
}
}
return
true
}
pkg/debugapi/debugapi.go
View file @
5ada5bcd
...
@@ -7,6 +7,7 @@ package debugapi
...
@@ -7,6 +7,7 @@ package debugapi
import
(
import
(
"net/http"
"net/http"
"github.com/ethersphere/bee/pkg/accounting"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/pingpong"
"github.com/ethersphere/bee/pkg/pingpong"
...
@@ -39,6 +40,7 @@ type Options struct {
...
@@ -39,6 +40,7 @@ type Options struct {
Logger
logging
.
Logger
Logger
logging
.
Logger
Tracer
*
tracing
.
Tracer
Tracer
*
tracing
.
Tracer
Tags
*
tags
.
Tags
Tags
*
tags
.
Tags
Accounting
accounting
.
Interface
}
}
func
New
(
o
Options
)
Service
{
func
New
(
o
Options
)
Service
{
...
...
pkg/debugapi/debugapi_test.go
View file @
5ada5bcd
...
@@ -11,35 +11,38 @@ import (
...
@@ -11,35 +11,38 @@ import (
"net/url"
"net/url"
"testing"
"testing"
accountingmock
"github.com/ethersphere/bee/pkg/accounting/mock"
"github.com/ethersphere/bee/pkg/api"
"github.com/ethersphere/bee/pkg/api"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/logging"
mockp2p
"github.com/ethersphere/bee/pkg/p2p/mock"
p2pmock
"github.com/ethersphere/bee/pkg/p2p/mock"
"github.com/ethersphere/bee/pkg/pingpong"
"github.com/ethersphere/bee/pkg/pingpong"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/tags"
"github.com/ethersphere/bee/pkg/tags"
"github.com/ethersphere/bee/pkg/topology/mock"
topologymock
"github.com/ethersphere/bee/pkg/topology/mock"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multiaddr"
"resenje.org/web"
"resenje.org/web"
)
)
type
testServerOptions
struct
{
type
testServerOptions
struct
{
Overlay
swarm
.
Address
Overlay
swarm
.
Address
P2P
*
mockp2p
.
Service
P2P
*
p2pmock
.
Service
Pingpong
pingpong
.
Interface
Pingpong
pingpong
.
Interface
Storer
storage
.
Storer
Storer
storage
.
Storer
TopologyOpts
[]
mock
.
Option
TopologyOpts
[]
topologymock
.
Option
Tags
*
tags
.
Tags
Tags
*
tags
.
Tags
AccountingOpts
[]
accountingmock
.
Option
}
}
type
testServer
struct
{
type
testServer
struct
{
Client
*
http
.
Client
Client
*
http
.
Client
P2PMock
*
mockp2p
.
Service
P2PMock
*
p2pmock
.
Service
}
}
func
newTestServer
(
t
*
testing
.
T
,
o
testServerOptions
)
*
testServer
{
func
newTestServer
(
t
*
testing
.
T
,
o
testServerOptions
)
*
testServer
{
topologyDriver
:=
mock
.
NewTopologyDriver
(
o
.
TopologyOpts
...
)
topologyDriver
:=
topologymock
.
NewTopologyDriver
(
o
.
TopologyOpts
...
)
acc
:=
accountingmock
.
NewAccounting
(
o
.
AccountingOpts
...
)
s
:=
debugapi
.
New
(
debugapi
.
Options
{
s
:=
debugapi
.
New
(
debugapi
.
Options
{
Overlay
:
o
.
Overlay
,
Overlay
:
o
.
Overlay
,
...
@@ -49,6 +52,7 @@ func newTestServer(t *testing.T, o testServerOptions) *testServer {
...
@@ -49,6 +52,7 @@ func newTestServer(t *testing.T, o testServerOptions) *testServer {
Logger
:
logging
.
New
(
ioutil
.
Discard
,
0
),
Logger
:
logging
.
New
(
ioutil
.
Discard
,
0
),
Storer
:
o
.
Storer
,
Storer
:
o
.
Storer
,
TopologyDriver
:
topologyDriver
,
TopologyDriver
:
topologyDriver
,
Accounting
:
acc
,
})
})
ts
:=
httptest
.
NewServer
(
s
)
ts
:=
httptest
.
NewServer
(
s
)
t
.
Cleanup
(
ts
.
Close
)
t
.
Cleanup
(
ts
.
Close
)
...
...
pkg/debugapi/export_test.go
View file @
5ada5bcd
...
@@ -15,4 +15,12 @@ type (
...
@@ -15,4 +15,12 @@ type (
TagResponse
=
tagResponse
TagResponse
=
tagResponse
WelcomeMessageRequest
=
welcomeMessageRequest
WelcomeMessageRequest
=
welcomeMessageRequest
WelcomeMessageResponse
=
welcomeMessageResponse
WelcomeMessageResponse
=
welcomeMessageResponse
BalancesResponse
=
balancesResponse
BalanceResponse
=
balanceResponse
)
var
(
ErrCantBalance
=
errCantBalance
ErrCantBalances
=
errCantBalances
ErrInvaliAddress
=
errInvaliAddress
)
)
pkg/debugapi/router.go
View file @
5ada5bcd
...
@@ -93,6 +93,12 @@ func (s *server) setupRouting() {
...
@@ -93,6 +93,12 @@ func (s *server) setupRouting() {
web
.
FinalHandlerFunc
(
s
.
setWelcomeMessageHandler
),
web
.
FinalHandlerFunc
(
s
.
setWelcomeMessageHandler
),
),
),
})
})
router
.
Handle
(
"/balances"
,
jsonhttp
.
MethodHandler
{
"GET"
:
http
.
HandlerFunc
(
s
.
balancesHandler
),
})
router
.
Handle
(
"/balances/{peer}"
,
jsonhttp
.
MethodHandler
{
"GET"
:
http
.
HandlerFunc
(
s
.
peerBalanceHandler
),
})
baseRouter
.
Handle
(
"/"
,
web
.
ChainHandlers
(
baseRouter
.
Handle
(
"/"
,
web
.
ChainHandlers
(
logging
.
NewHTTPAccessLogHandler
(
s
.
Logger
,
logrus
.
InfoLevel
,
"debug api access"
),
logging
.
NewHTTPAccessLogHandler
(
s
.
Logger
,
logrus
.
InfoLevel
,
"debug api access"
),
...
...
pkg/node/node.go
View file @
5ada5bcd
...
@@ -242,7 +242,7 @@ func NewBee(o Options) (*Bee, error) {
...
@@ -242,7 +242,7 @@ func NewBee(o Options) (*Bee, error) {
DisconnectThreshold
:
o
.
DisconnectThreshold
,
DisconnectThreshold
:
o
.
DisconnectThreshold
,
})
})
chunkvalidator
s
:=
swarm
.
NewChunkValidator
(
soc
.
NewValidator
(),
content
.
NewValidator
())
chunkvalidator
:=
swarm
.
NewChunkValidator
(
soc
.
NewValidator
(),
content
.
NewValidator
())
retrieve
:=
retrieval
.
New
(
retrieval
.
Options
{
retrieve
:=
retrieval
.
New
(
retrieval
.
Options
{
Streamer
:
p2ps
,
Streamer
:
p2ps
,
...
@@ -250,7 +250,7 @@ func NewBee(o Options) (*Bee, error) {
...
@@ -250,7 +250,7 @@ func NewBee(o Options) (*Bee, error) {
Logger
:
logger
,
Logger
:
logger
,
Accounting
:
acc
,
Accounting
:
acc
,
Pricer
:
accounting
.
NewFixedPricer
(
address
,
10
),
Pricer
:
accounting
.
NewFixedPricer
(
address
,
10
),
Validator
:
chunkvalidator
s
,
Validator
:
chunkvalidator
,
})
})
tagg
:=
tags
.
NewTags
()
tagg
:=
tags
.
NewTags
()
...
@@ -258,7 +258,7 @@ func NewBee(o Options) (*Bee, error) {
...
@@ -258,7 +258,7 @@ func NewBee(o Options) (*Bee, error) {
return
nil
,
fmt
.
Errorf
(
"retrieval service: %w"
,
err
)
return
nil
,
fmt
.
Errorf
(
"retrieval service: %w"
,
err
)
}
}
ns
:=
netstore
.
New
(
storer
,
retrieve
,
logger
,
chunkvalidator
s
)
ns
:=
netstore
.
New
(
storer
,
retrieve
,
logger
,
chunkvalidator
)
retrieve
.
SetStorer
(
ns
)
retrieve
.
SetStorer
(
ns
)
...
@@ -348,6 +348,7 @@ func NewBee(o Options) (*Bee, error) {
...
@@ -348,6 +348,7 @@ func NewBee(o Options) (*Bee, error) {
TopologyDriver
:
topologyDriver
,
TopologyDriver
:
topologyDriver
,
Storer
:
storer
,
Storer
:
storer
,
Tags
:
tagg
,
Tags
:
tagg
,
Accounting
:
acc
,
})
})
// register metrics from components
// register metrics from components
debugAPIService
.
MustRegisterMetrics
(
p2ps
.
Metrics
()
...
)
debugAPIService
.
MustRegisterMetrics
(
p2ps
.
Metrics
()
...
)
...
...
pkg/soc/validator.go
View file @
5ada5bcd
...
@@ -14,7 +14,7 @@ var _ swarm.Validator = (*Validator)(nil)
...
@@ -14,7 +14,7 @@ var _ swarm.Validator = (*Validator)(nil)
type
Validator
struct
{
type
Validator
struct
{
}
}
// New
SocValidator creates a new Soc
Validator.
// New
Validator creates a new
Validator.
func
NewValidator
()
swarm
.
Validator
{
func
NewValidator
()
swarm
.
Validator
{
return
&
Validator
{}
return
&
Validator
{}
}
}
...
...
pkg/soc/validator_test.go
View file @
5ada5bcd
...
@@ -14,10 +14,10 @@ import (
...
@@ -14,10 +14,10 @@ import (
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/swarm"
)
)
// Test
Soc
Validator verifies that the validator can detect both
// TestValidator verifies that the validator can detect both
// valid soc chunks, as well as chunks with invalid data and invalid
// valid soc chunks, as well as chunks with invalid data and invalid
// address.
// address.
func
Test
Soc
Validator
(
t
*
testing
.
T
)
{
func
TestValidator
(
t
*
testing
.
T
)
{
id
:=
make
([]
byte
,
soc
.
IdSize
)
id
:=
make
([]
byte
,
soc
.
IdSize
)
privKey
,
err
:=
crypto
.
GenerateSecp256k1Key
()
privKey
,
err
:=
crypto
.
GenerateSecp256k1Key
()
if
err
!=
nil
{
if
err
!=
nil
{
...
...
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