github.com/ethersphere/bee/v2@v2.2.0/pkg/pricer/pricer.go (about)

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