Skip to content

Commit

Permalink
Pricing no protocol updates (#1563)
Browse files Browse the repository at this point in the history
pushsync, retrieval, pricing: Pricing no protocol updates
  • Loading branch information
metacertain authored Apr 14, 2021
1 parent 78aba90 commit 3217c6e
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 1,475 deletions.
6 changes: 2 additions & 4 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,9 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey,
return nil, fmt.Errorf("invalid payment threshold: %s", paymentThreshold)
}

pricer := pricer.New(logger, stateStore, swarmAddress, 1000000000)
pricer.SetTopology(kad)
pricer := pricer.NewFixedPricer(swarmAddress, 1000000000)

pricing := pricing.New(p2ps, logger, paymentThreshold, pricer)
pricing.SetPriceTableObserver(pricer)
pricing := pricing.New(p2ps, logger, paymentThreshold)

if err = p2ps.AddProtocol(pricing.Protocol()); err != nil {
return nil, fmt.Errorf("pricing service: %w", err)
Expand Down
13 changes: 0 additions & 13 deletions pkg/pricer/export_test.go

This file was deleted.

109 changes: 9 additions & 100 deletions pkg/pricer/mock/pricer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,25 @@
package mock

import (
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/swarm"
)

type Service struct {
peerPrice uint64
price uint64
peerPriceFunc func(peer, chunk swarm.Address) uint64
priceForPeerFunc func(peer, chunk swarm.Address) uint64
priceTableFunc func() (priceTable []uint64)
notifyPriceTableFunc func(peer swarm.Address, priceTable []uint64) error
priceHeadlerFunc func(p2p.Headers, swarm.Address) p2p.Headers
notifyPeerPriceFunc func(peer swarm.Address, price uint64, index uint8) error
type MockPricer struct {
peerPrice uint64
price uint64
}

// WithReserveFunc sets the mock Reserve function
func WithPeerPriceFunc(f func(peer, chunk swarm.Address) uint64) Option {
return optionFunc(func(s *Service) {
s.peerPriceFunc = f
})
}

// WithReleaseFunc sets the mock Release function
func WithPriceForPeerFunc(f func(peer, chunk swarm.Address) uint64) Option {
return optionFunc(func(s *Service) {
s.priceForPeerFunc = f
})
}

func WithPrice(p uint64) Option {
return optionFunc(func(s *Service) {
s.price = p
})
}

func WithPeerPrice(p uint64) Option {
return optionFunc(func(s *Service) {
s.peerPrice = p
})
}

// WithPriceTableFunc sets the mock Release function
func WithPriceTableFunc(f func() (priceTable []uint64)) Option {
return optionFunc(func(s *Service) {
s.priceTableFunc = f
})
}

func WithPriceHeadlerFunc(f func(headers p2p.Headers, addr swarm.Address) p2p.Headers) Option {
return optionFunc(func(s *Service) {
s.priceHeadlerFunc = f
})
}

func NewMockService(opts ...Option) *Service {
mock := new(Service)
mock.price = 10
mock.peerPrice = 10
for _, o := range opts {
o.apply(mock)
func NewMockService(price, peerPrice uint64) *MockPricer {
return &MockPricer{
peerPrice: peerPrice,
price: price,
}
return mock
}

func (pricer *Service) PeerPrice(peer, chunk swarm.Address) uint64 {
if pricer.peerPriceFunc != nil {
return pricer.peerPriceFunc(peer, chunk)
}
func (pricer *MockPricer) PeerPrice(peer, chunk swarm.Address) uint64 {
return pricer.peerPrice
}

func (pricer *Service) PriceForPeer(peer, chunk swarm.Address) uint64 {
if pricer.priceForPeerFunc != nil {
return pricer.priceForPeerFunc(peer, chunk)
}
func (pricer *MockPricer) Price(chunk swarm.Address) uint64 {
return pricer.price
}

func (pricer *Service) PriceTable() (priceTable []uint64) {
if pricer.priceTableFunc != nil {
return pricer.priceTableFunc()
}
return nil
}
func (pricer *Service) NotifyPriceTable(peer swarm.Address, priceTable []uint64) error {
if pricer.notifyPriceTableFunc != nil {
return pricer.notifyPriceTableFunc(peer, priceTable)
}
return nil
}

func (pricer *Service) PriceHeadler(headers p2p.Headers, addr swarm.Address) p2p.Headers {
if pricer.priceHeadlerFunc != nil {
return pricer.priceHeadlerFunc(headers, addr)
}
return p2p.Headers{}
}

func (pricer *Service) NotifyPeerPrice(peer swarm.Address, price uint64, index uint8) error {
if pricer.notifyPeerPriceFunc != nil {
return pricer.notifyPeerPriceFunc(peer, price, index)
}
return nil
}

// Option is the option passed to the mock accounting service
type Option interface {
apply(*Service)
}

type optionFunc func(*Service)

func (f optionFunc) apply(r *Service) { f(r) }
Loading

0 comments on commit 3217c6e

Please sign in to comment.