@@ -38,6 +38,7 @@ import (
38
38
"github.com/ethersphere/swarm/p2p/protocols"
39
39
"github.com/ethersphere/swarm/spancontext"
40
40
"github.com/ethersphere/swarm/storage"
41
+ "github.com/ethersphere/swarm/swap"
41
42
opentracing "github.com/opentracing/opentracing-go"
42
43
olog "github.com/opentracing/opentracing-go/log"
43
44
)
53
54
54
55
retrievalPeers = metrics .GetOrRegisterGauge ("network.retrieve.peers" , nil )
55
56
56
- Spec = & protocols.Spec {
57
+ spec = & protocols.Spec {
57
58
Name : "bzz-retrieve" ,
58
59
Version : 1 ,
59
60
MaxMsgSize : 10 * 1024 * 1024 ,
@@ -79,11 +80,11 @@ func (p *RetrievalPrices) Price(msg interface{}) *protocols.Price {
79
80
}
80
81
81
82
func (p * RetrievalPrices ) retrieveRequestPrice () uint64 {
82
- return uint64 ( 1 )
83
+ return swap . RetrieveRequestPrice
83
84
}
84
85
85
86
func (p * RetrievalPrices ) chunkDeliveryPrice () uint64 {
86
- return uint64 ( 1 )
87
+ return swap . ChunkDeliveryPrice
87
88
}
88
89
89
90
// createPriceOracle sets up a matrix which can be queried to get
@@ -111,21 +112,27 @@ type Retrieval struct {
111
112
netStore * storage.NetStore
112
113
kad * network.Kademlia
113
114
peers map [enode.ID ]* Peer
115
+ spec * protocols.Spec
114
116
prices protocols.Prices
115
117
logger log.Logger
116
118
quit chan struct {}
117
119
}
118
120
119
121
// New returns a new instance of the retrieval protocol handler
120
- func New (kad * network.Kademlia , ns * storage.NetStore , baseKey []byte ) * Retrieval {
122
+ func New (kad * network.Kademlia , ns * storage.NetStore , baseKey []byte , balance protocols. Balance ) * Retrieval {
121
123
r := & Retrieval {
122
124
kad : kad ,
123
125
peers : make (map [enode.ID ]* Peer ),
126
+ spec : spec ,
124
127
netStore : ns ,
125
128
logger : log .New ("base" , hex .EncodeToString (baseKey )[:16 ]),
126
129
quit : make (chan struct {}),
127
130
}
128
131
r .createPriceOracle ()
132
+ if balance != nil && ! reflect .ValueOf (balance ).IsNil () {
133
+ // swap is enabled, so setup the hook
134
+ r .spec .Hook = protocols .NewAccounting (balance , r .prices )
135
+ }
129
136
return r
130
137
}
131
138
@@ -226,7 +233,7 @@ func (r *Retrieval) findPeer(ctx context.Context, req *storage.Request) (retPeer
226
233
r .kad .EachConn (req .Addr [:], 255 , func (p * network.Peer , po int ) bool {
227
234
id := p .ID ()
228
235
229
- if ! p .HasCap (Spec .Name ) {
236
+ if ! p .HasCap (r . spec .Name ) {
230
237
return true
231
238
}
232
239
@@ -453,16 +460,16 @@ func (r *Retrieval) Stop() error {
453
460
func (r * Retrieval ) Protocols () []p2p.Protocol {
454
461
return []p2p.Protocol {
455
462
{
456
- Name : Spec .Name ,
457
- Version : Spec .Version ,
458
- Length : Spec .Length (),
463
+ Name : r . spec .Name ,
464
+ Version : r . spec .Version ,
465
+ Length : r . spec .Length (),
459
466
Run : r .runProtocol ,
460
467
},
461
468
}
462
469
}
463
470
464
471
func (r * Retrieval ) runProtocol (p * p2p.Peer , rw p2p.MsgReadWriter ) error {
465
- peer := protocols .NewPeer (p , rw , Spec )
472
+ peer := protocols .NewPeer (p , rw , r . spec )
466
473
bp := network .NewBzzPeer (peer )
467
474
468
475
return r .Run (bp )
@@ -471,3 +478,7 @@ func (r *Retrieval) runProtocol(p *p2p.Peer, rw p2p.MsgReadWriter) error {
471
478
func (r * Retrieval ) APIs () []rpc.API {
472
479
return nil
473
480
}
481
+
482
+ func (r * Retrieval ) Spec () * protocols.Spec {
483
+ return r .spec
484
+ }
0 commit comments