Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarm, p2p/protocols: Stream accounting #18337

Merged
merged 15 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions swarm/network/stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 10 additions & 7 deletions swarm/network/stream/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not right :)

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")
}
}