github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/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/insight-chain/inb-go/metrics" 26 "github.com/insight-chain/inb-go/p2p/enode" 27 "github.com/insight-chain/inb-go/swarm/log" 28 "github.com/insight-chain/inb-go/swarm/network" 29 "github.com/insight-chain/inb-go/swarm/spancontext" 30 "github.com/insight-chain/inb-go/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 retrieveChunkFail = metrics.NewRegisteredCounter("network.stream.retrieve_chunks_fail.count", nil) 43 44 requestFromPeersCount = metrics.NewRegisteredCounter("network.stream.request_from_peers.count", nil) 45 requestFromPeersEachCount = metrics.NewRegisteredCounter("network.stream.request_from_peers_each.count", nil) 46 ) 47 48 type Delivery struct { 49 chunkStore storage.SyncChunkStore 50 kad *network.Kademlia 51 getPeer func(enode.ID) *Peer 52 } 53 54 func NewDelivery(kad *network.Kademlia, chunkStore storage.SyncChunkStore) *Delivery { 55 return &Delivery{ 56 chunkStore: chunkStore, 57 kad: kad, 58 } 59 } 60 61 // SwarmChunkServer implements Server 62 type SwarmChunkServer struct { 63 deliveryC chan []byte 64 batchC chan []byte 65 chunkStore storage.ChunkStore 66 currentLen uint64 67 quit chan struct{} 68 } 69 70 // NewSwarmChunkServer is SwarmChunkServer constructor 71 func NewSwarmChunkServer(chunkStore storage.ChunkStore) *SwarmChunkServer { 72 s := &SwarmChunkServer{ 73 deliveryC: make(chan []byte, deliveryCap), 74 batchC: make(chan []byte), 75 chunkStore: chunkStore, 76 quit: make(chan struct{}), 77 } 78 go s.processDeliveries() 79 return s 80 } 81 82 // processDeliveries handles delivered chunk hashes 83 func (s *SwarmChunkServer) processDeliveries() { 84 var hashes []byte 85 var batchC chan []byte 86 for { 87 select { 88 case <-s.quit: 89 return 90 case hash := <-s.deliveryC: 91 hashes = append(hashes, hash...) 92 batchC = s.batchC 93 case batchC <- hashes: 94 hashes = nil 95 batchC = nil 96 } 97 } 98 } 99 100 // SessionIndex returns zero in all cases for SwarmChunkServer. 101 func (s *SwarmChunkServer) SessionIndex() (uint64, error) { 102 return 0, nil 103 } 104 105 // SetNextBatch 106 func (s *SwarmChunkServer) SetNextBatch(_, _ uint64) (hashes []byte, from uint64, to uint64, proof *HandoverProof, err error) { 107 select { 108 case hashes = <-s.batchC: 109 case <-s.quit: 110 return 111 } 112 113 from = s.currentLen 114 s.currentLen += uint64(len(hashes)) 115 to = s.currentLen 116 return 117 } 118 119 // Close needs to be called on a stream server 120 func (s *SwarmChunkServer) Close() { 121 close(s.quit) 122 } 123 124 // GetData retrives chunk data from db store 125 func (s *SwarmChunkServer) GetData(ctx context.Context, key []byte) ([]byte, error) { 126 chunk, err := s.chunkStore.Get(ctx, storage.Address(key)) 127 if err != nil { 128 return nil, err 129 } 130 return chunk.Data(), nil 131 } 132 133 // RetrieveRequestMsg is the protocol msg for chunk retrieve requests 134 type RetrieveRequestMsg struct { 135 Addr storage.Address 136 SkipCheck bool 137 HopCount uint8 138 } 139 140 func (d *Delivery) handleRetrieveRequestMsg(ctx context.Context, sp *Peer, req *RetrieveRequestMsg) error { 141 log.Trace("received request", "peer", sp.ID(), "hash", req.Addr) 142 handleRetrieveRequestMsgCount.Inc(1) 143 144 var osp opentracing.Span 145 ctx, osp = spancontext.StartSpan( 146 ctx, 147 "retrieve.request") 148 defer osp.Finish() 149 150 s, err := sp.getServer(NewStream(swarmChunkServerStreamName, "", true)) 151 if err != nil { 152 return err 153 } 154 streamer := s.Server.(*SwarmChunkServer) 155 156 var cancel func() 157 // TODO: do something with this hardcoded timeout, maybe use TTL in the future 158 ctx = context.WithValue(ctx, "peer", sp.ID().String()) 159 ctx = context.WithValue(ctx, "hopcount", req.HopCount) 160 ctx, cancel = context.WithTimeout(ctx, network.RequestTimeout) 161 162 go func() { 163 select { 164 case <-ctx.Done(): 165 case <-streamer.quit: 166 } 167 cancel() 168 }() 169 170 go func() { 171 chunk, err := d.chunkStore.Get(ctx, req.Addr) 172 if err != nil { 173 retrieveChunkFail.Inc(1) 174 log.Debug("ChunkStore.Get can not retrieve chunk", "peer", sp.ID().String(), "addr", req.Addr, "hopcount", req.HopCount, "err", err) 175 return 176 } 177 if req.SkipCheck { 178 syncing := false 179 err = sp.Deliver(ctx, chunk, s.priority, syncing) 180 if err != nil { 181 log.Warn("ERROR in handleRetrieveRequestMsg", "err", err) 182 } 183 return 184 } 185 select { 186 case streamer.deliveryC <- chunk.Address()[:]: 187 case <-streamer.quit: 188 } 189 190 }() 191 192 return nil 193 } 194 195 //Chunk delivery always uses the same message type.... 196 type ChunkDeliveryMsg struct { 197 Addr storage.Address 198 SData []byte // the stored chunk Data (incl size) 199 peer *Peer // set in handleChunkDeliveryMsg 200 } 201 202 //...but swap accounting needs to disambiguate if it is a delivery for syncing or for retrieval 203 //as it decides based on message type if it needs to account for this message or not 204 205 //defines a chunk delivery for retrieval (with accounting) 206 type ChunkDeliveryMsgRetrieval ChunkDeliveryMsg 207 208 //defines a chunk delivery for syncing (without accounting) 209 type ChunkDeliveryMsgSyncing ChunkDeliveryMsg 210 211 // TODO: Fix context SNAFU 212 func (d *Delivery) handleChunkDeliveryMsg(ctx context.Context, sp *Peer, req *ChunkDeliveryMsg) error { 213 var osp opentracing.Span 214 ctx, osp = spancontext.StartSpan( 215 ctx, 216 "chunk.delivery") 217 defer osp.Finish() 218 219 processReceivedChunksCount.Inc(1) 220 221 go func() { 222 req.peer = sp 223 err := d.chunkStore.Put(ctx, storage.NewChunk(req.Addr, req.SData)) 224 if err != nil { 225 if err == storage.ErrChunkInvalid { 226 // we removed this log because it spams the logs 227 // TODO: Enable this log line 228 // log.Warn("invalid chunk delivered", "peer", sp.ID(), "chunk", req.Addr, ) 229 req.peer.Drop(err) 230 } 231 } 232 }() 233 return nil 234 } 235 236 // RequestFromPeers sends a chunk retrieve request to 237 func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) (*enode.ID, chan struct{}, error) { 238 requestFromPeersCount.Inc(1) 239 var sp *Peer 240 spID := req.Source 241 242 if spID != nil { 243 sp = d.getPeer(*spID) 244 if sp == nil { 245 return nil, nil, fmt.Errorf("source peer %v not found", spID.String()) 246 } 247 } else { 248 d.kad.EachConn(req.Addr[:], 255, func(p *network.Peer, po int, nn bool) bool { 249 id := p.ID() 250 if p.LightNode { 251 // skip light nodes 252 return true 253 } 254 if req.SkipPeer(id.String()) { 255 log.Trace("Delivery.RequestFromPeers: skip peer", "peer id", id) 256 return true 257 } 258 sp = d.getPeer(id) 259 if sp == nil { 260 //log.Warn("Delivery.RequestFromPeers: peer not found", "id", id) 261 return true 262 } 263 spID = &id 264 return false 265 }) 266 if sp == nil { 267 return nil, nil, errors.New("no peer found") 268 } 269 } 270 271 err := sp.SendPriority(ctx, &RetrieveRequestMsg{ 272 Addr: req.Addr, 273 SkipCheck: req.SkipCheck, 274 HopCount: req.HopCount, 275 }, Top) 276 if err != nil { 277 return nil, nil, err 278 } 279 requestFromPeersEachCount.Inc(1) 280 281 return spID, sp.quit, nil 282 }