Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

[PAN-2198] Update GetNodeDataFromPeerTask to return a map #931

Merged

Conversation

smatthewenglish
Copy link
Contributor

@smatthewenglish smatthewenglish commented Feb 20, 2019

PR description

Update GetNodeDataFromPeerTask to return map to node data by hash.

Fixed Issue(s)

@smatthewenglish smatthewenglish changed the title DeconstructedGetNodeDataFromPeerTaskTest [PAN-2198] Update GetNodeDataFromPeerTask to return a map Feb 20, 2019
@smatthewenglish smatthewenglish added the work in progress Work on this pull request is ongoing label Feb 20, 2019
@smatthewenglish smatthewenglish removed the work in progress Work on this pull request is ongoing label Feb 21, 2019
@@ -83,6 +87,13 @@ protected ResponseStream sendRequest(final EthPeer peer) throws PeerNotConnected
// Message contains unrequested data, must not be the response to our request.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The corresponding ticket for this task was badly written and didn't really mention this, but the main focus for this update should be to minimize the number of hash calculations we do to improve efficiency.

So, I suggest combining this check on line 86 (which checks that the data in the response corresponds to the data requested) with the construction of the map in mapNodeDataByHash. You could do this by writing a for loop that iterates over nodeData. For each element in the list, you can calculate the hash and check if the hash is in hashes. If not, you can return early without calculating all of the hashes. If it is in the list, you can add it to your map, which will be returned only if you make it all the way through the loop.

for (int i = 0; i < 3; i++) {
final BlockHeader blockHeader = blockchain.getBlockHeader(10 + i).get();
requestedData.add(
requestedData.put(
Hash.fromHexStringLenient(Integer.toHexString(i)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the hash of the node data you're requesting rather than a random hash.

final List<Hash> hashes = requestedData.stream().map(Hash::hash).collect(toList());
protected EthTask<PeerTaskResult<Map<Hash, BytesValue>>> createTask(
final Map<Hash, BytesValue> requestedData) {
final List<Hash> hashes = requestedData.values().stream().map(Hash::hash).collect(toList());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you update line 38 above, you can then just use: hashes = requestedData.keySet()

assertThat(requestedData).containsAll(partialResponse);
final List<BytesValue> requestData = new ArrayList<>(requestedData.values());
final List<BytesValue> resultData = new ArrayList<>(partialResponse.values());
assertThat(requestData).containsAll(resultData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're only checking the values here, but I think its worth checking that the values are mapped with the correct keys. Pending updates to line 38, you should be over to iterate over the partial response keyValue pairs and then look up each value in requestedData and check that it matches.

assertThat(response.getResult()).containsExactlyInAnyOrderElementsOf(requestedData);
final List<BytesValue> requestData = new ArrayList<>(requestedData.values());
final List<BytesValue> resultData = new ArrayList<>(response.getResult().values());
assertThat(resultData).containsExactlyInAnyOrderElementsOf(requestData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here as above: you should be able to check keys and values are as expected. Should just be an equality check here ...

} else if (nodeData.size() > hashes.size()) {
// Can't be the response to our request
return Optional.empty();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to keep this check here:

    if (nodeData.size() > hashes.size()) {
      // Can't be the response to our request
      return Optional.empty();
    }

Copy link
Contributor

@mbaxter mbaxter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@smatthewenglish smatthewenglish merged commit 77307e6 into PegaSysEng:master Feb 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants