Skip to content

Commit

Permalink
Merge pull request #515 from LIT-Protocol/feature/3404-datil-dev-support
Browse files Browse the repository at this point in the history
Feature/3404 datil dev support
  • Loading branch information
MaximusHaximus authored Jul 8, 2024
2 parents d4b488b + 51ca628 commit 31ff682
Show file tree
Hide file tree
Showing 100 changed files with 1,074 additions and 639 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div align="center">
<h1>Lit Protocol Javascript/Typescript SDK V6.0.0</h1>
<h1>Lit Protocol Javascript/Typescript SDK V6.1.x</h1>

<img src="https://i.ibb.co/p2xfzK1/Screenshot-2022-11-15-at-09-56-57.png">
<br/>
Expand Down
2 changes: 1 addition & 1 deletion 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": "6.0.5"
"version": "6.1.0-beta.4"
}
35 changes: 18 additions & 17 deletions local-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,27 @@ Below is the API documentation for the `ProcessEnvs` interface, detailing the co

**NOTE: a `.env.sample` is contained in the repository root for the below env tables**

| Variable | Description |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `MAX_ATTEMPTS` | Each test is executed in a loop with a maximum number of attempts specified by `devEnv.processEnvs.MAX_ATTEMPTS`. |
| `NETWORK` | The network to use for testing, which can be one of the following: `LIT_TESTNET.LOCALCHAIN`, `LIT_TESTNET.MANZANO`, or `LIT_TESTNET.CAYENNE`. |
| `DEBUG` | Specifies whether to enable debug mode. |
| `REQUEST_PER_KILOSECOND` | To execute a transaction with Lit, you must reserve capacity on the network using Capacity Credits. These allow a set number of requests over a period (default 2 days). |
| `WAIT_FOR_KEY_INTERVAL` | Wait time in milliseconds if no private keys are available. |
| `TIME_TO_RELEASE_KEY` | Time to wait before releasing the key after requesting it. |
| `RUN_IN_BAND` | Run all tests in a single thread. |
| `RUN_IN_BAND_INTERVAL` | The interval in milliseconds to run the tests in a single thread. |
| `LIT_RPC_URL` | The URL of the Lit RPC server. If running locally on Anvil, it should be 'http://127.0.0.1:8545'. |
| `LIT_OFFICIAL_RPC` | The URL of the official Lit RPC server, usually 'https://chain-rpc.litprotocol.com/http' but can be changed if needed. |
| `USE_SHIVA` | A flag to determine if `Shiva` should be used for the `localchain` network. |
| `PRIVATE_KEYS` | A set of private keys to use which will be used to perform chain operations. |

Below is te API Documentation forthe `ProccessEnvs` interface for the `shiva-client` detailing the configurable enviorment variables and their purposes:
| Variable | Description |
| ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `MAX_ATTEMPTS` | Each test is executed in a loop with a maximum number of attempts specified by `devEnv.processEnvs.MAX_ATTEMPTS`. |
| `NETWORK` | The network to use for testing, which can be one of the following: `LIT_TESTNET.LOCALCHAIN`, `LIT_TESTNET.MANZANO`, or `LIT_TESTNET.CAYENNE`. |
| `DEBUG` | Specifies whether to enable debug mode. |
| `REQUEST_PER_KILOSECOND` | To execute a transaction with Lit, you must reserve capacity on the network using Capacity Credits. These allow a set number of requests over a period (default 2 days). |
| `WAIT_FOR_KEY_INTERVAL` | Wait time in milliseconds if no private keys are available. |
| `TIME_TO_RELEASE_KEY` | Time to wait before releasing the key after requesting it. |
| `RUN_IN_BAND` | Run all tests in a single thread. |
| `RUN_IN_BAND_INTERVAL` | The interval in milliseconds to run the tests in a single thread. |
| `LIT_RPC_URL` | The URL of the Lit RPC server: <br> - For local Anvil: `http://127.0.0.1:8545` <br> - For Chronicle: `https://chain-rpc.litprotocol.com/http` <br> - For Vesuvius: `https://vesuvius-rpc.litprotocol.com` |
| `STOP_TESTNET` | Flag to stop a single running testnet after the test run concludes. |
| `USE_SHIVA` | A flag to determine if `Shiva` should be used for the `localchain` network. |
| `PRIVATE_KEYS` | A set of private keys to use which will be used to perform chain operations. |
| `CHUNK_SIZE` | Determines the number of tests run concurrently during parallel execution |

Below is the API Documentation for the `ProccessEnvs` interface for the `shiva-client` detailing the configurable environment variables and their purposes:

| Variable | Description |
| ------------------------ | ----------------------------------------------------------------------------------------------------------- |
| `TESTNET_MAANGER_URL` | URl to connect to Shiva (our testing tool for network management). |
| `TESTNET_MANANGER_URL` | URl to connect to Shiva (our testing tool for network management). |
| `LIT_NODE_BINARY_PATH` | Binary path for the lit node version you wish to run. |
| `LIT_Action_BINARY_PATH` | Binary path for the lit node version you wish to run. |
| `USE_LIT_BINARIES` | Flag to indicate if a binary path should be used for testnet spawning or if it should be built from source. |
Expand Down
8 changes: 4 additions & 4 deletions local-tests/setup/shiva-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export interface ShivaEnvs {

/**
* Client implementation for a single testnet instance managed by the Shiva tool
* Is essentially a localchain setup but allows for programmatic operations to be preformed
* on the network from the implementation wthin this class. Each testnet is a unique network
* Is essentially a localchain setup but allows for programmatic operations to be performed
* on the network from the implementation within this class. Each testnet is a unique network
*/
export class TestnetClient {
private _id: string;
Expand Down Expand Up @@ -173,7 +173,7 @@ export class TestnetClient {
if (res.status === 200) {
return res.json();
} else {
return Promise.reject(res);
throw res;
}
})
.then((body: TestNetResponse<boolean>) => {
Expand Down Expand Up @@ -216,7 +216,7 @@ export class ShivaClient {

constructor() {
this._clients = new Map();
console.log('Shiva enviorment loaded current config: ', this.processEnvs);
console.log('Shiva environment loaded current config: ', this.processEnvs);
}

/**
Expand Down
28 changes: 22 additions & 6 deletions local-tests/setup/tinny-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,25 @@ export enum LIT_TESTNET {
LOCALCHAIN = 'localchain',
MANZANO = 'manzano',
CAYENNE = 'cayenne',
DATIL_DEV = 'datil-dev',
}

export enum LIT_RPC {
LOCAL_ANVIL = 'http://127.0.0.1:8545',
CHRONICLE = 'https://chain-rpc.litprotocol.com/http',
VESUVIUS = 'https://vesuvius-rpc.litprotocol.com',
}

/**
* Mapping of testnet names to corresponding RPC endpoints.
*/
export const RPC_MAP = {
[LIT_TESTNET.LOCALCHAIN]: LIT_RPC.LOCAL_ANVIL,
[LIT_TESTNET.MANZANO]: LIT_RPC.CHRONICLE,
[LIT_TESTNET.CAYENNE]: LIT_RPC.CHRONICLE,
[LIT_TESTNET.DATIL_DEV]: LIT_RPC.VESUVIUS,
};

export interface ProcessEnvs {
/**
* Each test is executed in a loop with a maximum number of attempts specified by `devEnv.processEnvs.MAX_ATTEMPTS`.
Expand All @@ -18,6 +35,7 @@ export interface ProcessEnvs {
* - `LIT_TESTNET.LOCALCHAIN`
* - `LIT_TESTNET.MANZANO`
* - `LIT_TESTNET.CAYENNE`
* - `LIT_TESTNET.DATIL_DEV`
*/
NETWORK: LIT_TESTNET;

Expand Down Expand Up @@ -53,15 +71,13 @@ export interface ProcessEnvs {

// =========== In most cases you won't need to change the following values ===========
/**
* The URL of Lit RPC server. If it's running locally on Anvil, it should be 'http://127.0.0.1:8545'
* The URL of Lit RPC server.
* - If it's running locally on Anvil, it should be 'http://127.0.0.1:8545'
* - If it's running on Chronicle, it should be 'https://chain-rpc.litprotocol.com/http'
* - If it's running on Vesuvius, it should be 'https://vesuvius-rpc.litprotocol.com'
*/
LIT_RPC_URL: string;

/**
* The URL of the official Lit RPC server. Usually 'https://chain-rpc.litprotocol.com/http' but can be changed if needed
*/
LIT_OFFICIAL_RPC: string;

/**
* This is usually used when you're running tests locally depending how many nodes you are running.
*/
Expand Down
122 changes: 82 additions & 40 deletions local-tests/setup/tinny-environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { LIT_TESTNET, ProcessEnvs, TinnyEnvConfig } from './tinny-config';
import {
LIT_TESTNET,
ProcessEnvs,
RPC_MAP,
TinnyEnvConfig,
} from './tinny-config';
import { LitNodeClient } from '@lit-protocol/lit-node-client';
import { LitContracts } from '@lit-protocol/contracts-sdk';
import {
Expand Down Expand Up @@ -29,24 +34,20 @@ export class TinnyEnvironment {
DEBUG: process.env['DEBUG'] === 'true',
REQUEST_PER_KILOSECOND:
parseInt(process.env['REQUEST_PER_KILOSECOND']) || 200,
LIT_RPC_URL: process.env['LIT_RPC_URL'] || 'http://127.0.0.1:8545',
LIT_RPC_URL: process.env['LIT_RPC_URL'],
WAIT_FOR_KEY_INTERVAL:
parseInt(process.env['WAIT_FOR_KEY_INTERVAL']) || 3000,
BOOTSTRAP_URLS: process.env['BOOTSTRAP_URLS']?.split(',') || [
'http://127.0.0.1:7470',
'http://127.0.0.1:7471',
'http://127.0.0.1:7472',
],
LIT_OFFICIAL_RPC:
process.env['LIT_OFFICIAL_RPC'] ||
'https://chain-rpc.litprotocol.com/http',
TIME_TO_RELEASE_KEY: parseInt(process.env['TIME_TO_RELEASE_KEY']) || 10000,
RUN_IN_BAND: process.env['RUN_IN_BAND'] === 'true',
RUN_IN_BAND_INTERVAL: parseInt(process.env['RUN_IN_BAND_INTERVAL']) || 5000,

// Available Accounts
// ==================
// (0) "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" (10000.000000000000000000 ETH)
// (1) "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" (10000.000000000000000000 ETH)
// (2) "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC" (10000.000000000000000000 ETH)
// (3) "0x90F79bf6EB2c4f870365E785982E1f101E93b906" (10000.000000000000000000 ETH)
Expand Down Expand Up @@ -117,10 +118,22 @@ export class TinnyEnvironment {
).fill(false);

// -- setup rpc
if (this.network === LIT_TESTNET.LOCALCHAIN) {
// Priority:
// 1. Use environment variable if set
// 2. Use RPC_MAP if network is recognized
// 3. Throw error if neither condition is met
if (this.processEnvs.LIT_RPC_URL) {
// If LIT_RPC_URL is set in the environment, use it
this.rpc = this.processEnvs.LIT_RPC_URL;
} else if (this.network in RPC_MAP) {
// If the network is recognized in RPC_MAP, use the corresponding RPC URL
this.rpc = RPC_MAP[this.network];
} else {
this.rpc = this.processEnvs.LIT_OFFICIAL_RPC;
// If neither condition is met, throw an error with available options
const availableNetworks = Object.keys(RPC_MAP).join(', ');
throw new Error(
`No RPC URL found for network "${this.network}". Available networks are: ${availableNetworks}`
);
}

console.log(
Expand Down Expand Up @@ -213,7 +226,7 @@ export class TinnyEnvironment {
this?.testnet?.ContractContext ?? this._contractContext;
this.litNodeClient = new LitNodeClient({
litNetwork: 'custom',
rpcUrl: this.processEnvs.LIT_RPC_URL,
rpcUrl: this.rpc,
debug: this.processEnvs.DEBUG,
checkNodeAttestation: false, // disable node attestation check for local testing
contractContext: networkContext,
Expand Down Expand Up @@ -338,7 +351,7 @@ export class TinnyEnvironment {
let state = await this.testnet.pollTestnetForActive();
if (state === `UNKNOWN`) {
console.log(
'Testnet state found to be Unknown meannig there was an error with testnet creation. shutting downn'
'Testnet state found to be Unknown meaning there was an error with testnet creation. shutting down'
);
throw new Error(`Error while creating testnet, aborting test run`);
}
Expand Down Expand Up @@ -434,22 +447,34 @@ export class TinnyEnvironment {
this.contractsClient = new LitContracts({
signer: wallet,
debug: this.processEnvs.DEBUG,
rpc: this.processEnvs.LIT_RPC_URL, // anvil rpc
rpc: this.rpc,
customContext: networkContext,
});
} else {
// TODO: This wallet should be cached somehwere and reused to create delegation signatures.
// There is a correlation between the number of Capacity Credit NFTs in a wallet and the speed at which nodes can verify a given rate limit authorization. Creating a single wallet to hold all Capacity Credit NFTs improves network performance during tests.
const capacityCreditWallet =
ethers.Wallet.createRandom().connect(provider);

const transferTx = await wallet.sendTransaction({
to: capacityCreditWallet.address,
value: ethers.utils.parseEther('0.001'),
});
await transferTx.wait();
async function _switchWallet() {
// TODO: This wallet should be cached somehwere and reused to create delegation signatures.
// There is a correlation between the number of Capacity Credit NFTs in a wallet and the speed at which nodes can verify a given rate limit authorization. Creating a single wallet to hold all Capacity Credit NFTs improves network performance during tests.
const capacityCreditWallet =
ethers.Wallet.createRandom().connect(provider);

// get wallet balance
const balance = await wallet.getBalance();
console.log('this.rpc:', this.rpc);
console.log('this.wallet.address', wallet.address);
console.log('Balance:', balance.toString());

const transferTx = await wallet.sendTransaction({
to: capacityCreditWallet.address,
value: ethers.utils.parseEther('0.001'),
});
await transferTx.wait();
}

// await _switchWallet();

this.contractsClient = new LitContracts({
signer: capacityCreditWallet,
// signer: capacityCreditWallet, // disabled switch wallet for now
signer: wallet,
debug: this.processEnvs.DEBUG,
network: this.network,
});
Expand All @@ -462,23 +487,40 @@ export class TinnyEnvironment {
* Mint a Capacity Credits NFT and get a capacity delegation authSig with it
* ====================================
*/
console.log(
'[𐬺🧪 Tinny Environment𐬺] Mint a Capacity Credits NFT and get a capacity delegation authSig with it'
);
const capacityTokenId = (
await this.contractsClient.mintCapacityCreditsNFT({
requestsPerKilosecond: this.processEnvs.REQUEST_PER_KILOSECOND,
daysUntilUTCMidnightExpiration: 2,
})
).capacityTokenIdStr;

this.superCapacityDelegationAuthSig = (
await this.litNodeClient.createCapacityDelegationAuthSig({
dAppOwnerWallet: wallet,
capacityTokenId: capacityTokenId,
// Sets a maximum limit of 200 times that the delegation can be used and prevents usage beyond it
uses: '200',
})
).capacityDelegationAuthSig;

// Disabled for now
async function _mintSuperCapacityDelegationAuthSig() {
console.log(
'[𐬺🧪 Tinny Environment𐬺] Mint a Capacity Credits NFT and get a capacity delegation authSig with it'
);
try {
const capacityTokenId = (
await this.contractsClient.mintCapacityCreditsNFT({
requestsPerKilosecond: this.processEnvs.REQUEST_PER_KILOSECOND,
daysUntilUTCMidnightExpiration: 2,
})
).capacityTokenIdStr;

this.superCapacityDelegationAuthSig = (
await this.litNodeClient.createCapacityDelegationAuthSig({
dAppOwnerWallet: wallet,
capacityTokenId: capacityTokenId,
// Sets a maximum limit of 200 times that the delegation can be used and prevents usage beyond it
uses: '200',
})
).capacityDelegationAuthSig;
} catch (e: any) {
if (
e.message.includes(`Can't allocate capacity beyond the global max`)
) {
console.log('❗️Skipping capacity delegation auth sig setup.', e);
} else {
console.log(
'❗️Error while setting up capacity delegation auth sig',
e
);
}
}
}
};
}
Loading

0 comments on commit 31ff682

Please sign in to comment.