Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"editor.tabSize": 2,
"cSpell.words": [
"secp256k1"
"combinator",
"secp256k1",
"unls"
],
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
Expand Down
43 changes: 33 additions & 10 deletions src/api/routes/v1/get-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const log = logger({ name: 'api-get-network' })

const CRAWL_PORTS = [51235, 2459, 30001]

interface NetworkInfo {
network: string
unl?: string[]
Copy link
Collaborator

@pdp2121 pdp2121 Dec 14, 2022

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?

}

let maxNetwork: number

async function updateMaxNetwork(): Promise<void> {
Expand Down Expand Up @@ -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))
}
Expand All @@ -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],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we just show all unls similar to getNetworkInfoFromPublicKey below?

}
}
return undefined
}

/**
Expand All @@ -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
}

/**
Expand All @@ -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(','),
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/api/routes/v1/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Collaborator

Choose a reason for hiding this comment

The 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',
Expand Down