Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
fmt/prettier on the new changes in js/ts
Browse files Browse the repository at this point in the history
  • Loading branch information
StanChe committed May 27, 2024
1 parent f1fc030 commit 366f2bc
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 64 deletions.
227 changes: 184 additions & 43 deletions account-compression/sdk/tests/accountCompression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Account Compression', () => {

await provider.connection.confirmTransaction(
await provider.connection.requestAirdrop(payer, 1e10),
'confirmed'
'confirmed',
);
});

Expand Down Expand Up @@ -89,41 +89,58 @@ describe('Account Compression', () => {
another = anotherKeyPair.publicKey;
await provider.connection.confirmTransaction(
await provider.connection.requestAirdrop(another, 1e10),
'confirmed'
'confirmed',
);
});
it('Should be able to finalize the tree', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
const root = merkleTreeRaw.root;
const leaf = leaves[leaves.length - 1];

const finalize = createFinalizeMerkleTreeWithRootIx(cmt, payer, root, leaf, leaves.length - 1, merkleTreeRaw.getProof(leaves.length - 1).proof);
const finalize = createFinalizeMerkleTreeWithRootIx(
cmt,
payer,
root,
leaf,
leaves.length - 1,
merkleTreeRaw.getProof(leaves.length - 1).proof,
);

await execute(provider, [finalize], [payerKeypair]);

const splCMT = await ConcurrentMerkleTreeAccount.fromAccountAddress(connection, cmt);
assert(splCMT.getMaxBufferSize() === size, 'Buffer size does not match');
assert(splCMT.getCanopyDepth() === canopyDepth, 'Canopy depth does not match: expected ' + canopyDepth + ' but got ' + splCMT.getCanopyDepth());
assert(
splCMT.getCanopyDepth() === canopyDepth,
'Canopy depth does not match: expected ' + canopyDepth + ' but got ' + splCMT.getCanopyDepth(),
);
assert(splCMT.getBufferSize() == 1, 'Buffer size does not match');
});
it('Should fail to append canopy node for a tree without canopy', async () => {
const appendIx = createAppendCanopyNodesIx(cmt, payer, [crypto.randomBytes(32)], 0);
try {
await execute(provider, [appendIx], [payerKeypair]);
assert(false, 'Canopy appending should have failed to execute for a tree without canopy');
} catch { }
} catch {}
});
it('Should fail to finalize the tree with another payer authority', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
const root = merkleTreeRaw.root;
const leaf = leaves[leaves.length - 1];

const finalize = createFinalizeMerkleTreeWithRootIx(cmt, another, root, leaf, leaves.length - 1, merkleTreeRaw.getProof(leaves.length - 1).proof);
const finalize = createFinalizeMerkleTreeWithRootIx(
cmt,
another,
root,
leaf,
leaves.length - 1,
merkleTreeRaw.getProof(leaves.length - 1).proof,
);

try {
await execute(provider, [finalize], [anotherKeyPair]);
assert(false, 'Finalizing with another payer should have failed');
} catch { }
} catch {}
});
it('Should fail to finalize the tree with a wrong proof', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
Expand All @@ -140,25 +157,30 @@ describe('Account Compression', () => {
try {
await execute(provider, [finalize], [payerKeypair]);
assert(false, 'Finalizing with a wrong proof should have failed');
} catch { }
} catch {}
});
it('Should fail to double finalize the tree', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
const root = merkleTreeRaw.root;
const leaf = leaves[leaves.length - 1];

const finalize = createFinalizeMerkleTreeWithRootIx(cmt, payer, root, leaf, leaves.length - 1, merkleTreeRaw.getProof(leaves.length - 1).proof);
const finalize = createFinalizeMerkleTreeWithRootIx(
cmt,
payer,
root,
leaf,
leaves.length - 1,
merkleTreeRaw.getProof(leaves.length - 1).proof,
);

await execute(provider, [finalize], [payerKeypair]);

try {
await execute(provider, [finalize], [payerKeypair]);
assert(false, 'Double finalizing should have failed');
} catch { }
} catch {}
});
}

);
});
describe('Having prepared a tree with canopy', () => {
const depth = 3;
const size = 8;
Expand Down Expand Up @@ -190,7 +212,7 @@ describe('Account Compression', () => {
another = anotherKeyPair.publicKey;
await provider.connection.confirmTransaction(
await provider.connection.requestAirdrop(another, 1e10),
'confirmed'
'confirmed',
);
});
it('Should be able to append a single canopy node', async () => {
Expand All @@ -210,40 +232,57 @@ describe('Account Compression', () => {
try {
await execute(provider, [appendIx], [anotherKeyPair]);
assert(false, 'Appending with another payer should have failed');
} catch { }
} catch {}
});
it('Should fail to append canopy nodes over the limit', async () => {
const appendIx = createAppendCanopyNodesIx(cmt, payer, Array.from({ length: 3 }, () => crypto.randomBytes(32)), 0);
const appendIx = createAppendCanopyNodesIx(
cmt,
payer,
Array.from({ length: 3 }, () => crypto.randomBytes(32)),
0,
);
try {
await execute(provider, [appendIx], [payerKeypair]);
assert(false, 'Appending over the limit should have failed');
} catch { }
} catch {}
});
it('Should fail to append canopy nodes over the limit starting from the last index', async () => {
const appendIx = createAppendCanopyNodesIx(cmt, payer, Array.from({ length: 2 }, () => crypto.randomBytes(32)), 1);
const appendIx = createAppendCanopyNodesIx(
cmt,
payer,
Array.from({ length: 2 }, () => crypto.randomBytes(32)),
1,
);
try {
await execute(provider, [appendIx], [payerKeypair]);
assert(false, 'Appending over the limit should have failed');
} catch { }
} catch {}
});
it('Should fail to append 0 canopy nodes', async () => {
const appendIx = createAppendCanopyNodesIx(cmt, payer, [], 0);
try {
await execute(provider, [appendIx], [payerKeypair]);
assert(false, 'Appending 0 nodes should have failed');
} catch { }
} catch {}
});
it('Should fail to finalize the tree without canopy', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
const root = merkleTreeRaw.root;
const leaf = leaves[leaves.length - 1];

const finalize = createFinalizeMerkleTreeWithRootIx(cmt, payer, root, leaf, leaves.length - 1, merkleTreeRaw.getProof(leaves.length - 1).proof);
const finalize = createFinalizeMerkleTreeWithRootIx(
cmt,
payer,
root,
leaf,
leaves.length - 1,
merkleTreeRaw.getProof(leaves.length - 1).proof,
);

try {
await execute(provider, [finalize], [payerKeypair]);
assert(false, 'Finalizing without canopy should have failed');
} catch { }
} catch {}
});
it('Should fail to finalize the tree with an incomplete canopy', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
Expand All @@ -252,87 +291,189 @@ describe('Account Compression', () => {

const appendIx = createAppendCanopyNodesIx(cmt, payer, [merkleTreeRaw.leaves[0].parent!.node!], 0);
await execute(provider, [appendIx], [payerKeypair]);
const finalize = createFinalizeMerkleTreeWithRootIx(cmt, payer, root, leaf, leaves.length - 1, merkleTreeRaw.getProof(leaves.length - 1).proof);
const finalize = createFinalizeMerkleTreeWithRootIx(
cmt,
payer,
root,
leaf,
leaves.length - 1,
merkleTreeRaw.getProof(leaves.length - 1).proof,
);

try {
await execute(provider, [finalize], [payerKeypair]);
assert(false, 'Finalization for an incomplete canopy should have failed');
} catch { }
} catch {}
});
it('Should finalize the tree with a complete canopy', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
const root = merkleTreeRaw.root;
const leaf = leaves[leaves.length - 1];

// take every second leaf and append it's parent node to the canopy
const appendIx = createAppendCanopyNodesIx(cmt, payer, merkleTreeRaw.leaves.filter((_, i) => i % 2 === 0).map(leaf => leaf.parent!.node!), 0);
const appendIx = createAppendCanopyNodesIx(
cmt,
payer,
merkleTreeRaw.leaves.filter((_, i) => i % 2 === 0).map(leaf => leaf.parent!.node!),
0,
);
await execute(provider, [appendIx], [payerKeypair]);
const finalize = createFinalizeMerkleTreeWithRootIx(cmt, payer, root, leaf, leaves.length - 1, merkleTreeRaw.getProof(leaves.length - 1).proof);
const finalize = createFinalizeMerkleTreeWithRootIx(
cmt,
payer,
root,
leaf,
leaves.length - 1,
merkleTreeRaw.getProof(leaves.length - 1).proof,
);
await execute(provider, [finalize], [payerKeypair]);
});
it('Should be able to setup canopy with several transactions', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
const root = merkleTreeRaw.root;
const leaf = leaves[leaves.length - 1];
// take every second leaf of the first half of a tree and append it's parent node to the canopy
const appendIx = createAppendCanopyNodesIx(cmt, payer, merkleTreeRaw.leaves.slice(0, leaves.length / 2).filter((_, i) => i % 2 === 0).map(leaf => leaf.parent!.node!), 0);
const appendIx = createAppendCanopyNodesIx(
cmt,
payer,
merkleTreeRaw.leaves
.slice(0, leaves.length / 2)
.filter((_, i) => i % 2 === 0)
.map(leaf => leaf.parent!.node!),
0,
);
await execute(provider, [appendIx], [payerKeypair]);
// take every second leaf of the second half of a tree and append it's parent node to the canopy
const appendIx2 = createAppendCanopyNodesIx(cmt, payer, merkleTreeRaw.leaves.slice(leaves.length / 2).filter((_, i) => i % 2 === 0).map(leaf => leaf.parent!.node!), 2);
const appendIx2 = createAppendCanopyNodesIx(
cmt,
payer,
merkleTreeRaw.leaves
.slice(leaves.length / 2)
.filter((_, i) => i % 2 === 0)
.map(leaf => leaf.parent!.node!),
2,
);
await execute(provider, [appendIx2], [payerKeypair]);
const finalize = createFinalizeMerkleTreeWithRootIx(cmt, payer, root, leaf, leaves.length - 1, merkleTreeRaw.getProof(leaves.length - 1).proof);
const finalize = createFinalizeMerkleTreeWithRootIx(
cmt,
payer,
root,
leaf,
leaves.length - 1,
merkleTreeRaw.getProof(leaves.length - 1).proof,
);
await execute(provider, [finalize], [payerKeypair]);
});
it('Should be able to setup canopy with several transactions in reverse order', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
const root = merkleTreeRaw.root;
const leaf = leaves[leaves.length - 1];

const appendIx = createAppendCanopyNodesIx(cmt, payer, merkleTreeRaw.leaves.slice(leaves.length / 2).filter((_, i) => i % 2 === 0).map(leaf => leaf.parent!.node!), 2);
const appendIx = createAppendCanopyNodesIx(
cmt,
payer,
merkleTreeRaw.leaves
.slice(leaves.length / 2)
.filter((_, i) => i % 2 === 0)
.map(leaf => leaf.parent!.node!),
2,
);
await execute(provider, [appendIx], [payerKeypair]);
const appendIx2 = createAppendCanopyNodesIx(cmt, payer, merkleTreeRaw.leaves.slice(0, leaves.length / 2).filter((_, i) => i % 2 === 0).map(leaf => leaf.parent!.node!), 0);
const appendIx2 = createAppendCanopyNodesIx(
cmt,
payer,
merkleTreeRaw.leaves
.slice(0, leaves.length / 2)
.filter((_, i) => i % 2 === 0)
.map(leaf => leaf.parent!.node!),
0,
);
await execute(provider, [appendIx2], [payerKeypair]);
const finalize = createFinalizeMerkleTreeWithRootIx(cmt, payer, root, leaf, leaves.length - 1, merkleTreeRaw.getProof(leaves.length - 1).proof);
const finalize = createFinalizeMerkleTreeWithRootIx(
cmt,
payer,
root,
leaf,
leaves.length - 1,
merkleTreeRaw.getProof(leaves.length - 1).proof,
);
await execute(provider, [finalize], [payerKeypair]);
});
it('Should be able to replace a canopy node', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
const root = merkleTreeRaw.root;
const leaf = leaves[leaves.length - 1];

const appendIx = createAppendCanopyNodesIx(cmt, payer, merkleTreeRaw.leaves.slice(0, leaves.length / 2).filter((_, i) => i % 2 === 0).map(leaf => leaf.parent!.node!), 0);
const appendIx = createAppendCanopyNodesIx(
cmt,
payer,
merkleTreeRaw.leaves
.slice(0, leaves.length / 2)
.filter((_, i) => i % 2 === 0)
.map(leaf => leaf.parent!.node!),
0,
);
await execute(provider, [appendIx], [payerKeypair]);
const appendIx2 = createAppendCanopyNodesIx(cmt, payer, [crypto.randomBytes(32)], 2);
await execute(provider, [appendIx2], [payerKeypair]);
const replaceIx = createAppendCanopyNodesIx(cmt, payer, merkleTreeRaw.leaves.slice(leaves.length / 2).filter((_, i) => i % 2 === 0).map(leaf => leaf.parent!.node!), 2);
const replaceIx = createAppendCanopyNodesIx(
cmt,
payer,
merkleTreeRaw.leaves
.slice(leaves.length / 2)
.filter((_, i) => i % 2 === 0)
.map(leaf => leaf.parent!.node!),
2,
);
await execute(provider, [replaceIx], [payerKeypair]);
const finalize = createFinalizeMerkleTreeWithRootIx(cmt, payer, root, leaf, leaves.length - 1, merkleTreeRaw.getProof(leaves.length - 1).proof);
const finalize = createFinalizeMerkleTreeWithRootIx(
cmt,
payer,
root,
leaf,
leaves.length - 1,
merkleTreeRaw.getProof(leaves.length - 1).proof,
);
await execute(provider, [finalize], [payerKeypair]);
});
it('Should fail to replace a canopy node for a finalised tree', async () => {
const merkleTreeRaw = new MerkleTree(leaves);
const root = merkleTreeRaw.root;
const leaf = leaves[leaves.length - 1];

const appendIx = createAppendCanopyNodesIx(cmt, payer, merkleTreeRaw.leaves.filter((_, i) => i % 2 === 0).map(leaf => leaf.parent!.node!), 0);
const appendIx = createAppendCanopyNodesIx(
cmt,
payer,
merkleTreeRaw.leaves.filter((_, i) => i % 2 === 0).map(leaf => leaf.parent!.node!),
0,
);
await execute(provider, [appendIx], [payerKeypair]);
const finalize = createFinalizeMerkleTreeWithRootIx(cmt, payer, root, leaf, leaves.length - 1, merkleTreeRaw.getProof(leaves.length - 1).proof);
const finalize = createFinalizeMerkleTreeWithRootIx(
cmt,
payer,
root,
leaf,
leaves.length - 1,
merkleTreeRaw.getProof(leaves.length - 1).proof,
);
await execute(provider, [finalize], [payerKeypair]);
const replaceIx = createAppendCanopyNodesIx(cmt, payer, [crypto.randomBytes(32)], 0);
try {
await execute(provider, [replaceIx], [payerKeypair]);
assert(false, 'Replacing a canopy node for a finalised tree should have failed');
} catch { }
} catch {}
});
it('Should fail to initialize an empty tree after preparing a tree', async () => {
const ixs = [createInitEmptyMerkleTreeIx(cmt, payer, {
maxBufferSize: size,
maxDepth: depth,
})];
const ixs = [
createInitEmptyMerkleTreeIx(cmt, payer, {
maxBufferSize: size,
maxDepth: depth,
}),
];
try {
await execute(provider, ixs, [payerKeypair]);
assert(false, 'Initializing an empty tree after preparing a tree should have failed');
} catch { }
} catch {}
});
});

Expand Down
Loading

0 comments on commit 366f2bc

Please sign in to comment.