accounting

package
v1.17.4-rc2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2023 License: BSD-3-Clause Imports: 15 Imported by: 5

Documentation

Overview

Package accounting provides functionalities needed to do per-peer accounting.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrOverdraft denotes the expected debt in Reserve would exceed the payment thresholds.
	ErrOverdraft = errors.New("attempted overdraft")
	// ErrDisconnectThresholdExceeded denotes a peer has exceeded the disconnect threshold.
	ErrDisconnectThresholdExceeded = errors.New("disconnect threshold exceeded")
	// ErrPeerNoBalance is the error returned if no balance in store exists for a peer
	ErrPeerNoBalance = errors.New("no balance for peer")
	// ErrInvalidValue denotes an invalid value read from store
	ErrInvalidValue = errors.New("invalid value")
	// ErrOverRelease
	ErrOverRelease = errors.New("attempting to release more balance than was reserved for peer")
	// ErrEnforceRefresh
	ErrEnforceRefresh = errors.New("allowance expectation refused")
)
View Source
var ErrFailToLock = errors.New("failed to lock")

Functions

This section is empty.

Types

type Accounting

type Accounting struct {
	// contains filtered or unexported fields
}

Accounting is the main implementation of the accounting interface.

func NewAccounting

func NewAccounting(
	PaymentThreshold *big.Int,
	PaymentTolerance,
	EarlyPayment int64,
	logger log.Logger,
	Store storage.StateStorer,
	Pricing pricing.Interface,
	refreshRate *big.Int,
	lightFactor int64,
	p2pService p2p.Service,
) (*Accounting, error)

NewAccounting creates a new Accounting instance with the provided options.

func (*Accounting) Balance

func (a *Accounting) Balance(peer swarm.Address) (balance *big.Int, err error)

Balance returns the current balance for the given peer.

func (*Accounting) Balances

func (a *Accounting) Balances() (map[string]*big.Int, error)

Balances gets balances for all peers from store.

func (*Accounting) Close added in v1.0.0

func (a *Accounting) Close() error

Close hangs up running websockets on shutdown.

func (*Accounting) CompensatedBalance added in v0.4.0

func (a *Accounting) CompensatedBalance(peer swarm.Address) (compensated *big.Int, err error)

CompensatedBalance returns balance decreased by surplus balance

func (*Accounting) CompensatedBalances added in v0.4.0

func (a *Accounting) CompensatedBalances() (map[string]*big.Int, error)

CompensatedBalances gets balances for all peers from store.

func (*Accounting) Connect added in v1.0.0

func (a *Accounting) Connect(peer swarm.Address, fullNode bool)

func (*Accounting) Disconnect added in v1.0.0

func (a *Accounting) Disconnect(peer swarm.Address)

func (*Accounting) Metrics

func (a *Accounting) Metrics() []prometheus.Collector

Metrics returns the prometheus Collector for the accounting service.

func (*Accounting) NotifyPaymentReceived added in v0.6.0

func (a *Accounting) NotifyPaymentReceived(peer swarm.Address, amount *big.Int) error

NotifyPaymentReceived is called by Settlement when we receive a payment.

func (*Accounting) NotifyPaymentSent added in v0.6.0

func (a *Accounting) NotifyPaymentSent(peer swarm.Address, amount *big.Int, receivedError error)

NotifyPaymentSent is triggered by async monetary settlement to update our balance and remove it's price from the shadow reserve

func (*Accounting) NotifyPaymentThreshold added in v0.3.0

func (a *Accounting) NotifyPaymentThreshold(peer swarm.Address, paymentThreshold *big.Int) error

NotifyPaymentThreshold should be called to notify accounting of changes in the payment threshold

func (*Accounting) NotifyRefreshmentReceived added in v0.6.0

func (a *Accounting) NotifyRefreshmentReceived(peer swarm.Address, amount *big.Int, timestamp int64) error

NotifyRefreshmentReceived is called by pseudosettle when we receive a time based settlement.

func (*Accounting) NotifyRefreshmentSent added in v1.8.0

func (a *Accounting) NotifyRefreshmentSent(peer swarm.Address, attemptedAmount, amount *big.Int, timestamp int64, allegedInterval int64, receivedError error)

NotifyRefreshmentSent is called by pseudosettle when refreshment is done or failed

func (*Accounting) OriginatedBalance added in v1.0.0

func (a *Accounting) OriginatedBalance(peer swarm.Address) (balance *big.Int, err error)

OriginatedBalance returns the current balance for the given peer.

func (*Accounting) PeerAccounting added in v1.8.0

func (a *Accounting) PeerAccounting() (map[string]PeerInfo, error)

func (*Accounting) PeerDebt added in v0.6.0

func (a *Accounting) PeerDebt(peer swarm.Address) (*big.Int, error)

PeerDebt returns the positive part of the sum of the outstanding balance and the shadow reserve

func (*Accounting) PrepareCredit added in v1.3.0

func (a *Accounting) PrepareCredit(ctx context.Context, peer swarm.Address, price uint64, originated bool) (Action, error)

func (*Accounting) PrepareDebit added in v0.6.0

func (a *Accounting) PrepareDebit(ctx context.Context, peer swarm.Address, price uint64) (Action, error)

PrepareDebit prepares a debit operation by increasing the shadowReservedBalance

func (*Accounting) SetPayFunc added in v0.6.0

func (a *Accounting) SetPayFunc(f PayFunc)

func (*Accounting) SetRefreshFunc added in v0.6.0

func (a *Accounting) SetRefreshFunc(f RefreshFunc)

func (*Accounting) SurplusBalance added in v0.4.0

func (a *Accounting) SurplusBalance(peer swarm.Address) (balance *big.Int, err error)

SurplusBalance returns the current balance for the given peer.

type Action added in v0.6.0

type Action interface {
	// Cleanup cleans up an action. Must be called whether it was applied or not.
	Cleanup()
	// Apply applies an action
	Apply() error
}

Action represents an accounting action that can be applied

type Interface

type Interface interface {
	// PrepareCredit action to prevent overspending in case of concurrent requests.
	PrepareCredit(ctx context.Context, peer swarm.Address, price uint64, originated bool) (Action, error)
	// PrepareDebit returns an accounting Action for the later debit to be executed on and to implement shadowing a possibly credited part of reserve on the other side.
	PrepareDebit(ctx context.Context, peer swarm.Address, price uint64) (Action, error)
	// Balance returns the current balance for the given peer.
	Balance(peer swarm.Address) (*big.Int, error)
	// SurplusBalance returns the current surplus balance for the given peer.
	SurplusBalance(peer swarm.Address) (*big.Int, error)
	// Balances returns balances for all known peers.
	Balances() (map[string]*big.Int, error)
	// CompensatedBalance returns the current balance deducted by current surplus balance for the given peer.
	CompensatedBalance(peer swarm.Address) (*big.Int, error)
	// CompensatedBalances returns the compensated balances for all known peers.
	CompensatedBalances() (map[string]*big.Int, error)
	// PeerAccounting returns the associated values for all known peers
	PeerAccounting() (map[string]PeerInfo, error)
}

Interface is the Accounting interface.

type Mutex added in v1.6.2

type Mutex struct {
	// contains filtered or unexported fields
}

Mutex is a drop in replacement for the sync.Mutex it will not lock if the context is expired

func NewMutex added in v1.6.2

func NewMutex() *Mutex

func (*Mutex) Lock added in v1.6.2

func (m *Mutex) Lock()

func (*Mutex) TryLock added in v1.6.2

func (m *Mutex) TryLock(ctx context.Context) error

func (*Mutex) Unlock added in v1.6.2

func (m *Mutex) Unlock()

type PayFunc added in v0.6.0

type PayFunc func(context.Context, swarm.Address, *big.Int)

PayFunc is the function used for async monetary settlement

type PeerInfo added in v1.8.0

type PeerInfo struct {
	Balance                  *big.Int
	ConsumedBalance          *big.Int
	ThresholdReceived        *big.Int
	ThresholdGiven           *big.Int
	CurrentThresholdReceived *big.Int
	CurrentThresholdGiven    *big.Int
	SurplusBalance           *big.Int
	ReservedBalance          *big.Int
	ShadowReservedBalance    *big.Int
	GhostBalance             *big.Int
}

type RefreshFunc added in v0.6.0

type RefreshFunc func(context.Context, swarm.Address, *big.Int)

RefreshFunc is the function used for sync time-based settlement

Directories

Path Synopsis
Package mock provides a mock implementation for the accounting interface.
Package mock provides a mock implementation for the accounting interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL