Skip to content

Commit

Permalink
Massively removing any-kwyword usage from TypeScript code
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiy-skalelabs committed Mar 4, 2024
1 parent bd9b10a commit 07b96bd
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 113 deletions.
265 changes: 174 additions & 91 deletions src/bls.ts

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions src/clpTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import * as loop from "./loop.js";
import * as imaUtils from "./utils.js";
import * as imaBLS from "./bls.js";
import type * as imaTx from "./imaTx.js";
import type * as rpcCallFormats from "./rpcCallFormats.js";

export async function registerAll( isPrintSummaryRegistrationCosts: boolean ): Promise < boolean > {
if( !await registerStep1( false ) )
Expand Down Expand Up @@ -1643,7 +1644,10 @@ export function commandLineTaskLoopSimple(): void {
}

async function handleBrowseSkaleModesRpcInfoResult(
strLogPrefix: string, joCall: rpcCall.TRPCCall, joIn: any, joOut: any
strLogPrefix: string, joCall:
rpcCall.TRPCCall,
joIn: rpcCallFormats.TRPCInputBasicFieldsWithParams,
joOut: rpcCallFormats.TRPCOutputBasicFields
): Promise<void> {
const imaState: state.TIMAState = state.get();
log.information( "{p}S-Chain network information: {}",
Expand All @@ -1664,7 +1668,8 @@ async function handleBrowseSkaleModesRpcInfoResult(
joCall = await rpcCall.create( strNodeURL, rpcCallOpts );
if( !joCall )
throw new Error( `Failed to create JSON RPC call object to ${strNodeURL}` );
const jIn: any = { method: "skale_imaInfo", params: { } };
const jIn: rpcCallFormats.TRPCInputBasicFieldsWithParams =
{ method: "skale_imaInfo", params: { } };
if( discoveryTools.isSendImaAgentIndex() )
jIn.params.fromImaAgentIndex = imaState.nNodeNumber;
const joOut = await joCall.call( joIn );
Expand Down Expand Up @@ -1708,7 +1713,8 @@ export function commandLineTaskBrowseSChain(): void {
throw new Error( "Failed to create JSON RPC call object " +
`to ${imaState.chainProperties.sc.strURL}` );
}
const joIn: any = { method: "skale_nodesRpcInfo", params: { } };
const joIn: rpcCallFormats.TRPCInputBasicFieldsWithParams =
{ method: "skale_nodesRpcInfo", params: { } };
if( discoveryTools.isSendImaAgentIndex() )
joIn.params.fromImaAgentIndex = imaState.nNodeNumber;
const joOut = await joCall.call( joIn );
Expand Down
36 changes: 27 additions & 9 deletions src/discoveryTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ import * as imaUtils from "./utils.js";
import * as threadInfo from "./threadInfo.js";
import * as owaspUtils from "./owaspUtils.js";
import type * as clpTools from "./clpTools.js";
import type * as rpcCallFormats from "./rpcCallFormats.js";

export interface TRPCOutputFieldsDiscoverSkaleImaInfoResult {
result: TNodeImaInfo
error?: string
}

export interface TRPCOutputFieldsDiscoverSChainNetworkInfo {
result: TSChainNetworkInfo
error?: string
}

export type TFunctionAfterDiscovery = (
err?: Error | string | null,
Expand Down Expand Up @@ -447,7 +458,9 @@ export async function continueSChainDiscoveryInBackgroundIfNeeded(

function handleDiscoverSkaleImaInfoResult(
optsDiscover: TDiscoveryOptions, strNodeDescColorized: string,
joNode: any, joCall: rpcCall.TRPCCall, joIn: any, joOut: any
joNode: TSChainNode, joCall: rpcCall.TRPCCall,
joIn: rpcCallFormats.TRPCInputBasicFieldsWithParams,
joOut: TRPCOutputFieldsDiscoverSkaleImaInfoResult
): void {
joNode.imaInfo = joOut.result;
if( isSChainNodeFullyDiscovered( joNode ) )
Expand All @@ -463,7 +476,7 @@ async function discoverSChainWalkNodes( optsDiscover: TDiscoveryOptions ): Promi
optsDiscover.cntFailed = 0;
for( let i = 0; i < optsDiscover.cntNodes; ++i ) {
const nCurrentNodeIdx = owaspUtils.toInteger( i );
const joNode = optsDiscover.jarrNodes[nCurrentNodeIdx];
const joNode: TSChainNode = optsDiscover.jarrNodes[nCurrentNodeIdx];
const strNodeURL = imaUtils.composeSChainNodeUrl( joNode );
const strNodeDescColorized = log.fmtAttention( "#{}({url})", nCurrentNodeIdx, strNodeURL );
if( !optsDiscover.isSilentReDiscovery ) {
Expand Down Expand Up @@ -493,10 +506,11 @@ async function discoverSChainWalkNodes( optsDiscover: TDiscoveryOptions ): Promi
joCall = await rpcCall.create( strNodeURL, rpcCallOpts );
if( !joCall )
throw new Error( `Failed to create JSON RPC call object to ${strNodeURL}` );
const joIn: any = { method: "skale_imaInfo", params: { } };
const joIn: rpcCallFormats.TRPCInputBasicFieldsWithParams =
{ method: "skale_imaInfo", params: { } };
if( isSendImaAgentIndex() )
joIn.params.fromImaAgentIndex = optsDiscover.imaState.nNodeNumber;
const joOut = await joCall.call( joIn );
const joOut: TRPCOutputFieldsDiscoverSkaleImaInfoResult = await joCall.call( joIn );
handleDiscoverSkaleImaInfoResult(
optsDiscover, strNodeDescColorized, joNode, joCall, joIn, joOut );
} catch ( err ) {
Expand Down Expand Up @@ -582,7 +596,10 @@ async function discoverSChainWait( optsDiscover: TDiscoveryOptions ): Promise<vo

async function handleDiscoverSkaleNodesRpcInfoResult(
optsDiscover: TDiscoveryOptions, scURL: string,
joCall: rpcCall.TRPCCall, joIn: any, joOut: any ): Promise<boolean> {
joCall: rpcCall.TRPCCall,
joIn: rpcCallFormats.TRPCInputBasicFieldsWithParams,
joOut: TRPCOutputFieldsDiscoverSChainNetworkInfo
): Promise<boolean> {
if( !optsDiscover.isSilentReDiscovery ) {
log.trace( "{p}OK, got (own) S-Chain network information: {}",
optsDiscover.strLogPrefix, joOut.result );
Expand Down Expand Up @@ -653,12 +670,12 @@ async function handleDiscoverSkaleNodesRpcInfoResult(

export async function discoverSChainNetwork(
fnAfter: TFunctionAfterDiscovery | null, isSilentReDiscovery: boolean,
joPrevSChainNetworkInfo: any, nCountToWait: number
joPrevSChainNetworkInfo: TSChainNetworkInfo | null, nCountToWait: number
): Promise<TSChainNetworkInfo | null> {
const optsDiscover: TDiscoveryOptions = {
fnAfter,
isSilentReDiscovery: ( !!isSilentReDiscovery ),
joPrevSChainNetworkInfo: joPrevSChainNetworkInfo || null,
joPrevSChainNetworkInfo: joPrevSChainNetworkInfo ?? null,
nCountToWait,
imaState: state.get(),
strLogPrefix: "S-Chain network discovery: ",
Expand All @@ -682,10 +699,11 @@ export async function discoverSChainNetwork(
joCall = await rpcCall.create( scURL, rpcCallOpts );
if( !joCall )
throw new Error( `Failed to create JSON RPC call object to ${scURL}` );
const joIn: any = { method: "skale_nodesRpcInfo", params: { } };
const joIn: rpcCallFormats.TRPCInputBasicFieldsWithParams =
{ method: "skale_nodesRpcInfo", params: { } };
if( isSendImaAgentIndex() )
joIn.params.fromImaAgentIndex = optsDiscover.imaState.nNodeNumber;
const joOut = await joCall.call( joIn );
const joOut: TRPCOutputFieldsDiscoverSChainNetworkInfo = await joCall.call( joIn );
await handleDiscoverSkaleNodesRpcInfoResult(
optsDiscover, scURL, joCall, joIn, joOut
).catch( function( err: Error | string ): void {
Expand Down
4 changes: 2 additions & 2 deletions src/imaOracleOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type * as state from "./state.js";
import type * as IMA from "./imaCore.js";

export type TFunctionSignMsgOracle =
( u256: owaspUtils.ethersMod.BigNumber,
( u256: owaspUtils.ethersMod.BigNumber | string,
details: log.TLogger,
fnAfter: IMA.TFunctionAfterSigningMessages
) => Promise <void>;
Expand Down Expand Up @@ -285,7 +285,7 @@ export async function doOracleGasPriceSetup(
optsGasPriceSetup.details.trace( "{p}Using internal u256 signing stub function",
optsGasPriceSetup.strLogPrefix );
optsGasPriceSetup.fnSignMsgOracle =
async function( u256: owaspUtils.ethersMod.BigNumber, details: log.TLogger,
async function( u256: owaspUtils.ethersMod.BigNumber | string, details: log.TLogger,
fnAfter: IMA.TFunctionAfterSigningMessages ): Promise<void> {
details.trace( "{p}u256 signing callback was not provided",
optsGasPriceSetup.strLogPrefix );
Expand Down
13 changes: 11 additions & 2 deletions src/imaSgxExternalSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ import * as fs from "fs";
import * as log from "./log.js";
import * as owaspUtils from "./owaspUtils.js";
import * as rpcCall from "./rpcCall.js";
import type * as rpcCallFormats from "./rpcCallFormats.js";

const gIsDebugLogging = false; // development option only, must be always false
log.addStdout();

// allow self-signed wss and https
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

export interface TRPCInputECDSASignMessageHash extends rpcCallFormats.TRPCInputBasicFields {
params: {
keyName: string
messageHash: string
base: number
}
}

function finalizeOutput( jo: any ): void {
if( !jo )
return;
Expand Down Expand Up @@ -95,15 +104,15 @@ async function run(): Promise<void> {
const joCall: rpcCall.TRPCCall = await rpcCall.create( strSgxWalletURL, rpcCallOpts );
if( !joCall )
throw new Error( `Failed to create JSON RPC call object to ${strSgxWalletURL}` );
const joIn: any = {
const joIn: TRPCInputECDSASignMessageHash = {
method: "ecdsaSignMessageHash",
params: {
keyName: strSgxKeyName.toString(),
messageHash: txHash,
base: 16
}
};
const joOut: any = await joCall.call( joIn );
const joOut = await joCall.call( joIn );
try {
if( gIsDebugLogging )
log.debug( "SGX wallet ECDSA sign result is: {}", joOut );
Expand Down
10 changes: 7 additions & 3 deletions src/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import * as rpcCall from "./rpcCall.js";
import * as threadInfo from "./threadInfo.js";
import * as owaspUtils from "./owaspUtils.js";
import * as sha3Module from "sha3";
import type * as rpcCallFormats from "./rpcCallFormats.js";

const Keccak: any = sha3Module.Keccak;
export const gConstMinPowResultLimit: number = 10000;
export const gConstMaxPowResultLimit: number = 100000;
Expand Down Expand Up @@ -138,7 +140,8 @@ async function handleOracleSubmitRequestResult(
if( nMillisecondsToSleep > 0 )
await threadInfo.sleep( nMillisecondsToSleep );
try {
const joIn: any = { method: "oracle_checkResult", params: [ joOut.result ] };
const joIn: rpcCallFormats.TRPCInputBasicFieldsWithArray =
{ method: "oracle_checkResult", params: [ joOut.result ] };
if( isVerboseTraceDetails ) {
details.debug( "RPC call oracle_checkResult attempt {} " +
"of {}...", idxAttempt, cntAttempts );
Expand Down Expand Up @@ -186,10 +189,11 @@ export async function oracleGetGasPrice(
"\"post\":\"{\\\"jsonrpc\\\":\\\"2.0\\\"," +
"\\\"method\\\":\\\"eth_gasPrice\\\",\\\"params\\\":[],\\\"id\\\":1}\"",
details, isVerbose );
const joIn: any = { method: "oracle_submitRequest", params: [ s ] };
const joIn: rpcCallFormats.TRPCInputBasicFieldsWithArray =
{ method: "oracle_submitRequest", params: [ s ] };
if( isVerboseTraceDetails )
details.debug( "RPC call {} is {}", "oracle_submitRequest", joIn );
const joOut: any = await joCall.call( joIn );
const joOut = await joCall.call( joIn );
gp = await handleOracleSubmitRequestResult(
oracleOpts, details, isVerboseTraceDetails, joCall, joIn, joOut );
await joCall.disconnect();
Expand Down
19 changes: 16 additions & 3 deletions src/pwa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ import * as imaUtils from "./utils.js";
import type * as state from "./state.js";
import type * as discoveryTools from "./discoveryTools.js";
import * as owaspUtils from "./owaspUtils.js";
import type * as rpcCallFormats from "./rpcCallFormats.js";

export interface TRPCInputIMANotifyLoopWork extends rpcCallFormats.TRPCInputBasicFields {
params: {
nNodeNumber: number
strLoopWorkType: string
nIndexS2S: number
isStart: boolean
ts: number
signature: imaBLS.TSignResult | null
}
}

function computeWalkNodeIndices( nNodeNumber: number, nNodesCount: number ): number[] {
if( nNodeNumber === null || nNodeNumber === undefined ||
Expand Down Expand Up @@ -164,7 +176,7 @@ export async function checkOnLoopStart(

export async function handleLoopStateArrived(
imaState: state.TIMAState, nNodeNumber: number, strLoopWorkType: string, nIndexS2S: number,
isStart: boolean, ts: any, signature: any
isStart: boolean, ts: number, signature: any
): Promise<boolean> {
const se = isStart ? "start" : "end";
let isSuccess = false;
Expand Down Expand Up @@ -239,7 +251,8 @@ async function notifyOnLoopImpl(
const strMessageHash = imaBLS.keccak256ForPendingWorkAnalysis(
owaspUtils.toInteger( imaState.nNodeNumber ),
strLoopWorkType, isStart, nUtcUnixTimeStamp );
const signature = await imaBLS.doSignReadyHash( strMessageHash, imaState.isPrintPWA );
const signature: imaBLS.TSignResult | null =
await imaBLS.doSignReadyHash( strMessageHash, imaState.isPrintPWA );
await handleLoopStateArrived(
imaState, imaState.nNodeNumber, strLoopWorkType,
nIndexS2S, isStart, nUtcUnixTimeStamp, signature
Expand All @@ -263,7 +276,7 @@ async function notifyOnLoopImpl(
} );
if( !joCall )
return false;
const joIn: any = {
const joIn: TRPCInputIMANotifyLoopWork = {
method: "skale_imaNotifyLoopWork",
params: {
nNodeNumber: owaspUtils.toInteger( imaState.nNodeNumber ),
Expand Down
20 changes: 20 additions & 0 deletions src/rpcCallFormats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export interface TRPCInputBasicFields {
method: string
jsonrpc?: string
id?: string | number
}

export interface TRPCInputBasicFieldsWithParams extends TRPCInputBasicFields {
params: {
fromImaAgentIndex?: number
}
}

export interface TRPCInputBasicFieldsWithArray extends TRPCInputBasicFields {
params: any[]
}

export interface TRPCOutputBasicFields {
result?: any
error?: string
}
21 changes: 21 additions & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ export type TAddress = string;
export type TBalance = owaspUtils.ethersMod.BigNumber;
export type TTokenID = string;

export interface TQAInformation {
skaledNumber: number
sequenceId: string
ts: string
}

export interface TIMAMessage {
sender: TAddress
destinationContract: TAddress
data: string
savedBlockNumberForOptimizations?: number
}

export interface TIMAOutgoingMessage {
dstChainHash: string
msgCounter: number
srcContract: TAddress
dstContract: TAddress
data: string
}

export interface TLoopStateSubPart {
isInProgress: boolean
wasInProgress: boolean
Expand Down

0 comments on commit 07b96bd

Please sign in to comment.