Skip to content

Commit

Permalink
Merge pull request #152 from casper-network/fix/fixes-for-middleware
Browse files Browse the repository at this point in the history
Disabled peers details, performance fixes
  • Loading branch information
hoffmannjan authored Jan 30, 2023
2 parents f7e2e85 + 766d674 commit 671614b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 53 deletions.
6 changes: 3 additions & 3 deletions frontend/src/api/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ export interface GetPeers {
export interface Peer {
nodeId: string;
address: string;
uptime: string;
isAlive: boolean;
lastAddedBlockHash: string;
uptime?: string;
isAlive?: boolean;
lastAddedBlockHash?: string;
}
export interface GetBlocks {
blocks: Block[];
Expand Down
32 changes: 0 additions & 32 deletions frontend/src/components/tables/PeerTable/PeerTable.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React, { useMemo } from 'react';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import styled from '@emotion/styled';
import { ColumnDef } from '@tanstack/react-table';
import { colors, fontWeight } from 'src/styled-theme';
import { Peer } from '../../../api';
import { Table } from '../../base';
import { truncateHash } from '../../../utils';

interface PeerTableProps {
readonly peers: Peer[];
Expand All @@ -26,36 +24,6 @@ export const PeerTable: React.FC<PeerTableProps> = ({ peers }) => {
accessorKey: 'address',
enableSorting: false,
},
{
header: `${t('uptime')}`,
accessorKey: 'uptime',
enableSorting: false,
},
{
header: `${t('last-block-hash')}`,
accessorKey: 'lastAddedBlockHash',
enableSorting: false,
cell: ({ getValue }) => (
<div className="flex flex-row items-center">
<Link
to={{
pathname: `/block/${getValue<string>()}`,
}}>
{truncateHash(getValue<string>())}
</Link>
</div>
),
},
{
header: `${t('is-alive')}`,
accessorKey: 'isAlive',
enableSorting: false,
cell: ({ getValue }) => (
<div className="flex flex-row items-center">
{getValue<boolean>() ? t('up') : t('down')}
</div>
),
},
],
[t],
);
Expand Down
3 changes: 1 addition & 2 deletions middleware/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Support for SSE Sidecar (`SIDECAR_REST_URL` & `SIDECAR_EVENTSTREAM_URL` required to enable it)
- New `/peers` endpoint providing peers statuses (hash of last added block, uptime, ip)
- New endpoints: `/latest-block`, `/block/:hashOrHeight`, `/status`, `/blocks`, `/deploy/:hash`, `/account/:accountHashOrPublicKey`
- New endpoints: `/peers`, `/latest-block`, `/block/:hashOrHeight`, `/status`, `/blocks`, `/deploy/:hash`, `/account/:accountHashOrPublicKey`
- When it's possible, all of the endpoint support both Sidecar as well as RPC
- Added cache so number of RPC request is minimized

Expand Down
5 changes: 3 additions & 2 deletions middleware/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ router.use("/rpc", (req, res) => {
axios
.post(node.url, req.body)
.then((nodeRes) => res.json(nodeRes.data))
.catch(() => {
nodeManager.setDeadNode(node.id);
.catch((err) => {
console.log('>>>>>setDeadNode', err);
// nodeManager.setDeadNode(node.id);
rpcCall();
});
};
Expand Down
13 changes: 5 additions & 8 deletions middleware/src/services/peers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StatusCodes } from "http-status-codes";

import { Peer } from "../types";
import { ApiError } from "../utils";
import { getPeers, setPeers } from "./cache";
import { setPeers } from "./cache";
import { nodeManager } from "./node-manager";

export const fetchPeerInfo = async (ip: string) => {
Expand Down Expand Up @@ -31,11 +31,6 @@ export const fetchPeerInfo = async (ip: string) => {
};

export const fetchPeers = async (update = false): Promise<Peer[]> => {
const existPeers = getPeers();
if (existPeers && !update) {
return existPeers;
}

const rpcCall = async () => {
const activeNode = nodeManager.getActiveNode();
try {
Expand All @@ -58,14 +53,16 @@ export const fetchPeers = async (update = false): Promise<Peer[]> => {
const peers: Peer[] = await Promise.all(
peersToCheck.map(async (peer) => {
const { node_id: nodeId, address } = peer;
const info = await fetchPeerInfo(peer.address.split(":")[0]);
return { nodeId, address, ...info };
return { nodeId, address };
})
);


if (update) {
console.log("Force update peer list");
}

setPeers(peers);

return peers;
};
9 changes: 6 additions & 3 deletions middleware/src/services/rpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,24 @@ export class RpcClient {
const targetBlock =
orderByHeight === "DESC" ? fromBlock - count : fromBlock + count;

const blocks: Block[] = [];
const blockPromises: Promise<Block>[] = [];

for (
let i = fromBlock;
orderByHeight === "DESC" ? i > targetBlock : i < targetBlock;
orderByHeight === "DESC" ? i-- : i++
) {
try {
const block = await this.getBlockByHeight(i);
blocks.push(block);
const block = this.getBlockByHeight(i);
blockPromises.push(block);
} catch (error) {
console.log("ERROR", error);
break;
}
}

const blocks = await Promise.all(blockPromises);

latestBlockHeight = (await this.getLatestBlock()).header.height;

const total = latestBlockHeight + 1;
Expand Down
6 changes: 3 additions & 3 deletions middleware/src/types/node-status-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface StatusResponse {
export interface Peer {
nodeId: string;
address: string;
isAlive: boolean | null;
uptime: string | null;
lastAddedBlockHash: string | null;
isAlive?: boolean | null;
uptime?: string | null;
lastAddedBlockHash?: string | null;
}

0 comments on commit 671614b

Please sign in to comment.