ChainSafe / gossamer

🕸️ Go Implementation of the Polkadot Host
https://chainsafe.github.io/gossamer
GNU Lesser General Public License v3.0
427 stars 110 forks source link

Implement warp sync request handler #4052

Open EclesioMeloJunior opened 1 week ago

EclesioMeloJunior commented 1 week ago

Description

The warp sync request handler will respond request that arrives from the sync/warp protocol. The handler has as one dependency the WarpSyncProvider which is an interface that generates the proofs.

The MAX_RESPONSE_SIZE is 16 * 1024 * 1024

ref WarpSyncProvider trait

type WarpSyncProvider interface {
    // Generate proof starting at given block hash. The proof is accumulated until maximum proof
    // size is reached.
    generate(startHeader: *types.Header) (encodedProof []byte, err error)

    // Get current list of authorities. This is supposed to be genesis authorities when starting sync.
    current_authorities() []types.Authorities
}

The Warp Proof Request is a SCALE codec structure send through the wire.

Here's a impl suggestion based on substrate

type OutgoingResponse struct {
    payload []byte
}

type Request struct {
  peer peer.ID
  payload []byte
  pendingResponse chan OutgoingResponse
}

type WarpSyncRequestHandler struct {
  backend WarpSyncProvider
  // receive is a channel where the requests acquired in the network
  // layer reaches the handler
  receiver chan Request
}

func (w *WarpSyncRequestHandler) handleRequest(payload []byte, pendingResponse chan OutgoingResponse) error {
    // unmarshal the payload
    // use the backend to generate the warp proof
    // send the response through pendingResponse channel
} 

func (w *WarpSyncRequestHandler) Run() {
   for request := w.receiver {
        err := w.handleRequest(request.payload, request.pendingResponse)
        ...
   }
}

Acceptance Criteria