refactor isKnownRoot, add test

This commit is contained in:
poma 2019-11-03 12:06:58 +03:00
parent e710b243d7
commit f783b45559
2 changed files with 38 additions and 16 deletions

View File

@ -97,20 +97,16 @@ contract MerkleTreeWithHistory {
if (_root == 0) {
return false;
}
// search most recent first
uint256 i;
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--) {
uint256 i = currentRootIndex;
do {
if (_root == roots[i]) {
return true;
}
if (i == 0) {
i = ROOT_HISTORY_SIZE;
}
i--;
} while (i != currentRootIndex);
return false;
}

View File

@ -172,7 +172,7 @@ contract('MerkleTreeWithHistory', accounts => {
it('should reject if tree is full', async () => {
levels = 6
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
const merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
for (let i = 0; i < 2**levels; i++) {
await merkleTreeWithHistory.insert(i+42).should.be.fulfilled
@ -187,7 +187,7 @@ contract('MerkleTreeWithHistory', accounts => {
it.skip('hasher gas', async () => {
levels = 6
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
const merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
const zeroValue = await merkleTreeWithHistory.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 () => {
await revertSnapshot(snapshotId.result)
// eslint-disable-next-line require-atomic-updates