Skip to content

Commit

Permalink
Merge branch 'staging/3.2.6' into feature/generated-stuff-out-of-git
Browse files Browse the repository at this point in the history
Signed-off-by: Anson <[email protected]>
  • Loading branch information
Ansonhkg authored Mar 12, 2024
2 parents e3913e6 + d1439b1 commit 3c17e53
Show file tree
Hide file tree
Showing 36 changed files with 497 additions and 194 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ storage.test.db

.nx

.yalc
.yalc
60 changes: 30 additions & 30 deletions README.md

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions e2e-nodejs/0_manual-tests/test-epoch-cache.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { LitNodeClient } from '@lit-protocol/lit-node-client';
import crypto from '../polyfills.mjs';

if (typeof globalThis.crypto === 'undefined') {
globalThis.crypto = crypto;
}

globalThis.litConfig = {
debug: true,
};

const DATA_TO_SIGN = new Uint8Array(
await crypto.subtle.digest('SHA-256', new TextEncoder().encode('Hello world'))
);

export async function main() {
// ==================== Test Logic ====================
const litNodeClient = new LitNodeClient({
litNetwork: 'cayenne',
debug: true,
});

await litNodeClient.connect();

// for every second, get new epoch number
setInterval(async () => {
const epochNumber = await litNodeClient.getCurrentEpochNumber();
console.log('epochNumber:', epochNumber);
}, 1000);
}

main();
4 changes: 2 additions & 2 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useNx": true,
"useWorkspaces": true,
"version": "3.2.3"
}
"version": "3.2.6"
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@
"@nx/jest": "17.3.0",
"@nx/js": "17.3.0",
"@nx/linter": "17.3.0",
"@nx/next": "17.3.0",
"@nx/node": "17.3.0",
"@nx/plugin": "17.3.0",
"@nx/react": "17.3.0",
"@nx/web": "17.3.0",
Expand Down Expand Up @@ -200,7 +198,9 @@
"ts-jest": "29.1.2",
"ts-node": "10.9.1",
"typedoc": "^0.23.10",
"typescript": "~4.7.2"
"typescript": "~4.7.2",
"@nx/next": "17.3.0",
"@nx/node": "17.3.0"
},
"workspaces": [
"packages/*"
Expand Down
2 changes: 1 addition & 1 deletion packages/access-control-conditions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"tags": [
"universal"
],
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/auth-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"tags": [
"browser"
],
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/auth-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"crypto": false,
"stream": false
},
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/bls-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"buildOptions": {
"genReact": false
},
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/constants/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"tags": [
"universal"
],
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
3 changes: 2 additions & 1 deletion packages/constants/src/lib/constants/autogen_internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Last Modified: 2024-02-24 04:15:54
// This file is auto-generated by tools/scripts/gen-internal-dev.mjs
export const INTERNAL_DEV = [
'https://167.114.17.205:443',
Expand Down Expand Up @@ -26,4 +27,4 @@ export const INTERNAL_DEFAULT_CONFIG = {
],
litNetwork: 'internalDev',
connectTimeout: 20000,
};
};
2 changes: 1 addition & 1 deletion packages/constants/src/lib/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = '3.2.3';
export const version = '3.2.6';
2 changes: 1 addition & 1 deletion packages/contracts-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"tags": [
"universal"
],
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lit-protocol/core",
"version": "3.2.3",
"version": "3.2.5",
"type": "commonjs",
"license": "MIT",
"homepage": "https://github.com/Lit-Protocol/js-sdk",
Expand Down
96 changes: 88 additions & 8 deletions packages/core/src/lib/lit-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ import {
import { ethers } from 'ethers';
import { LitContracts } from '@lit-protocol/contracts-sdk';

export const DELAY_BEFORE_NEXT_EPOCH = 30000;
// export const MAX_CACHE_AGE = 30000;
export class LitCore {
config: LitNodeClientConfig;
connectedNodes: SetConstructor | Set<any> | any;
Expand All @@ -86,6 +88,16 @@ export class LitCore {
lastBlockHashRetrieved: number | null;
networkSyncInterval: any | null;

private epochChangeListenerSet = false;

epochCache: {
number: number | null;
lastUpdateTime: number | null;
} = {
number: null,
lastUpdateTime: null,
};

// ========== Constructor ==========
constructor(args: any[LitNodeClientConfig | CustomNetwork | any]) {
const customConfig = args;
Expand Down Expand Up @@ -294,6 +306,12 @@ export class LitCore {
* @returns {Promise<void>} A promise that resolves when the listener is successfully set up.
*/
listenForNewEpoch = async (): Promise<void> => {
// Check if we've already set up the listener to avoid duplicates
if (this.epochChangeListenerSet) {
log('Epoch change listener already set.');
return;
}

if (
this.config.litNetwork === LitNetwork.Manzano ||
this.config.litNetwork === LitNetwork.Habanero ||
Expand All @@ -305,10 +323,17 @@ export class LitCore {
);
log(
'listening for state change on staking contract: ',
stakingContract.address
stakingContract['address']
);

stakingContract.on('StateChanged', async (state: StakingStates) => {
// (epoch) step 2: listen for epoch changes and update the cache accordingly, with a 30-second delay for using the new epoch number
setTimeout(async () => {
const newEpochNumber = await this.getCurrentEpochNumber();
this.updateEpochCache(newEpochNumber);

}, DELAY_BEFORE_NEXT_EPOCH);

log(`New state detected: "${state}"`);
if (state === StakingStates.NextValidatorSetLocked) {
log(
Expand Down Expand Up @@ -348,7 +373,13 @@ export class LitCore {
);
}
}

const newEpochNumber = await this.getCurrentEpochNumber();
this.updateEpochCache(newEpochNumber);
});

// Mark that we've set up the listener
this.epochChangeListenerSet = true;
}
};

Expand Down Expand Up @@ -390,7 +421,12 @@ export class LitCore {
connect = async (): Promise<any> => {
// -- handshake with each node
await this.setNewConfig();

// (epoch) step 1: Initialize epoch number cache
this.epochCache.number = await this.getCurrentEpochNumber();
this.epochCache.lastUpdateTime = Date.now();
await this.listenForNewEpoch();

await this._runHandshakeWithBootstrapUrls();
};

Expand Down Expand Up @@ -604,13 +640,10 @@ export class LitCore {
const now = Date.now();
if (now - startTime > this.config.connectTimeout) {
clearInterval(interval);
const msg = `Error: Could not connect to enough nodes after timeout of ${
this.config.connectTimeout
}ms. Could only connect to ${
Object.keys(this.serverKeys).length
} of ${
this.config.bootstrapUrls.length
} required nodes. Please check your network connection and try again. Note that you can control this timeout with the connectTimeout config option which takes milliseconds.`;
const msg = `Error: Could not connect to enough nodes after timeout of ${this.config.connectTimeout
}ms. Could only connect to ${Object.keys(this.serverKeys).length
} of ${this.config.minNodeCount
} required nodes. Please check your network connection and try again. Note that you can control this timeout with the connectTimeout config option which takes milliseconds.`;
logErrorWithRequestId(requestId, msg);
reject(msg);
}
Expand Down Expand Up @@ -696,6 +729,49 @@ export class LitCore {
return res as NodeCommandServerKeysResponse;
};

updateEpochCache = async (epochNumber: number): Promise<void> => {
this.epochCache.number = epochNumber;
this.epochCache.lastUpdateTime = Date.now();
};

getCurrentEpochNumber = async (): Promise<number> => {
try {
// (epoch) step 3: first check the cache and use the cached value if it's not more than 30 seconds old. If it's a cache miss or the cached value is too old, fall back to fetching the current epoch number
// const now = Date.now();
// const cacheAge = now - this.epochCache.lastUpdateTime!;

// if (
this.epochCache.number !== null
// && cacheAge <= MAX_CACHE_AGE
// ) {
// log('Using cached epoch number', this.epochCache.number);
// return this.epochCache.number;
// }

// Cache miss or cached value too old, fetch new epoch number
const stakingContract = await LitContracts.getStakingContract(
this.config.litNetwork as any,
this.config.contractContext
);

log('Fetching current epoch number');
const epoch = await stakingContract['epoch']();
const epochNumber = epoch.number.toNumber();

// Update the cache
// this.epochCache.number = epochNumber;
// this.epochCache.lastUpdateTime = Date.now();

return epochNumber;
} catch (error) {
return throwError({
message: `Error getting current epoch number: ${error}`,
errorKind: LIT_ERROR.UNKNOWN_ERROR.kind,
errorCode: LIT_ERROR.UNKNOWN_ERROR.name,
});
}
};

// ==================== SENDING COMMAND ====================
/**
*
Expand All @@ -711,6 +787,10 @@ export class LitCore {
data,
requestId,
}: SendNodeCommand): Promise<any> => {
const epochNumber = await this.getCurrentEpochNumber();

data = { ...data, epochNumber };

logWithRequestId(
requestId,
`sendCommandToNode with url ${url} and data`,
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"tags": [
"universal"
],
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/ecdsa-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"tags": [
"universal"
],
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/encryption/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"crypto": false,
"stream": false
},
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/lit-auth-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lit-protocol/lit-auth-client",
"version": "3.2.3",
"version": "3.2.5",
"type": "commonjs",
"license": "MIT",
"homepage": "https://github.com/Lit-Protocol/js-sdk",
Expand Down
2 changes: 1 addition & 1 deletion packages/lit-node-client-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"tags": [
"nodejs"
],
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/lit-node-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"crypto": false,
"stream": false
},
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/logger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lit-protocol/logger",
"version": "3.2.3",
"version": "3.2.5",
"type": "commonjs",
"tags": [
"universal"
Expand Down
2 changes: 1 addition & 1 deletion packages/misc-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"tags": [
"browser"
],
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/misc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"tags": [
"universal"
],
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/nacl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"access": "public",
"directory": "../../dist/packages/nacl"
},
"version": "3.2.3",
"version": "3.2.5",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
Loading

0 comments on commit 3c17e53

Please sign in to comment.