Skip to content

Commit 880de23

Browse files
JerzyLazelig
authored andcommitted
p2p/protocols: accounting metrics rpc (#18336)
* p2p/protocols: accounting metrics rpc added (bnb-chain#847) * p2p/protocols: accounting api documentation added (bnb-chain#847) * p2p/protocols: accounting api doc updated (bnb-chain#847) * p2p/protocols: accounting api doc update (bnb-chain#847) * p2p/protocols: accounting api doc update (bnb-chain#847) * p2p/protocols: fix file is not gofmted * fix lint error * updated comments after review * add account balance to rpc * naming changed after review
1 parent 81c3dc7 commit 880de23

3 files changed

Lines changed: 145 additions & 0 deletions

File tree

‎internal/web3ext/web3ext.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package web3ext
1919

2020
var Modules = map[string]string{
21+
"accounting": Accounting_JS,
2122
"admin": Admin_JS,
2223
"chequebook": Chequebook_JS,
2324
"clique": Clique_JS,
@@ -704,3 +705,47 @@ web3._extend({
704705
]
705706
});
706707
`
708+
709+
const Accounting_JS = `
710+
web3._extend({
711+
property: 'accounting',
712+
methods: [
713+
new web3._extend.Property({
714+
name: 'balance',
715+
getter: 'account_balance'
716+
}),
717+
new web3._extend.Property({
718+
name: 'balanceCredit',
719+
getter: 'account_balanceCredit'
720+
}),
721+
new web3._extend.Property({
722+
name: 'balanceDebit',
723+
getter: 'account_balanceDebit'
724+
}),
725+
new web3._extend.Property({
726+
name: 'bytesCredit',
727+
getter: 'account_bytesCredit'
728+
}),
729+
new web3._extend.Property({
730+
name: 'bytesDebit',
731+
getter: 'account_bytesDebit'
732+
}),
733+
new web3._extend.Property({
734+
name: 'msgCredit',
735+
getter: 'account_msgCredit'
736+
}),
737+
new web3._extend.Property({
738+
name: 'msgDebit',
739+
getter: 'account_msgDebit'
740+
}),
741+
new web3._extend.Property({
742+
name: 'peerDrops',
743+
getter: 'account_peerDrops'
744+
}),
745+
new web3._extend.Property({
746+
name: 'selfDrops',
747+
getter: 'account_selfDrops'
748+
}),
749+
]
750+
});
751+
`

‎p2p/protocols/accounting_api.go‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package protocols
2+
3+
import (
4+
"errors"
5+
)
6+
7+
// Textual version number of accounting API
8+
const AccountingVersion = "1.0"
9+
10+
var errNoAccountingMetrics = errors.New("accounting metrics not enabled")
11+
12+
// AccountingApi provides an API to access account related information
13+
type AccountingApi struct {
14+
metrics *AccountingMetrics
15+
}
16+
17+
// NewAccountingApi creates a new AccountingApi
18+
// m will be used to check if accounting metrics are enabled
19+
func NewAccountingApi(m *AccountingMetrics) *AccountingApi {
20+
return &AccountingApi{m}
21+
}
22+
23+
// Balance returns local node balance (units credited - units debited)
24+
func (self *AccountingApi) Balance() (int64, error) {
25+
if self.metrics == nil {
26+
return 0, errNoAccountingMetrics
27+
}
28+
balance := mBalanceCredit.Count() - mBalanceDebit.Count()
29+
return balance, nil
30+
}
31+
32+
// BalanceCredit returns total amount of units credited by local node
33+
func (self *AccountingApi) BalanceCredit() (int64, error) {
34+
if self.metrics == nil {
35+
return 0, errNoAccountingMetrics
36+
}
37+
return mBalanceCredit.Count(), nil
38+
}
39+
40+
// BalanceCredit returns total amount of units debited by local node
41+
func (self *AccountingApi) BalanceDebit() (int64, error) {
42+
if self.metrics == nil {
43+
return 0, errNoAccountingMetrics
44+
}
45+
return mBalanceDebit.Count(), nil
46+
}
47+
48+
// BytesCredit returns total amount of bytes credited by local node
49+
func (self *AccountingApi) BytesCredit() (int64, error) {
50+
if self.metrics == nil {
51+
return 0, errNoAccountingMetrics
52+
}
53+
return mBytesCredit.Count(), nil
54+
}
55+
56+
// BalanceCredit returns total amount of bytes debited by local node
57+
func (self *AccountingApi) BytesDebit() (int64, error) {
58+
if self.metrics == nil {
59+
return 0, errNoAccountingMetrics
60+
}
61+
return mBytesDebit.Count(), nil
62+
}
63+
64+
// MsgCredit returns total amount of messages credited by local node
65+
func (self *AccountingApi) MsgCredit() (int64, error) {
66+
if self.metrics == nil {
67+
return 0, errNoAccountingMetrics
68+
}
69+
return mMsgCredit.Count(), nil
70+
}
71+
72+
// MsgDebit returns total amount of messages debited by local node
73+
func (self *AccountingApi) MsgDebit() (int64, error) {
74+
if self.metrics == nil {
75+
return 0, errNoAccountingMetrics
76+
}
77+
return mMsgDebit.Count(), nil
78+
}
79+
80+
// PeerDrops returns number of times when local node had to drop remote peers
81+
func (self *AccountingApi) PeerDrops() (int64, error) {
82+
if self.metrics == nil {
83+
return 0, errNoAccountingMetrics
84+
}
85+
return mPeerDrops.Count(), nil
86+
}
87+
88+
// SelfDrops returns number of times when local node was overdrafted and dropped
89+
func (self *AccountingApi) SelfDrops() (int64, error) {
90+
if self.metrics == nil {
91+
return 0, errNoAccountingMetrics
92+
}
93+
return mSelfDrops.Count(), nil
94+
}

‎swarm/swarm.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,12 @@ func (self *Swarm) APIs() []rpc.API {
518518
Service: self.sfs,
519519
Public: false,
520520
},
521+
{
522+
Namespace: "accounting",
523+
Version: protocols.AccountingVersion,
524+
Service: protocols.NewAccountingApi(self.accountingMetrics),
525+
Public: false,
526+
},
521527
}
522528

523529
apis = append(apis, self.bzz.APIs()...)

0 commit comments

Comments
 (0)