Skip to content

Commit 4101bc7

Browse files
committed
proof: update tests and tree to use the new Proof wrapper.
1 parent d70b8cd commit 4101bc7

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

lib/proof.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ class WrappedProof {
108108
return this._proof.verify(root, key, BLAKE2b, HASH_BITS);
109109
}
110110

111+
/**
112+
* Clear cached values.
113+
* @returns {this}
114+
*/
115+
116+
refresh() {
117+
this._raw = null;
118+
this._size = null;
119+
return this;
120+
}
121+
111122
/**
112123
* @param {Buffer} data
113124
* @param {Number} off

lib/tree.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -766,10 +766,8 @@ class Snapshot {
766766

767767
async prove(key) {
768768
assert(this.isOpen, ERR_TX_NOT_OPEN);
769-
const proof = new Proof();
770769
const raw = await nurkel.tx_prove(this.tx, key);
771-
proof.decode(raw);
772-
return proof;
770+
return Proof.decode(raw);;
773771
}
774772

775773
/**
@@ -780,10 +778,8 @@ class Snapshot {
780778

781779
proveSync(key) {
782780
assert(this.isOpen, ERR_TX_NOT_OPEN);
783-
const proof = new Proof();
784781
const raw = nurkel.tx_prove_sync(this.tx, key);
785-
proof.decode(raw);
786-
return proof;
782+
return Proof.decode(raw);;
787783
}
788784

789785
/**
@@ -1204,10 +1200,8 @@ class VirtualTransaction {
12041200
async prove(key) {
12051201
await this.maybeFlush();
12061202

1207-
const proof = new Proof();
12081203
const raw = await nurkel.tx_prove(this.tx, key);
1209-
proof.decode(raw);
1210-
return proof;
1204+
return Proof.decode(raw);;
12111205
}
12121206

12131207
/**
@@ -1219,10 +1213,8 @@ class VirtualTransaction {
12191213
proveSync(key) {
12201214
this.maybeFlushSync();
12211215

1222-
const proof = new Proof();
12231216
const raw = nurkel.tx_prove_sync(this.tx, key);
1224-
proof.decode(raw);
1225-
return proof;
1217+
return Proof.decode(raw);;
12261218
}
12271219

12281220
/**

lib/urkel.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,10 @@ class WrappedTree {
252252

253253
async prove(key) {
254254
assert(this.isOpen, ERR_NOT_OPEN);
255+
const proof = new Proof();
255256
const uproof = await this._tree.prove(key);
256-
const raw = uproof.encode(this.hash, this.bits);
257-
return Proof.decode(raw);
257+
proof._proof = uproof;
258+
return proof;
258259
}
259260

260261
/**
@@ -658,8 +659,10 @@ class WrappedSnapshot {
658659
} catch (e) {
659660
throw handleMissingError(e);
660661
}
661-
const raw = uproof.encode(this.tree.hash, this.tree.bits);
662-
return Proof.decode(raw);
662+
663+
const proof = new Proof();
664+
proof._proof = uproof;
665+
return proof;
663666
}
664667

665668
/**

test/transaction-test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,10 @@ describe(`Urkel Transaction (${name})`, function () {
109109
const proof = await txn1.prove(key);
110110

111111
assert(Proof.isProof(proof));
112-
assert(Buffer.isBuffer(proof.raw));
113112

114113
if (tree.supportsSync) {
115114
const proofS = txn1.proveSync(key);
116-
assert.bufferEqual(proof.raw, proofS.raw);
115+
assert.bufferEqual(proof.encode(), proofS.encode());
117116
}
118117
}
119118

test/tree-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ describe(`Urkel Tree (${name})`, function () {
247247
const treeProof = treeProofs[i];
248248

249249
if (tree.supportsSync)
250-
assert.bufferEqual(treeProofSync.raw, proof.raw);
251-
assert.bufferEqual(treeProof.raw, proof.raw);
250+
assert.bufferEqual(treeProofSync.encode(), proof.encode());
251+
assert.bufferEqual(treeProof.encode(), proof.encode());
252252

253253
if (tree.supportsSync) {
254254
const [codeSync, valueSync] = Tree.verifySync(

test/vtx-test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,10 @@ describe('Urkel Virtual Transaction', function () {
119119
const proof = await txn1.prove(key);
120120

121121
assert(Proof.isProof(proof));
122-
assert(Buffer.isBuffer(proof.raw));
123122

124123
if (tree.supportsSync) {
125124
const proofS = txn1.proveSync(key);
126-
assert.bufferEqual(proof.raw, proofS.raw);
125+
assert.bufferEqual(proof.encode(), proofS.encode());
127126
}
128127
}
129128

0 commit comments

Comments
 (0)