aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/protocols
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2019-02-20 16:48:12 +0800
committerGitHub <noreply@github.com>2019-02-20 16:48:12 +0800
commitc942700427557e3ff6de3aaf6b916e2f056c1ec2 (patch)
treecadf68e7206d6de42b1eefc6967214cf86e35ff2 /p2p/protocols
parent7fa3509e2eaf1a4ebc12344590e5699406690f15 (diff)
parentcde35439e058b4f9579830fec9fb65ae0b998346 (diff)
downloaddexon-c942700427557e3ff6de3aaf6b916e2f056c1ec2.tar
dexon-c942700427557e3ff6de3aaf6b916e2f056c1ec2.tar.gz
dexon-c942700427557e3ff6de3aaf6b916e2f056c1ec2.tar.bz2
dexon-c942700427557e3ff6de3aaf6b916e2f056c1ec2.tar.lz
dexon-c942700427557e3ff6de3aaf6b916e2f056c1ec2.tar.xz
dexon-c942700427557e3ff6de3aaf6b916e2f056c1ec2.tar.zst
dexon-c942700427557e3ff6de3aaf6b916e2f056c1ec2.zip
Merge pull request #19029 from holiman/update1.8
Update1.8
Diffstat (limited to 'p2p/protocols')
-rw-r--r--p2p/protocols/accounting.go35
-rw-r--r--p2p/protocols/protocol.go14
-rw-r--r--p2p/protocols/protocol_test.go12
-rw-r--r--p2p/protocols/reporter_test.go28
4 files changed, 48 insertions, 41 deletions
diff --git a/p2p/protocols/accounting.go b/p2p/protocols/accounting.go
index bdc490e59..558247254 100644
--- a/p2p/protocols/accounting.go
+++ b/p2p/protocols/accounting.go
@@ -27,23 +27,21 @@ var (
// All metrics are cumulative
// total amount of units credited
- mBalanceCredit metrics.Counter
+ mBalanceCredit = metrics.NewRegisteredCounterForced("account.balance.credit", metrics.AccountingRegistry)
// total amount of units debited
- mBalanceDebit metrics.Counter
+ mBalanceDebit = metrics.NewRegisteredCounterForced("account.balance.debit", metrics.AccountingRegistry)
// total amount of bytes credited
- mBytesCredit metrics.Counter
+ mBytesCredit = metrics.NewRegisteredCounterForced("account.bytes.credit", metrics.AccountingRegistry)
// total amount of bytes debited
- mBytesDebit metrics.Counter
+ mBytesDebit = metrics.NewRegisteredCounterForced("account.bytes.debit", metrics.AccountingRegistry)
// total amount of credited messages
- mMsgCredit metrics.Counter
+ mMsgCredit = metrics.NewRegisteredCounterForced("account.msg.credit", metrics.AccountingRegistry)
// total amount of debited messages
- mMsgDebit metrics.Counter
+ mMsgDebit = metrics.NewRegisteredCounterForced("account.msg.debit", metrics.AccountingRegistry)
// how many times local node had to drop remote peers
- mPeerDrops metrics.Counter
+ mPeerDrops = metrics.NewRegisteredCounterForced("account.peerdrops", metrics.AccountingRegistry)
// how many times local node overdrafted and dropped
- mSelfDrops metrics.Counter
-
- MetricsRegistry metrics.Registry
+ mSelfDrops = metrics.NewRegisteredCounterForced("account.selfdrops", metrics.AccountingRegistry)
)
// Prices defines how prices are being passed on to the accounting instance
@@ -110,24 +108,13 @@ func NewAccounting(balance Balance, po Prices) *Accounting {
return ah
}
-// SetupAccountingMetrics creates a separate registry for p2p accounting metrics;
+// 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 instantiates the given metrics and starts the persisting go-routine which
+// 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 an empty registry
- MetricsRegistry = metrics.NewRegistry()
- // instantiate the metrics
- mBalanceCredit = metrics.NewRegisteredCounterForced("account.balance.credit", MetricsRegistry)
- mBalanceDebit = metrics.NewRegisteredCounterForced("account.balance.debit", MetricsRegistry)
- mBytesCredit = metrics.NewRegisteredCounterForced("account.bytes.credit", MetricsRegistry)
- mBytesDebit = metrics.NewRegisteredCounterForced("account.bytes.debit", MetricsRegistry)
- mMsgCredit = metrics.NewRegisteredCounterForced("account.msg.credit", MetricsRegistry)
- mMsgDebit = metrics.NewRegisteredCounterForced("account.msg.debit", MetricsRegistry)
- mPeerDrops = metrics.NewRegisteredCounterForced("account.peerdrops", MetricsRegistry)
- mSelfDrops = metrics.NewRegisteredCounterForced("account.selfdrops", MetricsRegistry)
// create the DB and start persisting
- return NewAccountingMetrics(MetricsRegistry, reportInterval, path)
+ return NewAccountingMetrics(metrics.AccountingRegistry, reportInterval, path)
}
// Send takes a peer, a size and a msg and
diff --git a/p2p/protocols/protocol.go b/p2p/protocols/protocol.go
index b16720dd3..bf879b985 100644
--- a/p2p/protocols/protocol.go
+++ b/p2p/protocols/protocol.go
@@ -423,3 +423,17 @@ func (p *Peer) Handshake(ctx context.Context, hs interface{}, verify func(interf
}
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
+}
diff --git a/p2p/protocols/protocol_test.go b/p2p/protocols/protocol_test.go
index a26222cd8..4bc1e547e 100644
--- a/p2p/protocols/protocol_test.go
+++ b/p2p/protocols/protocol_test.go
@@ -142,9 +142,9 @@ func newProtocol(pp *p2ptest.TestPeerPool) func(*p2p.Peer, p2p.MsgReadWriter) er
}
}
-func protocolTester(t *testing.T, pp *p2ptest.TestPeerPool) *p2ptest.ProtocolTester {
+func protocolTester(pp *p2ptest.TestPeerPool) *p2ptest.ProtocolTester {
conf := adapters.RandomNodeConfig()
- return p2ptest.NewProtocolTester(t, conf.ID, 2, newProtocol(pp))
+ return p2ptest.NewProtocolTester(conf.ID, 2, newProtocol(pp))
}
func protoHandshakeExchange(id enode.ID, proto *protoHandshake) []p2ptest.Exchange {
@@ -173,7 +173,7 @@ func protoHandshakeExchange(id enode.ID, proto *protoHandshake) []p2ptest.Exchan
func runProtoHandshake(t *testing.T, proto *protoHandshake, errs ...error) {
pp := p2ptest.NewTestPeerPool()
- s := protocolTester(t, pp)
+ s := protocolTester(pp)
// TODO: make this more than one handshake
node := s.Nodes[0]
if err := s.TestExchanges(protoHandshakeExchange(node.ID(), proto)...); err != nil {
@@ -250,7 +250,7 @@ func TestProtocolHook(t *testing.T) {
}
conf := adapters.RandomNodeConfig()
- tester := p2ptest.NewProtocolTester(t, conf.ID, 2, runFunc)
+ tester := p2ptest.NewProtocolTester(conf.ID, 2, runFunc)
err := tester.TestExchanges(p2ptest.Exchange{
Expects: []p2ptest.Expect{
{
@@ -389,7 +389,7 @@ func moduleHandshakeExchange(id enode.ID, resp uint) []p2ptest.Exchange {
func runModuleHandshake(t *testing.T, resp uint, errs ...error) {
pp := p2ptest.NewTestPeerPool()
- s := protocolTester(t, pp)
+ s := protocolTester(pp)
node := s.Nodes[0]
if err := s.TestExchanges(protoHandshakeExchange(node.ID(), &protoHandshake{42, "420"})...); err != nil {
t.Fatal(err)
@@ -469,7 +469,7 @@ func testMultiPeerSetup(a, b enode.ID) []p2ptest.Exchange {
func runMultiplePeers(t *testing.T, peer int, errs ...error) {
pp := p2ptest.NewTestPeerPool()
- s := protocolTester(t, pp)
+ s := protocolTester(pp)
if err := s.TestExchanges(testMultiPeerSetup(s.Nodes[0].ID(), s.Nodes[1].ID())...); err != nil {
t.Fatal(err)
diff --git a/p2p/protocols/reporter_test.go b/p2p/protocols/reporter_test.go
index b9f06e674..c5c025d20 100644
--- a/p2p/protocols/reporter_test.go
+++ b/p2p/protocols/reporter_test.go
@@ -43,21 +43,27 @@ func TestReporter(t *testing.T) {
metrics := SetupAccountingMetrics(reportInterval, filepath.Join(dir, "test.db"))
log.Debug("Done.")
- //do some metrics
+ //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)
- //set the metrics to nil - this effectively simulates the node having shut down...
- mBalanceCredit = nil
- mBytesCredit = nil
- mMsgDebit = nil
//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"))
@@ -65,13 +71,13 @@ func TestReporter(t *testing.T) {
log.Debug("Done.")
//now check the metrics, they should have the same value as before "shutdown"
- if mBalanceCredit.Count() != 12 {
- t.Fatalf("Expected counter to be %d, but is %d", 12, mBalanceCredit.Count())
+ if mBalanceCredit.Count() != expectedBalanceCredit {
+ t.Fatalf("Expected counter to be %d, but is %d", expectedBalanceCredit, mBalanceCredit.Count())
}
- if mBytesCredit.Count() != 34 {
- t.Fatalf("Expected counter to be %d, but is %d", 23, mBytesCredit.Count())
+ if mBytesCredit.Count() != expectedBytesCredit {
+ t.Fatalf("Expected counter to be %d, but is %d", expectedBytesCredit, mBytesCredit.Count())
}
- if mMsgDebit.Count() != 9 {
- t.Fatalf("Expected counter to be %d, but is %d", 9, mMsgDebit.Count())
+ if mMsgDebit.Count() != expectedMsgDebit {
+ t.Fatalf("Expected counter to be %d, but is %d", expectedMsgDebit, mMsgDebit.Count())
}
}