aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJerzy Lasyk <jerzylasyk@gmail.com>2018-12-22 13:04:03 +0800
committerViktor TrĂ³n <viktor.tron@gmail.com>2018-12-22 13:04:03 +0800
commit880de230b44e20282abdef0f1f9a3294ce68e5d8 (patch)
tree9cfb8fb166b5117cddf54f6cdd740687e778dd64
parent81c3dc728f946db26a31a6974383b94b11dba977 (diff)
downloadgo-tangerine-880de230b44e20282abdef0f1f9a3294ce68e5d8.tar
go-tangerine-880de230b44e20282abdef0f1f9a3294ce68e5d8.tar.gz
go-tangerine-880de230b44e20282abdef0f1f9a3294ce68e5d8.tar.bz2
go-tangerine-880de230b44e20282abdef0f1f9a3294ce68e5d8.tar.lz
go-tangerine-880de230b44e20282abdef0f1f9a3294ce68e5d8.tar.xz
go-tangerine-880de230b44e20282abdef0f1f9a3294ce68e5d8.tar.zst
go-tangerine-880de230b44e20282abdef0f1f9a3294ce68e5d8.zip
p2p/protocols: accounting metrics rpc (#18336)
* p2p/protocols: accounting metrics rpc added (#847) * p2p/protocols: accounting api documentation added (#847) * p2p/protocols: accounting api doc updated (#847) * p2p/protocols: accounting api doc update (#847) * p2p/protocols: accounting api doc update (#847) * p2p/protocols: fix file is not gofmted * fix lint error * updated comments after review * add account balance to rpc * naming changed after review
-rw-r--r--internal/web3ext/web3ext.go45
-rw-r--r--p2p/protocols/accounting_api.go94
-rw-r--r--swarm/swarm.go6
3 files changed, 145 insertions, 0 deletions
diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go
index 06bfcef69..6b98c8b7e 100644
--- a/internal/web3ext/web3ext.go
+++ b/internal/web3ext/web3ext.go
@@ -18,6 +18,7 @@
package web3ext
var Modules = map[string]string{
+ "accounting": Accounting_JS,
"admin": Admin_JS,
"chequebook": Chequebook_JS,
"clique": Clique_JS,
@@ -704,3 +705,47 @@ web3._extend({
]
});
`
+
+const Accounting_JS = `
+web3._extend({
+ property: 'accounting',
+ methods: [
+ new web3._extend.Property({
+ name: 'balance',
+ getter: 'account_balance'
+ }),
+ new web3._extend.Property({
+ name: 'balanceCredit',
+ getter: 'account_balanceCredit'
+ }),
+ new web3._extend.Property({
+ name: 'balanceDebit',
+ getter: 'account_balanceDebit'
+ }),
+ new web3._extend.Property({
+ name: 'bytesCredit',
+ getter: 'account_bytesCredit'
+ }),
+ new web3._extend.Property({
+ name: 'bytesDebit',
+ getter: 'account_bytesDebit'
+ }),
+ new web3._extend.Property({
+ name: 'msgCredit',
+ getter: 'account_msgCredit'
+ }),
+ new web3._extend.Property({
+ name: 'msgDebit',
+ getter: 'account_msgDebit'
+ }),
+ new web3._extend.Property({
+ name: 'peerDrops',
+ getter: 'account_peerDrops'
+ }),
+ new web3._extend.Property({
+ name: 'selfDrops',
+ getter: 'account_selfDrops'
+ }),
+ ]
+});
+`
diff --git a/p2p/protocols/accounting_api.go b/p2p/protocols/accounting_api.go
new file mode 100644
index 000000000..48e2af9fe
--- /dev/null
+++ b/p2p/protocols/accounting_api.go
@@ -0,0 +1,94 @@
+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
+}
diff --git a/swarm/swarm.go b/swarm/swarm.go
index a4ff94051..89f4e87ef 100644
--- a/swarm/swarm.go
+++ b/swarm/swarm.go
@@ -518,6 +518,12 @@ func (self *Swarm) APIs() []rpc.API {
Service: self.sfs,
Public: false,
},
+ {
+ Namespace: "accounting",
+ Version: protocols.AccountingVersion,
+ Service: protocols.NewAccountingApi(self.accountingMetrics),
+ Public: false,
+ },
}
apis = append(apis, self.bzz.APIs()...)