-
Notifications
You must be signed in to change notification settings - Fork 338
/
pricer.go
41 lines (34 loc) · 1.17 KB
/
pricer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package accounting
import (
"github.com/ethersphere/bee/pkg/swarm"
)
// Pricer returns pricing information for chunk hashes.
type Pricer interface {
// PeerPrice is the price the peer charges for a given chunk hash.
PeerPrice(peer, chunk swarm.Address) uint64
// Price is the price we charge for a given chunk hash.
Price(chunk swarm.Address) uint64
}
// FixedPricer is a Pricer that has a fixed price for chunks.
type FixedPricer struct {
overlay swarm.Address
poPrice uint64
}
// NewFixedPricer returns a new FixedPricer with a given price.
func NewFixedPricer(overlay swarm.Address, poPrice uint64) *FixedPricer {
return &FixedPricer{
overlay: overlay,
poPrice: poPrice,
}
}
// PeerPrice implements Pricer.
func (pricer *FixedPricer) PeerPrice(peer, chunk swarm.Address) uint64 {
return uint64(swarm.MaxPO-swarm.Proximity(peer.Bytes(), chunk.Bytes())+1) * pricer.poPrice
}
// Price implements Pricer.
func (pricer *FixedPricer) Price(chunk swarm.Address) uint64 {
return pricer.PeerPrice(pricer.overlay, chunk)
}