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--) {
if (_root == roots[i]) {
return true;
}
}
uint256 i = currentRootIndex;
do {
if (_root == roots[i]) {
return true;
}
if (i == 0) {
i = ROOT_HISTORY_SIZE;
}
i--;
} while (i != currentRootIndex);
return false;
}