From d545bb74ba6f2e4fc55b8a14e7b0e77d9c75669a Mon Sep 17 00:00:00 2001 From: Jerzy Date: Wed, 5 Dec 2018 20:06:13 +0100 Subject: [PATCH 01/10] p2p/protocols: accounting metrics rpc added (#847) --- internal/web3ext/web3ext.go | 41 ++++++++++++++++++ p2p/protocols/accounting_api.go | 73 +++++++++++++++++++++++++++++++++ swarm/swarm.go | 6 +++ 3 files changed, 120 insertions(+) create mode 100644 p2p/protocols/accounting_api.go diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index a5f3196535c1..3651085870e3 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -18,6 +18,7 @@ package web3ext var Modules = map[string]string{ + "account": Account_JS, "admin": Admin_JS, "chequebook": Chequebook_JS, "clique": Clique_JS, @@ -692,3 +693,43 @@ web3._extend({ ] }); ` + +const Account_JS = ` +web3._extend({ + property: 'account', + methods: [ + 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 000000000000..356408c30f6e --- /dev/null +++ b/p2p/protocols/accounting_api.go @@ -0,0 +1,73 @@ +package protocols + +import ( + "errors" +) + +const AccountingVersion = "1.0" + +var errNoAccountingMetrics = errors.New("no accounting metrics") + +type AccountingApi struct { + metrics *AccountingMetrics +} + +func NewAccountingApi(m *AccountingMetrics) *AccountingApi { + return &AccountingApi{m} +} + +func (self *AccountingApi) BalanceCredit() (int64, error) { + if self.metrics == nil { + return 0, errNoAccountingMetrics + } + return mBalanceCredit.Count(), nil +} + +func (self *AccountingApi) BalanceDebit() (int64, error) { + if self.metrics == nil { + return 0, errNoAccountingMetrics + } + return mBalanceDebit.Count(), nil +} + +func (self *AccountingApi) BytesCredit() (int64, error) { + if self.metrics == nil { + return 0, errNoAccountingMetrics + } + return mBytesCredit.Count(), nil +} + +func (self *AccountingApi) BytesDebit() (int64, error) { + if self.metrics == nil { + return 0, errNoAccountingMetrics + } + return mBytesDebit.Count(), nil +} + +func (self *AccountingApi) MsgCredit() (int64, error) { + if self.metrics == nil { + return 0, errNoAccountingMetrics + } + return mMsgCredit.Count(), nil +} + +func (self *AccountingApi) MsgDebit() (int64, error) { + if self.metrics == nil { + return 0, errNoAccountingMetrics + } + return mMsgDebit.Count(), nil +} + +func (self *AccountingApi) PeerDrops() (int64, error) { + if self.metrics == nil { + return 0, errNoAccountingMetrics + } + return mPeerDrops.Count(), nil +} + +func (self *AccountingApi) SelfDrops() (int64, error) { + if self.metrics == nil { + return 0, errNoAccountingMetrics + } + return mSelfDrops.Count(), nil +} \ No newline at end of file diff --git a/swarm/swarm.go b/swarm/swarm.go index a4ff94051dd7..48048f34cde9 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: "account", + Version: protocols.AccountingVersion, + Service: protocols.NewAccountingApi(self.accountingMetrics), + Public: false, + }, } apis = append(apis, self.bzz.APIs()...) From b8454e6d9e93bc3448acf30fecbd3a131d2e0d4c Mon Sep 17 00:00:00 2001 From: Jerzy Date: Thu, 6 Dec 2018 08:18:22 +0100 Subject: [PATCH 02/10] p2p/protocols: accounting api documentation added (#847) --- p2p/protocols/accounting_api.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/p2p/protocols/accounting_api.go b/p2p/protocols/accounting_api.go index 356408c30f6e..7492b72e6a9c 100644 --- a/p2p/protocols/accounting_api.go +++ b/p2p/protocols/accounting_api.go @@ -6,7 +6,7 @@ import ( const AccountingVersion = "1.0" -var errNoAccountingMetrics = errors.New("no accounting metrics") +var errNoAccountingMetrics = errors.New("accounting metrics not enabled") type AccountingApi struct { metrics *AccountingMetrics @@ -16,6 +16,7 @@ func NewAccountingApi(m *AccountingMetrics) *AccountingApi { return &AccountingApi{m} } +// BalanceCredit returns total amount of units credited by local node func (self *AccountingApi) BalanceCredit() (int64, error) { if self.metrics == nil { return 0, errNoAccountingMetrics @@ -23,6 +24,7 @@ func (self *AccountingApi) BalanceCredit() (int64, error) { 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 @@ -30,6 +32,7 @@ func (self *AccountingApi) BalanceDebit() (int64, error) { 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 @@ -37,6 +40,7 @@ func (self *AccountingApi) BytesCredit() (int64, error) { 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 @@ -44,6 +48,7 @@ func (self *AccountingApi) BytesDebit() (int64, error) { 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 @@ -51,6 +56,7 @@ func (self *AccountingApi) MsgCredit() (int64, error) { 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 @@ -58,6 +64,7 @@ func (self *AccountingApi) MsgDebit() (int64, error) { 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 @@ -65,6 +72,7 @@ func (self *AccountingApi) PeerDrops() (int64, error) { 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 From 22f62b47f07d3a62b3c562ebc36a2504be242e7d Mon Sep 17 00:00:00 2001 From: Jerzy Date: Thu, 6 Dec 2018 08:32:55 +0100 Subject: [PATCH 03/10] p2p/protocols: accounting api doc updated (#847) --- p2p/protocols/accounting_api.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/p2p/protocols/accounting_api.go b/p2p/protocols/accounting_api.go index 7492b72e6a9c..3a203b2eb80e 100644 --- a/p2p/protocols/accounting_api.go +++ b/p2p/protocols/accounting_api.go @@ -12,6 +12,8 @@ type AccountingApi struct { metrics *AccountingMetrics } +// NewAccountingApi creates a new AccountingApi +// If m is not nil, it will be used to get accounting metrics func NewAccountingApi(m *AccountingMetrics) *AccountingApi { return &AccountingApi{m} } From 2a6c2b22a547857c3885a7986892efdbc77d2f52 Mon Sep 17 00:00:00 2001 From: Jerzy Date: Thu, 6 Dec 2018 09:45:21 +0100 Subject: [PATCH 04/10] p2p/protocols: accounting api doc update (#847) --- p2p/protocols/accounting_api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/protocols/accounting_api.go b/p2p/protocols/accounting_api.go index 3a203b2eb80e..ad3dca6c9c64 100644 --- a/p2p/protocols/accounting_api.go +++ b/p2p/protocols/accounting_api.go @@ -13,7 +13,7 @@ type AccountingApi struct { } // NewAccountingApi creates a new AccountingApi -// If m is not nil, it will be used to get accounting metrics +// m will be used to check if accounting metrics is enabled func NewAccountingApi(m *AccountingMetrics) *AccountingApi { return &AccountingApi{m} } From d1d0b88c8ff914167ce06f9c66851d8ffbbbb443 Mon Sep 17 00:00:00 2001 From: Jerzy Date: Thu, 6 Dec 2018 09:45:53 +0100 Subject: [PATCH 05/10] p2p/protocols: accounting api doc update (#847) --- p2p/protocols/accounting_api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/protocols/accounting_api.go b/p2p/protocols/accounting_api.go index ad3dca6c9c64..771a582cf695 100644 --- a/p2p/protocols/accounting_api.go +++ b/p2p/protocols/accounting_api.go @@ -13,7 +13,7 @@ type AccountingApi struct { } // NewAccountingApi creates a new AccountingApi -// m will be used to check if accounting metrics is enabled +// m will be used to check if accounting metrics are enabled func NewAccountingApi(m *AccountingMetrics) *AccountingApi { return &AccountingApi{m} } From 25964a1a601b88b714dc4eb5f21046f4ab07794d Mon Sep 17 00:00:00 2001 From: Jerzy Date: Thu, 6 Dec 2018 22:59:39 +0100 Subject: [PATCH 06/10] p2p/protocols: fix file is not gofmted --- p2p/protocols/accounting_api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/protocols/accounting_api.go b/p2p/protocols/accounting_api.go index 771a582cf695..6f2e28fad184 100644 --- a/p2p/protocols/accounting_api.go +++ b/p2p/protocols/accounting_api.go @@ -80,4 +80,4 @@ func (self *AccountingApi) SelfDrops() (int64, error) { return 0, errNoAccountingMetrics } return mSelfDrops.Count(), nil -} \ No newline at end of file +} From e5cc4baff73d1182fdc707a5f5b81eba545f01cd Mon Sep 17 00:00:00 2001 From: Jerzy Date: Thu, 6 Dec 2018 23:17:17 +0100 Subject: [PATCH 07/10] fix lint error --- swarm/swarm.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/swarm/swarm.go b/swarm/swarm.go index 48048f34cde9..8ede959a37e3 100644 --- a/swarm/swarm.go +++ b/swarm/swarm.go @@ -520,9 +520,9 @@ func (self *Swarm) APIs() []rpc.API { }, { Namespace: "account", - Version: protocols.AccountingVersion, - Service: protocols.NewAccountingApi(self.accountingMetrics), - Public: false, + Version: protocols.AccountingVersion, + Service: protocols.NewAccountingApi(self.accountingMetrics), + Public: false, }, } From 5875394cabf4e1f578bf50e3105bca248d310c5e Mon Sep 17 00:00:00 2001 From: Jerzy Date: Sun, 9 Dec 2018 22:19:25 +0100 Subject: [PATCH 08/10] updated comments after review --- p2p/protocols/accounting_api.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/p2p/protocols/accounting_api.go b/p2p/protocols/accounting_api.go index 6f2e28fad184..b6a29f1148e5 100644 --- a/p2p/protocols/accounting_api.go +++ b/p2p/protocols/accounting_api.go @@ -4,10 +4,12 @@ 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 } From ef66beb5cc365d0254b5dfebad76cb3ca6a03f63 Mon Sep 17 00:00:00 2001 From: Jerzy Date: Wed, 19 Dec 2018 11:35:32 +0100 Subject: [PATCH 09/10] add account balance to rpc --- internal/web3ext/web3ext.go | 4 ++++ p2p/protocols/accounting_api.go | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 3651085870e3..91a261d3a552 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -698,6 +698,10 @@ const Account_JS = ` web3._extend({ property: 'account', methods: [ + new web3._extend.Property({ + name: 'balance', + getter: 'account_balance' + }), new web3._extend.Property({ name: 'balanceCredit', getter: 'account_balanceCredit' diff --git a/p2p/protocols/accounting_api.go b/p2p/protocols/accounting_api.go index b6a29f1148e5..48e2af9feadd 100644 --- a/p2p/protocols/accounting_api.go +++ b/p2p/protocols/accounting_api.go @@ -20,6 +20,15 @@ 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 { From 01a374238c3401d760e535ff5e8bd286397807a9 Mon Sep 17 00:00:00 2001 From: Jerzy Date: Thu, 20 Dec 2018 17:51:06 +0100 Subject: [PATCH 10/10] naming changed after review --- internal/web3ext/web3ext.go | 6 +++--- swarm/swarm.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 91a261d3a552..df7638ad8c7f 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -18,7 +18,7 @@ package web3ext var Modules = map[string]string{ - "account": Account_JS, + "accounting": Accounting_JS, "admin": Admin_JS, "chequebook": Chequebook_JS, "clique": Clique_JS, @@ -694,9 +694,9 @@ web3._extend({ }); ` -const Account_JS = ` +const Accounting_JS = ` web3._extend({ - property: 'account', + property: 'accounting', methods: [ new web3._extend.Property({ name: 'balance', diff --git a/swarm/swarm.go b/swarm/swarm.go index 8ede959a37e3..89f4e87efbb8 100644 --- a/swarm/swarm.go +++ b/swarm/swarm.go @@ -519,7 +519,7 @@ func (self *Swarm) APIs() []rpc.API { Public: false, }, { - Namespace: "account", + Namespace: "accounting", Version: protocols.AccountingVersion, Service: protocols.NewAccountingApi(self.accountingMetrics), Public: false,