-
Notifications
You must be signed in to change notification settings - Fork 8
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
feat: add UNL to get_network
API
#60
Open
mvadari
wants to merge
3
commits into
main
Choose a base branch
from
network-unls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,11 @@ const log = logger({ name: 'api-get-network' }) | |
|
||
const CRAWL_PORTS = [51235, 2459, 30001] | ||
|
||
interface NetworkInfo { | ||
network: string | ||
unl?: string[] | ||
} | ||
|
||
let maxNetwork: number | ||
|
||
async function updateMaxNetwork(): Promise<void> { | ||
|
@@ -119,6 +124,7 @@ async function any( | |
*/ | ||
async function fetchCrawls(host: string): Promise<Crawl> { | ||
const promises: Array<Promise<Crawl | undefined>> = [] | ||
log.info(`crawling ${host}...`) | ||
for (const port of CRAWL_PORTS) { | ||
promises.push(crawlNode(host, port)) | ||
} | ||
|
@@ -132,11 +138,19 @@ async function fetchCrawls(host: string): Promise<Crawl> { | |
* @param unl - The UNL of the node. | ||
* @returns Whether the UNL of the node has been seen before. | ||
*/ | ||
async function getNetworkFromUNL(unl: string): Promise<string | undefined> { | ||
async function getNetworkInfoFromUNL( | ||
unl: string, | ||
): Promise<NetworkInfo | undefined> { | ||
const result = await query('networks') | ||
.select('id') | ||
.select('id', 'unls') | ||
.where('unls', 'like', `%${unl}%`) | ||
return result.length > 0 ? result[0].id : undefined | ||
if (result.length > 0) { | ||
return { | ||
network: result[0].id, | ||
unl: result[0].unls.split(',')[0], | ||
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. Should we just show all unls similar to |
||
} | ||
} | ||
return undefined | ||
} | ||
|
||
/** | ||
|
@@ -147,13 +161,20 @@ async function getNetworkFromUNL(unl: string): Promise<string | undefined> { | |
* @returns The network associated with the public key of the node. Undefined | ||
* if the public key has not been seen by the network. | ||
*/ | ||
async function getNetworkFromPublicKey( | ||
async function getNetworkInfoFromPublicKey( | ||
publicKey: string, | ||
): Promise<string | undefined> { | ||
): Promise<NetworkInfo | undefined> { | ||
const result = await query('crawls') | ||
.select('public_key', 'networks') | ||
.where('public_key', '=', publicKey) | ||
return result.length > 0 ? result[0].networks : undefined | ||
const networkIds = result.length > 0 ? result[0].networks : undefined | ||
if (networkIds == null) { | ||
return networkIds | ||
} | ||
const networkResult = await query('networks') | ||
.select('id', 'unls') | ||
.whereIn('id', networkIds.split(',')) | ||
return networkResult | ||
} | ||
|
||
/** | ||
|
@@ -173,6 +194,7 @@ async function addNode(url: string, unl: string | null): Promise<string> { | |
port: 51235, | ||
unls: unl ? [unl] : [], | ||
} | ||
log.info(`adding new network ${JSON.stringify(network)}`) | ||
await query('networks').insert({ | ||
...network, | ||
unls: network.unls.join(','), | ||
|
@@ -204,23 +226,23 @@ export default async function getNetworkOrAdd( | |
// check UNL | ||
const { node_unl } = crawl | ||
if (node_unl != null) { | ||
const unlNetwork = await getNetworkFromUNL(node_unl) | ||
const unlNetwork = await getNetworkInfoFromUNL(node_unl) | ||
// eslint-disable-next-line max-depth -- Necessary here | ||
if (unlNetwork != null) { | ||
return res.send({ | ||
result: 'success', | ||
network: unlNetwork, | ||
...unlNetwork, | ||
}) | ||
} | ||
} | ||
|
||
// check if node public key is already recorded | ||
const { public_key } = crawl.this_node | ||
const publicKeyNetwork = await getNetworkFromPublicKey(public_key) | ||
const publicKeyNetwork = await getNetworkInfoFromPublicKey(public_key) | ||
if (publicKeyNetwork != null) { | ||
return res.send({ | ||
result: 'success', | ||
network: publicKeyNetwork, | ||
...publicKeyNetwork, | ||
}) | ||
} | ||
// add node to networks list | ||
|
@@ -229,6 +251,7 @@ export default async function getNetworkOrAdd( | |
return res.send({ | ||
result: 'success', | ||
network: newNetwork, | ||
unl: node_unl, | ||
}) | ||
} catch (err) { | ||
log.error(err.stack) | ||
|
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 |
---|---|---|
|
@@ -6,6 +6,11 @@ const info = { | |
documentation: 'Put Docs HERE', | ||
release_notes: 'Release Notes HERE', | ||
endpoints: [ | ||
{ | ||
action: 'Get Network From Node', | ||
route: '/v1/network/get_network', | ||
example: 'https://data.xrpl.org/v1/network/get_network', | ||
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. The example should include that it takes a parameter. |
||
}, | ||
{ | ||
action: 'Get Topology Nodes', | ||
route: '/v1/network/topology/nodes', | ||
|
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.
Should it just be a string based on the functions below?