changed merkleTree vars to uint32 where appropriate

This commit is contained in:
poma 2019-11-03 13:11:22 +03:00
parent f783b45559
commit e9c2055bb4
5 changed files with 17 additions and 16 deletions

4
cli.js
View File

@ -150,13 +150,13 @@ async function withdraw(note, receiver) {
console.log('Getting current state from mixer contract') console.log('Getting current state from mixer contract')
const events = await mixer.getPastEvents('Deposit', { fromBlock: mixer.deployedBlock, toBlock: 'latest' }) const events = await mixer.getPastEvents('Deposit', { fromBlock: mixer.deployedBlock, toBlock: 'latest' })
const leaves = events const leaves = events
.sort((a, b) => a.returnValues.leafIndex.sub(b.returnValues.leafIndex)) // Sort events in chronological order .sort((a, b) => a.returnValues.leafIndex - b.returnValues.leafIndex) // Sort events in chronological order
.map(e => e.returnValues.commitment) .map(e => e.returnValues.commitment)
const tree = new merkleTree(MERKLE_TREE_HEIGHT, leaves) const tree = new merkleTree(MERKLE_TREE_HEIGHT, leaves)
// Find current commitment in the tree // Find current commitment in the tree
let depositEvent = events.find(e => e.returnValues.commitment.eq(paddedCommitment)) let depositEvent = events.find(e => e.returnValues.commitment.eq(paddedCommitment))
let leafIndex = depositEvent ? depositEvent.returnValues.leafIndex.toNumber() : -1 let leafIndex = depositEvent ? depositEvent.returnValues.leafIndex : -1
// Validate that our data is correct // Validate that our data is correct
const isValidRoot = await mixer.methods.isKnownRoot(await tree.root()).call() const isValidRoot = await mixer.methods.isKnownRoot(await tree.root()).call()

View File

@ -19,7 +19,7 @@ contract ERC20Mixer is Mixer {
constructor( constructor(
IVerifier _verifier, IVerifier _verifier,
uint256 _denomination, uint256 _denomination,
uint8 _merkleTreeHeight, uint32 _merkleTreeHeight,
address _operator, address _operator,
address _token address _token
) Mixer(_verifier, _denomination, _merkleTreeHeight, _operator) public { ) Mixer(_verifier, _denomination, _merkleTreeHeight, _operator) public {

View File

@ -17,7 +17,7 @@ contract ETHMixer is Mixer {
constructor( constructor(
IVerifier _verifier, IVerifier _verifier,
uint256 _denomination, uint256 _denomination,
uint8 _merkleTreeHeight, uint32 _merkleTreeHeight,
address _operator address _operator
) Mixer(_verifier, _denomination, _merkleTreeHeight, _operator) public { ) Mixer(_verifier, _denomination, _merkleTreeHeight, _operator) public {
} }

View File

@ -19,26 +19,27 @@ contract MerkleTreeWithHistory {
uint256 public constant FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617; uint256 public constant FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256 public constant ZERO_VALUE = 5702960885942360421128284892092891246826997279710054143430547229469817701242; // = MiMC("tornado") uint256 public constant ZERO_VALUE = 5702960885942360421128284892092891246826997279710054143430547229469817701242; // = MiMC("tornado")
uint256 public levels; uint32 public levels;
// the following variables are made public for easier testing and debugging and // the following variables are made public for easier testing and debugging and
// are not supposed to be accessed in regular code // are not supposed to be accessed in regular code
uint256 public constant ROOT_HISTORY_SIZE = 100; uint32 public constant ROOT_HISTORY_SIZE = 100;
uint256[ROOT_HISTORY_SIZE] public roots; uint256[ROOT_HISTORY_SIZE] public roots;
uint256 public currentRootIndex = 0; uint32 public currentRootIndex = 0;
uint32 public nextIndex = 0; uint32 public nextIndex = 0;
uint256[] public filledSubtrees; uint256[] public filledSubtrees;
uint256[] public zeros; uint256[] public zeros;
constructor(uint256 _treeLevels) public { constructor(uint32 _treeLevels) public {
require(_treeLevels > 0, "_treeLevels should be greater than zero"); require(_treeLevels > 0, "_treeLevels should be greater than zero");
require(_treeLevels < 32, "_treeLevels should be less than 32");
levels = _treeLevels; levels = _treeLevels;
uint256 currentZero = ZERO_VALUE; uint256 currentZero = ZERO_VALUE;
zeros.push(currentZero); zeros.push(currentZero);
filledSubtrees.push(currentZero); filledSubtrees.push(currentZero);
for (uint8 i = 1; i < levels; i++) { for (uint32 i = 1; i < levels; i++) {
currentZero = hashLeftRight(currentZero, currentZero); currentZero = hashLeftRight(currentZero, currentZero);
zeros.push(currentZero); zeros.push(currentZero);
filledSubtrees.push(currentZero); filledSubtrees.push(currentZero);
@ -61,15 +62,15 @@ contract MerkleTreeWithHistory {
return R; return R;
} }
function _insert(uint256 _leaf) internal returns(uint256 index) { function _insert(uint256 _leaf) internal returns(uint32 index) {
uint32 currentIndex = nextIndex; uint32 currentIndex = nextIndex;
require(currentIndex != 2**levels, "Merkle tree is full. No more leafs can be added"); require(currentIndex != uint32(2)**levels, "Merkle tree is full. No more leafs can be added");
nextIndex += 1; nextIndex += 1;
uint256 currentLevelHash = _leaf; uint256 currentLevelHash = _leaf;
uint256 left; uint256 left;
uint256 right; uint256 right;
for (uint256 i = 0; i < levels; i++) { for (uint32 i = 0; i < levels; i++) {
if (currentIndex % 2 == 0) { if (currentIndex % 2 == 0) {
left = currentLevelHash; left = currentLevelHash;
right = zeros[i]; right = zeros[i];
@ -97,7 +98,7 @@ contract MerkleTreeWithHistory {
if (_root == 0) { if (_root == 0) {
return false; return false;
} }
uint256 i = currentRootIndex; uint32 i = currentRootIndex;
do { do {
if (_root == roots[i]) { if (_root == roots[i]) {
return true; return true;

View File

@ -35,7 +35,7 @@ contract Mixer is MerkleTreeWithHistory {
_; _;
} }
event Deposit(uint256 indexed commitment, uint256 leafIndex, uint256 timestamp); event Deposit(uint256 indexed commitment, uint32 leafIndex, uint256 timestamp);
event Withdrawal(address to, uint256 nullifierHash, address indexed relayer, uint256 fee); event Withdrawal(address to, uint256 nullifierHash, address indexed relayer, uint256 fee);
/** /**
@ -48,7 +48,7 @@ contract Mixer is MerkleTreeWithHistory {
constructor( constructor(
IVerifier _verifier, IVerifier _verifier,
uint256 _denomination, uint256 _denomination,
uint8 _merkleTreeHeight, uint32 _merkleTreeHeight,
address _operator address _operator
) MerkleTreeWithHistory(_merkleTreeHeight) public { ) MerkleTreeWithHistory(_merkleTreeHeight) public {
require(_denomination > 0, "denomination should be greater than 0"); require(_denomination > 0, "denomination should be greater than 0");
@ -64,7 +64,7 @@ contract Mixer is MerkleTreeWithHistory {
function deposit(uint256 _commitment) public payable { function deposit(uint256 _commitment) public payable {
require(!isDepositsDisabled, "deposits are disabled"); require(!isDepositsDisabled, "deposits are disabled");
require(!commitments[_commitment], "The commitment has been submitted"); require(!commitments[_commitment], "The commitment has been submitted");
uint256 insertedIndex = _insert(_commitment); uint32 insertedIndex = _insert(_commitment);
commitments[_commitment] = true; commitments[_commitment] = true;
_processDeposit(); _processDeposit();