github.com/codingfuture/orig-energi3@v0.8.4/p2p/protocols/accounting_api.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package protocols
    19  
    20  import (
    21  	"errors"
    22  )
    23  
    24  // Textual version number of accounting API
    25  const AccountingVersion = "1.0"
    26  
    27  var errNoAccountingMetrics = errors.New("accounting metrics not enabled")
    28  
    29  // AccountingApi provides an API to access account related information
    30  type AccountingApi struct {
    31  	metrics *AccountingMetrics
    32  }
    33  
    34  // NewAccountingApi creates a new AccountingApi
    35  // m will be used to check if accounting metrics are enabled
    36  func NewAccountingApi(m *AccountingMetrics) *AccountingApi {
    37  	return &AccountingApi{m}
    38  }
    39  
    40  // Balance returns local node balance (units credited - units debited)
    41  func (self *AccountingApi) Balance() (int64, error) {
    42  	if self.metrics == nil {
    43  		return 0, errNoAccountingMetrics
    44  	}
    45  	balance := mBalanceCredit.Count() - mBalanceDebit.Count()
    46  	return balance, nil
    47  }
    48  
    49  // BalanceCredit returns total amount of units credited by local node
    50  func (self *AccountingApi) BalanceCredit() (int64, error) {
    51  	if self.metrics == nil {
    52  		return 0, errNoAccountingMetrics
    53  	}
    54  	return mBalanceCredit.Count(), nil
    55  }
    56  
    57  // BalanceCredit returns total amount of units debited by local node
    58  func (self *AccountingApi) BalanceDebit() (int64, error) {
    59  	if self.metrics == nil {
    60  		return 0, errNoAccountingMetrics
    61  	}
    62  	return mBalanceDebit.Count(), nil
    63  }
    64  
    65  // BytesCredit returns total amount of bytes credited by local node
    66  func (self *AccountingApi) BytesCredit() (int64, error) {
    67  	if self.metrics == nil {
    68  		return 0, errNoAccountingMetrics
    69  	}
    70  	return mBytesCredit.Count(), nil
    71  }
    72  
    73  // BalanceCredit returns total amount of bytes debited by local node
    74  func (self *AccountingApi) BytesDebit() (int64, error) {
    75  	if self.metrics == nil {
    76  		return 0, errNoAccountingMetrics
    77  	}
    78  	return mBytesDebit.Count(), nil
    79  }
    80  
    81  // MsgCredit returns total amount of messages credited by local node
    82  func (self *AccountingApi) MsgCredit() (int64, error) {
    83  	if self.metrics == nil {
    84  		return 0, errNoAccountingMetrics
    85  	}
    86  	return mMsgCredit.Count(), nil
    87  }
    88  
    89  // MsgDebit returns total amount of messages debited by local node
    90  func (self *AccountingApi) MsgDebit() (int64, error) {
    91  	if self.metrics == nil {
    92  		return 0, errNoAccountingMetrics
    93  	}
    94  	return mMsgDebit.Count(), nil
    95  }
    96  
    97  // PeerDrops returns number of times when local node had to drop remote peers
    98  func (self *AccountingApi) PeerDrops() (int64, error) {
    99  	if self.metrics == nil {
   100  		return 0, errNoAccountingMetrics
   101  	}
   102  	return mPeerDrops.Count(), nil
   103  }
   104  
   105  // SelfDrops returns number of times when local node was overdrafted and dropped
   106  func (self *AccountingApi) SelfDrops() (int64, error) {
   107  	if self.metrics == nil {
   108  		return 0, errNoAccountingMetrics
   109  	}
   110  	return mSelfDrops.Count(), nil
   111  }