github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/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  	"fmt"
    23  
    24  	"github.com/ethereum/go-ethereum/metrics"
    25  	"github.com/ethereum/go-ethereum/p2p/enode"
    26  	"github.com/ethereum/go-ethereum/swarm/log"
    27  	"github.com/ethereum/go-ethereum/swarm/network"
    28  	"github.com/ethereum/go-ethereum/swarm/spancontext"
    29  	"github.com/ethereum/go-ethereum/swarm/storage"
    30  	"github.com/ethereum/go-ethereum/swarm/tracing"
    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  		"stream.handle.retrieve")
   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  		defer osp.Finish()
   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  // chunk delivery msg is response to retrieverequest msg
   212  func (d *Delivery) handleChunkDeliveryMsg(ctx context.Context, sp *Peer, req *ChunkDeliveryMsg) error {
   213  
   214  	processReceivedChunksCount.Inc(1)
   215  
   216  	// retrieve the span for the originating retrieverequest
   217  	spanId := fmt.Sprintf("stream.send.request.%v.%v", sp.ID(), req.Addr)
   218  	span := tracing.ShiftSpanByKey(spanId)
   219  
   220  	go func() {
   221  		if span != nil {
   222  			defer span.(opentracing.Span).Finish()
   223  		}
   224  
   225  		req.peer = sp
   226  		err := d.chunkStore.Put(ctx, storage.NewChunk(req.Addr, req.SData))
   227  		if err != nil {
   228  			if err == storage.ErrChunkInvalid {
   229  				// we removed this log because it spams the logs
   230  				// TODO: Enable this log line
   231  				// log.Warn("invalid chunk delivered", "peer", sp.ID(), "chunk", req.Addr, )
   232  				req.peer.Drop(err)
   233  			}
   234  		}
   235  	}()
   236  	return nil
   237  }
   238  
   239  // RequestFromPeers sends a chunk retrieve request to a peer
   240  // The most eligible peer that hasn't already been sent to is chosen
   241  // TODO: define "eligible"
   242  func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) (*enode.ID, chan struct{}, error) {
   243  	requestFromPeersCount.Inc(1)
   244  	var sp *Peer
   245  	spID := req.Source
   246  
   247  	if spID != nil {
   248  		sp = d.getPeer(*spID)
   249  		if sp == nil {
   250  			return nil, nil, fmt.Errorf("source peer %v not found", spID.String())
   251  		}
   252  	} else {
   253  		d.kad.EachConn(req.Addr[:], 255, func(p *network.Peer, po int) bool {
   254  			id := p.ID()
   255  			if p.LightNode {
   256  				// skip light nodes
   257  				return true
   258  			}
   259  			if req.SkipPeer(id.String()) {
   260  				log.Trace("Delivery.RequestFromPeers: skip peer", "peer id", id)
   261  				return true
   262  			}
   263  			sp = d.getPeer(id)
   264  			// sp is nil, when we encounter a peer that is not registered for delivery, i.e. doesn't support the `stream` protocol
   265  			if sp == nil {
   266  				return true
   267  			}
   268  			spID = &id
   269  			return false
   270  		})
   271  		if sp == nil {
   272  			return nil, nil, errors.New("no peer found")
   273  		}
   274  	}
   275  
   276  	// setting this value in the context creates a new span that can persist across the sendpriority queue and the network roundtrip
   277  	// this span will finish only when delivery is handled (or times out)
   278  	ctx = context.WithValue(ctx, tracing.StoreLabelId, "stream.send.request")
   279  	ctx = context.WithValue(ctx, tracing.StoreLabelMeta, fmt.Sprintf("%v.%v", sp.ID(), req.Addr))
   280  	err := sp.SendPriority(ctx, &RetrieveRequestMsg{
   281  		Addr:      req.Addr,
   282  		SkipCheck: req.SkipCheck,
   283  		HopCount:  req.HopCount,
   284  	}, Top)
   285  	if err != nil {
   286  		return nil, nil, err
   287  	}
   288  	requestFromPeersEachCount.Inc(1)
   289  
   290  	return spID, sp.quit, nil
   291  }