Skip to content
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

constants and config refactor #905

Merged
merged 4 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions common-files/classes/transaction.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
/**
An optimistic Transaction class
*/
import config from 'config';
import gen from 'general-number';
import Web3 from 'web3';
import { compressProof } from '../utils/curve-maths/curves.mjs';
import constants from '../constants/index.mjs';

const { generalise } = gen;

const TOKEN_TYPES = { ERC20: 0, ERC721: 1, ERC1155: 2 };
const { TRANSACTION_TYPES } = constants;
const { SIGNATURES } = config;

const arrayEquality = (as, bs) => {
if (as.length === bs.length) {
Expand Down Expand Up @@ -54,7 +54,7 @@ function keccak(preimage) {
proof,
];

const encodedTransaction = web3.eth.abi.encodeParameters([TRANSACTION_TYPES], [transaction]);
const encodedTransaction = web3.eth.abi.encodeParameters([SIGNATURES.TRANSACTION], [transaction]);
return web3.utils.soliditySha3({
t: 'bytes',
v: encodedTransaction,
Expand Down
12 changes: 1 addition & 11 deletions common-files/constants/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,5 @@
"SHIELD_CONTRACT_NAME": "Shield",
"CHALLENGES_CONTRACT_NAME": "Challenges",
"STATE_CONTRACT_NAME": "State",
"BLOCK_PROPOSED_EVENT_NAME": "BlockProposed",
"ZKP_KEY_LENGTH": 32,
"NODE_HASHLENGTH": 32,
"ZERO": "0x0000000000000000000000000000000000000000000000000000000000000000",
"BLOCK_TYPES": "(uint48,address,bytes32,uint256,bytes32,bytes32)",
"TRANSACTION_TYPES": "(uint112,uint112,uint8,uint8,uint64[4],bytes32,bytes32,bytes32,bytes32[3],bytes32[4],bytes32[2],uint256[4])",
"PROPOSE_BLOCK_TYPES": [
"(uint48,address,bytes32,uint256,bytes32,bytes32)",
"(uint112,uint112,uint8,uint8,uint64[4],bytes32,bytes32,bytes32,bytes32[3],bytes32[4],bytes32[2],uint256[4])[]"
],
"SUBMIT_TRANSACTION_TYPES": "(uint112,uint112,uint8,uint8,uint64[4],bytes32,bytes32,bytes32,bytes32[3],bytes32[4],bytes32[2],uint256[4])"
"ZERO": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
18 changes: 11 additions & 7 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ module.exports = {
TRANSACTIONS_COLLECTION: 'transactions',
SUBMITTED_BLOCKS_COLLECTION: 'blocks',
INVALID_BLOCKS_COLLECTION: 'invalid_blocks',
NULLIFIER_COLLECTION: 'nullifiers',
COMMIT_COLLECTION: 'commits',
WALLETS_COLLECTION: 'wallets',
COMMITMENTS_COLLECTION: 'commitments',
PEERS_COLLECTION: 'peers',
TIMBER_COLLECTION: 'timber',
CIRCUIT_COLLECTION: 'circuit_storage',
CIRCUIT_HASH_COLLECTION: 'circuit_hash_storage',
KEYS_COLLECTION: 'keys',
CONTRACT_ARTIFACTS: '/app/build/contracts',
EXCLUDE_DIRS: 'common',
PROOF_QUEUE: 'generate-proof',
MAX_QUEUE: 5,
TIMBER_HEIGHT: 32,
TXHASH_TREE_HEIGHT: 5,
Expand Down Expand Up @@ -113,9 +109,6 @@ module.exports = {
MONTA: BigInt(168698),
MONTB: BigInt(1),
},
ELLIGATOR2: {
U: BigInt(5), // non square in Fp
},
MPC: {
MPC_PARAMS_URL:
'https://nightfallv3-proving-files.s3.eu-west-1.amazonaws.com/phase2/mpc_params',
Expand Down Expand Up @@ -429,4 +422,15 @@ module.exports = {
// assumption is if LOCAL_PROPOSER is true, wallet UI app
// is running in local machine
isLocalRun: process.env.LOCAL_PROPOSER === 'true',
SIGNATURES: {
BLOCK: '(uint48,address,bytes32,uint256,bytes32,bytes32)',
TRANSACTION:
'(uint112,uint112,uint8,uint8,uint64[4],bytes32,bytes32,bytes32,bytes32[3],bytes32[4],bytes32[2],uint256[4])',
PROPOSE_BLOCK: [
'(uint48,address,bytes32,uint256,bytes32,bytes32)',
'(uint112,uint112,uint8,uint8,uint64[4],bytes32,bytes32,bytes32,bytes32[3],bytes32[4],bytes32[2],uint256[4])[]',
],
SUBMIT_TRANSACTION:
'(uint112,uint112,uint8,uint8,uint64[4],bytes32,bytes32,bytes32,bytes32[3],bytes32[4],bytes32[2],uint256[4])',
},
};
6 changes: 3 additions & 3 deletions nightfall-client/src/services/process-calldata.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ Function to retreive calldata associated with a blockchain event.
This is used, rather than re-emmiting the calldata in the event because it's
much cheaper, although the offchain part is more complex.
*/
import config from 'config';
import Web3 from 'common-files/utils/web3.mjs';
import Transaction from 'common-files/classes/transaction.mjs';
import { decompressProof } from 'common-files/utils/curve-maths/curves.mjs';
import constants from 'common-files/constants/index.mjs';

const { PROPOSE_BLOCK_TYPES } = constants;
const { SIGNATURES } = config;

async function getProposeBlockCalldata(eventData) {
const web3 = Web3.connection();
const { transactionHash } = eventData;
const tx = await web3.eth.getTransaction(transactionHash);
// Remove the '0x' and function signature to recove rhte abi bytecode
const abiBytecode = `0x${tx.input.slice(10)}`;
const decoded = web3.eth.abi.decodeParameters(PROPOSE_BLOCK_TYPES, abiBytecode);
const decoded = web3.eth.abi.decodeParameters(SIGNATURES.PROPOSE_BLOCK, abiBytecode);
const blockData = decoded['0'];
const transactionsData = decoded['1'];
const [leafCount, proposer, root, blockNumberL2, previousBlockHash, transactionHashesRoot] =
Expand Down
4 changes: 2 additions & 2 deletions nightfall-optimist/src/services/block-utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Web3 from 'web3';
// of the block object isn't. They can thus be called directly when instantiating the Block class
// would be problematic because of its reliance on the Optimist database.

const { BLOCK_TYPES } = constants;
const { SIGNATURES } = constants;
LijuJoseJJ marked this conversation as resolved.
Show resolved Hide resolved

export function calcBlockHash(block) {
const web3 = new Web3();
Expand All @@ -19,7 +19,7 @@ export function calcBlockHash(block) {
previousBlockHash,
transactionHashesRoot,
];
const encoded = web3.eth.abi.encodeParameters([BLOCK_TYPES], [blockArray]);
const encoded = web3.eth.abi.encodeParameters([SIGNATURES.BLOCK], [blockArray]);
return web3.utils.soliditySha3({ t: 'bytes', v: encoded });
}

Expand Down
8 changes: 5 additions & 3 deletions nightfall-optimist/src/services/process-calldata.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ Function to retreive calldata associated with a blockchain event.
This is used, rather than re-emmiting the calldata in the event because it's
much cheaper, although the offchain part is more complex.
*/
import config from 'config';
import Web3 from 'common-files/utils/web3.mjs';
import { decompressProof } from 'common-files/utils/curve-maths/curves.mjs';
import constants from 'common-files/constants/index.mjs';
import Block from '../classes/block.mjs';
import { Transaction } from '../classes/index.mjs';

const { PROPOSE_BLOCK_TYPES, SUBMIT_TRANSACTION_TYPES, ZERO } = constants;
const { SIGNATURES } = config;
const { ZERO } = constants;

export async function getProposeBlockCalldata(eventData) {
const web3 = Web3.connection();
const { transactionHash } = eventData;
const tx = await web3.eth.getTransaction(transactionHash);
// Remove the '0x' and function signature to recove rhte abi bytecode
const abiBytecode = `0x${tx.input.slice(10)}`;
const decoded = web3.eth.abi.decodeParameters(PROPOSE_BLOCK_TYPES, abiBytecode);
const decoded = web3.eth.abi.decodeParameters(SIGNATURES.PROPOSE_BLOCK, abiBytecode);
const blockData = decoded['0'];
const transactionsData = decoded['1'];
const [leafCount, proposer, root, blockNumberL2, previousBlockHash, transactionHashesRoot] =
Expand Down Expand Up @@ -84,7 +86,7 @@ export async function getTransactionSubmittedCalldata(eventData) {
const tx = await web3.eth.getTransaction(transactionHash);
// Remove the '0x' and function signature to recove rhte abi bytecode
const abiBytecode = `0x${tx.input.slice(10)}`;
const transactionData = web3.eth.abi.decodeParameter(SUBMIT_TRANSACTION_TYPES, abiBytecode);
const transactionData = web3.eth.abi.decodeParameter(SIGNATURES.SUBMIT_TRANSACTION, abiBytecode);
const [
value,
fee,
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/common-files/classes/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { compressProof } from '../utils/curve-maths/curves';
const { generalise } = gen;

const TOKEN_TYPES = { ERC20: 0, ERC721: 1, ERC1155: 2 };
const { TRANSACTION_TYPES } = global.nightfallConstants;
const { SIGNATURES } = global.config;

const arrayEquality = (as, bs) => {
if (as.length === bs.length) {
Expand Down Expand Up @@ -52,7 +52,7 @@ function keccak(preimage) {
compressedSecrets,
proof,
];
const encodedTransaction = web3.eth.abi.encodeParameters([TRANSACTION_TYPES], [transaction]);
const encodedTransaction = web3.eth.abi.encodeParameters([SIGNATURES.TRANSACTION], [transaction]);
return web3.utils.soliditySha3({
t: 'bytes',
v: encodedTransaction,
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/nightfall-browser/services/process-calldata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import Web3 from '../../common-files/utils/web3';
import Transaction from '../../common-files/classes/transaction';
import { decompressProof } from '../../common-files/utils/curve-maths/curves';

const { PROPOSE_BLOCK_TYPES } = global.nightfallConstants;
const { SIGNATURES } = global.config;

async function getProposeBlockCalldata(eventData) {
const web3 = Web3.connection();
const { transactionHash } = eventData;
const tx = await web3.eth.getTransaction(transactionHash);
// Remove the '0x' and function signature to recove rhte abi bytecode
const abiBytecode = `0x${tx.input.slice(10)}`;
const decoded = web3.eth.abi.decodeParameters(PROPOSE_BLOCK_TYPES, abiBytecode);
const decoded = web3.eth.abi.decodeParameters(SIGNATURES.PROPOSE_BLOCK, abiBytecode);
const blockData = decoded['0'];
const transactionsData = decoded['1'];
const [leafCount, proposer, root, blockNumberL2, previousBlockHash, transactionHashesRoot] =
Expand Down