@@ -22,31 +22,33 @@ import (
22
22
"github.com/ethereum/go-ethereum/metrics"
23
23
)
24
24
25
- //define some metrics
25
+ // define some metrics
26
26
var (
27
- //All metrics are cumulative
27
+ // All metrics are cumulative
28
28
29
- //total amount of units credited
29
+ // total amount of units credited
30
30
mBalanceCredit metrics.Counter
31
- //total amount of units debited
31
+ // total amount of units debited
32
32
mBalanceDebit metrics.Counter
33
- //total amount of bytes credited
33
+ // total amount of bytes credited
34
34
mBytesCredit metrics.Counter
35
- //total amount of bytes debited
35
+ // total amount of bytes debited
36
36
mBytesDebit metrics.Counter
37
- //total amount of credited messages
37
+ // total amount of credited messages
38
38
mMsgCredit metrics.Counter
39
- //total amount of debited messages
39
+ // total amount of debited messages
40
40
mMsgDebit metrics.Counter
41
- //how many times local node had to drop remote peers
41
+ // how many times local node had to drop remote peers
42
42
mPeerDrops metrics.Counter
43
- //how many times local node overdrafted and dropped
43
+ // how many times local node overdrafted and dropped
44
44
mSelfDrops metrics.Counter
45
+
46
+ MetricsRegistry metrics.Registry
45
47
)
46
48
47
- //Prices defines how prices are being passed on to the accounting instance
49
+ // Prices defines how prices are being passed on to the accounting instance
48
50
type Prices interface {
49
- //Return the Price for a message
51
+ // Return the Price for a message
50
52
Price (interface {}) * Price
51
53
}
52
54
@@ -57,20 +59,20 @@ const (
57
59
Receiver = Payer (false )
58
60
)
59
61
60
- //Price represents the costs of a message
62
+ // Price represents the costs of a message
61
63
type Price struct {
62
- Value uint64 //
63
- PerByte bool // True if the price is per byte or for unit
64
+ Value uint64
65
+ PerByte bool // True if the price is per byte or for unit
64
66
Payer Payer
65
67
}
66
68
67
- //For gives back the price for a message
68
- //A protocol provides the message price in absolute value
69
- //This method then returns the correct signed amount,
70
- //depending on who pays, which is identified by the `payer` argument:
71
- //`Send` will pass a `Sender` payer, `Receive` will pass the `Receiver` argument.
72
- //Thus: If Sending and sender pays, amount positive, otherwise negative
73
- //If Receiving, and receiver pays, amount positive, otherwise negative
69
+ // For gives back the price for a message
70
+ // A protocol provides the message price in absolute value
71
+ // This method then returns the correct signed amount,
72
+ // depending on who pays, which is identified by the `payer` argument:
73
+ // `Send` will pass a `Sender` payer, `Receive` will pass the `Receiver` argument.
74
+ // Thus: If Sending and sender pays, amount positive, otherwise negative
75
+ // If Receiving, and receiver pays, amount positive, otherwise negative
74
76
func (p * Price ) For (payer Payer , size uint32 ) int64 {
75
77
price := p .Value
76
78
if p .PerByte {
@@ -82,22 +84,22 @@ func (p *Price) For(payer Payer, size uint32) int64 {
82
84
return int64 (price )
83
85
}
84
86
85
- //Balance is the actual accounting instance
86
- //Balance defines the operations needed for accounting
87
- //Implementations internally maintain the balance for every peer
87
+ // Balance is the actual accounting instance
88
+ // Balance defines the operations needed for accounting
89
+ // Implementations internally maintain the balance for every peer
88
90
type Balance interface {
89
- //Adds amount to the local balance with remote node `peer`;
90
- //positive amount = credit local node
91
- //negative amount = debit local node
91
+ // Adds amount to the local balance with remote node `peer`;
92
+ // positive amount = credit local node
93
+ // negative amount = debit local node
92
94
Add (amount int64 , peer * Peer ) error
93
95
}
94
96
95
- //Accounting implements the Hook interface
96
- //It interfaces to the balances through the Balance interface,
97
- //while interfacing with protocols and its prices through the Prices interface
97
+ // Accounting implements the Hook interface
98
+ // It interfaces to the balances through the Balance interface,
99
+ // while interfacing with protocols and its prices through the Prices interface
98
100
type Accounting struct {
99
- Balance //interface to accounting logic
100
- Prices //interface to prices logic
101
+ Balance // interface to accounting logic
102
+ Prices // interface to prices logic
101
103
}
102
104
103
105
func NewAccounting (balance Balance , po Prices ) * Accounting {
@@ -108,87 +110,85 @@ func NewAccounting(balance Balance, po Prices) *Accounting {
108
110
return ah
109
111
}
110
112
111
- //SetupAccountingMetrics creates a separate registry for p2p accounting metrics;
112
- //this registry should be independent of any other metrics as it persists at different endpoints.
113
- //It also instantiates the given metrics and starts the persisting go-routine which
114
- //at the passed interval writes the metrics to a LevelDB
113
+ // SetupAccountingMetrics creates a separate registry for p2p accounting metrics;
114
+ // this registry should be independent of any other metrics as it persists at different endpoints.
115
+ // It also instantiates the given metrics and starts the persisting go-routine which
116
+ // at the passed interval writes the metrics to a LevelDB
115
117
func SetupAccountingMetrics (reportInterval time.Duration , path string ) * AccountingMetrics {
116
- //create an empty registry
117
- registry : = metrics .NewRegistry ()
118
- //instantiate the metrics
119
- mBalanceCredit = metrics .NewRegisteredCounterForced ("account.balance.credit" , registry )
120
- mBalanceDebit = metrics .NewRegisteredCounterForced ("account.balance.debit" , registry )
121
- mBytesCredit = metrics .NewRegisteredCounterForced ("account.bytes.credit" , registry )
122
- mBytesDebit = metrics .NewRegisteredCounterForced ("account.bytes.debit" , registry )
123
- mMsgCredit = metrics .NewRegisteredCounterForced ("account.msg.credit" , registry )
124
- mMsgDebit = metrics .NewRegisteredCounterForced ("account.msg.debit" , registry )
125
- mPeerDrops = metrics .NewRegisteredCounterForced ("account.peerdrops" , registry )
126
- mSelfDrops = metrics .NewRegisteredCounterForced ("account.selfdrops" , registry )
127
- //create the DB and start persisting
128
- return NewAccountingMetrics (registry , reportInterval , path )
118
+ // create an empty registry
119
+ MetricsRegistry = metrics .NewRegistry ()
120
+ // instantiate the metrics
121
+ mBalanceCredit = metrics .NewRegisteredCounterForced ("account.balance.credit" , MetricsRegistry )
122
+ mBalanceDebit = metrics .NewRegisteredCounterForced ("account.balance.debit" , MetricsRegistry )
123
+ mBytesCredit = metrics .NewRegisteredCounterForced ("account.bytes.credit" , MetricsRegistry )
124
+ mBytesDebit = metrics .NewRegisteredCounterForced ("account.bytes.debit" , MetricsRegistry )
125
+ mMsgCredit = metrics .NewRegisteredCounterForced ("account.msg.credit" , MetricsRegistry )
126
+ mMsgDebit = metrics .NewRegisteredCounterForced ("account.msg.debit" , MetricsRegistry )
127
+ mPeerDrops = metrics .NewRegisteredCounterForced ("account.peerdrops" , MetricsRegistry )
128
+ mSelfDrops = metrics .NewRegisteredCounterForced ("account.selfdrops" , MetricsRegistry )
129
+ // create the DB and start persisting
130
+ return NewAccountingMetrics (MetricsRegistry , reportInterval , path )
129
131
}
130
132
131
- //Implement Hook.Send
132
133
// Send takes a peer, a size and a msg and
133
- // - calculates the cost for the local node sending a msg of size to peer using the Prices interface
134
- // - credits/debits local node using balance interface
134
+ // - calculates the cost for the local node sending a msg of size to peer using the Prices interface
135
+ // - credits/debits local node using balance interface
135
136
func (ah * Accounting ) Send (peer * Peer , size uint32 , msg interface {}) error {
136
- //get the price for a message (through the protocol spec)
137
+ // get the price for a message (through the protocol spec)
137
138
price := ah .Price (msg )
138
- //this message doesn't need accounting
139
+ // this message doesn't need accounting
139
140
if price == nil {
140
141
return nil
141
142
}
142
- //evaluate the price for sending messages
143
+ // evaluate the price for sending messages
143
144
costToLocalNode := price .For (Sender , size )
144
- //do the accounting
145
+ // do the accounting
145
146
err := ah .Add (costToLocalNode , peer )
146
- //record metrics: just increase counters for user-facing metrics
147
+ // record metrics: just increase counters for user-facing metrics
147
148
ah .doMetrics (costToLocalNode , size , err )
148
149
return err
149
150
}
150
151
151
- //Implement Hook.Receive
152
152
// Receive takes a peer, a size and a msg and
153
- // - calculates the cost for the local node receiving a msg of size from peer using the Prices interface
154
- // - credits/debits local node using balance interface
153
+ // - calculates the cost for the local node receiving a msg of size from peer using the Prices interface
154
+ // - credits/debits local node using balance interface
155
155
func (ah * Accounting ) Receive (peer * Peer , size uint32 , msg interface {}) error {
156
- //get the price for a message (through the protocol spec)
156
+ // get the price for a message (through the protocol spec)
157
157
price := ah .Price (msg )
158
- //this message doesn't need accounting
158
+ // this message doesn't need accounting
159
159
if price == nil {
160
160
return nil
161
161
}
162
- //evaluate the price for receiving messages
162
+ // evaluate the price for receiving messages
163
163
costToLocalNode := price .For (Receiver , size )
164
- //do the accounting
164
+ // do the accounting
165
165
err := ah .Add (costToLocalNode , peer )
166
- //record metrics: just increase counters for user-facing metrics
166
+ // record metrics: just increase counters for user-facing metrics
167
167
ah .doMetrics (costToLocalNode , size , err )
168
168
return err
169
169
}
170
170
171
- //record some metrics
172
- //this is not an error handling. `err` is returned by both `Send` and `Receive`
173
- //`err` will only be non-nil if a limit has been violated (overdraft), in which case the peer has been dropped.
174
- //if the limit has been violated and `err` is thus not nil:
175
- // * if the price is positive, local node has been credited; thus `err` implicitly signals the REMOTE has been dropped
176
- // * if the price is negative, local node has been debited, thus `err` implicitly signals LOCAL node "overdraft"
171
+ // record some metrics
172
+ // this is not an error handling. `err` is returned by both `Send` and `Receive`
173
+ // `err` will only be non-nil if a limit has been violated (overdraft), in which case the peer has been dropped.
174
+ // if the limit has been violated and `err` is thus not nil:
175
+ // * if the price is positive, local node has been credited; thus `err` implicitly signals the REMOTE has been dropped
176
+ // * if the price is negative, local node has been debited, thus `err` implicitly signals LOCAL node "overdraft"
177
177
func (ah * Accounting ) doMetrics (price int64 , size uint32 , err error ) {
178
178
if price > 0 {
179
179
mBalanceCredit .Inc (price )
180
180
mBytesCredit .Inc (int64 (size ))
181
181
mMsgCredit .Inc (1 )
182
182
if err != nil {
183
- //increase the number of times a remote node has been dropped due to "overdraft"
183
+ // increase the number of times a remote node has been dropped due to "overdraft"
184
184
mPeerDrops .Inc (1 )
185
185
}
186
186
} else {
187
187
mBalanceDebit .Inc (price )
188
188
mBytesDebit .Inc (int64 (size ))
189
189
mMsgDebit .Inc (1 )
190
190
if err != nil {
191
- //increase the number of times the local node has done an "overdraft" in respect to other nodes
191
+ // increase the number of times the local node has done an "overdraft" in respect to other nodes
192
192
mSelfDrops .Inc (1 )
193
193
}
194
194
}
0 commit comments