-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: check status and version to find peers #30
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c9ddfce
fix: check status and version to find peers
luciorubeens 05e4fd5
test: fix peer version in v2 test
alexbarnsley 892a9ae
refactor: use cloneDeep for peer list
alexbarnsley 99500ad
fix: clonedeep instead of cloneDeep
luciorubeens 49c5862
chore: bump version
alexbarnsley e4ff81a
docs: update changelog
alexbarnsley File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
const shuffle = require('lodash.shuffle') | ||
const cloneDeep = require('lodash.clonedeep') | ||
const HttpClient = require('./http') | ||
const resources = require('./resources') | ||
const initialPeers = require('./peers') | ||
|
@@ -19,6 +20,10 @@ const mergePeerArrays = (oldArray, newArray) => { | |
return peers | ||
} | ||
|
||
const getPeerVersion = (peer) => { | ||
return !peer.version || /^1\./.test(peer.version) ? 1 : 2 | ||
} | ||
|
||
module.exports = class ApiClient { | ||
/** | ||
* Finds all the available peers, sorted by block height and delay | ||
|
@@ -38,10 +43,7 @@ module.exports = class ApiClient { | |
shuffle(networkPeers) | ||
|
||
const selfIps = ['127.0.0.1', '::ffff:127.0.0.1', '::1'] | ||
const versionStatus = { | ||
1: 'OK', | ||
2: 200 | ||
} | ||
const versionStatus = ['OK', 200] | ||
|
||
// Connect to each peer to get an updated list of peers until a success response | ||
const client = new ApiClient('http://', version) | ||
|
@@ -69,7 +71,7 @@ module.exports = class ApiClient { | |
if (responsePeers.length) { | ||
// Ignore local and unavailable peers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // Ignore local and unavailable peers, and those with wrong version |
||
responsePeers = responsePeers.filter(peer => { | ||
return selfIps.indexOf(peer.ip) === -1 && peer.status === versionStatus[version] | ||
return selfIps.indexOf(peer.ip) === -1 && versionStatus.includes(peer.status) && getPeerVersion(peer) === version | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use return !selfIps.includes(peer.ip) && versionStatus.includes(peer.status) && getPeerVersion(peer) === version |
||
}) | ||
|
||
peers = mergePeerArrays(peers, responsePeers) | ||
|
@@ -86,7 +88,7 @@ module.exports = class ApiClient { | |
} | ||
|
||
// Return at least the initial (hardcoded) peers | ||
return sortPeers(peers && peers.length ? peers : networkPeers) | ||
return cloneDeep(sortPeers(peers && peers.length ? peers : networkPeers)) | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use something like: