accounting_api.go 2.6 KB

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