Skip to content

Commit 7320fe6

Browse files
authored
Merge pull request #3 from commitground/feature/safe-get
Feature/safe get
2 parents 117dd2c + acb3c72 commit 7320fe6

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

contracts/implementation.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ contract PartialMerkleTreeImplementation {
2424
return tree.get(key);
2525
}
2626

27+
function safeGet(bytes key) public view returns (bytes) {
28+
return tree.safeGet(key);
29+
}
30+
31+
function doesInclude(bytes key) public view returns (bool) {
32+
return tree.doesInclude(key);
33+
}
34+
2735
function getValue(bytes32 hash) public view returns (bytes) {
2836
return tree.values[hash];
2937
}

contracts/tree.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ library PartialMerkleTree {
8888
return getValue(tree, _findNode(tree, key));
8989
}
9090

91+
function safeGet(Tree storage tree, bytes key) internal view returns (bytes value) {
92+
bytes32 valueHash = _findNode(tree, key);
93+
require(valueHash != bytes32(0));
94+
value = getValue(tree, valueHash);
95+
require(valueHash == keccak256(value));
96+
}
97+
98+
function doesInclude(Tree storage tree, bytes key) internal view returns (bool) {
99+
bytes32 valueHash = _findNode(tree, key);
100+
return (valueHash != bytes32(0));
101+
}
102+
91103
function getValue(Tree storage tree, bytes32 valueHash) internal view returns (bytes) {
92104
return tree.values[valueHash];
93105
}

test/PartialMerkleTree.Test.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,30 @@ contract('PartialMerkleTree', async ([_, primary, nonPrimary]) => {
142142
assert.equal(web3.toUtf8(await tree.get('foo')), 'bar')
143143
})
144144
})
145+
146+
describe('safeGet()', async () => {
147+
it('should return stored value for the given key', async () => {
148+
await tree.insert('foo', 'bar', { from: primary })
149+
assert.equal(web3.toUtf8(await tree.get('foo')), 'bar')
150+
})
151+
it('should throw if the given key is not included', async () => {
152+
await tree.insert('foo', 'bar', { from: primary })
153+
try {
154+
await tree.get('fuz')
155+
assert.fail('Did not reverted')
156+
} catch (e) {
157+
assert.ok('Reverted successfully')
158+
}
159+
})
160+
})
161+
162+
describe('doesInclude()', async () => {
163+
it('should return boolean whether the tree includes the given key or not', async () => {
164+
await tree.insert('foo', 'bar', { from: primary })
165+
assert.equal(await tree.doesInclude('foo'), true)
166+
assert.equal(await tree.doesInclude('fuz'), false)
167+
})
168+
})
145169
})
146170

147171
context('We can reenact merkle tree transformation by submitting only referred siblings instead of submitting all nodes', async () => {
@@ -166,19 +190,19 @@ contract('PartialMerkleTree', async ([_, primary, nonPrimary]) => {
166190
siblingsForKey1 = proof[1]
167191
})
168192

169-
it('should start with same root hash by initialization', async()=> {
193+
it('should start with same root hash by initialization', async () => {
170194
//initilaze with the first root hash
171195
await treeB.initialize(firstPhaseOfTreeA)
172196
assert.equal(await treeB.getRootHash(), firstPhaseOfTreeA)
173197
})
174198

175-
it('should not change root after committing branch data', async ()=> {
199+
it('should not change root after committing branch data', async () => {
176200
// commit branch data
177201
await treeB.commitBranch('key1', referredValueForKey1, branchMaskForKey1, siblingsForKey1)
178202
assert.equal(await treeB.getRootHash(), firstPhaseOfTreeA)
179203
})
180204

181-
it('should be able to return proof data', async ()=> {
205+
it('should be able to return proof data', async () => {
182206
// commit branch data
183207
await treeB.getProof('key1')
184208
})

0 commit comments

Comments
 (0)