swarm/network/stream: don't hardcode price

This commit is contained in:
Fabio Barone 2019-01-04 11:48:15 -05:00
parent b3493cfc57
commit e3be2ed3ab
2 changed files with 27 additions and 12 deletions

View file

@ -781,25 +781,37 @@ func (sp *StreamerPrices) Price(msg interface{}) *protocols.Price {
return sp.priceMatrix[t] 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 // createPriceOracle sets up a matrix which can be queried to get
// the price for a message via the Price method // the price for a message via the Price method
func (r *Registry) createPriceOracle() { func (r *Registry) createPriceOracle() {
po := &StreamerPrices{ sp := &StreamerPrices{
registry: r, registry: r,
} }
po.priceMatrix = map[reflect.Type]*protocols.Price{ sp.priceMatrix = map[reflect.Type]*protocols.Price{
reflect.TypeOf(ChunkDeliveryMsgRetrieval{}): { reflect.TypeOf(ChunkDeliveryMsgRetrieval{}): {
Value: uint64(1), // arbitrary price for now Value: sp.getChunkDeliveryMsgRetrievalPrice(), // arbitrary price for now
PerByte: true, PerByte: true,
Payer: protocols.Receiver, Payer: protocols.Receiver,
}, },
reflect.TypeOf(RetrieveRequestMsg{}): { reflect.TypeOf(RetrieveRequestMsg{}): {
Value: uint64(1), // arbitrary price for now Value: sp.getRetrieveRequestMsgPrice(), // arbitrary price for now
PerByte: false, PerByte: false,
Payer: protocols.Sender, Payer: protocols.Sender,
}, },
} }
r.prices = po r.prices = sp
} }
func (r *Registry) Protocols() []p2p.Protocol { func (r *Registry) Protocols() []p2p.Protocol {

View file

@ -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) { func TestHasPriceImplementation(t *testing.T) {
_, r, _, teardown, err := newStreamerTester(t, &RegistryOptions{ _, r, _, teardown, err := newStreamerTester(t, &RegistryOptions{
Retrieval: RetrievalDisabled, Retrieval: RetrievalDisabled,
@ -939,13 +938,17 @@ func TestHasPriceImplementation(t *testing.T) {
t.Fatal("No prices implementation available for the stream protocol") t.Fatal("No prices implementation available for the stream protocol")
} }
price := r.prices.Price(&ChunkDeliveryMsgRetrieval{}) pricesInstance, ok := r.prices.(*StreamerPrices)
if price == nil || price.Value == 0 { 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") t.Fatal("No prices set for chunk delivery msg")
} }
price = r.prices.Price(&RetrieveRequestMsg{}) price = pricesInstance.Price(&RetrieveRequestMsg{})
if price == nil || price.Value == 0 { if price == nil || price.Value == 0 && price.Value != pricesInstance.getRetrieveRequestMsgPrice() {
t.Fatal("No prices set for chunk delivery msg") t.Fatal("No prices set for chunk delivery msg")
} }
} }