mirror of
https://github.com/tornadocash/tornado-core.git
synced 2024-11-22 09:47:13 +01:00
refactor isKnownRoot, add test
This commit is contained in:
parent
e710b243d7
commit
f783b45559
@ -97,20 +97,16 @@ contract MerkleTreeWithHistory {
|
|||||||
if (_root == 0) {
|
if (_root == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// search most recent first
|
uint256 i = currentRootIndex;
|
||||||
uint256 i;
|
do {
|
||||||
for(i = currentRootIndex; i < 2**256 - 1; i--) {
|
|
||||||
if (_root == roots[i]) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// process the rest of roots
|
|
||||||
for(i = ROOT_HISTORY_SIZE - 1; i > currentRootIndex; i--) {
|
|
||||||
if (_root == roots[i]) {
|
if (_root == roots[i]) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (i == 0) {
|
||||||
|
i = ROOT_HISTORY_SIZE;
|
||||||
}
|
}
|
||||||
|
i--;
|
||||||
|
} while (i != currentRootIndex);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,7 +172,7 @@ contract('MerkleTreeWithHistory', accounts => {
|
|||||||
|
|
||||||
it('should reject if tree is full', async () => {
|
it('should reject if tree is full', async () => {
|
||||||
levels = 6
|
levels = 6
|
||||||
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
|
const merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
|
||||||
|
|
||||||
for (let i = 0; i < 2**levels; i++) {
|
for (let i = 0; i < 2**levels; i++) {
|
||||||
await merkleTreeWithHistory.insert(i+42).should.be.fulfilled
|
await merkleTreeWithHistory.insert(i+42).should.be.fulfilled
|
||||||
@ -187,7 +187,7 @@ contract('MerkleTreeWithHistory', accounts => {
|
|||||||
|
|
||||||
it.skip('hasher gas', async () => {
|
it.skip('hasher gas', async () => {
|
||||||
levels = 6
|
levels = 6
|
||||||
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
|
const merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
|
||||||
const zeroValue = await merkleTreeWithHistory.zeroValue()
|
const zeroValue = await merkleTreeWithHistory.zeroValue()
|
||||||
|
|
||||||
const gas = await merkleTreeWithHistory.hashLeftRight.estimateGas(zeroValue, zeroValue)
|
const gas = await merkleTreeWithHistory.hashLeftRight.estimateGas(zeroValue, zeroValue)
|
||||||
@ -195,6 +195,32 @@ contract('MerkleTreeWithHistory', accounts => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#isKnownRoot', () => {
|
||||||
|
it('should work', async () => {
|
||||||
|
let path
|
||||||
|
|
||||||
|
for (let i = 1; i < 5; i++) {
|
||||||
|
await merkleTreeWithHistory.insert(i, { from: sender }).should.be.fulfilled
|
||||||
|
await tree.insert(i)
|
||||||
|
path = await tree.path(i - 1)
|
||||||
|
let isKnown = await merkleTreeWithHistory.isKnownRoot(path.root)
|
||||||
|
isKnown.should.be.equal(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
await merkleTreeWithHistory.insert(42, { from: sender }).should.be.fulfilled
|
||||||
|
// check outdated root
|
||||||
|
let isKnown = await merkleTreeWithHistory.isKnownRoot(path.root)
|
||||||
|
isKnown.should.be.equal(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not return uninitialized roots', async () => {
|
||||||
|
await merkleTreeWithHistory.insert(42, { from: sender }).should.be.fulfilled
|
||||||
|
let isKnown = await merkleTreeWithHistory.isKnownRoot(0)
|
||||||
|
isKnown.should.be.equal(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
await revertSnapshot(snapshotId.result)
|
await revertSnapshot(snapshotId.result)
|
||||||
// eslint-disable-next-line require-atomic-updates
|
// eslint-disable-next-line require-atomic-updates
|
||||||
|
Loading…
Reference in New Issue
Block a user