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

Common connection config allows the basic connection rate limiting code to slow things down #281

Merged
merged 2 commits into from
Jul 20, 2022
Merged
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: 2 additions & 2 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Tooltip from 'react-bootstrap/Tooltip';
import { NavLink, Outlet, Route, Routes } from 'react-router-dom';
import { toast, ToastContainer } from 'react-toastify';

import { logger } from './common/globals';
import { GetValidatorConnection, logger } from './common/globals';
import 'react-toastify/dist/ReactToastify.css';
import './App.scss';
import { airdropSol, getElectronStorageWallet } from './data/accounts/account';
Expand Down Expand Up @@ -154,7 +154,7 @@ function Topbar() {
default:
amount = 1000;
}
const solConn = new sol.Connection(netToURL(net));
const solConn = GetValidatorConnection(net);
if (wallet.publicKey) {
try {
const balance = await solConn.getBalance(wallet.publicKey);
Expand Down
25 changes: 24 additions & 1 deletion src/renderer/common/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as sol from '@solana/web3.js';
import { Net, netToURL } from '../data/ValidatorNetwork/validatorNetworkState';

// eslint-disable-next-line import/prefer-default-export
export const logger = (() => {
return window.electron?.log;
Expand All @@ -6,5 +9,25 @@ export const logger = (() => {
// TODO: make this selectable - Return information at the selected commitment level
// [possible values: processed, confirmed, finalized]
// cli default seems to be finalized
// The _get_ data commitment level - can cause mutation API calls to fail (i think due to wallet-adapter things)
// The _get_ data commitment level - can cause some mutation API calls to fail (i think due to wallet-adapter / metaplex things)
export const commitmentLevel = 'processed';

let solConn: sol.Connection | undefined;
let connNet: Net;

export function GetValidatorConnection(net: Net) {
if (connNet === net) {
if (solConn) {
return solConn;
}
solConn = undefined;
connNet = net;
}
const cfg: sol.ConnectionConfig = {
commitment: commitmentLevel,
disableRetryOnRateLimit: true,
};
solConn = new sol.Connection(netToURL(net), cfg);

return solConn;
}
9 changes: 3 additions & 6 deletions src/renderer/components/AccountView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { Program, AnchorProvider, setProvider } from '@project-serum/anchor';
import * as sol from '@solana/web3.js';
import { useQueryClient } from 'react-query';
import { logger } from '../common/globals';
import { GetValidatorConnection, logger } from '../common/globals';
import { useAppDispatch, useAppSelector } from '../hooks';

import {
Expand All @@ -26,10 +26,7 @@ import {
truncateLamportAmount,
useParsedAccount,
} from '../data/accounts/getAccount';
import {
netToURL,
selectValidatorNetworkState,
} from '../data/ValidatorNetwork/validatorNetworkState';
import { selectValidatorNetworkState } from '../data/ValidatorNetwork/validatorNetworkState';
import AirDropSolButton from './AirDropSolButton';
import EditableText from './base/EditableText';
import InlinePK from './InlinePK';
Expand Down Expand Up @@ -88,7 +85,7 @@ function AccountView(props: { pubKey: string | undefined }) {
// TODO: Why do I have to set this every time
setProvider(
new AnchorProvider(
new sol.Connection(netToURL(net)),
GetValidatorConnection(net),
wallet,
AnchorProvider.defaultOptions()
)
Expand Down
9 changes: 6 additions & 3 deletions src/renderer/components/LogView.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as sol from '@solana/web3.js';
import { useEffect, useState } from 'react';
import { logger, commitmentLevel } from '../common/globals';
import {
logger,
commitmentLevel,
GetValidatorConnection,
} from '../common/globals';
import {
NetStatus,
netToURL,
selectValidatorNetworkState,
} from '../data/ValidatorNetwork/validatorNetworkState';
import { useAppSelector } from '../hooks';
Expand All @@ -28,7 +31,7 @@ function LogView() {
return () => {};
}

const solConn = new sol.Connection(netToURL(net));
const solConn = GetValidatorConnection(net);
const subscriptionID = solConn.onLogs(
'all',
(logsInfo) => {
Expand Down
7 changes: 3 additions & 4 deletions src/renderer/data/ValidatorNetwork/ValidatorNetwork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { useEffect } from 'react';
import Dropdown from 'react-bootstrap/Dropdown';
import DropdownButton from 'react-bootstrap/DropdownButton';
import { NavLink } from 'react-router-dom';
import { logger } from '../../common/globals';
import { GetValidatorConnection, logger } from '../../common/globals';
import { useAppDispatch, useAppSelector, useInterval } from '../../hooks';
import {
Net,
NetStatus,
netToURL,
selectValidatorNetworkState,
setNet,
setState,
Expand All @@ -19,7 +18,7 @@ const validatorState = async (net: Net): Promise<NetStatus> => {

// Connect to cluster
try {
solConn = new sol.Connection(netToURL(net));
solConn = GetValidatorConnection(net);
await solConn.getEpochInfo();
} catch (error) {
return NetStatus.Unavailable;
Expand Down Expand Up @@ -51,7 +50,7 @@ function ValidatorNetwork() {
.catch((err) => {
logger.debug(err);
});
}, 5000);
}, 11111);

const netDropdownSelect = (eventKey: string | null) => {
// TODO: analytics('selectNet', { prevNet: net, newNet: eventKey });
Expand Down
14 changes: 7 additions & 7 deletions src/renderer/data/accounts/account.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { AnyAction, Dispatch, ThunkDispatch } from '@reduxjs/toolkit';
import { WalletContextState } from '@solana/wallet-adapter-react';
import * as sol from '@solana/web3.js';
import { logger, commitmentLevel } from '../../common/globals';
import {
logger,
commitmentLevel,
GetValidatorConnection,
} from '../../common/globals';
import { NewKeyPairInfo } from '../../../types/types';
import { ConfigState, setConfigValue } from '../Config/configState';
import { SelectedAccountsList } from '../SelectedAccountsList/selectedAccountsState';
import {
Net,
netToURL,
ValidatorState,
} from '../ValidatorNetwork/validatorNetworkState';
import { Net, ValidatorState } from '../ValidatorNetwork/validatorNetworkState';
import { AccountsState, reloadFromMain } from './accountState';

export async function airdropSol(
Expand All @@ -21,7 +21,7 @@ export async function airdropSol(
const sols =
typeof solAmount === 'number' ? solAmount : parseFloat(solAmount);

const connection = new sol.Connection(netToURL(net));
const connection = GetValidatorConnection(net);

const airdropSignature = await connection.requestAirdrop(
to,
Expand Down
13 changes: 9 additions & 4 deletions src/renderer/data/accounts/getAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import * as metaplex from '@metaplex/js';

import { LRUCache } from 'typescript-lru-cache';
import { useQuery } from 'react-query';
import { logger, commitmentLevel } from '../../common/globals';
import {
logger,
commitmentLevel,
GetValidatorConnection,
} from '../../common/globals';
import { Net, netToURL } from '../ValidatorNetwork/validatorNetworkState';
import { AccountInfo } from './accountInfo';
import { AccountMetaValues } from './accountState';
Expand Down Expand Up @@ -41,7 +45,7 @@ export async function getParsedAccount(
net: Net,
pubKey: string
): Promise<sol.AccountInfo<sol.ParsedAccountData> | undefined> {
const solConn = new sol.Connection(netToURL(net), commitmentLevel);
const solConn = GetValidatorConnection(net);

const key = new sol.PublicKey(pubKey);

Expand Down Expand Up @@ -145,7 +149,7 @@ export async function getTokenAccounts(
net: Net,
pubKey: string
): Promise<sol.RpcResponseAndContext<TokenAccountArray>> {
const solConn = new sol.Connection(netToURL(net), commitmentLevel);
const solConn = GetValidatorConnection(net);
const key = new sol.PublicKey(pubKey);
const filter: sol.TokenAccountsFilter = {
programId: new sol.PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'),
Expand Down Expand Up @@ -180,6 +184,7 @@ export async function getTokenMetadata(
net: Net,
tokenPublicKey: string
): Promise<metaplex.programs.metadata.Metadata> {
// TODO: ...
const conn = new metaplex.Connection(netToURL(net), commitmentLevel);
try {
// TODO: this console.logs "metadata load Error: Unable to find account: HKCjVqNU35H3zsXAVetgo743qCDMu7ssGnET1yvN4RSJ"
Expand Down Expand Up @@ -269,7 +274,7 @@ export const getHumanName = (meta: AccountMetaValues | undefined) => {
};

export async function refreshAccountInfos(net: Net, keys: string[]) {
const solConn = new sol.Connection(netToURL(net), commitmentLevel);
const solConn = GetValidatorConnection(net);
const pubKeys = keys.map((k) => new sol.PublicKey(k));
const accountInfos = await solConn.getMultipleAccountsInfo(pubKeys);

Expand Down
6 changes: 3 additions & 3 deletions src/renderer/data/accounts/programChanges.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as sol from '@solana/web3.js';
import { logger } from '../../common/globals';
import { Net, netToURL } from '../ValidatorNetwork/validatorNetworkState';
import { GetValidatorConnection, logger } from '../../common/globals';
import { Net } from '../ValidatorNetwork/validatorNetworkState';
import { AccountInfo } from './accountInfo';
import { peekAccount, updateCache } from './getAccount';

Expand Down Expand Up @@ -40,7 +40,7 @@ export const subscribeProgramChanges = async (
) {
logger.silly('subscribeProgramChanges', programID);

const solConn = new sol.Connection(netToURL(net));
const solConn = GetValidatorConnection(net);
const subscriptionID = solConn.onProgramAccountChange(
programIDPubkey,
(info: sol.KeyedAccountInfo, ctx: sol.Context) => {
Expand Down
13 changes: 6 additions & 7 deletions src/renderer/nav/ValidatorNetworkInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { useEffect, useState } from 'react';
import { Col, Row } from 'react-bootstrap';
import Container from 'react-bootstrap/Container';
import { VictoryPie } from 'victory';
import { logger } from '../common/globals';
import { GetValidatorConnection, logger } from '../common/globals';
import {
netToURL,
Net,
selectValidatorNetworkState,
} from '../data/ValidatorNetwork/validatorNetworkState';
import { useAppSelector } from '../hooks';
Expand All @@ -23,8 +23,8 @@ export type ValidatorNetworkInfoResponse = {
versionCount: VCount[];
};
// https://docs.solana.com/developing/clients/jsonrpc-api#getclusternodes
const fetchValidatorNetworkInfo = async (url: string) => {
const solConn = new sol.Connection(url);
const fetchValidatorNetworkInfo = async (net: Net) => {
const solConn = GetValidatorConnection(net);
const contactInfo = await solConn.getClusterNodes();
// TODO: on success / failure update the ValidatorNetworkState..
const nodeVersion = await solConn.getVersion();
Expand Down Expand Up @@ -64,7 +64,6 @@ const fetchValidatorNetworkInfo = async (url: string) => {
function ValidatorNetworkInfo() {
const validator = useAppSelector(selectValidatorNetworkState);
const { net } = validator;
const url = netToURL(net);

const [data, setData] = useState<ValidatorNetworkInfoResponse>({
version: 'unknown',
Expand All @@ -73,10 +72,10 @@ function ValidatorNetworkInfo() {
});
useEffect(() => {
// TODO: set a spinner while waiting for response
fetchValidatorNetworkInfo(url)
fetchValidatorNetworkInfo(net)
.then((d) => setData(d))
.catch(logger.info);
}, [validator, url]);
}, [validator, net]);

// TODO: maybe show te version spread as a histogram and feature info ala
// solana --url mainnet-beta feature status
Expand Down
1 change: 1 addition & 0 deletions src/renderer/wallet-adapter/electronAppStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class ElectronAppStorageWalletProvider implements ElectronAppStorageWallet {
transaction: sol.Transaction,
options?: sol.SendOptions
): Promise<{ signature: sol.TransactionSignature }> => {
// TODO: test carefully to see if GetValidatorConnection(net) will work
const connection = new sol.Connection(netToURL(globalNetworkSet.net));

transaction.recentBlockhash = (
Expand Down