github.com/dotlike13/wemix30_go@v1.8.23/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  	opentracing "github.com/opentracing/opentracing-go"
    31  )
    32  
    33  const (
    34  	swarmChunkServerStreamName = "RETRIEVE_REQUEST"
    35  	deliveryCap                = 32
    36  )
    37  
    38  var (
    39  	processReceivedChunksCount    = metrics.NewRegisteredCounter("network.stream.received_chunks.count", nil)
    40  	handleRetrieveRequestMsgCount = metrics.NewRegisteredCounter("network.stream.handle_retrieve_request_msg.count", nil)
    41  	retrieveChunkFail             = metrics.NewRegisteredCounter("network.stream.retrieve_chunks_fail.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  
   148  	s, err := sp.getServer(NewStream(swarmChunkServerStreamName, "", true))
   149  	if err != nil {
   150  		return err
   151  	}
   152  	streamer := s.Server.(*SwarmChunkServer)
   153  
   154  	var cancel func()
   155  	// TODO: do something with this hardcoded timeout, maybe use TTL in the future
   156  	ctx = context.WithValue(ctx, "peer", sp.ID().String())
   157  	ctx = context.WithValue(ctx, "hopcount", req.HopCount)
   158  	ctx, cancel = context.WithTimeout(ctx, network.RequestTimeout)
   159  
   160  	go func() {
   161  		select {
   162  		case <-ctx.Done():
   163  		case <-streamer.quit:
   164  		}
   165  		cancel()
   166  	}()
   167  
   168  	go func() {
   169  		defer osp.Finish()
   170  		chunk, err := d.chunkStore.Get(ctx, req.Addr)
   171  		if err != nil {
   172  			retrieveChunkFail.Inc(1)
   173  			log.Debug("ChunkStore.Get can not retrieve chunk", "peer", sp.ID().String(), "addr", req.Addr, "hopcount", req.HopCount, "err", err)
   174  			return
   175  		}
   176  		if req.SkipCheck {
   177  			syncing := false
   178  			err = sp.Deliver(ctx, chunk, s.priority, syncing)
   179  			if err != nil {
   180  				log.Warn("ERROR in handleRetrieveRequestMsg", "err", err)
   181  			}
   182  			return
   183  		}
   184  		select {
   185  		case streamer.deliveryC <- chunk.Address()[:]:
   186  		case <-streamer.quit:
   187  		}
   188  
   189  	}()
   190  
   191  	return nil
   192  }
   193  
   194  //Chunk delivery always uses the same message type....
   195  type ChunkDeliveryMsg struct {
   196  	Addr  storage.Address
   197  	SData []byte // the stored chunk Data (incl size)
   198  	peer  *Peer  // set in handleChunkDeliveryMsg
   199  }
   200  
   201  //...but swap accounting needs to disambiguate if it is a delivery for syncing or for retrieval
   202  //as it decides based on message type if it needs to account for this message or not
   203  
   204  //defines a chunk delivery for retrieval (with accounting)
   205  type ChunkDeliveryMsgRetrieval ChunkDeliveryMsg
   206  
   207  //defines a chunk delivery for syncing (without accounting)
   208  type ChunkDeliveryMsgSyncing ChunkDeliveryMsg
   209  
   210  // TODO: Fix context SNAFU
   211  func (d *Delivery) handleChunkDeliveryMsg(ctx context.Context, sp *Peer, req *ChunkDeliveryMsg) error {
   212  	var osp opentracing.Span
   213  	ctx, osp = spancontext.StartSpan(
   214  		ctx,
   215  		"chunk.delivery")
   216  
   217  	processReceivedChunksCount.Inc(1)
   218  
   219  	go func() {
   220  		defer osp.Finish()
   221  
   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) 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  			// sp is nil, when we encounter a peer that is not registered for delivery, i.e. doesn't support the `stream` protocol
   260  			if sp == nil {
   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, "request.from.peers")
   276  	if err != nil {
   277  		return nil, nil, err
   278  	}
   279  	requestFromPeersEachCount.Inc(1)
   280  
   281  	return spID, sp.quit, nil
   282  }