github.com/lukso-network/go-ethereum@v1.8.22/p2p/protocols/accounting_api.go (about)

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