Skip to content

Commit

Permalink
chore: added enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Mani Brar authored and Mani Brar committed Jan 8, 2025
1 parent 76dc985 commit f9a91e9
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 135 deletions.
8 changes: 2 additions & 6 deletions validator-cli/src/ArbToEth/transactionHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,15 @@ describe("ArbToEthTransactionHandler", () => {
transactionHandler.transactions.sendSnapshotTxn,
mockMessageExecutor
);
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith(
transactionHandler.transactions.sendSnapshotTxn,
ContractType.OUTBOX
);
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.sendSnapshotTxn = "0x1234";
transactionHandler.transactions.executeSnapshotTxn = "0x1234";
await transactionHandler.resolveChallengedClaim(mockMessageExecutor);
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith(
transactionHandler.transactions.sendSnapshotTxn,
transactionHandler.transactions.executeSnapshotTxn,
ContractType.OUTBOX
);
});
Expand Down
41 changes: 28 additions & 13 deletions validator-cli/src/ArbToEth/transactionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ type Transactions = {
executeSnapshotTxn: string | null;
};

enum TransactionStatus {
NOT_MADE = 0,
PENDING = 1,
NOT_FINAL = 2,
FINAL = 3,
}

export enum ContractType {
INBOX = "inbox",
OUTBOX = "outbox",
Expand Down Expand Up @@ -71,9 +78,9 @@ export class ArbToEthTransactionHandler {
* @param trnxHash Transaction hash to check the status of.
* @param contract Contract type to check the transaction status in.
*
* @returns False if transaction is pending || not final || not made, else True.
* @returns TransactionStatus.
*/
public async checkTransactionStatus(trnxHash: string | null, contract: ContractType): Promise<number> {
public async checkTransactionStatus(trnxHash: string | null, contract: ContractType): Promise<TransactionStatus> {
let provider: JsonRpcProvider;
if (contract === ContractType.INBOX) {
provider = this.veaInboxProvider;
Expand All @@ -82,27 +89,25 @@ export class ArbToEthTransactionHandler {
}

if (trnxHash == null) {
return 0;
return TransactionStatus.NOT_MADE;
}

const receipt = await provider.getTransactionReceipt(trnxHash);

if (!receipt) {
// TODO: Add transaction pending timeout- redo transaction.
this.emitter.emit(BotEvents.TXN_PENDING, trnxHash);
return 1;
return TransactionStatus.PENDING;
}

const currentBlock = await provider.getBlock("latest");
const confirmations = currentBlock.number - receipt.blockNumber;

if (confirmations >= this.requiredConfirmations) {
this.emitter.emit(BotEvents.TXN_FINAL, trnxHash, confirmations);
return 3;
} else {
this.emitter.emit(BotEvents.TXN_NOT_FINAL, trnxHash, confirmations);
return 2;
return TransactionStatus.FINAL;
}
this.emitter.emit(BotEvents.TXN_NOT_FINAL, trnxHash, confirmations);
return TransactionStatus.NOT_FINAL;
}

/**
Expand All @@ -114,7 +119,8 @@ export class ArbToEthTransactionHandler {
if (!this.claim) {
throw new ClaimNotSetError();
}
if ((await this.checkTransactionStatus(this.transactions.challengeTxn, ContractType.OUTBOX)) > 0) {
const transactionStatus = await this.checkTransactionStatus(this.transactions.challengeTxn, ContractType.OUTBOX);
if (transactionStatus > 0) {
return;
}
const { deposit } = getBridgeConfig(this.chainId);
Expand Down Expand Up @@ -152,7 +158,11 @@ export class ArbToEthTransactionHandler {
if (!this.claim) {
throw new ClaimNotSetError();
}
if ((await this.checkTransactionStatus(this.transactions.withdrawChallengeDepositTxn, ContractType.OUTBOX)) > 0) {
const transactionStatus = await this.checkTransactionStatus(
this.transactions.withdrawChallengeDepositTxn,
ContractType.OUTBOX
);
if (transactionStatus > 0) {
return;
}
const withdrawDepositTxn = await this.veaOutbox.withdrawChallengeDeposit(this.epoch, this.claim);
Expand All @@ -168,7 +178,8 @@ export class ArbToEthTransactionHandler {
if (!this.claim) {
throw new ClaimNotSetError();
}
if ((await this.checkTransactionStatus(this.transactions.sendSnapshotTxn, ContractType.INBOX)) > 0) {
const transactionStatus = await this.checkTransactionStatus(this.transactions.sendSnapshotTxn, ContractType.INBOX);
if (transactionStatus > 0) {
return;
}
const sendSnapshotTxn = await this.veaInbox.sendSnapshot(this.epoch, this.claim);
Expand All @@ -181,7 +192,11 @@ export class ArbToEthTransactionHandler {
*/
public async resolveChallengedClaim(sendSnapshotTxn: string, executeMsg: typeof messageExecutor = messageExecutor) {
this.emitter.emit(BotEvents.EXECUTING_SNAPSHOT, this.epoch);
if ((await this.checkTransactionStatus(this.transactions.sendSnapshotTxn, ContractType.OUTBOX)) > 0) {
const transactionStatus = await this.checkTransactionStatus(
this.transactions.executeSnapshotTxn,
ContractType.OUTBOX
);
if (transactionStatus > 0) {
return;
}
const msgExecuteTrnx = await executeMsg(sendSnapshotTxn, this.veaInboxProvider, this.veaOutboxProvider);
Expand Down
114 changes: 34 additions & 80 deletions validator-cli/src/ArbToEth/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("validator", () => {
let mockClaim: any;
let mockGetClaimState: any;
let mockGetBlockFinality: any;

let mockDeps: any;
beforeEach(() => {
veaInbox = {
snapshots: jest.fn(),
Expand Down Expand Up @@ -41,24 +41,25 @@ describe("validator", () => {
};
mockGetClaim = jest.fn();
mockGetBlockFinality = jest.fn().mockResolvedValue([{ number: 0 }, { number: 0, timestamp: 100 }, false]);
mockDeps = {
epoch: 0,
epochPeriod: 10,
veaInbox,
veaInboxProvider,
veaOutboxProvider,
veaOutbox,
transactionHandler: null,
emitter,
fetchClaim: mockGetClaim,
fetchClaimResolveState: mockGetClaimState,
fetchBlocksAndCheckFinality: mockGetBlockFinality,
};
});
describe("challengeAndResolveClaim", () => {
it("should return null if no claim is made", async () => {
const transactionHandler = null;
mockGetClaim = jest.fn().mockReturnValue(null);
const result = await challengeAndResolveClaim(
0,
10,
veaInbox,
veaInboxProvider,
veaOutbox,
veaOutboxProvider,
transactionHandler,
emitter,
mockGetClaim,
mockGetClaimState,
mockGetBlockFinality
);
mockDeps.fetchClaim = mockGetClaim;
const result = await challengeAndResolveClaim(mockDeps);

expect(result).toBeNull();
expect(emitter.emit).toHaveBeenCalledWith(BotEvents.NO_CLAIM, 0);
Expand All @@ -77,19 +78,10 @@ describe("validator", () => {
};
veaInbox.snapshots = jest.fn().mockReturnValue("0x321");
mockGetClaim = jest.fn().mockReturnValue(mockClaim);
const updatedTransactionHandler = await challengeAndResolveClaim(
0,
10,
veaInbox,
veaInboxProvider,
veaOutbox,
veaOutboxProvider,
mockTransactionHandler as any,
emitter,
mockGetClaim,
mockGetClaimState,
mockGetBlockFinality
);

mockDeps.transactionHandler = mockTransactionHandler;
mockDeps.fetchClaim = mockGetClaim;
const updatedTransactionHandler = await challengeAndResolveClaim(mockDeps);
expect(updatedTransactionHandler.transactions.challengeTxn).toBe(challengeTxn);
expect(mockTransactionHandler.challengeClaim).toHaveBeenCalled();
});
Expand All @@ -98,19 +90,8 @@ describe("validator", () => {
mockClaim.challenger = mockClaim.claimer;
mockGetClaim = jest.fn().mockReturnValue(mockClaim);
veaInbox.snapshots = jest.fn().mockReturnValue(mockClaim.stateRoot);
const updatedTransactionHandler = await challengeAndResolveClaim(
0,
10,
veaInbox,
veaInboxProvider,
veaOutbox,
veaOutboxProvider,
null,
emitter,
mockGetClaim,
mockGetClaimState,
mockGetBlockFinality
);
mockDeps.fetchClaim = mockGetClaim;
const updatedTransactionHandler = await challengeAndResolveClaim(mockDeps);
expect(updatedTransactionHandler).toBeNull();
});

Expand All @@ -130,19 +111,10 @@ describe("validator", () => {
sendSnapshotTxn: "0x0",
},
};
const updatedTransactionHandler = await challengeAndResolveClaim(
0,
10,
veaInbox,
veaInboxProvider,
veaOutbox,
veaOutboxProvider,
mockTransactionHandler as any,
emitter,
mockGetClaim,
mockGetClaimState,
mockGetBlockFinality
);
mockDeps.transactionHandler = mockTransactionHandler;
mockDeps.fetchClaimResolveState = mockGetClaimState;
mockDeps.fetchClaim = mockGetClaim;
const updatedTransactionHandler = await challengeAndResolveClaim(mockDeps);
expect(updatedTransactionHandler.transactions.sendSnapshotTxn).toEqual("0x123");
expect(mockTransactionHandler.sendSnapshot).toHaveBeenCalled();
expect(updatedTransactionHandler.claim).toEqual(mockClaim);
Expand All @@ -164,19 +136,10 @@ describe("validator", () => {
executeSnapshotTxn: "0x0",
},
};
const updatedTransactionHandler = await challengeAndResolveClaim(
0,
10,
veaInbox,
veaInboxProvider,
veaOutbox,
veaOutboxProvider,
mockTransactionHandler as any,
emitter,
mockGetClaim,
mockGetClaimState,
mockGetBlockFinality
);
mockDeps.transactionHandler = mockTransactionHandler;
mockDeps.fetchClaimResolveState = mockGetClaimState;
mockDeps.fetchClaim = mockGetClaim;
const updatedTransactionHandler = await challengeAndResolveClaim(mockDeps);
expect(updatedTransactionHandler.transactions.executeSnapshotTxn).toEqual("0x123");
expect(mockTransactionHandler.resolveChallengedClaim).toHaveBeenCalled();
expect(updatedTransactionHandler.claim).toEqual(mockClaim);
Expand All @@ -199,19 +162,10 @@ describe("validator", () => {
withdrawChallengeDepositTxn: "0x0",
},
};
const updatedTransactionHandler = await challengeAndResolveClaim(
0,
10,
veaInbox,
veaInboxProvider,
veaOutbox,
veaOutboxProvider,
mockTransactionHandler as any,
emitter,
mockGetClaim,
mockGetClaimState,
mockGetBlockFinality
);
mockDeps.transactionHandler = mockTransactionHandler;
mockDeps.fetchClaimResolveState = mockGetClaimState;
mockDeps.fetchClaim = mockGetClaim;
const updatedTransactionHandler = await challengeAndResolveClaim(mockDeps);
expect(updatedTransactionHandler.transactions.withdrawChallengeDepositTxn).toEqual("0x1234");
expect(mockTransactionHandler.withdrawChallengeDeposit).toHaveBeenCalled();
expect(updatedTransactionHandler.claim).toEqual(mockClaim);
Expand Down
40 changes: 27 additions & 13 deletions validator-cli/src/ArbToEth/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,33 @@ import { getBlocksAndCheckFinality } from "../utils/arbToEthState";
// https://github.com/prysmaticlabs/prysm/blob/493905ee9e33a64293b66823e69704f012b39627/config/params/mainnet_config.go#L103
const secondsPerSlotEth = 12;

export async function challengeAndResolveClaim(
epoch: number,
epochPeriod: number,
veaInbox: VeaInboxArbToEth,
veaInboxProvider: JsonRpcProvider,
veaOutboxProvider: JsonRpcProvider,
veaOutbox: VeaOutboxArbToEth,
transactionHandler: ArbToEthTransactionHandler | null,
emitter: typeof defaultEmitter = defaultEmitter,
fetchClaim: typeof getClaim = getClaim,
fetchClaimResolveState: typeof getClaimResolveState = getClaimResolveState,
fetchBlocksAndCheckFinality: typeof getBlocksAndCheckFinality = getBlocksAndCheckFinality
): Promise<ArbToEthTransactionHandler | null> {
export interface ChallengeAndResolveClaimParams {
epoch: number;
epochPeriod: number;
veaInbox: any;
veaInboxProvider: JsonRpcProvider;
veaOutboxProvider: JsonRpcProvider;
veaOutbox: any;
transactionHandler: ArbToEthTransactionHandler | null;
emitter?: typeof defaultEmitter;
fetchClaim?: typeof getClaim;
fetchClaimResolveState?: typeof getClaimResolveState;
fetchBlocksAndCheckFinality?: typeof getBlocksAndCheckFinality;
}

export async function challengeAndResolveClaim({
epoch,
epochPeriod,
veaInbox,
veaInboxProvider,
veaOutboxProvider,
veaOutbox,
transactionHandler,
emitter = defaultEmitter,
fetchClaim = getClaim,
fetchClaimResolveState = getClaimResolveState,
fetchBlocksAndCheckFinality = getBlocksAndCheckFinality,
}: ChallengeAndResolveClaimParams): Promise<ArbToEthTransactionHandler | null> {
const [arbitrumBlock, ethFinalizedBlock, finalityIssueFlagEth] = await fetchBlocksAndCheckFinality(
veaOutboxProvider,
veaInboxProvider,
Expand Down
17 changes: 11 additions & 6 deletions validator-cli/src/utils/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import { getMessageStatus } from "./arbMsgExecutor";
* @param epoch epoch number of the claim to be fetched
* @returns claim type of ClaimStruct
*/

enum ClaimHonestState {
NONE = 0,
CLAIMER = 1,
CHALLENGER = 2,
}
const getClaim = async (
veaOutbox: any,
veaOutboxProvider: JsonRpcProvider,
Expand Down Expand Up @@ -52,11 +58,11 @@ const getClaim = async (
if (hashClaim(claim) == claimHash) {
return claim;
}
claim.honest = 1; // Assuming claimer is honest
claim.honest = ClaimHonestState.CLAIMER; // Assuming claimer is honest
if (hashClaim(claim) == claimHash) {
return claim;
}
claim.honest = 2; // Assuming challenger is honest
claim.honest = ClaimHonestState.CHALLENGER; // Assuming challenger is honest
if (hashClaim(claim) == claimHash) {
return claim;
}
Expand Down Expand Up @@ -91,10 +97,9 @@ const getClaimResolveState = async (
};

if (sentSnapshotLogs.length === 0) return claimResolveState;
else {
claimResolveState.sendSnapshot.status = true;
claimResolveState.sendSnapshot.txHash = sentSnapshotLogs[0].transactionHash;
}

claimResolveState.sendSnapshot.status = true;
claimResolveState.sendSnapshot.txHash = sentSnapshotLogs[0].transactionHash;

const status = await fetchMessageStatus(sentSnapshotLogs[0].transactionHash, veaInboxProvider, veaOutboxProvider);
claimResolveState.execution.status = status;
Expand Down
1 change: 0 additions & 1 deletion validator-cli/src/utils/epochHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const getLatestChallengeableEpoch = (
now: number = Date.now(),
fetchBridgeConfig: typeof getBridgeConfig = getBridgeConfig
): number => {
// NOTE: Add logic to check if claim was made here or in main function?
const { epochPeriod } = fetchBridgeConfig(chainId);
return Math.floor(now / 1000 / epochPeriod) - 2;
};
Expand Down
Loading

0 comments on commit f9a91e9

Please sign in to comment.