From 880de230b44e20282abdef0f1f9a3294ce68e5d8 Mon Sep 17 00:00:00 2001 From: Jerzy Lasyk Date: Sat, 22 Dec 2018 06:04:03 +0100 Subject: 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 --- p2p/protocols/accounting_api.go | 94 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 p2p/protocols/accounting_api.go (limited to 'p2p') 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 +} -- cgit v1.2.3