-
Notifications
You must be signed in to change notification settings - Fork 157
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
feat(genLocalState) - debugging utility for log fetching and local maci state generation #758
Merged
+2,653
−29,223
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
03e4aa7
feat(fetch-logs) - add back fetch log and adapt to MACI v1
ctrlc03 bd0f964
feat(fetch logs) - update genMaciState to fetch logs in batches
ctrlc03 3d3d52e
refactor(fetchlogs) - remove fetchlogs in favor of genMaciState
ctrlc03 327e2ea
fix(logs) - do not make start block mandatory
ctrlc03 962eea4
fix(fetchlogs) - ensure we set a end block for fetching logs
ctrlc03 7267aa2
fix(fetchlogs) - ensure we are fetching logs coming from our desired …
ctrlc03 cc69e46
fix(contracts) - fix bug in genMaciState causing duplicate log fetching
ctrlc03 e9acf83
feat(fetchlogs) - ensure to use cli parameters to determine blocks pe…
ctrlc03 887dc59
feat(fetchlogs) - add maci state serialization
ctrlc03 ae72108
feat(fetchlogs) - add toJSON method to various domainobjs classes so …
ctrlc03 8788fd7
feat(fetchlogs) - add more serialization and deserialization methods
ctrlc03 f19c5fd
feat(fetchlogs) - ensure Poll equals function returns true after dese…
ctrlc03 530d7cb
feat(fetchlogs) - fix wrong deserialization of Keypair objects and en…
ctrlc03 a1a62ac
feat(fetchlogs) - ensure that we are deserializing the Command object…
ctrlc03 80255e6
feat(genLocalState) - add e2e test, add sleep feature to prevent rate…
ctrlc03 e9a4d36
feat(fetchlogs) - update documentation to show usage of the new commands
ctrlc03 f7eb5a3
feat(genLocalState) - ensure we are deleting the test file in the cor…
ctrlc03 f67b56f
feat(genLocalState) - fix issues with package-lock files
ctrlc03 ff73fd1
feat(genLocalState) - ensure we are passing the correct arguments to …
ctrlc03 b0d0d0d
feat(genLocalState) - do not store coordinator key but instead take i…
ctrlc03 efe151f
feat(genLocalState) - remove sleeps in between each log request, but …
ctrlc03 1cd4e27
feat(genLocalState) - remove coordinator key from fromJSON and add a …
ctrlc03 c56a558
feat(genLocalState) - sort out conflicts and ensure code runs correctly
ctrlc03 61b2591
feat(genLocalState) - fix corrupted package-lock file
ctrlc03 5002078
feat(genLocalState) - regenerate package-lock files
ctrlc03 f8e3c4a
feat(genLocalState) - ensure @types/chai version is consistent across…
ctrlc03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -51,6 +51,55 @@ clean() { | |
|
||
} | ||
|
||
gen_proofs_pre_fetch() { | ||
echo "merge messages ..." | ||
$MACI_CLI mergeMessages \ | ||
--poll-id $1 | ||
|
||
echo "merge signups ..." | ||
$MACI_CLI mergeSignups \ | ||
--poll-id $1 | ||
|
||
echo "gen proofs ..." | ||
|
||
# generate the local state | ||
$MACI_CLI genLocalState \ | ||
--poll-id $1 \ | ||
--output localState.json \ | ||
--privkey macisk.49953af3585856f539d194b46c82f4ed54ec508fb9b882940cbe68bbc57e59e \ | ||
--num-blocks-per-request 50 | ||
|
||
ARCH=$(uname -m) | ||
if [[ $ARCH == *"arm"* ]]; then | ||
# ARM parameters | ||
$MACI_CLI genProofs \ | ||
--privkey macisk.49953af3585856f539d194b46c82f4ed54ec508fb9b882940cbe68bbc57e59e \ | ||
--poll-id $1 \ | ||
--process-wasm ./zkeys/ProcessMessages_"$PROCESS_MESSAGES_PARAMS"_js/ProcessMessages_"$PROCESS_MESSAGES_PARAMS".wasm \ | ||
--tally-wasm ./zkeys/TallyVotes_"$TALLY_VOTES_PARAMS"_js/TallyVotes_"$TALLY_VOTES_PARAMS".wasm \ | ||
--process-zkey "$ZKEYS_DIR"/ProcessMessages_"$PROCESS_MESSAGES_PARAMS".0.zkey \ | ||
--tally-zkey "$ZKEYS_DIR"/TallyVotes_"$TALLY_VOTES_PARAMS".0.zkey \ | ||
--tally-file tally.json \ | ||
--output proofs/ \ | ||
--state-file localState.json \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice demonstration of how to use new command +2 |
||
$SUBSIDDY_OPTION_GEN_PROOFS | ||
else | ||
# Intel parameters | ||
$MACI_CLI genProofs \ | ||
--privkey macisk.49953af3585856f539d194b46c82f4ed54ec508fb9b882940cbe68bbc57e59e \ | ||
--poll-id $1 \ | ||
--rapidsnark ~/rapidsnark/build/prover \ | ||
--process-witnessgen ./zkeys/ProcessMessages_"$PROCESS_MESSAGES_PARAMS" \ | ||
--tally-witnessgen ./zkeys/TallyVotes_"$TALLY_VOTES_PARAMS" \ | ||
--process-zkey "$ZKEYS_DIR"/ProcessMessages_"$PROCESS_MESSAGES_PARAMS".0.zkey \ | ||
--tally-zkey "$ZKEYS_DIR"/TallyVotes_"$TALLY_VOTES_PARAMS".0.zkey \ | ||
--tally-file tally.json \ | ||
--output proofs/ \ | ||
--state-file localState.json \ | ||
$SUBSIDDY_OPTION_GEN_PROOFS | ||
fi | ||
} | ||
|
||
gen_proofs() { | ||
echo "merge messages ..." | ||
$MACI_CLI mergeMessages \ | ||
|
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,35 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
BASE_DIR="$(dirname "$BASH_SOURCE")" | ||
|
||
. "$BASE_DIR"/../prepare_test.sh | ||
|
||
# 1 signup and 1 message | ||
clean | ||
|
||
POLL_ID=0 | ||
|
||
init_maci | ||
deploy_poll | ||
|
||
|
||
$MACI_CLI signup \ | ||
--pubkey macipk.3e7bb2d7f0a1b7e980f1b6f363d1e3b7a12b9ae354c2cd60a9cfa9fd12917391 | ||
|
||
$MACI_CLI publish \ | ||
--pubkey macipk.3e7bb2d7f0a1b7e980f1b6f363d1e3b7a12b9ae354c2cd60a9cfa9fd12917391 \ | ||
--privkey macisk.fd7aa614ec4a82716ffc219c24fd7e7b52a2b63b5afb17e81c22fe21515539c \ | ||
--state-index 1 \ | ||
--vote-option-index 0 \ | ||
--new-vote-weight 9 \ | ||
--nonce 1 \ | ||
--poll-id "$POLL_ID" | ||
|
||
$MACI_CLI timeTravel \ | ||
--seconds 90 | ||
|
||
gen_proofs_pre_fetch "$POLL_ID" | ||
|
||
prove_and_verify_on_chain "$POLL_ID" |
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,217 @@ | ||
import * as ethers from 'ethers' | ||
import * as fs from 'fs' | ||
|
||
import { | ||
genMaciStateFromContract, | ||
} from 'maci-contracts' | ||
|
||
import { | ||
validateEthAddress, | ||
contractExists, | ||
promptPwd, | ||
} from './utils' | ||
|
||
import { | ||
DEFAULT_ETH_PROVIDER, | ||
} from './defaults' | ||
|
||
import {readJSONFile} from 'maci-common' | ||
import {contractFilepath} from './config' | ||
import { Keypair, PrivKey } from 'maci-domainobjs' | ||
|
||
const configureSubparser = (subparsers: any) => { | ||
const parser = subparsers.addParser( | ||
'genLocalState', | ||
{ addHelp: true }, | ||
) | ||
|
||
const maciPrivkeyGroup = parser.addMutuallyExclusiveGroup({ required: true }) | ||
|
||
maciPrivkeyGroup.addArgument( | ||
['-dsk', '--prompt-for-maci-privkey'], | ||
{ | ||
action: 'storeTrue', | ||
help: 'Whether to prompt for your serialized MACI private key', | ||
} | ||
) | ||
|
||
maciPrivkeyGroup.addArgument( | ||
['-sk', '--privkey'], | ||
{ | ||
action: 'store', | ||
type: 'string', | ||
help: 'Your serialized MACI private key', | ||
} | ||
) | ||
|
||
parser.addArgument( | ||
['-e', '--eth-provider'], | ||
{ | ||
action: 'store', | ||
type: 'string', | ||
help: `A connection string to an Ethereum provider. Default: ${DEFAULT_ETH_PROVIDER}`, | ||
} | ||
) | ||
|
||
parser.addArgument( | ||
['-x', '--maci-contract'], | ||
{ | ||
required: false, | ||
type: 'string', | ||
help: 'The MACI contract address', | ||
} | ||
) | ||
|
||
parser.addArgument( | ||
['-i', '--poll-id'], | ||
{ | ||
required: false, | ||
type: 'int', | ||
help: 'The Poll ID', | ||
} | ||
) | ||
|
||
parser.addArgument( | ||
['-p', '--poll-contract'], | ||
{ | ||
required: false, | ||
type: 'string', | ||
help: 'The Poll contract address', | ||
} | ||
) | ||
|
||
parser.addArgument( | ||
['-b', '--start-block'], | ||
{ | ||
type: 'int', | ||
help: 'The block number at which the contract was deployed', | ||
} | ||
) | ||
|
||
parser.addArgument( | ||
['-f', '--end-block'], | ||
{ | ||
required: false, | ||
type: 'int', | ||
help: 'The last block number to fetch logs from. If not specified, the current block number is used', | ||
} | ||
) | ||
|
||
parser.addArgument( | ||
['-n', '--num-blocks-per-request'], | ||
{ | ||
required: true, | ||
type: 'int', | ||
help: 'The number of logs to fetch per RPC request', | ||
} | ||
) | ||
|
||
parser.addArgument( | ||
['-o', '--output'], | ||
{ | ||
required: true, | ||
type: 'string', | ||
help: 'The output file for signups and messages', | ||
} | ||
) | ||
|
||
parser.addArgument( | ||
['-s', '--sleep'], | ||
{ | ||
type: 'int', | ||
help: 'The number of milliseconds to sleep between each RPC request to avoid getting rate limited', | ||
} | ||
) | ||
} | ||
|
||
const genLocalState = async (args: any) => { | ||
// read the contract addresses from the config file | ||
const contractAddrs = readJSONFile(contractFilepath) | ||
|
||
const pollId = args.poll_id ? args.poll_id : 0 | ||
const pollArtifactName = `Poll-${pollId}` | ||
|
||
// ensure we have at least one address | ||
if ((!contractAddrs||!contractAddrs["MACI"]||!contractAddrs[pollArtifactName]) && !args.maci_contract && !args.poll_contract) { | ||
console.error('Error: If no contract address is stored locally, please specify the poll id, the maci contract address and the poll address') | ||
return | ||
} | ||
// prioritize cli flag arg | ||
const maciAddress = args.maci_contract ? args.maci_contract: contractAddrs["MACI"] | ||
const pollAddress = args.poll_contract ? args.poll_contract: contractAddrs[pollArtifactName] | ||
|
||
// validate it's a valid eth address | ||
if (!validateEthAddress(maciAddress)) { | ||
console.error("Error: invalid MACI contract address. Ensure the address starts with '0x' followed by the 40 hexadecimal characters") | ||
return | ||
} | ||
|
||
if (!validateEthAddress(pollAddress)) { | ||
console.error("Error: invalid Poll contract address. Ensure the address starts with '0x' followed by the 40 hexadecimal characters") | ||
return | ||
} | ||
|
||
// Ethereum provider | ||
const ethProvider = args.eth_provider ? args.eth_provider : DEFAULT_ETH_PROVIDER | ||
|
||
const provider = new ethers.providers.JsonRpcProvider(ethProvider) | ||
|
||
if (!(await contractExists(provider, maciAddress))) { | ||
console.error('Error: there is no MACI contract deployed at the specified address') | ||
return | ||
} | ||
|
||
if (!(await contractExists(provider, pollAddress))) { | ||
console.error('Error: there is no Poll contract deployed at the specified address') | ||
return | ||
} | ||
|
||
// The coordinator's MACI private key | ||
let serializedPrivkey: string | ||
if (args.prompt_for_maci_privkey) { | ||
serializedPrivkey = await promptPwd('Your MACI private key') | ||
} else { | ||
serializedPrivkey = args.privkey | ||
} | ||
|
||
if (!PrivKey.isValidSerializedPrivKey(serializedPrivkey)) { | ||
console.error('Error: invalid MACI private key') | ||
return | ||
} | ||
|
||
const maciPrivkey = PrivKey.unserialize(serializedPrivkey) | ||
const coordinatorKeypair = new Keypair(maciPrivkey) | ||
|
||
// calculate the end block number | ||
const endBlockNumber = args.end_block ? args.end_block : await provider.getBlockNumber() | ||
|
||
console.log('Fetching signup and publish message logs') | ||
// some rpc endpoint like bsc chain has limitation to retreive history logs | ||
let fromBlock = args.start_block ? args.start_block : 0 | ||
const txHash = args.transaction_hash | ||
if (txHash) { | ||
const txn = await provider.getTransaction(txHash); | ||
fromBlock = txn.blockNumber | ||
} | ||
console.log(`Fetching logs from ${fromBlock} till ${endBlockNumber} and generating the offline maci state`) | ||
|
||
const maciState = await genMaciStateFromContract( | ||
provider, | ||
maciAddress, | ||
coordinatorKeypair, | ||
pollId, | ||
fromBlock, | ||
args.blocks_per_batch, | ||
args.end_block, | ||
args.sleep | ||
) | ||
|
||
// write the state to a file | ||
const serializedState = maciState.toJSON() | ||
fs.writeFileSync(args.output, JSON.stringify(serializedState, null, 4)) | ||
} | ||
|
||
export { | ||
genLocalState, | ||
configureSubparser, | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice demonstration of how to use new command!