Skip to content

Commit

Permalink
fix: network status peers
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Cruz committed Aug 5, 2022
1 parent 8cdd168 commit 22cdf67
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions cardano-rosetta-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"http-status-codes": "1.4.0",
"moment": "2.29.2",
"pg": "8.3.0",
"systeminformation": "5.11.2",
"temp-write": "4.0.0"
},
"resolutions": {
Expand Down
Binary file not shown.
26 changes: 17 additions & 9 deletions cardano-rosetta-server/src/server/services/network-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { MAIN_TESTNET_NETWORK_MAGIC } from '../utils/constants';
import { BlockService } from './block-service';
import fs from 'fs';
import path from 'path';
import systemInformation from 'systeminformation';

const filePath = process.env.EXEMPTION_TYPES_PATH;
let exemptionsFile: Components.Schemas.BalanceExemption[] = [];

Expand All @@ -20,10 +22,10 @@ export interface NetworkService {
}

interface Producer {
addr: string;
port: number;
}
interface AccessPoint {
address: string;
port: number;
}
interface PublicRoot {
publicRoots: {
Expand All @@ -40,13 +42,20 @@ export interface Peer {
}

const getPublicRoots = (publicRoots?: PublicRoot[]) =>
publicRoots?.map(pr => pr.publicRoots.accessPoints.map(ap => ({ addr: ap.address }))).flat() || [];
publicRoots?.map(pr => pr.publicRoots.accessPoints.map(ap => ({ port: ap.port }))).flat() || [];

const getPeersFromConfig = (logger: Logger, topologyFile: TopologyConfig): Peer[] => {
const getPeersFromConfig = async (logger: Logger, topologyFile: TopologyConfig): Promise<Peer[]> => {
logger.info('[getPeersFromConfig] Looking for peers from topologyFile');
const Producers = topologyFile?.Producers || getPublicRoots(topologyFile.PublicRoots);
logger.debug(`[getPeersFromConfig] Found ${Producers.length} peers`);
return Producers as Peer[];
const producersPort = (topologyFile?.Producers || getPublicRoots(topologyFile.PublicRoots))
.filter(i => i !== undefined)
.map(p => p!.port.toString());

const peers = (await systemInformation.networkConnections())
.filter(item => producersPort.includes(item.peerPort))
.map(i => ({ addr: i.peerAddress }));

logger.debug(`[getPeersFromConfig] Found ${peers.length} peers`);
return peers;
};

const getExemptionFile = (logger: Logger): Components.Schemas.BalanceExemption[] => {
Expand Down Expand Up @@ -81,11 +90,10 @@ const configure = (
logger.info('[networkStatus] Looking for genesis block');
const genesisBlock = await blockchainService.getGenesisBlock(logger);
logger.debug({ genesisBlock }, '[networkStatus] Genesis block found');

return {
latestBlock,
genesisBlock,
peers: getPeersFromConfig(logger, topologyFile)
peers: await getPeersFromConfig(logger, topologyFile)
};
},
getExemptionTypes(logger: Logger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { generateNetworkPayload } from './common';
import { latestBlockIdentifier } from '../fixture-data';

const NETWORK_STATUS_ENDPOINT = '/network/status';
const peers = [{ peer_id: 'relays-new.cardano-mainnet.iohk.io' }];
const genesis_block_identifier = {
hash: '5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb',
index: 0 // FIXME this is not ok
Expand Down Expand Up @@ -36,7 +35,7 @@ describe('/network/status endpoint', () => {
expect(response.statusCode).toEqual(StatusCodes.OK);
expect(response.json().genesis_block_identifier).toEqual(genesis_block_identifier);
expect(response.json().current_block_identifier).toEqual(latestBlockIdentifier);
expect(response.json().peers).toEqual(peers);
expect(response.json().peers).toEqual([]);
});

testInvalidNetworkParameters(
Expand Down
5 changes: 5 additions & 0 deletions cardano-rosetta-server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7463,6 +7463,11 @@ symbol-tree@^3.2.4:
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2"
integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==

systeminformation@5.11.2:
version "5.11.2"
resolved "https://registry.yarnpkg.com/systeminformation/-/systeminformation-5.11.2.tgz#291280715c91f06d84f8748e78dab97df2f7ac1f"
integrity sha512-/6+p1ze2curwSMZQgQ9Gv4eLgtAaX9ROBEVELddkYy7llpP0SQjpSx4qDFEBscxqm/6yu+Khwi0Pneoy/wTwVg==

table@^5.2.3:
version "5.4.6"
resolved "https://registry.yarnpkg.com/table/-/table-5.4.6.tgz#1292d19500ce3f86053b05f0e8e7e4a3bb21079e"
Expand Down

0 comments on commit 22cdf67

Please sign in to comment.