Skip to content

Commit c97c71d

Browse files
committed
calc depth in right way; sync json tags with golang implementation
1 parent 9ffaf74 commit c97c71d

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/lib/merkletree/proof.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Bytes } from '../../types';
1010
export interface ProofJSON {
1111
existence: boolean;
1212
siblings: string[];
13-
nodeAux: NodeAuxJSON | undefined;
13+
node_aux: NodeAuxJSON | undefined;
1414
}
1515

1616
export interface NodeAuxJSON {
@@ -28,7 +28,7 @@ export class Proof {
2828

2929
constructor(obj?: { siblings: Siblings; nodeAux: NodeAux | undefined; existence: boolean }) {
3030
this.existence = obj?.existence ?? false;
31-
this.depth = obj?.siblings.length ?? 0;
31+
this.depth = 0;
3232
this.nodeAux = obj?.nodeAux;
3333

3434
const { siblings, notEmpties } = this.reduceSiblings(obj?.siblings);
@@ -66,7 +66,7 @@ export class Proof {
6666
return {
6767
existence: this.existence,
6868
siblings: this.allSiblings().map((s) => s.toJSON()),
69-
nodeAux: this.nodeAux
69+
node_aux: this.nodeAux
7070
? {
7171
key: this.nodeAux.key.toJSON(),
7272
value: this.nodeAux.value.toJSON()
@@ -87,17 +87,18 @@ export class Proof {
8787
if (JSON.stringify(siblings[i]) !== JSON.stringify(ZERO_HASH)) {
8888
setBitBigEndian(notEmpties, i);
8989
reducedSiblings.push(sibling);
90+
this.depth = i + 1;
9091
}
9192
}
9293
return { notEmpties, siblings: reducedSiblings };
9394
}
9495

9596
public static fromJSON(obj: ProofJSON): Proof {
9697
let nodeAux: NodeAux | undefined = undefined;
97-
if (obj.nodeAux) {
98+
if (obj.node_aux) {
9899
nodeAux = {
99-
key: Hash.fromString(obj.nodeAux.key),
100-
value: Hash.fromString(obj.nodeAux.value)
100+
key: Hash.fromString(obj.node_aux.key),
101+
value: Hash.fromString(obj.node_aux.value)
101102
};
102103
}
103104
const existence = obj.existence ?? false;

tests/full.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,29 @@ for (let index = 0; index < storages.length; index++) {
11831183
expect(cvp.value.string()).to.be.equal('22');
11841184
expect(cvp.fnc).to.be.equal(0);
11851185
});
1186+
it('calcualte depth for mtp', async () => {
1187+
const storage = getTreeStorage('calculatedepth');
1188+
const mt = new Merkletree(storage, true, 40);
1189+
1190+
await mt.add(BigInt('1'), BigInt('2'));
1191+
await mt.add(BigInt('3'), BigInt('8'));
1192+
await mt.add(BigInt('7'), BigInt('8'));
1193+
await mt.add(BigInt('9'), BigInt('8'));
1194+
1195+
const { proof }: { proof: Proof } = await mt.generateProof(BigInt('11'), await mt.root());
1196+
1197+
const given = `{ "existence": false, "siblings": [ "0", "12166698708103333637493481507263348370172773813051235807348785759284762677336", "7750564177398573185975752951631372712868228752107043582052272719841058100111", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" ], "node_aux": { "key": "3", "value": "8" }}`;
1198+
const p = Proof.fromJSON(JSON.parse(given));
1199+
1200+
expect(proof.allSiblings()).to.deep.equal(p.allSiblings());
1201+
expect(proof.nodeAux).to.deep.equal(p.nodeAux);
1202+
expect(proof.existence).to.equal(p.existence);
1203+
1204+
let isValid = await verifyProof(await mt.root(), proof, BigInt('11'), BigInt('0'));
1205+
expect(isValid).to.be.true;
1206+
isValid = await verifyProof(await mt.root(), p, BigInt('11'), BigInt('0'));
1207+
expect(isValid).to.be.true;
1208+
});
11861209
});
11871210
}
11881211

0 commit comments

Comments
 (0)