-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contract_manager): latency script for entropy v2 (#1494)
* latency script for entropy v2 * add block number difference * correct desc * refactor request randomness * refactor and use chain as arg instead of contract * unnecessary condition * js doc * correct desc * use blockhash
- Loading branch information
Dev Kalra
authored
Apr 23, 2024
1 parent
c7883c8
commit 4b8b9bf
Showing
4 changed files
with
149 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import yargs from "yargs"; | ||
import { hideBin } from "yargs/helpers"; | ||
import { toPrivateKey } from "../src"; | ||
import { | ||
COMMON_DEPLOY_OPTIONS, | ||
findEntropyContract, | ||
findEvmChain, | ||
} from "./common"; | ||
import Web3 from "web3"; | ||
|
||
const parser = yargs(hideBin(process.argv)) | ||
.usage( | ||
"Requests a random number from an entropy contract and measures the\n" + | ||
"latency between request submission and fulfillment by the Fortuna keeper service.\n" + | ||
"Usage: $0 --chain-id <chain-id> --private-key <private-key>" | ||
) | ||
.options({ | ||
chain: { | ||
type: "string", | ||
demandOption: true, | ||
desc: "test latency for the contract on this chain", | ||
}, | ||
"private-key": COMMON_DEPLOY_OPTIONS["private-key"], | ||
}); | ||
|
||
async function main() { | ||
const argv = await parser.argv; | ||
|
||
const chain = findEvmChain(argv.chain); | ||
const contract = findEntropyContract(chain); | ||
|
||
const provider = await contract.getDefaultProvider(); | ||
const userRandomNumber = contract.generateUserRandomNumber(); | ||
const privateKey = toPrivateKey(argv.privateKey); | ||
const requestResponse = await contract.requestRandomness( | ||
userRandomNumber, | ||
provider, | ||
privateKey, | ||
true // with callback | ||
); | ||
console.log(`Request tx hash : ${requestResponse.transactionHash}`); | ||
// Read the sequence number for the request from the transaction events. | ||
const sequenceNumber = | ||
requestResponse.events.RequestedWithCallback.returnValues.sequenceNumber; | ||
console.log(`sequence : ${sequenceNumber}`); | ||
|
||
const startTime = Date.now(); | ||
|
||
let fromBlock = requestResponse.blockNumber; | ||
const web3 = new Web3(contract.chain.getRpcUrl()); | ||
const entropyContract = contract.getContract(); | ||
|
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const currentBlock = await web3.eth.getBlockNumber(); | ||
|
||
if (fromBlock > currentBlock) { | ||
continue; | ||
} | ||
|
||
const events = await entropyContract.getPastEvents("RevealedWithCallback", { | ||
fromBlock: fromBlock, | ||
toBlock: currentBlock, | ||
}); | ||
fromBlock = currentBlock + 1; | ||
|
||
const event = events.find( | ||
(event) => event.returnValues.request[1] == sequenceNumber | ||
); | ||
|
||
if (event !== undefined) { | ||
console.log(`Random number : ${event.returnValues.randomNumber}`); | ||
const endTime = Date.now(); | ||
console.log(`Fortuna Latency : ${endTime - startTime}ms`); | ||
console.log( | ||
`Revealed after : ${ | ||
currentBlock - requestResponse.blockNumber | ||
} blocks` | ||
); | ||
break; | ||
} | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 300)); | ||
} | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters