swarm: codebase split from go-ethereum (#1405)
This commit is contained in:
committed by
Anton Evangelatov
parent
7a22da98b9
commit
b046760db1
@ -1,182 +0,0 @@
|
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package protocols
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/metrics"
|
||||
)
|
||||
|
||||
// define some metrics
|
||||
var (
|
||||
// All metrics are cumulative
|
||||
|
||||
// total amount of units credited
|
||||
mBalanceCredit = metrics.NewRegisteredCounterForced("account.balance.credit", metrics.AccountingRegistry)
|
||||
// total amount of units debited
|
||||
mBalanceDebit = metrics.NewRegisteredCounterForced("account.balance.debit", metrics.AccountingRegistry)
|
||||
// total amount of bytes credited
|
||||
mBytesCredit = metrics.NewRegisteredCounterForced("account.bytes.credit", metrics.AccountingRegistry)
|
||||
// total amount of bytes debited
|
||||
mBytesDebit = metrics.NewRegisteredCounterForced("account.bytes.debit", metrics.AccountingRegistry)
|
||||
// total amount of credited messages
|
||||
mMsgCredit = metrics.NewRegisteredCounterForced("account.msg.credit", metrics.AccountingRegistry)
|
||||
// total amount of debited messages
|
||||
mMsgDebit = metrics.NewRegisteredCounterForced("account.msg.debit", metrics.AccountingRegistry)
|
||||
// how many times local node had to drop remote peers
|
||||
mPeerDrops = metrics.NewRegisteredCounterForced("account.peerdrops", metrics.AccountingRegistry)
|
||||
// how many times local node overdrafted and dropped
|
||||
mSelfDrops = metrics.NewRegisteredCounterForced("account.selfdrops", metrics.AccountingRegistry)
|
||||
)
|
||||
|
||||
// Prices defines how prices are being passed on to the accounting instance
|
||||
type Prices interface {
|
||||
// Return the Price for a message
|
||||
Price(interface{}) *Price
|
||||
}
|
||||
|
||||
type Payer bool
|
||||
|
||||
const (
|
||||
Sender = Payer(true)
|
||||
Receiver = Payer(false)
|
||||
)
|
||||
|
||||
// Price represents the costs of a message
|
||||
type Price struct {
|
||||
Value uint64
|
||||
PerByte bool // True if the price is per byte or for unit
|
||||
Payer Payer
|
||||
}
|
||||
|
||||
// For gives back the price for a message
|
||||
// A protocol provides the message price in absolute value
|
||||
// This method then returns the correct signed amount,
|
||||
// depending on who pays, which is identified by the `payer` argument:
|
||||
// `Send` will pass a `Sender` payer, `Receive` will pass the `Receiver` argument.
|
||||
// Thus: If Sending and sender pays, amount positive, otherwise negative
|
||||
// If Receiving, and receiver pays, amount positive, otherwise negative
|
||||
func (p *Price) For(payer Payer, size uint32) int64 {
|
||||
price := p.Value
|
||||
if p.PerByte {
|
||||
price *= uint64(size)
|
||||
}
|
||||
if p.Payer == payer {
|
||||
return 0 - int64(price)
|
||||
}
|
||||
return int64(price)
|
||||
}
|
||||
|
||||
// Balance is the actual accounting instance
|
||||
// Balance defines the operations needed for accounting
|
||||
// Implementations internally maintain the balance for every peer
|
||||
type Balance interface {
|
||||
// Adds amount to the local balance with remote node `peer`;
|
||||
// positive amount = credit local node
|
||||
// negative amount = debit local node
|
||||
Add(amount int64, peer *Peer) error
|
||||
}
|
||||
|
||||
// Accounting implements the Hook interface
|
||||
// It interfaces to the balances through the Balance interface,
|
||||
// while interfacing with protocols and its prices through the Prices interface
|
||||
type Accounting struct {
|
||||
Balance // interface to accounting logic
|
||||
Prices // interface to prices logic
|
||||
}
|
||||
|
||||
func NewAccounting(balance Balance, po Prices) *Accounting {
|
||||
ah := &Accounting{
|
||||
Prices: po,
|
||||
Balance: balance,
|
||||
}
|
||||
return ah
|
||||
}
|
||||
|
||||
// SetupAccountingMetrics uses a separate registry for p2p accounting metrics;
|
||||
// this registry should be independent of any other metrics as it persists at different endpoints.
|
||||
// It also starts the persisting go-routine which
|
||||
// at the passed interval writes the metrics to a LevelDB
|
||||
func SetupAccountingMetrics(reportInterval time.Duration, path string) *AccountingMetrics {
|
||||
// create the DB and start persisting
|
||||
return NewAccountingMetrics(metrics.AccountingRegistry, reportInterval, path)
|
||||
}
|
||||
|
||||
// Send takes a peer, a size and a msg and
|
||||
// - calculates the cost for the local node sending a msg of size to peer using the Prices interface
|
||||
// - credits/debits local node using balance interface
|
||||
func (ah *Accounting) Send(peer *Peer, size uint32, msg interface{}) error {
|
||||
// get the price for a message (through the protocol spec)
|
||||
price := ah.Price(msg)
|
||||
// this message doesn't need accounting
|
||||
if price == nil {
|
||||
return nil
|
||||
}
|
||||
// evaluate the price for sending messages
|
||||
costToLocalNode := price.For(Sender, size)
|
||||
// do the accounting
|
||||
err := ah.Add(costToLocalNode, peer)
|
||||
// record metrics: just increase counters for user-facing metrics
|
||||
ah.doMetrics(costToLocalNode, size, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Receive takes a peer, a size and a msg and
|
||||
// - calculates the cost for the local node receiving a msg of size from peer using the Prices interface
|
||||
// - credits/debits local node using balance interface
|
||||
func (ah *Accounting) Receive(peer *Peer, size uint32, msg interface{}) error {
|
||||
// get the price for a message (through the protocol spec)
|
||||
price := ah.Price(msg)
|
||||
// this message doesn't need accounting
|
||||
if price == nil {
|
||||
return nil
|
||||
}
|
||||
// evaluate the price for receiving messages
|
||||
costToLocalNode := price.For(Receiver, size)
|
||||
// do the accounting
|
||||
err := ah.Add(costToLocalNode, peer)
|
||||
// record metrics: just increase counters for user-facing metrics
|
||||
ah.doMetrics(costToLocalNode, size, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// record some metrics
|
||||
// this is not an error handling. `err` is returned by both `Send` and `Receive`
|
||||
// `err` will only be non-nil if a limit has been violated (overdraft), in which case the peer has been dropped.
|
||||
// if the limit has been violated and `err` is thus not nil:
|
||||
// * if the price is positive, local node has been credited; thus `err` implicitly signals the REMOTE has been dropped
|
||||
// * if the price is negative, local node has been debited, thus `err` implicitly signals LOCAL node "overdraft"
|
||||
func (ah *Accounting) doMetrics(price int64, size uint32, err error) {
|
||||
if price > 0 {
|
||||
mBalanceCredit.Inc(price)
|
||||
mBytesCredit.Inc(int64(size))
|
||||
mMsgCredit.Inc(1)
|
||||
if err != nil {
|
||||
// increase the number of times a remote node has been dropped due to "overdraft"
|
||||
mPeerDrops.Inc(1)
|
||||
}
|
||||
} else {
|
||||
mBalanceDebit.Inc(price)
|
||||
mBytesDebit.Inc(int64(size))
|
||||
mMsgDebit.Inc(1)
|
||||
if err != nil {
|
||||
// increase the number of times the local node has done an "overdraft" in respect to other nodes
|
||||
mSelfDrops.Inc(1)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
package protocols
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Textual version number of accounting API
|
||||
const AccountingVersion = "1.0"
|
||||
|
||||
var errNoAccountingMetrics = errors.New("accounting metrics not enabled")
|
||||
|
||||
// AccountingApi provides an API to access account related information
|
||||
type AccountingApi struct {
|
||||
metrics *AccountingMetrics
|
||||
}
|
||||
|
||||
// NewAccountingApi creates a new AccountingApi
|
||||
// m will be used to check if accounting metrics are enabled
|
||||
func NewAccountingApi(m *AccountingMetrics) *AccountingApi {
|
||||
return &AccountingApi{m}
|
||||
}
|
||||
|
||||
// Balance returns local node balance (units credited - units debited)
|
||||
func (self *AccountingApi) Balance() (int64, error) {
|
||||
if self.metrics == nil {
|
||||
return 0, errNoAccountingMetrics
|
||||
}
|
||||
balance := mBalanceCredit.Count() - mBalanceDebit.Count()
|
||||
return balance, nil
|
||||
}
|
||||
|
||||
// BalanceCredit returns total amount of units credited by local node
|
||||
func (self *AccountingApi) BalanceCredit() (int64, error) {
|
||||
if self.metrics == nil {
|
||||
return 0, errNoAccountingMetrics
|
||||
}
|
||||
return mBalanceCredit.Count(), nil
|
||||
}
|
||||
|
||||
// BalanceCredit returns total amount of units debited by local node
|
||||
func (self *AccountingApi) BalanceDebit() (int64, error) {
|
||||
if self.metrics == nil {
|
||||
return 0, errNoAccountingMetrics
|
||||
}
|
||||
return mBalanceDebit.Count(), nil
|
||||
}
|
||||
|
||||
// BytesCredit returns total amount of bytes credited by local node
|
||||
func (self *AccountingApi) BytesCredit() (int64, error) {
|
||||
if self.metrics == nil {
|
||||
return 0, errNoAccountingMetrics
|
||||
}
|
||||
return mBytesCredit.Count(), nil
|
||||
}
|
||||
|
||||
// BalanceCredit returns total amount of bytes debited by local node
|
||||
func (self *AccountingApi) BytesDebit() (int64, error) {
|
||||
if self.metrics == nil {
|
||||
return 0, errNoAccountingMetrics
|
||||
}
|
||||
return mBytesDebit.Count(), nil
|
||||
}
|
||||
|
||||
// MsgCredit returns total amount of messages credited by local node
|
||||
func (self *AccountingApi) MsgCredit() (int64, error) {
|
||||
if self.metrics == nil {
|
||||
return 0, errNoAccountingMetrics
|
||||
}
|
||||
return mMsgCredit.Count(), nil
|
||||
}
|
||||
|
||||
// MsgDebit returns total amount of messages debited by local node
|
||||
func (self *AccountingApi) MsgDebit() (int64, error) {
|
||||
if self.metrics == nil {
|
||||
return 0, errNoAccountingMetrics
|
||||
}
|
||||
return mMsgDebit.Count(), nil
|
||||
}
|
||||
|
||||
// PeerDrops returns number of times when local node had to drop remote peers
|
||||
func (self *AccountingApi) PeerDrops() (int64, error) {
|
||||
if self.metrics == nil {
|
||||
return 0, errNoAccountingMetrics
|
||||
}
|
||||
return mPeerDrops.Count(), nil
|
||||
}
|
||||
|
||||
// SelfDrops returns number of times when local node was overdrafted and dropped
|
||||
func (self *AccountingApi) SelfDrops() (int64, error) {
|
||||
if self.metrics == nil {
|
||||
return 0, errNoAccountingMetrics
|
||||
}
|
||||
return mSelfDrops.Count(), nil
|
||||
}
|
@ -1,323 +0,0 @@
|
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package protocols
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-colorable"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
|
||||
"github.com/ethereum/go-ethereum/node"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/p2p/simulations"
|
||||
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
||||
)
|
||||
|
||||
const (
|
||||
content = "123456789"
|
||||
)
|
||||
|
||||
var (
|
||||
nodes = flag.Int("nodes", 30, "number of nodes to create (default 30)")
|
||||
msgs = flag.Int("msgs", 100, "number of messages sent by node (default 100)")
|
||||
loglevel = flag.Int("loglevel", 0, "verbosity of logs")
|
||||
rawlog = flag.Bool("rawlog", false, "remove terminal formatting from logs")
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.Parse()
|
||||
log.PrintOrigins(true)
|
||||
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(!*rawlog))))
|
||||
}
|
||||
|
||||
//TestAccountingSimulation runs a p2p/simulations simulation
|
||||
//It creates a *nodes number of nodes, connects each one with each other,
|
||||
//then sends out a random selection of messages up to *msgs amount of messages
|
||||
//from the test protocol spec.
|
||||
//The spec has some accounted messages defined through the Prices interface.
|
||||
//The test does accounting for all the message exchanged, and then checks
|
||||
//that every node has the same balance with a peer, but with opposite signs.
|
||||
//Balance(AwithB) = 0 - Balance(BwithA) or Abs|Balance(AwithB)| == Abs|Balance(BwithA)|
|
||||
func TestAccountingSimulation(t *testing.T) {
|
||||
//setup the balances objects for every node
|
||||
bal := newBalances(*nodes)
|
||||
//setup the metrics system or tests will fail trying to write metrics
|
||||
dir, err := ioutil.TempDir("", "account-sim")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
SetupAccountingMetrics(1*time.Second, filepath.Join(dir, "metrics.db"))
|
||||
//define the node.Service for this test
|
||||
services := adapters.Services{
|
||||
"accounting": func(ctx *adapters.ServiceContext) (node.Service, error) {
|
||||
return bal.newNode(), nil
|
||||
},
|
||||
}
|
||||
//setup the simulation
|
||||
adapter := adapters.NewSimAdapter(services)
|
||||
net := simulations.NewNetwork(adapter, &simulations.NetworkConfig{DefaultService: "accounting"})
|
||||
defer net.Shutdown()
|
||||
|
||||
// we send msgs messages per node, wait for all messages to arrive
|
||||
bal.wg.Add(*nodes * *msgs)
|
||||
trigger := make(chan enode.ID)
|
||||
go func() {
|
||||
// wait for all of them to arrive
|
||||
bal.wg.Wait()
|
||||
// then trigger a check
|
||||
// the selected node for the trigger is irrelevant,
|
||||
// we just want to trigger the end of the simulation
|
||||
trigger <- net.Nodes[0].ID()
|
||||
}()
|
||||
|
||||
// create nodes and start them
|
||||
for i := 0; i < *nodes; i++ {
|
||||
conf := adapters.RandomNodeConfig()
|
||||
bal.id2n[conf.ID] = i
|
||||
if _, err := net.NewNodeWithConfig(conf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := net.Start(conf.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// fully connect nodes
|
||||
for i, n := range net.Nodes {
|
||||
for _, m := range net.Nodes[i+1:] {
|
||||
if err := net.Connect(n.ID(), m.ID()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// empty action
|
||||
action := func(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
// check always checks out
|
||||
check := func(ctx context.Context, id enode.ID) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// run simulation
|
||||
timeout := 30 * time.Second
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
result := simulations.NewSimulation(net).Run(ctx, &simulations.Step{
|
||||
Action: action,
|
||||
Trigger: trigger,
|
||||
Expect: &simulations.Expectation{
|
||||
Nodes: []enode.ID{net.Nodes[0].ID()},
|
||||
Check: check,
|
||||
},
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
t.Fatal(result.Error)
|
||||
}
|
||||
|
||||
// check if balance matrix is symmetric
|
||||
if err := bal.symmetric(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// matrix is a matrix of nodes and its balances
|
||||
// matrix is in fact a linear array of size n*n,
|
||||
// so the balance for any node A with B is at index
|
||||
// A*n + B, while the balance of node B with A is at
|
||||
// B*n + A
|
||||
// (n entries in the array will not be filled -
|
||||
// the balance of a node with itself)
|
||||
type matrix struct {
|
||||
n int //number of nodes
|
||||
m []int64 //array of balances
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
// create a new matrix
|
||||
func newMatrix(n int) *matrix {
|
||||
return &matrix{
|
||||
n: n,
|
||||
m: make([]int64, n*n),
|
||||
}
|
||||
}
|
||||
|
||||
// called from the testBalance's Add accounting function: register balance change
|
||||
func (m *matrix) add(i, j int, v int64) error {
|
||||
// index for the balance of local node i with remote nodde j is
|
||||
// i * number of nodes + remote node
|
||||
mi := i*m.n + j
|
||||
// register that balance
|
||||
m.lock.Lock()
|
||||
m.m[mi] += v
|
||||
m.lock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// check that the balances are symmetric:
|
||||
// balance of node i with node j is the same as j with i but with inverted signs
|
||||
func (m *matrix) symmetric() error {
|
||||
//iterate all nodes
|
||||
for i := 0; i < m.n; i++ {
|
||||
//iterate starting +1
|
||||
for j := i + 1; j < m.n; j++ {
|
||||
log.Debug("bal", "1", i, "2", j, "i,j", m.m[i*m.n+j], "j,i", m.m[j*m.n+i])
|
||||
if m.m[i*m.n+j] != -m.m[j*m.n+i] {
|
||||
return fmt.Errorf("value mismatch. m[%v, %v] = %v; m[%v, %v] = %v", i, j, m.m[i*m.n+j], j, i, m.m[j*m.n+i])
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// all the balances
|
||||
type balances struct {
|
||||
i int
|
||||
*matrix
|
||||
id2n map[enode.ID]int
|
||||
wg *sync.WaitGroup
|
||||
}
|
||||
|
||||
func newBalances(n int) *balances {
|
||||
return &balances{
|
||||
matrix: newMatrix(n),
|
||||
id2n: make(map[enode.ID]int),
|
||||
wg: &sync.WaitGroup{},
|
||||
}
|
||||
}
|
||||
|
||||
// create a new testNode for every node created as part of the service
|
||||
func (b *balances) newNode() *testNode {
|
||||
defer func() { b.i++ }()
|
||||
return &testNode{
|
||||
bal: b,
|
||||
i: b.i,
|
||||
peers: make([]*testPeer, b.n), //a node will be connected to n-1 peers
|
||||
}
|
||||
}
|
||||
|
||||
type testNode struct {
|
||||
bal *balances
|
||||
i int
|
||||
lock sync.Mutex
|
||||
peers []*testPeer
|
||||
peerCount int
|
||||
}
|
||||
|
||||
// do the accounting for the peer's test protocol
|
||||
// testNode implements protocols.Balance
|
||||
func (t *testNode) Add(a int64, p *Peer) error {
|
||||
//get the index for the remote peer
|
||||
remote := t.bal.id2n[p.ID()]
|
||||
log.Debug("add", "local", t.i, "remote", remote, "amount", a)
|
||||
return t.bal.add(t.i, remote, a)
|
||||
}
|
||||
|
||||
//run the p2p protocol
|
||||
//for every node, represented by testNode, create a remote testPeer
|
||||
func (t *testNode) run(p *p2p.Peer, rw p2p.MsgReadWriter) error {
|
||||
spec := createTestSpec()
|
||||
//create accounting hook
|
||||
spec.Hook = NewAccounting(t, &dummyPrices{})
|
||||
|
||||
//create a peer for this node
|
||||
tp := &testPeer{NewPeer(p, rw, spec), t.i, t.bal.id2n[p.ID()], t.bal.wg}
|
||||
t.lock.Lock()
|
||||
t.peers[t.bal.id2n[p.ID()]] = tp
|
||||
t.peerCount++
|
||||
if t.peerCount == t.bal.n-1 {
|
||||
//when all peer connections are established, start sending messages from this peer
|
||||
go t.send()
|
||||
}
|
||||
t.lock.Unlock()
|
||||
return tp.Run(tp.handle)
|
||||
}
|
||||
|
||||
// p2p message receive handler function
|
||||
func (tp *testPeer) handle(ctx context.Context, msg interface{}) error {
|
||||
tp.wg.Done()
|
||||
log.Debug("receive", "from", tp.remote, "to", tp.local, "type", reflect.TypeOf(msg), "msg", msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
type testPeer struct {
|
||||
*Peer
|
||||
local, remote int
|
||||
wg *sync.WaitGroup
|
||||
}
|
||||
|
||||
func (t *testNode) send() {
|
||||
log.Debug("start sending")
|
||||
for i := 0; i < *msgs; i++ {
|
||||
//determine randomly to which peer to send
|
||||
whom := rand.Intn(t.bal.n - 1)
|
||||
if whom >= t.i {
|
||||
whom++
|
||||
}
|
||||
t.lock.Lock()
|
||||
p := t.peers[whom]
|
||||
t.lock.Unlock()
|
||||
|
||||
//determine a random message from the spec's messages to be sent
|
||||
which := rand.Intn(len(p.spec.Messages))
|
||||
msg := p.spec.Messages[which]
|
||||
switch msg.(type) {
|
||||
case *perBytesMsgReceiverPays:
|
||||
msg = &perBytesMsgReceiverPays{Content: content[:rand.Intn(len(content))]}
|
||||
case *perBytesMsgSenderPays:
|
||||
msg = &perBytesMsgSenderPays{Content: content[:rand.Intn(len(content))]}
|
||||
}
|
||||
log.Debug("send", "from", t.i, "to", whom, "type", reflect.TypeOf(msg), "msg", msg)
|
||||
p.Send(context.TODO(), msg)
|
||||
}
|
||||
}
|
||||
|
||||
// define the protocol
|
||||
func (t *testNode) Protocols() []p2p.Protocol {
|
||||
return []p2p.Protocol{{
|
||||
Length: 100,
|
||||
Run: t.run,
|
||||
}}
|
||||
}
|
||||
|
||||
func (t *testNode) APIs() []rpc.API {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *testNode) Start(server *p2p.Server) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *testNode) Stop() error {
|
||||
return nil
|
||||
}
|
@ -1,223 +0,0 @@
|
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package protocols
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
//dummy Balance implementation
|
||||
type dummyBalance struct {
|
||||
amount int64
|
||||
peer *Peer
|
||||
}
|
||||
|
||||
//dummy Prices implementation
|
||||
type dummyPrices struct{}
|
||||
|
||||
//a dummy message which needs size based accounting
|
||||
//sender pays
|
||||
type perBytesMsgSenderPays struct {
|
||||
Content string
|
||||
}
|
||||
|
||||
//a dummy message which needs size based accounting
|
||||
//receiver pays
|
||||
type perBytesMsgReceiverPays struct {
|
||||
Content string
|
||||
}
|
||||
|
||||
//a dummy message which is paid for per unit
|
||||
//sender pays
|
||||
type perUnitMsgSenderPays struct{}
|
||||
|
||||
//receiver pays
|
||||
type perUnitMsgReceiverPays struct{}
|
||||
|
||||
//a dummy message which has zero as its price
|
||||
type zeroPriceMsg struct{}
|
||||
|
||||
//a dummy message which has no accounting
|
||||
type nilPriceMsg struct{}
|
||||
|
||||
//return the price for the defined messages
|
||||
func (d *dummyPrices) Price(msg interface{}) *Price {
|
||||
switch msg.(type) {
|
||||
//size based message cost, receiver pays
|
||||
case *perBytesMsgReceiverPays:
|
||||
return &Price{
|
||||
PerByte: true,
|
||||
Value: uint64(100),
|
||||
Payer: Receiver,
|
||||
}
|
||||
//size based message cost, sender pays
|
||||
case *perBytesMsgSenderPays:
|
||||
return &Price{
|
||||
PerByte: true,
|
||||
Value: uint64(100),
|
||||
Payer: Sender,
|
||||
}
|
||||
//unitary cost, receiver pays
|
||||
case *perUnitMsgReceiverPays:
|
||||
return &Price{
|
||||
PerByte: false,
|
||||
Value: uint64(99),
|
||||
Payer: Receiver,
|
||||
}
|
||||
//unitary cost, sender pays
|
||||
case *perUnitMsgSenderPays:
|
||||
return &Price{
|
||||
PerByte: false,
|
||||
Value: uint64(99),
|
||||
Payer: Sender,
|
||||
}
|
||||
case *zeroPriceMsg:
|
||||
return &Price{
|
||||
PerByte: false,
|
||||
Value: uint64(0),
|
||||
Payer: Sender,
|
||||
}
|
||||
case *nilPriceMsg:
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//dummy accounting implementation, only stores values for later check
|
||||
func (d *dummyBalance) Add(amount int64, peer *Peer) error {
|
||||
d.amount = amount
|
||||
d.peer = peer
|
||||
return nil
|
||||
}
|
||||
|
||||
type testCase struct {
|
||||
msg interface{}
|
||||
size uint32
|
||||
sendResult int64
|
||||
recvResult int64
|
||||
}
|
||||
|
||||
//lowest level unit test
|
||||
func TestBalance(t *testing.T) {
|
||||
//create instances
|
||||
balance := &dummyBalance{}
|
||||
prices := &dummyPrices{}
|
||||
//create the spec
|
||||
spec := createTestSpec()
|
||||
//create the accounting hook for the spec
|
||||
acc := NewAccounting(balance, prices)
|
||||
//create a peer
|
||||
id := adapters.RandomNodeConfig().ID
|
||||
p := p2p.NewPeer(id, "testPeer", nil)
|
||||
peer := NewPeer(p, &dummyRW{}, spec)
|
||||
//price depends on size, receiver pays
|
||||
msg := &perBytesMsgReceiverPays{Content: "testBalance"}
|
||||
size, _ := rlp.EncodeToBytes(msg)
|
||||
|
||||
testCases := []testCase{
|
||||
{
|
||||
msg,
|
||||
uint32(len(size)),
|
||||
int64(len(size) * 100),
|
||||
int64(len(size) * -100),
|
||||
},
|
||||
{
|
||||
&perBytesMsgSenderPays{Content: "testBalance"},
|
||||
uint32(len(size)),
|
||||
int64(len(size) * -100),
|
||||
int64(len(size) * 100),
|
||||
},
|
||||
{
|
||||
&perUnitMsgSenderPays{},
|
||||
0,
|
||||
int64(-99),
|
||||
int64(99),
|
||||
},
|
||||
{
|
||||
&perUnitMsgReceiverPays{},
|
||||
0,
|
||||
int64(99),
|
||||
int64(-99),
|
||||
},
|
||||
{
|
||||
&zeroPriceMsg{},
|
||||
0,
|
||||
int64(0),
|
||||
int64(0),
|
||||
},
|
||||
{
|
||||
&nilPriceMsg{},
|
||||
0,
|
||||
int64(0),
|
||||
int64(0),
|
||||
},
|
||||
}
|
||||
checkAccountingTestCases(t, testCases, acc, peer, balance, true)
|
||||
checkAccountingTestCases(t, testCases, acc, peer, balance, false)
|
||||
}
|
||||
|
||||
func checkAccountingTestCases(t *testing.T, cases []testCase, acc *Accounting, peer *Peer, balance *dummyBalance, send bool) {
|
||||
for _, c := range cases {
|
||||
var err error
|
||||
var expectedResult int64
|
||||
//reset balance before every check
|
||||
balance.amount = 0
|
||||
if send {
|
||||
err = acc.Send(peer, c.size, c.msg)
|
||||
expectedResult = c.sendResult
|
||||
} else {
|
||||
err = acc.Receive(peer, c.size, c.msg)
|
||||
expectedResult = c.recvResult
|
||||
}
|
||||
|
||||
checkResults(t, err, balance, peer, expectedResult)
|
||||
}
|
||||
}
|
||||
|
||||
func checkResults(t *testing.T, err error, balance *dummyBalance, peer *Peer, result int64) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if balance.peer != peer {
|
||||
t.Fatalf("expected Add to be called with peer %v, got %v", peer, balance.peer)
|
||||
}
|
||||
if balance.amount != result {
|
||||
t.Fatalf("Expected balance to be %d but is %d", result, balance.amount)
|
||||
}
|
||||
}
|
||||
|
||||
//create a test spec
|
||||
func createTestSpec() *Spec {
|
||||
spec := &Spec{
|
||||
Name: "test",
|
||||
Version: 42,
|
||||
MaxMsgSize: 10 * 1024,
|
||||
Messages: []interface{}{
|
||||
&perBytesMsgReceiverPays{},
|
||||
&perBytesMsgSenderPays{},
|
||||
&perUnitMsgReceiverPays{},
|
||||
&perUnitMsgSenderPays{},
|
||||
&zeroPriceMsg{},
|
||||
&nilPriceMsg{},
|
||||
},
|
||||
}
|
||||
return spec
|
||||
}
|
@ -1,443 +0,0 @@
|
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/*
|
||||
Package protocols is an extension to p2p. It offers a user friendly simple way to define
|
||||
devp2p subprotocols by abstracting away code standardly shared by protocols.
|
||||
|
||||
* automate assigments of code indexes to messages
|
||||
* automate RLP decoding/encoding based on reflecting
|
||||
* provide the forever loop to read incoming messages
|
||||
* standardise error handling related to communication
|
||||
* standardised handshake negotiation
|
||||
* TODO: automatic generation of wire protocol specification for peers
|
||||
|
||||
*/
|
||||
package protocols
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/metrics"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/swarm/spancontext"
|
||||
"github.com/ethereum/go-ethereum/swarm/tracing"
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
)
|
||||
|
||||
// error codes used by this protocol scheme
|
||||
const (
|
||||
ErrMsgTooLong = iota
|
||||
ErrDecode
|
||||
ErrWrite
|
||||
ErrInvalidMsgCode
|
||||
ErrInvalidMsgType
|
||||
ErrHandshake
|
||||
ErrNoHandler
|
||||
ErrHandler
|
||||
)
|
||||
|
||||
// error description strings associated with the codes
|
||||
var errorToString = map[int]string{
|
||||
ErrMsgTooLong: "Message too long",
|
||||
ErrDecode: "Invalid message (RLP error)",
|
||||
ErrWrite: "Error sending message",
|
||||
ErrInvalidMsgCode: "Invalid message code",
|
||||
ErrInvalidMsgType: "Invalid message type",
|
||||
ErrHandshake: "Handshake error",
|
||||
ErrNoHandler: "No handler registered error",
|
||||
ErrHandler: "Message handler error",
|
||||
}
|
||||
|
||||
/*
|
||||
Error implements the standard go error interface.
|
||||
Use:
|
||||
|
||||
errorf(code, format, params ...interface{})
|
||||
|
||||
Prints as:
|
||||
|
||||
<description>: <details>
|
||||
|
||||
where description is given by code in errorToString
|
||||
and details is fmt.Sprintf(format, params...)
|
||||
|
||||
exported field Code can be checked
|
||||
*/
|
||||
type Error struct {
|
||||
Code int
|
||||
message string
|
||||
format string
|
||||
params []interface{}
|
||||
}
|
||||
|
||||
func (e Error) Error() (message string) {
|
||||
if len(e.message) == 0 {
|
||||
name, ok := errorToString[e.Code]
|
||||
if !ok {
|
||||
panic("invalid message code")
|
||||
}
|
||||
e.message = name
|
||||
if e.format != "" {
|
||||
e.message += ": " + fmt.Sprintf(e.format, e.params...)
|
||||
}
|
||||
}
|
||||
return e.message
|
||||
}
|
||||
|
||||
func errorf(code int, format string, params ...interface{}) *Error {
|
||||
return &Error{
|
||||
Code: code,
|
||||
format: format,
|
||||
params: params,
|
||||
}
|
||||
}
|
||||
|
||||
// WrappedMsg is used to propagate marshalled context alongside message payloads
|
||||
type WrappedMsg struct {
|
||||
Context []byte
|
||||
Size uint32
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
//For accounting, the design is to allow the Spec to describe which and how its messages are priced
|
||||
//To access this functionality, we provide a Hook interface which will call accounting methods
|
||||
//NOTE: there could be more such (horizontal) hooks in the future
|
||||
type Hook interface {
|
||||
//A hook for sending messages
|
||||
Send(peer *Peer, size uint32, msg interface{}) error
|
||||
//A hook for receiving messages
|
||||
Receive(peer *Peer, size uint32, msg interface{}) error
|
||||
}
|
||||
|
||||
// Spec is a protocol specification including its name and version as well as
|
||||
// the types of messages which are exchanged
|
||||
type Spec struct {
|
||||
// Name is the name of the protocol, often a three-letter word
|
||||
Name string
|
||||
|
||||
// Version is the version number of the protocol
|
||||
Version uint
|
||||
|
||||
// MaxMsgSize is the maximum accepted length of the message payload
|
||||
MaxMsgSize uint32
|
||||
|
||||
// Messages is a list of message data types which this protocol uses, with
|
||||
// each message type being sent with its array index as the code (so
|
||||
// [&foo{}, &bar{}, &baz{}] would send foo, bar and baz with codes
|
||||
// 0, 1 and 2 respectively)
|
||||
// each message must have a single unique data type
|
||||
Messages []interface{}
|
||||
|
||||
//hook for accounting (could be extended to multiple hooks in the future)
|
||||
Hook Hook
|
||||
|
||||
initOnce sync.Once
|
||||
codes map[reflect.Type]uint64
|
||||
types map[uint64]reflect.Type
|
||||
}
|
||||
|
||||
func (s *Spec) init() {
|
||||
s.initOnce.Do(func() {
|
||||
s.codes = make(map[reflect.Type]uint64, len(s.Messages))
|
||||
s.types = make(map[uint64]reflect.Type, len(s.Messages))
|
||||
for i, msg := range s.Messages {
|
||||
code := uint64(i)
|
||||
typ := reflect.TypeOf(msg)
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
}
|
||||
s.codes[typ] = code
|
||||
s.types[code] = typ
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Length returns the number of message types in the protocol
|
||||
func (s *Spec) Length() uint64 {
|
||||
return uint64(len(s.Messages))
|
||||
}
|
||||
|
||||
// GetCode returns the message code of a type, and boolean second argument is
|
||||
// false if the message type is not found
|
||||
func (s *Spec) GetCode(msg interface{}) (uint64, bool) {
|
||||
s.init()
|
||||
typ := reflect.TypeOf(msg)
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
}
|
||||
code, ok := s.codes[typ]
|
||||
return code, ok
|
||||
}
|
||||
|
||||
// NewMsg construct a new message type given the code
|
||||
func (s *Spec) NewMsg(code uint64) (interface{}, bool) {
|
||||
s.init()
|
||||
typ, ok := s.types[code]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return reflect.New(typ).Interface(), true
|
||||
}
|
||||
|
||||
// Peer represents a remote peer or protocol instance that is running on a peer connection with
|
||||
// a remote peer
|
||||
type Peer struct {
|
||||
*p2p.Peer // the p2p.Peer object representing the remote
|
||||
rw p2p.MsgReadWriter // p2p.MsgReadWriter to send messages to and read messages from
|
||||
spec *Spec
|
||||
}
|
||||
|
||||
// NewPeer constructs a new peer
|
||||
// this constructor is called by the p2p.Protocol#Run function
|
||||
// the first two arguments are the arguments passed to p2p.Protocol.Run function
|
||||
// the third argument is the Spec describing the protocol
|
||||
func NewPeer(p *p2p.Peer, rw p2p.MsgReadWriter, spec *Spec) *Peer {
|
||||
return &Peer{
|
||||
Peer: p,
|
||||
rw: rw,
|
||||
spec: spec,
|
||||
}
|
||||
}
|
||||
|
||||
// Run starts the forever loop that handles incoming messages
|
||||
// called within the p2p.Protocol#Run function
|
||||
// the handler argument is a function which is called for each message received
|
||||
// from the remote peer, a returned error causes the loop to exit
|
||||
// resulting in disconnection
|
||||
func (p *Peer) Run(handler func(ctx context.Context, msg interface{}) error) error {
|
||||
for {
|
||||
if err := p.handleIncoming(handler); err != nil {
|
||||
if err != io.EOF {
|
||||
metrics.GetOrRegisterCounter("peer.handleincoming.error", nil).Inc(1)
|
||||
log.Error("peer.handleIncoming", "err", err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Drop disconnects a peer.
|
||||
// TODO: may need to implement protocol drop only? don't want to kick off the peer
|
||||
// if they are useful for other protocols
|
||||
func (p *Peer) Drop() {
|
||||
p.Disconnect(p2p.DiscSubprotocolError)
|
||||
}
|
||||
|
||||
// Send takes a message, encodes it in RLP, finds the right message code and sends the
|
||||
// message off to the peer
|
||||
// this low level call will be wrapped by libraries providing routed or broadcast sends
|
||||
// but often just used to forward and push messages to directly connected peers
|
||||
func (p *Peer) Send(ctx context.Context, msg interface{}) error {
|
||||
defer metrics.GetOrRegisterResettingTimer("peer.send_t", nil).UpdateSince(time.Now())
|
||||
metrics.GetOrRegisterCounter("peer.send", nil).Inc(1)
|
||||
metrics.GetOrRegisterCounter(fmt.Sprintf("peer.send.%T", msg), nil).Inc(1)
|
||||
|
||||
var b bytes.Buffer
|
||||
if tracing.Enabled {
|
||||
writer := bufio.NewWriter(&b)
|
||||
|
||||
tracer := opentracing.GlobalTracer()
|
||||
|
||||
sctx := spancontext.FromContext(ctx)
|
||||
|
||||
if sctx != nil {
|
||||
err := tracer.Inject(
|
||||
sctx,
|
||||
opentracing.Binary,
|
||||
writer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
writer.Flush()
|
||||
}
|
||||
|
||||
r, err := rlp.EncodeToBytes(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
wmsg := WrappedMsg{
|
||||
Context: b.Bytes(),
|
||||
Size: uint32(len(r)),
|
||||
Payload: r,
|
||||
}
|
||||
|
||||
//if the accounting hook is set, call it
|
||||
if p.spec.Hook != nil {
|
||||
err := p.spec.Hook.Send(p, wmsg.Size, msg)
|
||||
if err != nil {
|
||||
p.Drop()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
code, found := p.spec.GetCode(msg)
|
||||
if !found {
|
||||
return errorf(ErrInvalidMsgType, "%v", code)
|
||||
}
|
||||
return p2p.Send(p.rw, code, wmsg)
|
||||
}
|
||||
|
||||
// handleIncoming(code)
|
||||
// is called each cycle of the main forever loop that dispatches incoming messages
|
||||
// if this returns an error the loop returns and the peer is disconnected with the error
|
||||
// this generic handler
|
||||
// * checks message size,
|
||||
// * checks for out-of-range message codes,
|
||||
// * handles decoding with reflection,
|
||||
// * call handlers as callbacks
|
||||
func (p *Peer) handleIncoming(handle func(ctx context.Context, msg interface{}) error) error {
|
||||
msg, err := p.rw.ReadMsg()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// make sure that the payload has been fully consumed
|
||||
defer msg.Discard()
|
||||
|
||||
if msg.Size > p.spec.MaxMsgSize {
|
||||
return errorf(ErrMsgTooLong, "%v > %v", msg.Size, p.spec.MaxMsgSize)
|
||||
}
|
||||
|
||||
// unmarshal wrapped msg, which might contain context
|
||||
var wmsg WrappedMsg
|
||||
err = msg.Decode(&wmsg)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// if tracing is enabled and the context coming within the request is
|
||||
// not empty, try to unmarshal it
|
||||
if tracing.Enabled && len(wmsg.Context) > 0 {
|
||||
var sctx opentracing.SpanContext
|
||||
|
||||
tracer := opentracing.GlobalTracer()
|
||||
sctx, err = tracer.Extract(
|
||||
opentracing.Binary,
|
||||
bytes.NewReader(wmsg.Context))
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
ctx = spancontext.WithContext(ctx, sctx)
|
||||
}
|
||||
|
||||
val, ok := p.spec.NewMsg(msg.Code)
|
||||
if !ok {
|
||||
return errorf(ErrInvalidMsgCode, "%v", msg.Code)
|
||||
}
|
||||
if err := rlp.DecodeBytes(wmsg.Payload, val); err != nil {
|
||||
return errorf(ErrDecode, "<= %v: %v", msg, err)
|
||||
}
|
||||
|
||||
//if the accounting hook is set, call it
|
||||
if p.spec.Hook != nil {
|
||||
err := p.spec.Hook.Receive(p, wmsg.Size, val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// call the registered handler callbacks
|
||||
// a registered callback take the decoded message as argument as an interface
|
||||
// which the handler is supposed to cast to the appropriate type
|
||||
// it is entirely safe not to check the cast in the handler since the handler is
|
||||
// chosen based on the proper type in the first place
|
||||
if err := handle(ctx, val); err != nil {
|
||||
return errorf(ErrHandler, "(msg code %v): %v", msg.Code, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handshake negotiates a handshake on the peer connection
|
||||
// * arguments
|
||||
// * context
|
||||
// * the local handshake to be sent to the remote peer
|
||||
// * function to be called on the remote handshake (can be nil)
|
||||
// * expects a remote handshake back of the same type
|
||||
// * the dialing peer needs to send the handshake first and then waits for remote
|
||||
// * the listening peer waits for the remote handshake and then sends it
|
||||
// returns the remote handshake and an error
|
||||
func (p *Peer) Handshake(ctx context.Context, hs interface{}, verify func(interface{}) error) (interface{}, error) {
|
||||
if _, ok := p.spec.GetCode(hs); !ok {
|
||||
return nil, errorf(ErrHandshake, "unknown handshake message type: %T", hs)
|
||||
}
|
||||
|
||||
var rhs interface{}
|
||||
errc := make(chan error, 2)
|
||||
handle := func(ctx context.Context, msg interface{}) error {
|
||||
rhs = msg
|
||||
if verify != nil {
|
||||
return verify(rhs)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
send := func() { errc <- p.Send(ctx, hs) }
|
||||
receive := func() { errc <- p.handleIncoming(handle) }
|
||||
|
||||
go func() {
|
||||
if p.Inbound() {
|
||||
receive()
|
||||
send()
|
||||
} else {
|
||||
send()
|
||||
receive()
|
||||
}
|
||||
}()
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
var err error
|
||||
select {
|
||||
case err = <-errc:
|
||||
case <-ctx.Done():
|
||||
err = ctx.Err()
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errorf(ErrHandshake, err.Error())
|
||||
}
|
||||
}
|
||||
return rhs, nil
|
||||
}
|
||||
|
||||
// HasCap returns true if Peer has a capability
|
||||
// with provided name.
|
||||
func (p *Peer) HasCap(capName string) (yes bool) {
|
||||
if p == nil || p.Peer == nil {
|
||||
return false
|
||||
}
|
||||
for _, c := range p.Caps() {
|
||||
if c.Name == capName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
@ -1,624 +0,0 @@
|
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package protocols
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
||||
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
|
||||
)
|
||||
|
||||
// handshake message type
|
||||
type hs0 struct {
|
||||
C uint
|
||||
}
|
||||
|
||||
// message to kill/drop the peer with nodeID
|
||||
type kill struct {
|
||||
C enode.ID
|
||||
}
|
||||
|
||||
// message to drop connection
|
||||
type drop struct {
|
||||
}
|
||||
|
||||
/// protoHandshake represents module-independent aspects of the protocol and is
|
||||
// the first message peers send and receive as part the initial exchange
|
||||
type protoHandshake struct {
|
||||
Version uint // local and remote peer should have identical version
|
||||
NetworkID string // local and remote peer should have identical network id
|
||||
}
|
||||
|
||||
// checkProtoHandshake verifies local and remote protoHandshakes match
|
||||
func checkProtoHandshake(testVersion uint, testNetworkID string) func(interface{}) error {
|
||||
return func(rhs interface{}) error {
|
||||
remote := rhs.(*protoHandshake)
|
||||
if remote.NetworkID != testNetworkID {
|
||||
return fmt.Errorf("%s (!= %s)", remote.NetworkID, testNetworkID)
|
||||
}
|
||||
|
||||
if remote.Version != testVersion {
|
||||
return fmt.Errorf("%d (!= %d)", remote.Version, testVersion)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// newProtocol sets up a protocol
|
||||
// the run function here demonstrates a typical protocol using peerPool, handshake
|
||||
// and messages registered to handlers
|
||||
func newProtocol(pp *p2ptest.TestPeerPool) func(*p2p.Peer, p2p.MsgReadWriter) error {
|
||||
spec := &Spec{
|
||||
Name: "test",
|
||||
Version: 42,
|
||||
MaxMsgSize: 10 * 1024,
|
||||
Messages: []interface{}{
|
||||
protoHandshake{},
|
||||
hs0{},
|
||||
kill{},
|
||||
drop{},
|
||||
},
|
||||
}
|
||||
return func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
|
||||
peer := NewPeer(p, rw, spec)
|
||||
|
||||
// initiate one-off protohandshake and check validity
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
phs := &protoHandshake{42, "420"}
|
||||
hsCheck := checkProtoHandshake(phs.Version, phs.NetworkID)
|
||||
_, err := peer.Handshake(ctx, phs, hsCheck)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lhs := &hs0{42}
|
||||
// module handshake demonstrating a simple repeatable exchange of same-type message
|
||||
hs, err := peer.Handshake(ctx, lhs, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rmhs := hs.(*hs0); rmhs.C > lhs.C {
|
||||
return fmt.Errorf("handshake mismatch remote %v > local %v", rmhs.C, lhs.C)
|
||||
}
|
||||
|
||||
handle := func(ctx context.Context, msg interface{}) error {
|
||||
switch msg := msg.(type) {
|
||||
|
||||
case *protoHandshake:
|
||||
return errors.New("duplicate handshake")
|
||||
|
||||
case *hs0:
|
||||
rhs := msg
|
||||
if rhs.C > lhs.C {
|
||||
return fmt.Errorf("handshake mismatch remote %v > local %v", rhs.C, lhs.C)
|
||||
}
|
||||
lhs.C += rhs.C
|
||||
return peer.Send(ctx, lhs)
|
||||
|
||||
case *kill:
|
||||
// demonstrates use of peerPool, killing another peer connection as a response to a message
|
||||
id := msg.C
|
||||
pp.Get(id).Drop()
|
||||
return nil
|
||||
|
||||
case *drop:
|
||||
// for testing we can trigger self induced disconnect upon receiving drop message
|
||||
return errors.New("dropped")
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unknown message type: %T", msg)
|
||||
}
|
||||
}
|
||||
|
||||
pp.Add(peer)
|
||||
defer pp.Remove(peer)
|
||||
return peer.Run(handle)
|
||||
}
|
||||
}
|
||||
|
||||
func protocolTester(pp *p2ptest.TestPeerPool) *p2ptest.ProtocolTester {
|
||||
prvkey, err := crypto.GenerateKey()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return p2ptest.NewProtocolTester(prvkey, 2, newProtocol(pp))
|
||||
}
|
||||
|
||||
func protoHandshakeExchange(id enode.ID, proto *protoHandshake) []p2ptest.Exchange {
|
||||
|
||||
return []p2ptest.Exchange{
|
||||
{
|
||||
Expects: []p2ptest.Expect{
|
||||
{
|
||||
Code: 0,
|
||||
Msg: &protoHandshake{42, "420"},
|
||||
Peer: id,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Triggers: []p2ptest.Trigger{
|
||||
{
|
||||
Code: 0,
|
||||
Msg: proto,
|
||||
Peer: id,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func runProtoHandshake(t *testing.T, proto *protoHandshake, errs ...error) {
|
||||
t.Helper()
|
||||
pp := p2ptest.NewTestPeerPool()
|
||||
s := protocolTester(pp)
|
||||
defer s.Stop()
|
||||
|
||||
// TODO: make this more than one handshake
|
||||
node := s.Nodes[0]
|
||||
if err := s.TestExchanges(protoHandshakeExchange(node.ID(), proto)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var disconnects []*p2ptest.Disconnect
|
||||
for i, err := range errs {
|
||||
disconnects = append(disconnects, &p2ptest.Disconnect{Peer: s.Nodes[i].ID(), Error: err})
|
||||
}
|
||||
if err := s.TestDisconnected(disconnects...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
type dummyHook struct {
|
||||
peer *Peer
|
||||
size uint32
|
||||
msg interface{}
|
||||
send bool
|
||||
err error
|
||||
waitC chan struct{}
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
type dummyMsg struct {
|
||||
Content string
|
||||
}
|
||||
|
||||
func (d *dummyHook) Send(peer *Peer, size uint32, msg interface{}) error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
d.peer = peer
|
||||
d.size = size
|
||||
d.msg = msg
|
||||
d.send = true
|
||||
return d.err
|
||||
}
|
||||
|
||||
func (d *dummyHook) Receive(peer *Peer, size uint32, msg interface{}) error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
d.peer = peer
|
||||
d.size = size
|
||||
d.msg = msg
|
||||
d.send = false
|
||||
d.waitC <- struct{}{}
|
||||
return d.err
|
||||
}
|
||||
|
||||
func TestProtocolHook(t *testing.T) {
|
||||
testHook := &dummyHook{
|
||||
waitC: make(chan struct{}, 1),
|
||||
}
|
||||
spec := &Spec{
|
||||
Name: "test",
|
||||
Version: 42,
|
||||
MaxMsgSize: 10 * 1024,
|
||||
Messages: []interface{}{
|
||||
dummyMsg{},
|
||||
},
|
||||
Hook: testHook,
|
||||
}
|
||||
|
||||
runFunc := func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
|
||||
peer := NewPeer(p, rw, spec)
|
||||
ctx := context.TODO()
|
||||
err := peer.Send(ctx, &dummyMsg{
|
||||
Content: "handshake"})
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handle := func(ctx context.Context, msg interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
return peer.Run(handle)
|
||||
}
|
||||
|
||||
prvkey, err := crypto.GenerateKey()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tester := p2ptest.NewProtocolTester(prvkey, 2, runFunc)
|
||||
defer tester.Stop()
|
||||
err = tester.TestExchanges(p2ptest.Exchange{
|
||||
Expects: []p2ptest.Expect{
|
||||
{
|
||||
Code: 0,
|
||||
Msg: &dummyMsg{Content: "handshake"},
|
||||
Peer: tester.Nodes[0].ID(),
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
testHook.mu.Lock()
|
||||
if testHook.msg == nil || testHook.msg.(*dummyMsg).Content != "handshake" {
|
||||
t.Fatal("Expected msg to be set, but it is not")
|
||||
}
|
||||
if !testHook.send {
|
||||
t.Fatal("Expected a send message, but it is not")
|
||||
}
|
||||
if testHook.peer == nil {
|
||||
t.Fatal("Expected peer to be set, is nil")
|
||||
}
|
||||
if peerId := testHook.peer.ID(); peerId != tester.Nodes[0].ID() && peerId != tester.Nodes[1].ID() {
|
||||
t.Fatalf("Expected peer ID to be set correctly, but it is not (got %v, exp %v or %v", peerId, tester.Nodes[0].ID(), tester.Nodes[1].ID())
|
||||
}
|
||||
if testHook.size != 11 { //11 is the length of the encoded message
|
||||
t.Fatalf("Expected size to be %d, but it is %d ", 1, testHook.size)
|
||||
}
|
||||
testHook.mu.Unlock()
|
||||
|
||||
err = tester.TestExchanges(p2ptest.Exchange{
|
||||
Triggers: []p2ptest.Trigger{
|
||||
{
|
||||
Code: 0,
|
||||
Msg: &dummyMsg{Content: "response"},
|
||||
Peer: tester.Nodes[1].ID(),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
<-testHook.waitC
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
testHook.mu.Lock()
|
||||
if testHook.msg == nil || testHook.msg.(*dummyMsg).Content != "response" {
|
||||
t.Fatal("Expected msg to be set, but it is not")
|
||||
}
|
||||
if testHook.send {
|
||||
t.Fatal("Expected a send message, but it is not")
|
||||
}
|
||||
if testHook.peer == nil || testHook.peer.ID() != tester.Nodes[1].ID() {
|
||||
t.Fatal("Expected peer ID to be set correctly, but it is not")
|
||||
}
|
||||
if testHook.size != 10 { //11 is the length of the encoded message
|
||||
t.Fatalf("Expected size to be %d, but it is %d ", 1, testHook.size)
|
||||
}
|
||||
testHook.mu.Unlock()
|
||||
|
||||
testHook.err = fmt.Errorf("dummy error")
|
||||
err = tester.TestExchanges(p2ptest.Exchange{
|
||||
Triggers: []p2ptest.Trigger{
|
||||
{
|
||||
Code: 0,
|
||||
Msg: &dummyMsg{Content: "response"},
|
||||
Peer: tester.Nodes[1].ID(),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
<-testHook.waitC
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
err = tester.TestDisconnected(&p2ptest.Disconnect{Peer: tester.Nodes[1].ID(), Error: testHook.err})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected a specific disconnect error, but got different one: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
//We need to test that if the hook is not defined, then message infrastructure
|
||||
//(send,receive) still works
|
||||
func TestNoHook(t *testing.T) {
|
||||
//create a test spec
|
||||
spec := createTestSpec()
|
||||
//a random node
|
||||
id := adapters.RandomNodeConfig().ID
|
||||
//a peer
|
||||
p := p2p.NewPeer(id, "testPeer", nil)
|
||||
rw := &dummyRW{}
|
||||
peer := NewPeer(p, rw, spec)
|
||||
ctx := context.TODO()
|
||||
msg := &perBytesMsgSenderPays{Content: "testBalance"}
|
||||
//send a message
|
||||
|
||||
if err := peer.Send(ctx, msg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
//simulate receiving a message
|
||||
rw.msg = msg
|
||||
handler := func(ctx context.Context, msg interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := peer.handleIncoming(handler); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtoHandshakeVersionMismatch(t *testing.T) {
|
||||
runProtoHandshake(t, &protoHandshake{41, "420"}, errorf(ErrHandshake, errorf(ErrHandler, "(msg code 0): 41 (!= 42)").Error()))
|
||||
}
|
||||
|
||||
func TestProtoHandshakeNetworkIDMismatch(t *testing.T) {
|
||||
runProtoHandshake(t, &protoHandshake{42, "421"}, errorf(ErrHandshake, errorf(ErrHandler, "(msg code 0): 421 (!= 420)").Error()))
|
||||
}
|
||||
|
||||
func TestProtoHandshakeSuccess(t *testing.T) {
|
||||
runProtoHandshake(t, &protoHandshake{42, "420"})
|
||||
}
|
||||
|
||||
func moduleHandshakeExchange(id enode.ID, resp uint) []p2ptest.Exchange {
|
||||
|
||||
return []p2ptest.Exchange{
|
||||
{
|
||||
Expects: []p2ptest.Expect{
|
||||
{
|
||||
Code: 1,
|
||||
Msg: &hs0{42},
|
||||
Peer: id,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Triggers: []p2ptest.Trigger{
|
||||
{
|
||||
Code: 1,
|
||||
Msg: &hs0{resp},
|
||||
Peer: id,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func runModuleHandshake(t *testing.T, resp uint, errs ...error) {
|
||||
t.Helper()
|
||||
pp := p2ptest.NewTestPeerPool()
|
||||
s := protocolTester(pp)
|
||||
defer s.Stop()
|
||||
|
||||
node := s.Nodes[0]
|
||||
if err := s.TestExchanges(protoHandshakeExchange(node.ID(), &protoHandshake{42, "420"})...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.TestExchanges(moduleHandshakeExchange(node.ID(), resp)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var disconnects []*p2ptest.Disconnect
|
||||
for i, err := range errs {
|
||||
disconnects = append(disconnects, &p2ptest.Disconnect{Peer: s.Nodes[i].ID(), Error: err})
|
||||
}
|
||||
if err := s.TestDisconnected(disconnects...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModuleHandshakeError(t *testing.T) {
|
||||
runModuleHandshake(t, 43, fmt.Errorf("handshake mismatch remote 43 > local 42"))
|
||||
}
|
||||
|
||||
func TestModuleHandshakeSuccess(t *testing.T) {
|
||||
runModuleHandshake(t, 42)
|
||||
}
|
||||
|
||||
// testing complex interactions over multiple peers, relaying, dropping
|
||||
func testMultiPeerSetup(a, b enode.ID) []p2ptest.Exchange {
|
||||
|
||||
return []p2ptest.Exchange{
|
||||
{
|
||||
Label: "primary handshake",
|
||||
Expects: []p2ptest.Expect{
|
||||
{
|
||||
Code: 0,
|
||||
Msg: &protoHandshake{42, "420"},
|
||||
Peer: a,
|
||||
},
|
||||
{
|
||||
Code: 0,
|
||||
Msg: &protoHandshake{42, "420"},
|
||||
Peer: b,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Label: "module handshake",
|
||||
Triggers: []p2ptest.Trigger{
|
||||
{
|
||||
Code: 0,
|
||||
Msg: &protoHandshake{42, "420"},
|
||||
Peer: a,
|
||||
},
|
||||
{
|
||||
Code: 0,
|
||||
Msg: &protoHandshake{42, "420"},
|
||||
Peer: b,
|
||||
},
|
||||
},
|
||||
Expects: []p2ptest.Expect{
|
||||
{
|
||||
Code: 1,
|
||||
Msg: &hs0{42},
|
||||
Peer: a,
|
||||
},
|
||||
{
|
||||
Code: 1,
|
||||
Msg: &hs0{42},
|
||||
Peer: b,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
{Label: "alternative module handshake", Triggers: []p2ptest.Trigger{{Code: 1, Msg: &hs0{41}, Peer: a},
|
||||
{Code: 1, Msg: &hs0{41}, Peer: b}}},
|
||||
{Label: "repeated module handshake", Triggers: []p2ptest.Trigger{{Code: 1, Msg: &hs0{1}, Peer: a}}},
|
||||
{Label: "receiving repeated module handshake", Expects: []p2ptest.Expect{{Code: 1, Msg: &hs0{43}, Peer: a}}}}
|
||||
}
|
||||
|
||||
func runMultiplePeers(t *testing.T, peer int, errs ...error) {
|
||||
t.Helper()
|
||||
pp := p2ptest.NewTestPeerPool()
|
||||
s := protocolTester(pp)
|
||||
defer s.Stop()
|
||||
|
||||
if err := s.TestExchanges(testMultiPeerSetup(s.Nodes[0].ID(), s.Nodes[1].ID())...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// after some exchanges of messages, we can test state changes
|
||||
// here this is simply demonstrated by the peerPool
|
||||
// after the handshake negotiations peers must be added to the pool
|
||||
// time.Sleep(1)
|
||||
tick := time.NewTicker(10 * time.Millisecond)
|
||||
timeout := time.NewTimer(1 * time.Second)
|
||||
WAIT:
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
if pp.Has(s.Nodes[0].ID()) {
|
||||
break WAIT
|
||||
}
|
||||
case <-timeout.C:
|
||||
t.Fatal("timeout")
|
||||
}
|
||||
}
|
||||
if !pp.Has(s.Nodes[1].ID()) {
|
||||
t.Fatalf("missing peer test-1: %v (%v)", pp, s.Nodes)
|
||||
}
|
||||
|
||||
// peer 0 sends kill request for peer with index <peer>
|
||||
err := s.TestExchanges(p2ptest.Exchange{
|
||||
Triggers: []p2ptest.Trigger{
|
||||
{
|
||||
Code: 2,
|
||||
Msg: &kill{s.Nodes[peer].ID()},
|
||||
Peer: s.Nodes[0].ID(),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// the peer not killed sends a drop request
|
||||
err = s.TestExchanges(p2ptest.Exchange{
|
||||
Triggers: []p2ptest.Trigger{
|
||||
{
|
||||
Code: 3,
|
||||
Msg: &drop{},
|
||||
Peer: s.Nodes[(peer+1)%2].ID(),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// check the actual discconnect errors on the individual peers
|
||||
var disconnects []*p2ptest.Disconnect
|
||||
for i, err := range errs {
|
||||
disconnects = append(disconnects, &p2ptest.Disconnect{Peer: s.Nodes[i].ID(), Error: err})
|
||||
}
|
||||
if err := s.TestDisconnected(disconnects...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// test if disconnected peers have been removed from peerPool
|
||||
if pp.Has(s.Nodes[peer].ID()) {
|
||||
t.Fatalf("peer test-%v not dropped: %v (%v)", peer, pp, s.Nodes)
|
||||
}
|
||||
|
||||
}
|
||||
func TestMultiplePeersDropSelf(t *testing.T) {
|
||||
runMultiplePeers(t, 0,
|
||||
fmt.Errorf("subprotocol error"),
|
||||
fmt.Errorf("Message handler error: (msg code 3): dropped"),
|
||||
)
|
||||
}
|
||||
|
||||
func TestMultiplePeersDropOther(t *testing.T) {
|
||||
runMultiplePeers(t, 1,
|
||||
fmt.Errorf("Message handler error: (msg code 3): dropped"),
|
||||
fmt.Errorf("subprotocol error"),
|
||||
)
|
||||
}
|
||||
|
||||
//dummy implementation of a MsgReadWriter
|
||||
//this allows for quick and easy unit tests without
|
||||
//having to build up the complete protocol
|
||||
type dummyRW struct {
|
||||
msg interface{}
|
||||
size uint32
|
||||
code uint64
|
||||
}
|
||||
|
||||
func (d *dummyRW) WriteMsg(msg p2p.Msg) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *dummyRW) ReadMsg() (p2p.Msg, error) {
|
||||
enc := bytes.NewReader(d.getDummyMsg())
|
||||
return p2p.Msg{
|
||||
Code: d.code,
|
||||
Size: d.size,
|
||||
Payload: enc,
|
||||
ReceivedAt: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *dummyRW) getDummyMsg() []byte {
|
||||
r, _ := rlp.EncodeToBytes(d.msg)
|
||||
var b bytes.Buffer
|
||||
wmsg := WrappedMsg{
|
||||
Context: b.Bytes(),
|
||||
Size: uint32(len(r)),
|
||||
Payload: r,
|
||||
}
|
||||
rr, _ := rlp.EncodeToBytes(wmsg)
|
||||
d.size = uint32(len(rr))
|
||||
return rr
|
||||
}
|
@ -1,162 +0,0 @@
|
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package protocols
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/metrics"
|
||||
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
)
|
||||
|
||||
//AccountMetrics abstracts away the metrics DB and
|
||||
//the reporter to persist metrics
|
||||
type AccountingMetrics struct {
|
||||
reporter *reporter
|
||||
}
|
||||
|
||||
//Close will be called when the node is being shutdown
|
||||
//for a graceful cleanup
|
||||
func (am *AccountingMetrics) Close() {
|
||||
close(am.reporter.quit)
|
||||
// wait for reporter loop to finish saving metrics
|
||||
// before reporter database is closed
|
||||
select {
|
||||
case <-time.After(10 * time.Second):
|
||||
log.Error("accounting metrics reporter timeout")
|
||||
case <-am.reporter.done:
|
||||
}
|
||||
am.reporter.db.Close()
|
||||
}
|
||||
|
||||
//reporter is an internal structure used to write p2p accounting related
|
||||
//metrics to a LevelDB. It will periodically write the accrued metrics to the DB.
|
||||
type reporter struct {
|
||||
reg metrics.Registry //the registry for these metrics (independent of other metrics)
|
||||
interval time.Duration //duration at which the reporter will persist metrics
|
||||
db *leveldb.DB //the actual DB
|
||||
quit chan struct{} //quit the reporter loop
|
||||
done chan struct{} //signal that reporter loop is done
|
||||
}
|
||||
|
||||
//NewMetricsDB creates a new LevelDB instance used to persist metrics defined
|
||||
//inside p2p/protocols/accounting.go
|
||||
func NewAccountingMetrics(r metrics.Registry, d time.Duration, path string) *AccountingMetrics {
|
||||
var val = make([]byte, 8)
|
||||
var err error
|
||||
|
||||
//Create the LevelDB
|
||||
db, err := leveldb.OpenFile(path, nil)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
//Check for all defined metrics that there is a value in the DB
|
||||
//If there is, assign it to the metric. This means that the node
|
||||
//has been running before and that metrics have been persisted.
|
||||
metricsMap := map[string]metrics.Counter{
|
||||
"account.balance.credit": mBalanceCredit,
|
||||
"account.balance.debit": mBalanceDebit,
|
||||
"account.bytes.credit": mBytesCredit,
|
||||
"account.bytes.debit": mBytesDebit,
|
||||
"account.msg.credit": mMsgCredit,
|
||||
"account.msg.debit": mMsgDebit,
|
||||
"account.peerdrops": mPeerDrops,
|
||||
"account.selfdrops": mSelfDrops,
|
||||
}
|
||||
//iterate the map and get the values
|
||||
for key, metric := range metricsMap {
|
||||
val, err = db.Get([]byte(key), nil)
|
||||
//until the first time a value is being written,
|
||||
//this will return an error.
|
||||
//it could be beneficial though to log errors later,
|
||||
//but that would require a different logic
|
||||
if err == nil {
|
||||
metric.Inc(int64(binary.BigEndian.Uint64(val)))
|
||||
}
|
||||
}
|
||||
|
||||
//create the reporter
|
||||
rep := &reporter{
|
||||
reg: r,
|
||||
interval: d,
|
||||
db: db,
|
||||
quit: make(chan struct{}),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
|
||||
//run the go routine
|
||||
go rep.run()
|
||||
|
||||
m := &AccountingMetrics{
|
||||
reporter: rep,
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
//run is the goroutine which periodically sends the metrics to the configured LevelDB
|
||||
func (r *reporter) run() {
|
||||
// signal that the reporter loop is done
|
||||
defer close(r.done)
|
||||
|
||||
intervalTicker := time.NewTicker(r.interval)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-intervalTicker.C:
|
||||
//at each tick send the metrics
|
||||
if err := r.save(); err != nil {
|
||||
log.Error("unable to send metrics to LevelDB", "err", err)
|
||||
//If there is an error in writing, exit the routine; we assume here that the error is
|
||||
//severe and don't attempt to write again.
|
||||
//Also, this should prevent leaking when the node is stopped
|
||||
return
|
||||
}
|
||||
case <-r.quit:
|
||||
//graceful shutdown
|
||||
if err := r.save(); err != nil {
|
||||
log.Error("unable to send metrics to LevelDB", "err", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//send the metrics to the DB
|
||||
func (r *reporter) save() error {
|
||||
//create a LevelDB Batch
|
||||
batch := leveldb.Batch{}
|
||||
//for each metric in the registry (which is independent)...
|
||||
r.reg.Each(func(name string, i interface{}) {
|
||||
metric, ok := i.(metrics.Counter)
|
||||
if ok {
|
||||
//assuming every metric here to be a Counter (separate registry)
|
||||
//...create a snapshot...
|
||||
ms := metric.Snapshot()
|
||||
byteVal := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(byteVal, uint64(ms.Count()))
|
||||
//...and save the value to the DB
|
||||
batch.Put([]byte(name), byteVal)
|
||||
}
|
||||
})
|
||||
return r.db.Write(&batch, nil)
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package protocols
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
)
|
||||
|
||||
//TestReporter tests that the metrics being collected for p2p accounting
|
||||
//are being persisted and available after restart of a node.
|
||||
//It simulates restarting by just recreating the DB as if the node had restarted.
|
||||
func TestReporter(t *testing.T) {
|
||||
//create a test directory
|
||||
dir, err := ioutil.TempDir("", "reporter-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
//setup the metrics
|
||||
log.Debug("Setting up metrics first time")
|
||||
reportInterval := 2 * time.Millisecond
|
||||
metrics := SetupAccountingMetrics(reportInterval, filepath.Join(dir, "test.db"))
|
||||
log.Debug("Done.")
|
||||
|
||||
//change metrics
|
||||
mBalanceCredit.Inc(12)
|
||||
mBytesCredit.Inc(34)
|
||||
mMsgDebit.Inc(9)
|
||||
|
||||
//store expected metrics
|
||||
expectedBalanceCredit := mBalanceCredit.Count()
|
||||
expectedBytesCredit := mBytesCredit.Count()
|
||||
expectedMsgDebit := mMsgDebit.Count()
|
||||
|
||||
//give the reporter time to write the metrics to DB
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
|
||||
//close the DB also, or we can't create a new one
|
||||
metrics.Close()
|
||||
|
||||
//clear the metrics - this effectively simulates the node having shut down...
|
||||
mBalanceCredit.Clear()
|
||||
mBytesCredit.Clear()
|
||||
mMsgDebit.Clear()
|
||||
|
||||
//setup the metrics again
|
||||
log.Debug("Setting up metrics second time")
|
||||
metrics = SetupAccountingMetrics(reportInterval, filepath.Join(dir, "test.db"))
|
||||
defer metrics.Close()
|
||||
log.Debug("Done.")
|
||||
|
||||
//now check the metrics, they should have the same value as before "shutdown"
|
||||
if mBalanceCredit.Count() != expectedBalanceCredit {
|
||||
t.Fatalf("Expected counter to be %d, but is %d", expectedBalanceCredit, mBalanceCredit.Count())
|
||||
}
|
||||
if mBytesCredit.Count() != expectedBytesCredit {
|
||||
t.Fatalf("Expected counter to be %d, but is %d", expectedBytesCredit, mBytesCredit.Count())
|
||||
}
|
||||
if mMsgDebit.Count() != expectedMsgDebit {
|
||||
t.Fatalf("Expected counter to be %d, but is %d", expectedMsgDebit, mMsgDebit.Count())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user