github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/p2p/protocols/accounting_api.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:41</date>
    10  //</624450105809833984>
    11  
    12  package protocols
    13  
    14  import (
    15  	"errors"
    16  )
    17  
    18  //会计API文本版本号
    19  const AccountingVersion = "1.0"
    20  
    21  var errNoAccountingMetrics = errors.New("accounting metrics not enabled")
    22  
    23  //AccountingAPI提供了一个API来访问与帐户相关的信息
    24  type AccountingApi struct {
    25  	metrics *AccountingMetrics
    26  }
    27  
    28  //new accountingapi创建新的accountingapi
    29  //m将用于检查会计指标是否启用
    30  func NewAccountingApi(m *AccountingMetrics) *AccountingApi {
    31  	return &AccountingApi{m}
    32  }
    33  
    34  //余额返回本地节点余额(贷记单位-借记单位)
    35  func (self *AccountingApi) Balance() (int64, error) {
    36  	if self.metrics == nil {
    37  		return 0, errNoAccountingMetrics
    38  	}
    39  	balance := mBalanceCredit.Count() - mBalanceDebit.Count()
    40  	return balance, nil
    41  }
    42  
    43  //BalanceCredit返回本地节点贷记的单位总数
    44  func (self *AccountingApi) BalanceCredit() (int64, error) {
    45  	if self.metrics == nil {
    46  		return 0, errNoAccountingMetrics
    47  	}
    48  	return mBalanceCredit.Count(), nil
    49  }
    50  
    51  //BalanceCredit返回本地节点借记的单位总数
    52  func (self *AccountingApi) BalanceDebit() (int64, error) {
    53  	if self.metrics == nil {
    54  		return 0, errNoAccountingMetrics
    55  	}
    56  	return mBalanceDebit.Count(), nil
    57  }
    58  
    59  //BytesCredit返回本地节点贷记的字节总数
    60  func (self *AccountingApi) BytesCredit() (int64, error) {
    61  	if self.metrics == nil {
    62  		return 0, errNoAccountingMetrics
    63  	}
    64  	return mBytesCredit.Count(), nil
    65  }
    66  
    67  //BalanceCredit返回本地节点借记的字节总数
    68  func (self *AccountingApi) BytesDebit() (int64, error) {
    69  	if self.metrics == nil {
    70  		return 0, errNoAccountingMetrics
    71  	}
    72  	return mBytesDebit.Count(), nil
    73  }
    74  
    75  //msgcredit返回本地节点贷记的消息总数
    76  func (self *AccountingApi) MsgCredit() (int64, error) {
    77  	if self.metrics == nil {
    78  		return 0, errNoAccountingMetrics
    79  	}
    80  	return mMsgCredit.Count(), nil
    81  }
    82  
    83  //
    84  func (self *AccountingApi) MsgDebit() (int64, error) {
    85  	if self.metrics == nil {
    86  		return 0, errNoAccountingMetrics
    87  	}
    88  	return mMsgDebit.Count(), nil
    89  }
    90  
    91  //
    92  func (self *AccountingApi) PeerDrops() (int64, error) {
    93  	if self.metrics == nil {
    94  		return 0, errNoAccountingMetrics
    95  	}
    96  	return mPeerDrops.Count(), nil
    97  }
    98  
    99  //
   100  func (self *AccountingApi) SelfDrops() (int64, error) {
   101  	if self.metrics == nil {
   102  		return 0, errNoAccountingMetrics
   103  	}
   104  	return mSelfDrops.Count(), nil
   105  }
   106