swarm, p2p/protocols: added stream pricing

This commit is contained in:
Fabio Barone 2018-11-26 17:43:13 -05:00
parent 5c116e3beb
commit bc9c966052
3 changed files with 23 additions and 27 deletions

View file

@ -42,6 +42,8 @@ var (
mPeerDrops metrics.Counter mPeerDrops metrics.Counter
//how many times local node overdrafted and dropped //how many times local node overdrafted and dropped
mSelfDrops metrics.Counter mSelfDrops metrics.Counter
MetricsRegistry metrics.Registry
) )
//Prices defines how prices are being passed on to the accounting instance //Prices defines how prices are being passed on to the accounting instance
@ -114,18 +116,18 @@ func NewAccounting(balance Balance, po Prices) *Accounting {
//at the passed interval writes the metrics to a LevelDB //at the passed interval writes the metrics to a LevelDB
func SetupAccountingMetrics(reportInterval time.Duration, path string) *AccountingMetrics { func SetupAccountingMetrics(reportInterval time.Duration, path string) *AccountingMetrics {
//create an empty registry //create an empty registry
registry := metrics.NewRegistry() MetricsRegistry = metrics.NewRegistry()
//instantiate the metrics //instantiate the metrics
mBalanceCredit = metrics.NewRegisteredCounterForced("account.balance.credit", registry) mBalanceCredit = metrics.NewRegisteredCounterForced("account.balance.credit", MetricsRegistry)
mBalanceDebit = metrics.NewRegisteredCounterForced("account.balance.debit", registry) mBalanceDebit = metrics.NewRegisteredCounterForced("account.balance.debit", MetricsRegistry)
mBytesCredit = metrics.NewRegisteredCounterForced("account.bytes.credit", registry) mBytesCredit = metrics.NewRegisteredCounterForced("account.bytes.credit", MetricsRegistry)
mBytesDebit = metrics.NewRegisteredCounterForced("account.bytes.debit", registry) mBytesDebit = metrics.NewRegisteredCounterForced("account.bytes.debit", MetricsRegistry)
mMsgCredit = metrics.NewRegisteredCounterForced("account.msg.credit", registry) mMsgCredit = metrics.NewRegisteredCounterForced("account.msg.credit", MetricsRegistry)
mMsgDebit = metrics.NewRegisteredCounterForced("account.msg.debit", registry) mMsgDebit = metrics.NewRegisteredCounterForced("account.msg.debit", MetricsRegistry)
mPeerDrops = metrics.NewRegisteredCounterForced("account.peerdrops", registry) mPeerDrops = metrics.NewRegisteredCounterForced("account.peerdrops", MetricsRegistry)
mSelfDrops = metrics.NewRegisteredCounterForced("account.selfdrops", registry) mSelfDrops = metrics.NewRegisteredCounterForced("account.selfdrops", MetricsRegistry)
//create the DB and start persisting //create the DB and start persisting
return NewAccountingMetrics(registry, reportInterval, path) return NewAccountingMetrics(MetricsRegistry, reportInterval, path)
} }
//Implement Hook.Send //Implement Hook.Send

View file

@ -775,24 +775,24 @@ type StreamerPrices struct {
registry *Registry registry *Registry
} }
//Price implements the accounting interface and returns the price for a specific message
func (spo *StreamerPrices) Price(msg interface{}) *protocols.Price { func (spo *StreamerPrices) Price(msg interface{}) *protocols.Price {
typ := reflect.TypeOf(msg).Elem() typ := reflect.TypeOf(msg).Elem()
return spo.priceMatrix[typ] return spo.priceMatrix[typ]
} }
//createPriceOracle sets up a matrix which can be queried to get
//the price for a message via the Price method
func (r *Registry) createPriceOracle() { func (r *Registry) createPriceOracle() {
po := &StreamerPrices{ po := &StreamerPrices{
registry: r, registry: r,
} }
po.priceMatrix = map[reflect.Type]*protocols.Price{ po.priceMatrix = map[reflect.Type]*protocols.Price{
reflect.TypeOf(ChunkDeliveryMsgRetrieval{}): &protocols.Price{ reflect.TypeOf(ChunkDeliveryMsgRetrieval{}): &protocols.Price{
Value: uint64(100), Value: uint64(100),
PerByte: true, PerByte: true,
Payer: protocols.Receiver, Payer: protocols.Receiver,
}, },
reflect.TypeOf(RetrieveRequestMsg{}): &protocols.Price{ reflect.TypeOf(RetrieveRequestMsg{}): &protocols.Price{
Value: uint64(10), Value: uint64(10),
PerByte: false, PerByte: false,

View file

@ -19,6 +19,7 @@ package swarm
import ( import (
"context" "context"
"errors" "errors"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
@ -29,7 +30,6 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters" "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
@ -39,6 +39,11 @@ import (
"github.com/ethereum/go-ethereum/swarm/storage" "github.com/ethereum/go-ethereum/swarm/storage"
) )
var (
printStats = flag.Bool("printstats", false, "print swap stats")
bucketKeySwarm = simulation.BucketKey("swarm")
)
//In TestSwapNetworkSymmetricFileUpload we set up a network with arbitrary number of nodes //In TestSwapNetworkSymmetricFileUpload we set up a network with arbitrary number of nodes
//(16), and each of the nodes uploads a file of same size //(16), and each of the nodes uploads a file of same size
//Afterwards we check that every node's balance WITH ANOTHER PEER //Afterwards we check that every node's balance WITH ANOTHER PEER
@ -110,7 +115,7 @@ func TestSwapNetworkSymmetricFileUpload(t *testing.T) {
} }
nodeIDs := sim.UpNodeIDs() nodeIDs := sim.UpNodeIDs()
shuffle(len(nodeIDs), func(i, j int) { rand.Shuffle(len(nodeIDs), func(i, j int) {
nodeIDs[i], nodeIDs[j] = nodeIDs[j], nodeIDs[i] nodeIDs[i], nodeIDs[j] = nodeIDs[j], nodeIDs[i]
}) })
//upload a file for every node //upload a file for every node
@ -186,17 +191,6 @@ func TestSwapNetworkSymmetricFileUpload(t *testing.T) {
fmt.Println(fmt.Sprintf(".........with node %s: balance %d", kk.TerminalString(), vv)) fmt.Println(fmt.Sprintf(".........with node %s: balance %d", kk.TerminalString(), vv))
} }
} }
//NOTE: this are currently metrics over ALL nodes, not per node
fmt.Println(fmt.Sprintf("Total units credited: %d", metrics.Get("account.balance.credit").(metrics.Counter).Count()))
fmt.Println(fmt.Sprintf("Total units debited: %d", metrics.Get("account.balance.debit").(metrics.Counter).Count()))
fmt.Println(fmt.Sprintf("Total bytes credited: %d", metrics.Get("account.bytes.credit").(metrics.Counter).Count()))
fmt.Println(fmt.Sprintf("Total bytes debited: %d", metrics.Get("account.bytes.debit").(metrics.Counter).Count()))
//fmt.Println(fmt.Sprintf("Cheques issued: %d", metrics.Get("account.cheques.issued").(metrics.Counter).Count()))
//fmt.Println(fmt.Sprintf("Cheques received: %d", metrics.Get("account.cheques.received").(metrics.Counter).Count()))
fmt.Println(fmt.Sprintf("Number of messages credited: %d", metrics.Get("account.msg.credit").(metrics.Counter).Count()))
fmt.Println(fmt.Sprintf("Number of messages debited: %d", metrics.Get("account.msg.debit").(metrics.Counter).Count()))
fmt.Println(fmt.Sprintf("Peers dropped: %d", metrics.Get("account.peerdrops").(metrics.Counter).Count()))
fmt.Println(fmt.Sprintf("Number of times node dropped itself: %d", metrics.Get("account.selfdrops").(metrics.Counter).Count()))
} }
//now iterate the whole map //now iterate the whole map
@ -308,7 +302,7 @@ func TestSwapNetworkAsymmetricFileUpload(t *testing.T) {
} }
nodeIDs := sim.UpNodeIDs() nodeIDs := sim.UpNodeIDs()
shuffle(len(nodeIDs), func(i, j int) { rand.Shuffle(len(nodeIDs), func(i, j int) {
nodeIDs[i], nodeIDs[j] = nodeIDs[j], nodeIDs[i] nodeIDs[i], nodeIDs[j] = nodeIDs[j], nodeIDs[i]
}) })
for i, id := range nodeIDs { for i, id := range nodeIDs {