github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/swarm/network/stream/delivery.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package stream 18 19 import ( 20 "context" 21 "errors" 22 23 "fmt" 24 25 "github.com/ethereum/go-ethereum/metrics" 26 "github.com/ethereum/go-ethereum/p2p/enode" 27 "github.com/ethereum/go-ethereum/swarm/log" 28 "github.com/ethereum/go-ethereum/swarm/network" 29 "github.com/ethereum/go-ethereum/swarm/spancontext" 30 "github.com/ethereum/go-ethereum/swarm/storage" 31 opentracing "github.com/opentracing/opentracing-go" 32 ) 33 34 const ( 35 swarmChunkServerStreamName = "RETRIEVE_REQUEST" 36 deliveryCap = 32 37 ) 38 39 var ( 40 processReceivedChunksCount = metrics.NewRegisteredCounter("network.stream.received_chunks.count", nil) 41 handleRetrieveRequestMsgCount = metrics.NewRegisteredCounter("network.stream.handle_retrieve_request_msg.count", nil) 42 43 requestFromPeersCount = metrics.NewRegisteredCounter("network.stream.request_from_peers.count", nil) 44 requestFromPeersEachCount = metrics.NewRegisteredCounter("network.stream.request_from_peers_each.count", nil) 45 ) 46 47 type Delivery struct { 48 chunkStore storage.SyncChunkStore 49 kad *network.Kademlia 50 getPeer func(enode.ID) *Peer 51 } 52 53 func NewDelivery(kad *network.Kademlia, chunkStore storage.SyncChunkStore) *Delivery { 54 return &Delivery{ 55 chunkStore: chunkStore, 56 kad: kad, 57 } 58 } 59 60 // SwarmChunkServer implements Server 61 type SwarmChunkServer struct { 62 deliveryC chan []byte 63 batchC chan []byte 64 chunkStore storage.ChunkStore 65 currentLen uint64 66 quit chan struct{} 67 } 68 69 // NewSwarmChunkServer is SwarmChunkServer constructor 70 func NewSwarmChunkServer(chunkStore storage.ChunkStore) *SwarmChunkServer { 71 s := &SwarmChunkServer{ 72 deliveryC: make(chan []byte, deliveryCap), 73 batchC: make(chan []byte), 74 chunkStore: chunkStore, 75 quit: make(chan struct{}), 76 } 77 go s.processDeliveries() 78 return s 79 } 80 81 // processDeliveries handles delivered chunk hashes 82 func (s *SwarmChunkServer) processDeliveries() { 83 var hashes []byte 84 var batchC chan []byte 85 for { 86 select { 87 case <-s.quit: 88 return 89 case hash := <-s.deliveryC: 90 hashes = append(hashes, hash...) 91 batchC = s.batchC 92 case batchC <- hashes: 93 hashes = nil 94 batchC = nil 95 } 96 } 97 } 98 99 // SessionIndex returns zero in all cases for SwarmChunkServer. 100 func (s *SwarmChunkServer) SessionIndex() (uint64, error) { 101 return 0, nil 102 } 103 104 // SetNextBatch 105 func (s *SwarmChunkServer) SetNextBatch(_, _ uint64) (hashes []byte, from uint64, to uint64, proof *HandoverProof, err error) { 106 select { 107 case hashes = <-s.batchC: 108 case <-s.quit: 109 return 110 } 111 112 from = s.currentLen 113 s.currentLen += uint64(len(hashes)) 114 to = s.currentLen 115 return 116 } 117 118 // Close needs to be called on a stream server 119 func (s *SwarmChunkServer) Close() { 120 close(s.quit) 121 } 122 123 // GetData retrives chunk data from db store 124 func (s *SwarmChunkServer) GetData(ctx context.Context, key []byte) ([]byte, error) { 125 chunk, err := s.chunkStore.Get(ctx, storage.Address(key)) 126 if err != nil { 127 return nil, err 128 } 129 return chunk.Data(), nil 130 } 131 132 // RetrieveRequestMsg is the protocol msg for chunk retrieve requests 133 type RetrieveRequestMsg struct { 134 Addr storage.Address 135 SkipCheck bool 136 HopCount uint8 137 } 138 139 func (d *Delivery) handleRetrieveRequestMsg(ctx context.Context, sp *Peer, req *RetrieveRequestMsg) error { 140 log.Trace("received request", "peer", sp.ID(), "hash", req.Addr) 141 handleRetrieveRequestMsgCount.Inc(1) 142 143 var osp opentracing.Span 144 ctx, osp = spancontext.StartSpan( 145 ctx, 146 "retrieve.request") 147 defer osp.Finish() 148 149 s, err := sp.getServer(NewStream(swarmChunkServerStreamName, "", true)) 150 if err != nil { 151 return err 152 } 153 streamer := s.Server.(*SwarmChunkServer) 154 155 var cancel func() 156 // TODO: do something with this hardcoded timeout, maybe use TTL in the future 157 ctx = context.WithValue(ctx, "peer", sp.ID().String()) 158 ctx = context.WithValue(ctx, "hopcount", req.HopCount) 159 ctx, cancel = context.WithTimeout(ctx, network.RequestTimeout) 160 161 go func() { 162 select { 163 case <-ctx.Done(): 164 case <-streamer.quit: 165 } 166 cancel() 167 }() 168 169 go func() { 170 chunk, err := d.chunkStore.Get(ctx, req.Addr) 171 if err != nil { 172 log.Warn("ChunkStore.Get can not retrieve chunk", "err", err) 173 return 174 } 175 if req.SkipCheck { 176 syncing := false 177 err = sp.Deliver(ctx, chunk, s.priority, syncing) 178 if err != nil { 179 log.Warn("ERROR in handleRetrieveRequestMsg", "err", err) 180 } 181 return 182 } 183 select { 184 case streamer.deliveryC <- chunk.Address()[:]: 185 case <-streamer.quit: 186 } 187 188 }() 189 190 return nil 191 } 192 193 //Chunk delivery always uses the same message type.... 194 type ChunkDeliveryMsg struct { 195 Addr storage.Address 196 SData []byte // the stored chunk Data (incl size) 197 peer *Peer // set in handleChunkDeliveryMsg 198 } 199 200 //...but swap accounting needs to disambiguate if it is a delivery for syncing or for retrieval 201 //as it decides based on message type if it needs to account for this message or not 202 203 //defines a chunk delivery for retrieval (with accounting) 204 type ChunkDeliveryMsgRetrieval ChunkDeliveryMsg 205 206 //defines a chunk delivery for syncing (without accounting) 207 type ChunkDeliveryMsgSyncing ChunkDeliveryMsg 208 209 // TODO: Fix context SNAFU 210 func (d *Delivery) handleChunkDeliveryMsg(ctx context.Context, sp *Peer, req *ChunkDeliveryMsg) error { 211 var osp opentracing.Span 212 ctx, osp = spancontext.StartSpan( 213 ctx, 214 "chunk.delivery") 215 defer osp.Finish() 216 217 processReceivedChunksCount.Inc(1) 218 219 go func() { 220 req.peer = sp 221 err := d.chunkStore.Put(ctx, storage.NewChunk(req.Addr, req.SData)) 222 if err != nil { 223 if err == storage.ErrChunkInvalid { 224 // we removed this log because it spams the logs 225 // TODO: Enable this log line 226 // log.Warn("invalid chunk delivered", "peer", sp.ID(), "chunk", req.Addr, ) 227 req.peer.Drop(err) 228 } 229 } 230 }() 231 return nil 232 } 233 234 // RequestFromPeers sends a chunk retrieve request to 235 func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) (*enode.ID, chan struct{}, error) { 236 requestFromPeersCount.Inc(1) 237 var sp *Peer 238 spID := req.Source 239 240 if spID != nil { 241 sp = d.getPeer(*spID) 242 if sp == nil { 243 return nil, nil, fmt.Errorf("source peer %v not found", spID.String()) 244 } 245 } else { 246 d.kad.EachConn(req.Addr[:], 255, func(p *network.Peer, po int, nn bool) bool { 247 id := p.ID() 248 // TODO: skip light nodes that do not accept retrieve requests 249 if req.SkipPeer(id.String()) { 250 log.Trace("Delivery.RequestFromPeers: skip peer", "peer id", id) 251 return true 252 } 253 sp = d.getPeer(id) 254 if sp == nil { 255 log.Warn("Delivery.RequestFromPeers: peer not found", "id", id) 256 return true 257 } 258 spID = &id 259 return false 260 }) 261 if sp == nil { 262 return nil, nil, errors.New("no peer found") 263 } 264 } 265 266 err := sp.SendPriority(ctx, &RetrieveRequestMsg{ 267 Addr: req.Addr, 268 SkipCheck: req.SkipCheck, 269 HopCount: req.HopCount, 270 }, Top) 271 if err != nil { 272 return nil, nil, err 273 } 274 requestFromPeersEachCount.Inc(1) 275 276 return spID, sp.quit, nil 277 }