diff --git a/swarm/network/stream/stream.go b/swarm/network/stream/stream.go index d30d68dac8..56435e22b4 100644 --- a/swarm/network/stream/stream.go +++ b/swarm/network/stream/stream.go @@ -781,25 +781,37 @@ func (sp *StreamerPrices) Price(msg interface{}) *protocols.Price { return sp.priceMatrix[t] } +// Instead of hardcoding the price, get it +// through a function - it could be quite complex in the future +func (sp *StreamerPrices) getRetrieveRequestMsgPrice() uint64 { + return uint64(1) +} + +// Instead of hardcoding the price, get it +// through a function - it could be quite complex in the future +func (sp *StreamerPrices) getChunkDeliveryMsgRetrievalPrice() uint64 { + return uint64(1) +} + // createPriceOracle sets up a matrix which can be queried to get // the price for a message via the Price method func (r *Registry) createPriceOracle() { - po := &StreamerPrices{ + sp := &StreamerPrices{ registry: r, } - po.priceMatrix = map[reflect.Type]*protocols.Price{ + sp.priceMatrix = map[reflect.Type]*protocols.Price{ reflect.TypeOf(ChunkDeliveryMsgRetrieval{}): { - Value: uint64(1), // arbitrary price for now + Value: sp.getChunkDeliveryMsgRetrievalPrice(), // arbitrary price for now PerByte: true, Payer: protocols.Receiver, }, reflect.TypeOf(RetrieveRequestMsg{}): { - Value: uint64(1), // arbitrary price for now + Value: sp.getRetrieveRequestMsgPrice(), // arbitrary price for now PerByte: false, Payer: protocols.Sender, }, } - r.prices = po + r.prices = sp } func (r *Registry) Protocols() []p2p.Protocol { diff --git a/swarm/network/stream/streamer_test.go b/swarm/network/stream/streamer_test.go index 74b796b3dd..d325d3336e 100644 --- a/swarm/network/stream/streamer_test.go +++ b/swarm/network/stream/streamer_test.go @@ -922,9 +922,8 @@ func TestMaxPeerServersWithoutUnsubscribe(t *testing.T) { } } -func TestRetrievalIsBilled(t *testing.T) { -} - +//TestHasPriceImplementation is to check that the Registry has a +//`Price` interface implementation func TestHasPriceImplementation(t *testing.T) { _, r, _, teardown, err := newStreamerTester(t, &RegistryOptions{ Retrieval: RetrievalDisabled, @@ -939,13 +938,17 @@ func TestHasPriceImplementation(t *testing.T) { t.Fatal("No prices implementation available for the stream protocol") } - price := r.prices.Price(&ChunkDeliveryMsgRetrieval{}) - if price == nil || price.Value == 0 { + pricesInstance, ok := r.prices.(*StreamerPrices) + if !ok { + t.Fatal("`Registry` does not have the expected Prices instance") + } + price := pricesInstance.Price(&ChunkDeliveryMsgRetrieval{}) + if price == nil || price.Value == 0 && price.Value != pricesInstance.getChunkDeliveryMsgRetrievalPrice() { t.Fatal("No prices set for chunk delivery msg") } - price = r.prices.Price(&RetrieveRequestMsg{}) - if price == nil || price.Value == 0 { + price = pricesInstance.Price(&RetrieveRequestMsg{}) + if price == nil || price.Value == 0 && price.Value != pricesInstance.getRetrieveRequestMsgPrice() { t.Fatal("No prices set for chunk delivery msg") } }