|
1 | 1 | import { expect } from "chai"; |
2 | 2 | import hre from "hardhat"; |
3 | 3 | const { ethers } = hre; |
| 4 | +import { upgrades } from "hardhat"; |
4 | 5 |
|
5 | 6 | function randomBytes32(): string { |
6 | 7 | return ethers.hexlify(ethers.randomBytes(32)); |
@@ -50,29 +51,23 @@ export function shouldBehaveLikeCommitmentRegistry(): void { |
50 | 51 |
|
51 | 52 | it("should revert when initializing with zero owner", async function () { |
52 | 53 | const CommitmentRegistry = await ethers.getContractFactory("CommitmentRegistry"); |
53 | | - const impl = await CommitmentRegistry.deploy(); |
54 | | - await impl.waitForDeployment(); |
55 | | - const ERC1967Proxy = await ethers.getContractFactory("ERC1967Proxy"); |
56 | | - const initData = CommitmentRegistry.interface.encodeFunctionData("initialize", [ |
57 | | - ethers.ZeroAddress, |
58 | | - this.poster.address, |
59 | | - ]); |
60 | 54 | await expect( |
61 | | - ERC1967Proxy.deploy(await impl.getAddress(), initData) |
| 55 | + upgrades.deployProxy( |
| 56 | + CommitmentRegistry, |
| 57 | + [ethers.ZeroAddress, this.poster.address], |
| 58 | + { kind: "uups", initializer: "initialize" }, |
| 59 | + ) |
62 | 60 | ).to.be.reverted; |
63 | 61 | }); |
64 | 62 |
|
65 | 63 | it("should revert when initializing with zero poster", async function () { |
66 | 64 | const CommitmentRegistry = await ethers.getContractFactory("CommitmentRegistry"); |
67 | | - const impl = await CommitmentRegistry.deploy(); |
68 | | - await impl.waitForDeployment(); |
69 | | - const ERC1967Proxy = await ethers.getContractFactory("ERC1967Proxy"); |
70 | | - const initData = CommitmentRegistry.interface.encodeFunctionData("initialize", [ |
71 | | - this.owner.address, |
72 | | - ethers.ZeroAddress, |
73 | | - ]); |
74 | 65 | await expect( |
75 | | - ERC1967Proxy.deploy(await impl.getAddress(), initData) |
| 66 | + upgrades.deployProxy( |
| 67 | + CommitmentRegistry, |
| 68 | + [this.owner.address, ethers.ZeroAddress], |
| 69 | + { kind: "uups", initializer: "initialize" }, |
| 70 | + ) |
76 | 71 | ).to.be.reverted; |
77 | 72 | }); |
78 | 73 | }); |
@@ -593,6 +588,70 @@ export function shouldBehaveLikeCommitmentRegistry(): void { |
593 | 588 | expect(await this.registry.getSize(VERSION_1)).to.equal(1); |
594 | 589 | }); |
595 | 590 |
|
| 591 | + it("should return handles with getHandles pagination", async function () { |
| 592 | + await this.registry.setVersionStatus(VERSION_1, VersionStatus.Active); |
| 593 | + const handles = Array.from({ length: 10 }, () => randomBytes32()); |
| 594 | + const commitHashes = Array.from({ length: 10 }, () => randomBytes32()); |
| 595 | + const registryAsPoster = this.registry.connect(this.poster); |
| 596 | + await registryAsPoster.postCommitments(VERSION_1, handles, commitHashes); |
| 597 | + |
| 598 | + // First page |
| 599 | + const page1 = await this.registry.getHandles(VERSION_1, 0, 5); |
| 600 | + expect(page1.length).to.equal(5); |
| 601 | + for (let i = 0; i < 5; i++) { |
| 602 | + expect(page1[i]).to.equal(handles[i]); |
| 603 | + } |
| 604 | + |
| 605 | + // Second page |
| 606 | + const page2 = await this.registry.getHandles(VERSION_1, 5, 5); |
| 607 | + expect(page2.length).to.equal(5); |
| 608 | + for (let i = 0; i < 5; i++) { |
| 609 | + expect(page2[i]).to.equal(handles[5 + i]); |
| 610 | + } |
| 611 | + }); |
| 612 | + |
| 613 | + it("should return empty array when offset exceeds total", async function () { |
| 614 | + await this.registry.setVersionStatus(VERSION_1, VersionStatus.Active); |
| 615 | + const registryAsPoster = this.registry.connect(this.poster); |
| 616 | + await registryAsPoster.postCommitments(VERSION_1, [randomBytes32()], [randomBytes32()]); |
| 617 | + |
| 618 | + const result = await this.registry.getHandles(VERSION_1, 100, 10); |
| 619 | + expect(result.length).to.equal(0); |
| 620 | + }); |
| 621 | + |
| 622 | + it("should clamp limit when it exceeds remaining items", async function () { |
| 623 | + await this.registry.setVersionStatus(VERSION_1, VersionStatus.Active); |
| 624 | + const handles = Array.from({ length: 3 }, () => randomBytes32()); |
| 625 | + const commitHashes = Array.from({ length: 3 }, () => randomBytes32()); |
| 626 | + const registryAsPoster = this.registry.connect(this.poster); |
| 627 | + await registryAsPoster.postCommitments(VERSION_1, handles, commitHashes); |
| 628 | + |
| 629 | + const result = await this.registry.getHandles(VERSION_1, 1, 100); |
| 630 | + expect(result.length).to.equal(2); |
| 631 | + expect(result[0]).to.equal(handles[1]); |
| 632 | + expect(result[1]).to.equal(handles[2]); |
| 633 | + }); |
| 634 | + |
| 635 | + it("should return handle by index", async function () { |
| 636 | + await this.registry.setVersionStatus(VERSION_1, VersionStatus.Active); |
| 637 | + const handles = Array.from({ length: 5 }, () => randomBytes32()); |
| 638 | + const commitHashes = Array.from({ length: 5 }, () => randomBytes32()); |
| 639 | + const registryAsPoster = this.registry.connect(this.poster); |
| 640 | + await registryAsPoster.postCommitments(VERSION_1, handles, commitHashes); |
| 641 | + |
| 642 | + for (let i = 0; i < 5; i++) { |
| 643 | + expect(await this.registry.getHandleByIndex(VERSION_1, i)).to.equal(handles[i]); |
| 644 | + } |
| 645 | + }); |
| 646 | + |
| 647 | + it("should revert when getHandleByIndex is out of bounds", async function () { |
| 648 | + await this.registry.setVersionStatus(VERSION_1, VersionStatus.Active); |
| 649 | + const registryAsPoster = this.registry.connect(this.poster); |
| 650 | + await registryAsPoster.postCommitments(VERSION_1, [randomBytes32()], [randomBytes32()]); |
| 651 | + |
| 652 | + await expect(this.registry.getHandleByIndex(VERSION_1, 1)).to.be.reverted; |
| 653 | + }); |
| 654 | + |
596 | 655 | it("should still return commitments after version is Revoked", async function () { |
597 | 656 | await this.registry.setVersionStatus(VERSION_1, VersionStatus.Active); |
598 | 657 | const handle = randomBytes32(); |
|
0 commit comments