max elements tree count test

This commit is contained in:
Alexey 2019-07-16 14:04:14 +03:00
parent 9b927f39a6
commit 39777a46e1
2 changed files with 25 additions and 8 deletions

View File

@ -5,7 +5,7 @@ library MiMC {
} }
contract MerkleTreeWithHistory { contract MerkleTreeWithHistory {
uint8 levels; uint256 public levels;
uint8 constant ROOT_HISTORY_SIZE = 100; uint8 constant ROOT_HISTORY_SIZE = 100;
uint256[] private _roots; uint256[] private _roots;
@ -18,7 +18,7 @@ contract MerkleTreeWithHistory {
event LeafAdded(uint256 leaf, uint32 leaf_index); event LeafAdded(uint256 leaf, uint32 leaf_index);
constructor(uint8 tree_levels, uint256 zero_value) public { constructor(uint256 tree_levels, uint256 zero_value) public {
levels = tree_levels; levels = tree_levels;
_zeros.push(zero_value); _zeros.push(zero_value);
@ -50,13 +50,13 @@ contract MerkleTreeWithHistory {
function _insert(uint256 leaf) internal { function _insert(uint256 leaf) internal {
uint32 leaf_index = next_index; uint32 leaf_index = next_index;
uint32 current_index = next_index; uint32 current_index = next_index;
require(current_index != 2**(levels - 1), "Merkle tree is full");
next_index += 1; next_index += 1;
uint256 current_level_hash = leaf; uint256 current_level_hash = leaf;
uint256 left; uint256 left;
uint256 right; uint256 right;
for (uint8 i = 0; i < levels; i++) { for (uint256 i = 0; i < levels; i++) {
if (current_index % 2 == 0) { if (current_index % 2 == 0) {
left = current_level_hash; left = current_level_hash;
right = _zeros[i]; right = _zeros[i];
@ -78,7 +78,7 @@ contract MerkleTreeWithHistory {
emit LeafAdded(leaf, leaf_index); emit LeafAdded(leaf, leaf_index);
} }
function isKnownRoot(uint root) public view returns(bool) { function isKnownRoot(uint256 root) public view returns(bool) {
if (root == 0) { if (root == 0) {
return false; return false;
} }

View File

@ -12,6 +12,8 @@ const MiMC = artifacts.require('./MiMC.sol')
const MerkleTree = require('../lib/MerkleTree') const MerkleTree = require('../lib/MerkleTree')
const MimcHasher = require('../lib/MiMC') const MimcHasher = require('../lib/MiMC')
const { AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env
function BNArrayToStringArray(array) { function BNArrayToStringArray(array) {
const arrayToPrint = [] const arrayToPrint = []
array.forEach(item => { array.forEach(item => {
@ -25,8 +27,9 @@ contract('MerkleTreeWithHistory', async accounts => {
let miMC let miMC
const sender = accounts[0] const sender = accounts[0]
const emptyAddress = '0x0000000000000000000000000000000000000000' const emptyAddress = '0x0000000000000000000000000000000000000000'
const levels = 16 const levels = MERKLE_TREE_HEIGHT || 16
const zeroValue = 1337 const zeroValue = EMPTY_ELEMENT || 1337
const value = AMOUNT || '1000000000000000000'
let snapshotId let snapshotId
let prefix = 'test' let prefix = 'test'
let tree let tree
@ -145,7 +148,6 @@ contract('MerkleTreeWithHistory', async accounts => {
describe('#insert', async () => { describe('#insert', async () => {
it('should insert', async () => { it('should insert', async () => {
let filled_subtrees
let rootFromContract let rootFromContract
for (i = 1; i < 11; i++) { for (i = 1; i < 11; i++) {
@ -161,6 +163,21 @@ contract('MerkleTreeWithHistory', async accounts => {
// console.log('rootFromCon', root.toString()) // console.log('rootFromCon', root.toString())
} }
}) })
it.skip('should reject if tree is full', async () => {
// consider change the merkle tree height to run the test
// it should be changed in .env and ./circuits/withdraw.circom files (default is 16)
for (i = 0; i < 2**(MERKLE_TREE_HEIGHT - 1); i++) {
await merkleTreeWithHistory.insert(i+42).should.be.fulfilled
}
let error = await merkleTreeWithHistory.insert(1337).should.be.rejected
error.reason.should.be.equal('Merkle tree is full')
error = await merkleTreeWithHistory.insert(1).should.be.rejected
error.reason.should.be.equal('Merkle tree is full')
})
}) })
describe('#MIMC', async () => { describe('#MIMC', async () => {