2018-10-26 01:26:31 +03:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package protocols
|
|
|
|
|
2018-11-26 19:05:18 +03:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
|
|
|
)
|
2018-10-26 01:26:31 +03:00
|
|
|
|
2019-01-08 02:59:00 +03:00
|
|
|
// define some metrics
|
2018-10-26 01:26:31 +03:00
|
|
|
var (
|
2019-01-08 02:59:00 +03:00
|
|
|
// All metrics are cumulative
|
2018-10-26 01:26:31 +03:00
|
|
|
|
2019-01-08 02:59:00 +03:00
|
|
|
// total amount of units credited
|
2019-01-24 20:57:20 +03:00
|
|
|
mBalanceCredit = metrics.NewRegisteredCounterForced("account.balance.credit", metrics.AccountingRegistry)
|
2019-01-08 02:59:00 +03:00
|
|
|
// total amount of units debited
|
2019-01-24 20:57:20 +03:00
|
|
|
mBalanceDebit = metrics.NewRegisteredCounterForced("account.balance.debit", metrics.AccountingRegistry)
|
2019-01-08 02:59:00 +03:00
|
|
|
// total amount of bytes credited
|
2019-01-24 20:57:20 +03:00
|
|
|
mBytesCredit = metrics.NewRegisteredCounterForced("account.bytes.credit", metrics.AccountingRegistry)
|
2019-01-08 02:59:00 +03:00
|
|
|
// total amount of bytes debited
|
2019-01-24 20:57:20 +03:00
|
|
|
mBytesDebit = metrics.NewRegisteredCounterForced("account.bytes.debit", metrics.AccountingRegistry)
|
2019-01-08 02:59:00 +03:00
|
|
|
// total amount of credited messages
|
2019-01-24 20:57:20 +03:00
|
|
|
mMsgCredit = metrics.NewRegisteredCounterForced("account.msg.credit", metrics.AccountingRegistry)
|
2019-01-08 02:59:00 +03:00
|
|
|
// total amount of debited messages
|
2019-01-24 20:57:20 +03:00
|
|
|
mMsgDebit = metrics.NewRegisteredCounterForced("account.msg.debit", metrics.AccountingRegistry)
|
2019-01-08 02:59:00 +03:00
|
|
|
// how many times local node had to drop remote peers
|
2019-01-24 20:57:20 +03:00
|
|
|
mPeerDrops = metrics.NewRegisteredCounterForced("account.peerdrops", metrics.AccountingRegistry)
|
2019-01-08 02:59:00 +03:00
|
|
|
// how many times local node overdrafted and dropped
|
2019-01-24 20:57:20 +03:00
|
|
|
mSelfDrops = metrics.NewRegisteredCounterForced("account.selfdrops", metrics.AccountingRegistry)
|
2018-10-26 01:26:31 +03:00
|
|
|
)
|
|
|
|
|
2019-01-08 02:59:00 +03:00
|
|
|
// Prices defines how prices are being passed on to the accounting instance
|
2018-10-26 01:26:31 +03:00
|
|
|
type Prices interface {
|
2019-01-08 02:59:00 +03:00
|
|
|
// Return the Price for a message
|
2018-10-26 01:26:31 +03:00
|
|
|
Price(interface{}) *Price
|
|
|
|
}
|
|
|
|
|
|
|
|
type Payer bool
|
|
|
|
|
|
|
|
const (
|
|
|
|
Sender = Payer(true)
|
|
|
|
Receiver = Payer(false)
|
|
|
|
)
|
|
|
|
|
2019-01-08 02:59:00 +03:00
|
|
|
// Price represents the costs of a message
|
2018-10-26 01:26:31 +03:00
|
|
|
type Price struct {
|
2019-01-08 02:59:00 +03:00
|
|
|
Value uint64
|
|
|
|
PerByte bool // True if the price is per byte or for unit
|
2018-10-26 01:26:31 +03:00
|
|
|
Payer Payer
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:59:00 +03:00
|
|
|
// For gives back the price for a message
|
|
|
|
// A protocol provides the message price in absolute value
|
|
|
|
// This method then returns the correct signed amount,
|
|
|
|
// depending on who pays, which is identified by the `payer` argument:
|
|
|
|
// `Send` will pass a `Sender` payer, `Receive` will pass the `Receiver` argument.
|
|
|
|
// Thus: If Sending and sender pays, amount positive, otherwise negative
|
|
|
|
// If Receiving, and receiver pays, amount positive, otherwise negative
|
2018-10-26 01:26:31 +03:00
|
|
|
func (p *Price) For(payer Payer, size uint32) int64 {
|
|
|
|
price := p.Value
|
|
|
|
if p.PerByte {
|
|
|
|
price *= uint64(size)
|
|
|
|
}
|
|
|
|
if p.Payer == payer {
|
|
|
|
return 0 - int64(price)
|
|
|
|
}
|
|
|
|
return int64(price)
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:59:00 +03:00
|
|
|
// Balance is the actual accounting instance
|
|
|
|
// Balance defines the operations needed for accounting
|
|
|
|
// Implementations internally maintain the balance for every peer
|
2018-10-26 01:26:31 +03:00
|
|
|
type Balance interface {
|
2019-01-08 02:59:00 +03:00
|
|
|
// Adds amount to the local balance with remote node `peer`;
|
|
|
|
// positive amount = credit local node
|
|
|
|
// negative amount = debit local node
|
2018-10-26 01:26:31 +03:00
|
|
|
Add(amount int64, peer *Peer) error
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:59:00 +03:00
|
|
|
// Accounting implements the Hook interface
|
|
|
|
// It interfaces to the balances through the Balance interface,
|
|
|
|
// while interfacing with protocols and its prices through the Prices interface
|
2018-10-26 01:26:31 +03:00
|
|
|
type Accounting struct {
|
2019-01-08 02:59:00 +03:00
|
|
|
Balance // interface to accounting logic
|
|
|
|
Prices // interface to prices logic
|
2018-10-26 01:26:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAccounting(balance Balance, po Prices) *Accounting {
|
|
|
|
ah := &Accounting{
|
|
|
|
Prices: po,
|
|
|
|
Balance: balance,
|
|
|
|
}
|
|
|
|
return ah
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:57:20 +03:00
|
|
|
// SetupAccountingMetrics uses a separate registry for p2p accounting metrics;
|
2019-01-08 02:59:00 +03:00
|
|
|
// this registry should be independent of any other metrics as it persists at different endpoints.
|
2019-01-24 20:57:20 +03:00
|
|
|
// It also starts the persisting go-routine which
|
2019-01-08 02:59:00 +03:00
|
|
|
// at the passed interval writes the metrics to a LevelDB
|
2018-11-26 19:05:18 +03:00
|
|
|
func SetupAccountingMetrics(reportInterval time.Duration, path string) *AccountingMetrics {
|
2019-01-08 02:59:00 +03:00
|
|
|
// create the DB and start persisting
|
2019-01-24 20:57:20 +03:00
|
|
|
return NewAccountingMetrics(metrics.AccountingRegistry, reportInterval, path)
|
2018-11-26 19:05:18 +03:00
|
|
|
}
|
|
|
|
|
2018-10-26 01:26:31 +03:00
|
|
|
// Send takes a peer, a size and a msg and
|
2019-01-08 02:59:00 +03:00
|
|
|
// - calculates the cost for the local node sending a msg of size to peer using the Prices interface
|
|
|
|
// - credits/debits local node using balance interface
|
2018-10-26 01:26:31 +03:00
|
|
|
func (ah *Accounting) Send(peer *Peer, size uint32, msg interface{}) error {
|
2019-01-08 02:59:00 +03:00
|
|
|
// get the price for a message (through the protocol spec)
|
2018-10-26 01:26:31 +03:00
|
|
|
price := ah.Price(msg)
|
2019-01-08 02:59:00 +03:00
|
|
|
// this message doesn't need accounting
|
2018-10-26 01:26:31 +03:00
|
|
|
if price == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-08 02:59:00 +03:00
|
|
|
// evaluate the price for sending messages
|
2018-10-26 01:26:31 +03:00
|
|
|
costToLocalNode := price.For(Sender, size)
|
2019-01-08 02:59:00 +03:00
|
|
|
// do the accounting
|
2018-10-26 01:26:31 +03:00
|
|
|
err := ah.Add(costToLocalNode, peer)
|
2019-01-08 02:59:00 +03:00
|
|
|
// record metrics: just increase counters for user-facing metrics
|
2018-10-26 01:26:31 +03:00
|
|
|
ah.doMetrics(costToLocalNode, size, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receive takes a peer, a size and a msg and
|
2019-01-08 02:59:00 +03:00
|
|
|
// - calculates the cost for the local node receiving a msg of size from peer using the Prices interface
|
|
|
|
// - credits/debits local node using balance interface
|
2018-10-26 01:26:31 +03:00
|
|
|
func (ah *Accounting) Receive(peer *Peer, size uint32, msg interface{}) error {
|
2019-01-08 02:59:00 +03:00
|
|
|
// get the price for a message (through the protocol spec)
|
2018-10-26 01:26:31 +03:00
|
|
|
price := ah.Price(msg)
|
2019-01-08 02:59:00 +03:00
|
|
|
// this message doesn't need accounting
|
2018-10-26 01:26:31 +03:00
|
|
|
if price == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-08 02:59:00 +03:00
|
|
|
// evaluate the price for receiving messages
|
2018-10-26 01:26:31 +03:00
|
|
|
costToLocalNode := price.For(Receiver, size)
|
2019-01-08 02:59:00 +03:00
|
|
|
// do the accounting
|
2018-10-26 01:26:31 +03:00
|
|
|
err := ah.Add(costToLocalNode, peer)
|
2019-01-08 02:59:00 +03:00
|
|
|
// record metrics: just increase counters for user-facing metrics
|
2018-10-26 01:26:31 +03:00
|
|
|
ah.doMetrics(costToLocalNode, size, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-08 02:59:00 +03:00
|
|
|
// record some metrics
|
|
|
|
// this is not an error handling. `err` is returned by both `Send` and `Receive`
|
|
|
|
// `err` will only be non-nil if a limit has been violated (overdraft), in which case the peer has been dropped.
|
|
|
|
// if the limit has been violated and `err` is thus not nil:
|
|
|
|
// * if the price is positive, local node has been credited; thus `err` implicitly signals the REMOTE has been dropped
|
|
|
|
// * if the price is negative, local node has been debited, thus `err` implicitly signals LOCAL node "overdraft"
|
2018-10-26 01:26:31 +03:00
|
|
|
func (ah *Accounting) doMetrics(price int64, size uint32, err error) {
|
|
|
|
if price > 0 {
|
|
|
|
mBalanceCredit.Inc(price)
|
|
|
|
mBytesCredit.Inc(int64(size))
|
|
|
|
mMsgCredit.Inc(1)
|
|
|
|
if err != nil {
|
2019-01-08 02:59:00 +03:00
|
|
|
// increase the number of times a remote node has been dropped due to "overdraft"
|
2018-10-26 01:26:31 +03:00
|
|
|
mPeerDrops.Inc(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mBalanceDebit.Inc(price)
|
|
|
|
mBytesDebit.Inc(int64(size))
|
|
|
|
mMsgDebit.Inc(1)
|
|
|
|
if err != nil {
|
2019-01-08 02:59:00 +03:00
|
|
|
// increase the number of times the local node has done an "overdraft" in respect to other nodes
|
2018-10-26 01:26:31 +03:00
|
|
|
mSelfDrops.Inc(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|