Skip to content

Commit

Permalink
Workobject for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed May 14, 2024
1 parent 959cb39 commit 144d124
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions p2p/node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (p *P2PNode) Stop() error {
}
}

func (p *P2PNode) requestFromPeers(topic *pubsubManager.Topic, location common.Location, requestData interface{}, responseDataType interface{}, resultChan chan interface{}) {
func (p *P2PNode) requestFromPeers(topic *pubsubManager.Topic, requestData interface{}, resultChan chan interface{}) {
go func() {
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -215,7 +215,7 @@ func (p *P2PNode) Request(location common.Location, requestData interface{}, res
}
}

p.requestFromPeers(topic, location, requestData, responseDataType, resultChan)
p.requestFromPeers(topic, requestData, resultChan)
// TODO: optimize with waitgroups or a doneChan to only query if no peers responded
// Right now this creates too many streams, so don't call this until we have a better solution
// p.queryDHT(location, requestData, responseDataType, resultChan)
Expand Down
5 changes: 5 additions & 0 deletions p2p/node/p2p_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func (p *P2PNode) requestFromPeer(peerID peer.ID, topic *pubsubManager.Topic, re
case *types.WorkObject:
if block, ok := recvdType.(*types.WorkObject); ok {
switch data := reqData.(type) {
case *types.WorkObjectBlockView, *types.WorkObjectHeaderView:
if block.Hash() == data.(*types.WorkObject).Hash() {
return data, nil
}
return nil, errors.Errorf("invalid response: expected block with hash %s, got %s", data.(*types.WorkObject).Hash(), block.Hash())
case common.Hash:
if block.Hash() == data {
return block, nil
Expand Down
4 changes: 2 additions & 2 deletions quai/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ func (h *handler) GetNextPrimeBlock(number *big.Int) {
// If the blockHash for the asked number is not present in the
// appended database we ask the peer for the block with this hash
if block == nil {
resultCh := h.p2pBackend.Request(h.nodeLocation, blockHash, &types.WorkObject{})
resultCh := h.p2pBackend.Request(h.nodeLocation, blockHash, &types.WorkObjectBlockView{})
block := <-resultCh
if block != nil {
h.core.WriteBlock(block.(*types.WorkObject))
h.core.WriteBlock(block.(*types.WorkObjectBlockView).WorkObject)
}
}
}
Expand Down

0 comments on commit 144d124

Please sign in to comment.