Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

p2p/protocols: accounting metrics rpc added (#847) #1048

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package web3ext

var Modules = map[string]string{
"account": Account_JS,
"admin": Admin_JS,
"chequebook": Chequebook_JS,
"clique": Clique_JS,
Expand Down Expand Up @@ -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'
}),
]
});
`
85 changes: 85 additions & 0 deletions p2p/protocols/accounting_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package protocols

import (
"errors"
)

// Textual version number of accounting API
const AccountingVersion = "1.0"
zelig marked this conversation as resolved.
Show resolved Hide resolved

var errNoAccountingMetrics = errors.New("accounting metrics not enabled")

// AccountingApi provides an API to access account related information
type AccountingApi struct {
JerzyLa marked this conversation as resolved.
Show resolved Hide resolved
metrics *AccountingMetrics
}

// NewAccountingApi creates a new AccountingApi
zelig marked this conversation as resolved.
Show resolved Hide resolved
// m will be used to check if accounting metrics are enabled
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
}
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
}
6 changes: 6 additions & 0 deletions swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()...)
Expand Down