-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat/validator tests #388
Open
mani99brar
wants to merge
9
commits into
dev
Choose a base branch
from
feat/validator-tests
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat/validator tests #388
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a1b4008
feat: transaction handler & custom logging,errors
295a2e7
feat: test init
2d2c850
feat: epoch handler
25b88b6
feat: claim helper
759fd1f
fix: epoch tests
a38d3ea
feat: watcher & validator
c4fa598
fix: sendSnapshot txn update
76dc985
chore: dev comments
f9a91e9
chore: added enums
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Config } from "jest"; | ||
|
||
const config: Config = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
collectCoverage: true, | ||
collectCoverageFrom: ["**/*.ts"], | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,304 @@ | ||
import { ArbToEthTransactionHandler, ContractType } from "./transactionHandler"; | ||
import { MockEmitter, defaultEmitter } from "../utils/emitter"; | ||
import { BotEvents } from "../utils/botEvents"; | ||
import { ClaimNotSetError } from "../utils/errors"; | ||
import { ClaimStruct } from "@kleros/vea-contracts/typechain-types/arbitrumToEth/VeaInboxArbToEth"; | ||
|
||
describe("ArbToEthTransactionHandler", () => { | ||
let epoch: number = 100; | ||
let veaInbox: any; | ||
let veaOutbox: any; | ||
let veaInboxProvider: any; | ||
let veaOutboxProvider: any; | ||
let claim: ClaimStruct = null; | ||
|
||
beforeEach(() => { | ||
veaInboxProvider = { | ||
getTransactionReceipt: jest.fn(), | ||
getBlock: jest.fn(), | ||
}; | ||
veaOutbox = { | ||
estimateGas: jest.fn(), | ||
withdrawChallengeDeposit: jest.fn(), | ||
["challenge(uint256,(bytes32,address,uint32,uint32,uint32,uint8,address))"]: jest.fn(), | ||
}; | ||
veaInbox = { | ||
sendSnapshot: jest.fn(), | ||
}; | ||
claim = { | ||
stateRoot: "0x1234", | ||
claimer: "0x1234", | ||
timestampClaimed: 1234, | ||
timestampVerification: 0, | ||
blocknumberVerification: 0, | ||
honest: 0, | ||
challenger: "0x1234", | ||
}; | ||
}); | ||
|
||
describe("constructor", () => { | ||
it("should create a new TransactionHandler without claim", () => { | ||
const transactionHandler = new ArbToEthTransactionHandler( | ||
epoch, | ||
veaInbox, | ||
veaOutbox, | ||
veaInboxProvider, | ||
veaOutboxProvider | ||
); | ||
expect(transactionHandler).toBeDefined(); | ||
expect(transactionHandler.epoch).toEqual(epoch); | ||
expect(transactionHandler.veaOutbox).toEqual(veaOutbox); | ||
expect(transactionHandler.emitter).toEqual(defaultEmitter); | ||
}); | ||
|
||
it("should create a new TransactionHandler with claim", () => { | ||
const transactionHandler = new ArbToEthTransactionHandler( | ||
epoch, | ||
veaInbox, | ||
veaOutbox, | ||
veaInboxProvider, | ||
veaOutboxProvider, | ||
defaultEmitter, | ||
claim | ||
); | ||
expect(transactionHandler).toBeDefined(); | ||
expect(transactionHandler.epoch).toEqual(epoch); | ||
expect(transactionHandler.veaOutbox).toEqual(veaOutbox); | ||
expect(transactionHandler.claim).toEqual(claim); | ||
expect(transactionHandler.emitter).toEqual(defaultEmitter); | ||
}); | ||
}); | ||
|
||
describe("checkTransactionStatus", () => { | ||
let transactionHandler: ArbToEthTransactionHandler; | ||
let finalityBlock: number = 100; | ||
const mockEmitter = new MockEmitter(); | ||
beforeEach(() => { | ||
transactionHandler = new ArbToEthTransactionHandler( | ||
epoch, | ||
veaInbox, | ||
veaOutbox, | ||
veaInboxProvider, | ||
veaOutboxProvider, | ||
mockEmitter | ||
); | ||
veaInboxProvider.getBlock.mockResolvedValue({ number: finalityBlock }); | ||
}); | ||
|
||
it("should return 2 if transaction is not final", async () => { | ||
jest.spyOn(mockEmitter, "emit"); | ||
veaInboxProvider.getTransactionReceipt.mockResolvedValue({ | ||
blockNumber: finalityBlock - (transactionHandler.requiredConfirmations - 1), | ||
}); | ||
const trnxHash = "0x123456"; | ||
const status = await transactionHandler.checkTransactionStatus(trnxHash, ContractType.INBOX); | ||
expect(status).toEqual(2); | ||
expect(mockEmitter.emit).toHaveBeenCalledWith( | ||
BotEvents.TXN_NOT_FINAL, | ||
trnxHash, | ||
transactionHandler.requiredConfirmations - 1 | ||
); | ||
}); | ||
|
||
it("should return 1 if transaction is pending", async () => { | ||
jest.spyOn(mockEmitter, "emit"); | ||
veaInboxProvider.getTransactionReceipt.mockResolvedValue(null); | ||
const trnxHash = "0x123456"; | ||
const status = await transactionHandler.checkTransactionStatus(trnxHash, ContractType.INBOX); | ||
expect(status).toEqual(1); | ||
expect(mockEmitter.emit).toHaveBeenCalledWith(BotEvents.TXN_PENDING, trnxHash); | ||
}); | ||
|
||
it("should return 3 if transaction is final", async () => { | ||
jest.spyOn(mockEmitter, "emit"); | ||
veaInboxProvider.getTransactionReceipt.mockResolvedValue({ | ||
blockNumber: finalityBlock - transactionHandler.requiredConfirmations, | ||
}); | ||
const trnxHash = "0x123456"; | ||
const status = await transactionHandler.checkTransactionStatus(trnxHash, ContractType.INBOX); | ||
expect(status).toEqual(3); | ||
expect(mockEmitter.emit).toHaveBeenCalledWith( | ||
BotEvents.TXN_FINAL, | ||
trnxHash, | ||
transactionHandler.requiredConfirmations | ||
); | ||
}); | ||
|
||
it("should return 0 if transaction hash is null", async () => { | ||
const trnxHash = null; | ||
const status = await transactionHandler.checkTransactionStatus(trnxHash, ContractType.INBOX); | ||
expect(status).toEqual(0); | ||
}); | ||
}); | ||
|
||
describe("challengeClaim", () => { | ||
let transactionHandler: ArbToEthTransactionHandler; | ||
const mockEmitter = new MockEmitter(); | ||
beforeEach(() => { | ||
transactionHandler = new ArbToEthTransactionHandler( | ||
epoch, | ||
veaInbox, | ||
veaOutbox, | ||
veaInboxProvider, | ||
veaOutboxProvider, | ||
mockEmitter | ||
); | ||
transactionHandler.claim = claim; | ||
}); | ||
|
||
it("should emit CHALLENGING event and throw error if claim is not set", async () => { | ||
jest.spyOn(mockEmitter, "emit"); | ||
transactionHandler.claim = null; | ||
await expect(transactionHandler.challengeClaim()).rejects.toThrow(ClaimNotSetError); | ||
expect(mockEmitter.emit).toHaveBeenCalledWith(BotEvents.CHALLENGING); | ||
}); | ||
|
||
it("should not challenge claim if txn is pending", async () => { | ||
jest.spyOn(transactionHandler, "checkTransactionStatus").mockResolvedValue(1); | ||
transactionHandler.transactions.challengeTxn = "0x1234"; | ||
await transactionHandler.challengeClaim(); | ||
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith( | ||
transactionHandler.transactions.challengeTxn, | ||
ContractType.OUTBOX | ||
); | ||
expect(veaOutbox.estimateGas).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should challenge claim", async () => { | ||
jest.spyOn(transactionHandler, "checkTransactionStatus").mockResolvedValue(0); | ||
const mockChallenge = jest.fn().mockResolvedValue({ hash: "0x1234" }) as any; | ||
(mockChallenge as any).estimateGas = jest.fn().mockResolvedValue(BigInt(100000)); | ||
veaOutbox["challenge(uint256,(bytes32,address,uint32,uint32,uint32,uint8,address))"] = mockChallenge; | ||
await transactionHandler.challengeClaim(); | ||
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith(null, ContractType.OUTBOX); | ||
expect(transactionHandler.transactions.challengeTxn).toEqual("0x1234"); | ||
}); | ||
|
||
it.todo("should set challengeTxn as completed when txn is final"); | ||
}); | ||
|
||
describe("withdrawDeposit", () => { | ||
let transactionHandler: ArbToEthTransactionHandler; | ||
const mockEmitter = new MockEmitter(); | ||
beforeEach(() => { | ||
transactionHandler = new ArbToEthTransactionHandler( | ||
epoch, | ||
veaInbox, | ||
veaOutbox, | ||
veaInboxProvider, | ||
veaOutboxProvider, | ||
mockEmitter | ||
); | ||
veaOutbox.withdrawChallengeDeposit.mockResolvedValue("0x1234"); | ||
transactionHandler.claim = claim; | ||
}); | ||
|
||
it("should withdraw deposit", async () => { | ||
jest.spyOn(transactionHandler, "checkTransactionStatus").mockResolvedValue(0); | ||
veaOutbox.withdrawChallengeDeposit.mockResolvedValue({ hash: "0x1234" }); | ||
await transactionHandler.withdrawChallengeDeposit(); | ||
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith(null, ContractType.OUTBOX); | ||
expect(transactionHandler.transactions.withdrawChallengeDepositTxn).toEqual("0x1234"); | ||
}); | ||
|
||
it("should not withdraw deposit if txn is pending", async () => { | ||
jest.spyOn(transactionHandler, "checkTransactionStatus").mockResolvedValue(1); | ||
transactionHandler.transactions.withdrawChallengeDepositTxn = "0x1234"; | ||
await transactionHandler.withdrawChallengeDeposit(); | ||
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith( | ||
transactionHandler.transactions.withdrawChallengeDepositTxn, | ||
ContractType.OUTBOX | ||
); | ||
}); | ||
|
||
it("should throw an error if claim is not set", async () => { | ||
transactionHandler.claim = null; | ||
await expect(transactionHandler.withdrawChallengeDeposit()).rejects.toThrow(ClaimNotSetError); | ||
}); | ||
|
||
it("should emit WITHDRAWING event", async () => { | ||
jest.spyOn(mockEmitter, "emit"); | ||
await transactionHandler.withdrawChallengeDeposit(); | ||
expect(mockEmitter.emit).toHaveBeenCalledWith(BotEvents.WITHDRAWING); | ||
}); | ||
}); | ||
|
||
describe("sendSnapshot", () => { | ||
let transactionHandler: ArbToEthTransactionHandler; | ||
const mockEmitter = new MockEmitter(); | ||
beforeEach(() => { | ||
transactionHandler = new ArbToEthTransactionHandler( | ||
epoch, | ||
veaInbox, | ||
veaOutbox, | ||
veaInboxProvider, | ||
veaOutboxProvider, | ||
mockEmitter | ||
); | ||
transactionHandler.claim = claim; | ||
}); | ||
|
||
it("should send snapshot", async () => { | ||
jest.spyOn(transactionHandler, "checkTransactionStatus").mockResolvedValue(0); | ||
veaInbox.sendSnapshot.mockResolvedValue({ hash: "0x1234" }); | ||
await transactionHandler.sendSnapshot(); | ||
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith(null, ContractType.INBOX); | ||
expect(transactionHandler.transactions.sendSnapshotTxn).toEqual("0x1234"); | ||
}); | ||
|
||
it("should not send snapshot if txn is pending", async () => { | ||
jest.spyOn(transactionHandler, "checkTransactionStatus").mockResolvedValue(1); | ||
transactionHandler.transactions.sendSnapshotTxn = "0x1234"; | ||
await transactionHandler.sendSnapshot(); | ||
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith( | ||
transactionHandler.transactions.sendSnapshotTxn, | ||
ContractType.INBOX | ||
); | ||
expect(veaInbox.sendSnapshot).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should throw an error if claim is not set", async () => { | ||
jest.spyOn(mockEmitter, "emit"); | ||
transactionHandler.claim = null; | ||
await expect(transactionHandler.sendSnapshot()).rejects.toThrow(ClaimNotSetError); | ||
expect(mockEmitter.emit).toHaveBeenCalledWith(BotEvents.SENDING_SNAPSHOT, epoch); | ||
}); | ||
}); | ||
|
||
describe("resolveChallengedClaim", () => { | ||
let mockMessageExecutor: any; | ||
let transactionHandler: ArbToEthTransactionHandler; | ||
const mockEmitter = new MockEmitter(); | ||
beforeEach(() => { | ||
mockMessageExecutor = jest.fn(); | ||
transactionHandler = new ArbToEthTransactionHandler( | ||
epoch, | ||
veaInbox, | ||
veaOutbox, | ||
veaInboxProvider, | ||
veaOutboxProvider, | ||
mockEmitter | ||
); | ||
}); | ||
it("should resolve challenged claim", async () => { | ||
jest.spyOn(transactionHandler, "checkTransactionStatus").mockResolvedValue(0); | ||
transactionHandler.transactions.sendSnapshotTxn = "0x1234"; | ||
mockMessageExecutor.mockResolvedValue({ hash: "0x1234" }); | ||
await transactionHandler.resolveChallengedClaim( | ||
transactionHandler.transactions.sendSnapshotTxn, | ||
mockMessageExecutor | ||
); | ||
expect(transactionHandler.transactions.executeSnapshotTxn).toEqual("0x1234"); | ||
}); | ||
|
||
it("should not resolve challenged claim if txn is pending", async () => { | ||
jest.spyOn(transactionHandler, "checkTransactionStatus").mockResolvedValue(1); | ||
transactionHandler.transactions.executeSnapshotTxn = "0x1234"; | ||
await transactionHandler.resolveChallengedClaim(mockMessageExecutor); | ||
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith( | ||
transactionHandler.transactions.executeSnapshotTxn, | ||
ContractType.OUTBOX | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consolidate duplicate RPC endpoints.
The same RPC URLs are defined multiple times:
RPC_ARB_SEPOLIA
andRPC_ARB
both point tohttps://sepolia-rollup.arbitrum.io/rpc
RPC_CHIADO
andRPC_GNOSIS
both point tohttps://rpc.chiadochain.net
Consider consolidating these into single variables with clear naming to avoid confusion:
📝 Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RPC urls are same but the env vars are different for devnet and testnet/mainnet. So it's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mani99brar, understood. Thanks for clarifying your approach regarding separate environment variables for devnet vs. testnet/mainnet. No changes needed here!
✏️ Learnings added