-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tag support to rebalance in and out
- Loading branch information
1 parent
379bf77
commit 202c55c
Showing
7 changed files
with
204 additions
and
14 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const {shuffle} = require('./../arrays'); | ||
|
||
/** Find a node for a tag query | ||
{ | ||
channels: [{ | ||
partner_public_key: <Peer Public Key Hex String> | ||
}] | ||
query: <Query String> | ||
tags: [{ | ||
[alias]: <Tag Alias String> | ||
id: <Tag Id Hex String> | ||
[nodes]: [<Public Key Hex String>] | ||
}] | ||
} | ||
@returns | ||
{ | ||
[match]: <Matching Node Public Key Hex String> | ||
[matches]: [{ | ||
[alias]: <Tag Alias String> | ||
id: <Tag Id Hex String> | ||
[nodes]: [<Public Key Hex String>] | ||
}] | ||
} | ||
*/ | ||
module.exports = ({channels, tags, query}) => { | ||
const peerKeys = channels.map(n => n.partner_public_key); | ||
|
||
// Find tags that match on id or on alias, and also have relevant nodes | ||
const matches = tags.filter(tag => { | ||
const nodes = (tag.nodes || []).filter(n => peerKeys.includes(n)); | ||
|
||
if (!nodes.length) { | ||
return false; | ||
} | ||
|
||
const alias = tag.alias || String(); | ||
|
||
const isAliasMatch = alias.toLowerCase().includes(query); | ||
const isIdMatch = tag.id.startsWith(query); | ||
|
||
return isAliasMatch || isIdMatch; | ||
}); | ||
|
||
const [tagMatch, ...otherTagMatches] = matches; | ||
|
||
// Exit early when there are no matches at all | ||
if (!tagMatch) { | ||
return {}; | ||
} | ||
|
||
// Exit early when there is ambiguity around the matching | ||
if (!!otherTagMatches.length) { | ||
return {matches}; | ||
} | ||
|
||
// Get the array of nodes in the tag match | ||
const array = tagMatch.nodes.filter(n => peerKeys.includes(n)); | ||
|
||
// Shuffle the results | ||
const {shuffled} = shuffle({array}); | ||
|
||
const [match] = shuffled; | ||
|
||
return {match}; | ||
}; |
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,3 +1,4 @@ | ||
const findTagMatch = require('./find_tag_match'); | ||
const rejectInboundChannels = require('./reject_inbound_channels'); | ||
|
||
module.exports = {rejectInboundChannels}; | ||
module.exports = {findTagMatch, rejectInboundChannels}; |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const {test} = require('tap'); | ||
|
||
const {findTagMatch} = require('./../../peers'); | ||
|
||
const makeArgs = overrides => { | ||
const args = { | ||
channels: [{partner_public_key: Buffer.alloc(33, 3).toString('hex')}], | ||
query: '0000', | ||
tags: [{ | ||
id: Buffer.alloc(32).toString('hex'), | ||
nodes: [Buffer.alloc(33, 3).toString('hex')], | ||
}], | ||
}; | ||
|
||
Object.keys(overrides).forEach(k => args[k] = overrides[k]); | ||
|
||
return args; | ||
}; | ||
|
||
const tests = [ | ||
{ | ||
args: makeArgs({}), | ||
description: 'A tagged node is found', | ||
expected: {match: Buffer.alloc(33, 3).toString('hex')}, | ||
}, | ||
{ | ||
args: makeArgs({ | ||
tags: [{id: Buffer.alloc(32).toString('hex')}], | ||
}), | ||
description: 'No tagged node is found', | ||
expected: {}, | ||
}, | ||
{ | ||
args: makeArgs({ | ||
channels: [ | ||
{partner_public_key: Buffer.alloc(33, 2).toString('hex')}, | ||
{partner_public_key: Buffer.alloc(33, 3).toString('hex')}, | ||
], | ||
query: 'alias', | ||
tags: [ | ||
{ | ||
alias: 'alias1', | ||
id: Buffer.alloc(32).toString('hex'), | ||
nodes: [Buffer.alloc(33, 2).toString('hex')], | ||
}, | ||
{ | ||
alias: 'alias2', | ||
id: Buffer.alloc(32, 1).toString('hex'), | ||
nodes: [Buffer.alloc(33, 3).toString('hex')], | ||
}, | ||
], | ||
}), | ||
description: 'Multiple tags are found', | ||
expected: { | ||
matches: [ | ||
{ | ||
alias: 'alias1', | ||
id: Buffer.alloc(32).toString('hex'), | ||
nodes: [Buffer.alloc(33, 2).toString('hex')], | ||
}, | ||
{ | ||
alias: 'alias2', | ||
id: Buffer.alloc(32, 1).toString('hex'), | ||
nodes: [Buffer.alloc(33, 3).toString('hex')], | ||
}, | ||
], | ||
}, | ||
}, | ||
]; | ||
|
||
tests.forEach(({args, description, error, expected}) => { | ||
return test(description, ({deepIs, end, equal, throws}) => { | ||
if (!!error) { | ||
throws(() => findTagMatch(args), new Error(error), 'Got error'); | ||
} else { | ||
const res = findTagMatch(args); | ||
|
||
deepIs(res, expected, 'Got expected rule violation'); | ||
} | ||
|
||
return end(); | ||
}); | ||
}); |