Skip to content

Commit f9a91e9

Browse files
Mani BrarMani Brar
authored andcommitted
chore: added enums
1 parent 76dc985 commit f9a91e9

File tree

8 files changed

+111
-135
lines changed

8 files changed

+111
-135
lines changed

validator-cli/src/ArbToEth/transactionHandler.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,15 @@ describe("ArbToEthTransactionHandler", () => {
288288
transactionHandler.transactions.sendSnapshotTxn,
289289
mockMessageExecutor
290290
);
291-
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith(
292-
transactionHandler.transactions.sendSnapshotTxn,
293-
ContractType.OUTBOX
294-
);
295291
expect(transactionHandler.transactions.executeSnapshotTxn).toEqual("0x1234");
296292
});
297293

298294
it("should not resolve challenged claim if txn is pending", async () => {
299295
jest.spyOn(transactionHandler, "checkTransactionStatus").mockResolvedValue(1);
300-
transactionHandler.transactions.sendSnapshotTxn = "0x1234";
296+
transactionHandler.transactions.executeSnapshotTxn = "0x1234";
301297
await transactionHandler.resolveChallengedClaim(mockMessageExecutor);
302298
expect(transactionHandler.checkTransactionStatus).toHaveBeenCalledWith(
303-
transactionHandler.transactions.sendSnapshotTxn,
299+
transactionHandler.transactions.executeSnapshotTxn,
304300
ContractType.OUTBOX
305301
);
306302
});

validator-cli/src/ArbToEth/transactionHandler.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ type Transactions = {
2323
executeSnapshotTxn: string | null;
2424
};
2525

26+
enum TransactionStatus {
27+
NOT_MADE = 0,
28+
PENDING = 1,
29+
NOT_FINAL = 2,
30+
FINAL = 3,
31+
}
32+
2633
export enum ContractType {
2734
INBOX = "inbox",
2835
OUTBOX = "outbox",
@@ -71,9 +78,9 @@ export class ArbToEthTransactionHandler {
7178
* @param trnxHash Transaction hash to check the status of.
7279
* @param contract Contract type to check the transaction status in.
7380
*
74-
* @returns False if transaction is pending || not final || not made, else True.
81+
* @returns TransactionStatus.
7582
*/
76-
public async checkTransactionStatus(trnxHash: string | null, contract: ContractType): Promise<number> {
83+
public async checkTransactionStatus(trnxHash: string | null, contract: ContractType): Promise<TransactionStatus> {
7784
let provider: JsonRpcProvider;
7885
if (contract === ContractType.INBOX) {
7986
provider = this.veaInboxProvider;
@@ -82,27 +89,25 @@ export class ArbToEthTransactionHandler {
8289
}
8390

8491
if (trnxHash == null) {
85-
return 0;
92+
return TransactionStatus.NOT_MADE;
8693
}
8794

8895
const receipt = await provider.getTransactionReceipt(trnxHash);
8996

9097
if (!receipt) {
91-
// TODO: Add transaction pending timeout- redo transaction.
9298
this.emitter.emit(BotEvents.TXN_PENDING, trnxHash);
93-
return 1;
99+
return TransactionStatus.PENDING;
94100
}
95101

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

99105
if (confirmations >= this.requiredConfirmations) {
100106
this.emitter.emit(BotEvents.TXN_FINAL, trnxHash, confirmations);
101-
return 3;
102-
} else {
103-
this.emitter.emit(BotEvents.TXN_NOT_FINAL, trnxHash, confirmations);
104-
return 2;
107+
return TransactionStatus.FINAL;
105108
}
109+
this.emitter.emit(BotEvents.TXN_NOT_FINAL, trnxHash, confirmations);
110+
return TransactionStatus.NOT_FINAL;
106111
}
107112

108113
/**
@@ -114,7 +119,8 @@ export class ArbToEthTransactionHandler {
114119
if (!this.claim) {
115120
throw new ClaimNotSetError();
116121
}
117-
if ((await this.checkTransactionStatus(this.transactions.challengeTxn, ContractType.OUTBOX)) > 0) {
122+
const transactionStatus = await this.checkTransactionStatus(this.transactions.challengeTxn, ContractType.OUTBOX);
123+
if (transactionStatus > 0) {
118124
return;
119125
}
120126
const { deposit } = getBridgeConfig(this.chainId);
@@ -152,7 +158,11 @@ export class ArbToEthTransactionHandler {
152158
if (!this.claim) {
153159
throw new ClaimNotSetError();
154160
}
155-
if ((await this.checkTransactionStatus(this.transactions.withdrawChallengeDepositTxn, ContractType.OUTBOX)) > 0) {
161+
const transactionStatus = await this.checkTransactionStatus(
162+
this.transactions.withdrawChallengeDepositTxn,
163+
ContractType.OUTBOX
164+
);
165+
if (transactionStatus > 0) {
156166
return;
157167
}
158168
const withdrawDepositTxn = await this.veaOutbox.withdrawChallengeDeposit(this.epoch, this.claim);
@@ -168,7 +178,8 @@ export class ArbToEthTransactionHandler {
168178
if (!this.claim) {
169179
throw new ClaimNotSetError();
170180
}
171-
if ((await this.checkTransactionStatus(this.transactions.sendSnapshotTxn, ContractType.INBOX)) > 0) {
181+
const transactionStatus = await this.checkTransactionStatus(this.transactions.sendSnapshotTxn, ContractType.INBOX);
182+
if (transactionStatus > 0) {
172183
return;
173184
}
174185
const sendSnapshotTxn = await this.veaInbox.sendSnapshot(this.epoch, this.claim);
@@ -181,7 +192,11 @@ export class ArbToEthTransactionHandler {
181192
*/
182193
public async resolveChallengedClaim(sendSnapshotTxn: string, executeMsg: typeof messageExecutor = messageExecutor) {
183194
this.emitter.emit(BotEvents.EXECUTING_SNAPSHOT, this.epoch);
184-
if ((await this.checkTransactionStatus(this.transactions.sendSnapshotTxn, ContractType.OUTBOX)) > 0) {
195+
const transactionStatus = await this.checkTransactionStatus(
196+
this.transactions.executeSnapshotTxn,
197+
ContractType.OUTBOX
198+
);
199+
if (transactionStatus > 0) {
185200
return;
186201
}
187202
const msgExecuteTrnx = await executeMsg(sendSnapshotTxn, this.veaInboxProvider, this.veaOutboxProvider);

validator-cli/src/ArbToEth/validator.test.ts

Lines changed: 34 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("validator", () => {
1212
let mockClaim: any;
1313
let mockGetClaimState: any;
1414
let mockGetBlockFinality: any;
15-
15+
let mockDeps: any;
1616
beforeEach(() => {
1717
veaInbox = {
1818
snapshots: jest.fn(),
@@ -41,24 +41,25 @@ describe("validator", () => {
4141
};
4242
mockGetClaim = jest.fn();
4343
mockGetBlockFinality = jest.fn().mockResolvedValue([{ number: 0 }, { number: 0, timestamp: 100 }, false]);
44+
mockDeps = {
45+
epoch: 0,
46+
epochPeriod: 10,
47+
veaInbox,
48+
veaInboxProvider,
49+
veaOutboxProvider,
50+
veaOutbox,
51+
transactionHandler: null,
52+
emitter,
53+
fetchClaim: mockGetClaim,
54+
fetchClaimResolveState: mockGetClaimState,
55+
fetchBlocksAndCheckFinality: mockGetBlockFinality,
56+
};
4457
});
4558
describe("challengeAndResolveClaim", () => {
4659
it("should return null if no claim is made", async () => {
47-
const transactionHandler = null;
4860
mockGetClaim = jest.fn().mockReturnValue(null);
49-
const result = await challengeAndResolveClaim(
50-
0,
51-
10,
52-
veaInbox,
53-
veaInboxProvider,
54-
veaOutbox,
55-
veaOutboxProvider,
56-
transactionHandler,
57-
emitter,
58-
mockGetClaim,
59-
mockGetClaimState,
60-
mockGetBlockFinality
61-
);
61+
mockDeps.fetchClaim = mockGetClaim;
62+
const result = await challengeAndResolveClaim(mockDeps);
6263

6364
expect(result).toBeNull();
6465
expect(emitter.emit).toHaveBeenCalledWith(BotEvents.NO_CLAIM, 0);
@@ -77,19 +78,10 @@ describe("validator", () => {
7778
};
7879
veaInbox.snapshots = jest.fn().mockReturnValue("0x321");
7980
mockGetClaim = jest.fn().mockReturnValue(mockClaim);
80-
const updatedTransactionHandler = await challengeAndResolveClaim(
81-
0,
82-
10,
83-
veaInbox,
84-
veaInboxProvider,
85-
veaOutbox,
86-
veaOutboxProvider,
87-
mockTransactionHandler as any,
88-
emitter,
89-
mockGetClaim,
90-
mockGetClaimState,
91-
mockGetBlockFinality
92-
);
81+
82+
mockDeps.transactionHandler = mockTransactionHandler;
83+
mockDeps.fetchClaim = mockGetClaim;
84+
const updatedTransactionHandler = await challengeAndResolveClaim(mockDeps);
9385
expect(updatedTransactionHandler.transactions.challengeTxn).toBe(challengeTxn);
9486
expect(mockTransactionHandler.challengeClaim).toHaveBeenCalled();
9587
});
@@ -98,19 +90,8 @@ describe("validator", () => {
9890
mockClaim.challenger = mockClaim.claimer;
9991
mockGetClaim = jest.fn().mockReturnValue(mockClaim);
10092
veaInbox.snapshots = jest.fn().mockReturnValue(mockClaim.stateRoot);
101-
const updatedTransactionHandler = await challengeAndResolveClaim(
102-
0,
103-
10,
104-
veaInbox,
105-
veaInboxProvider,
106-
veaOutbox,
107-
veaOutboxProvider,
108-
null,
109-
emitter,
110-
mockGetClaim,
111-
mockGetClaimState,
112-
mockGetBlockFinality
113-
);
93+
mockDeps.fetchClaim = mockGetClaim;
94+
const updatedTransactionHandler = await challengeAndResolveClaim(mockDeps);
11495
expect(updatedTransactionHandler).toBeNull();
11596
});
11697

@@ -130,19 +111,10 @@ describe("validator", () => {
130111
sendSnapshotTxn: "0x0",
131112
},
132113
};
133-
const updatedTransactionHandler = await challengeAndResolveClaim(
134-
0,
135-
10,
136-
veaInbox,
137-
veaInboxProvider,
138-
veaOutbox,
139-
veaOutboxProvider,
140-
mockTransactionHandler as any,
141-
emitter,
142-
mockGetClaim,
143-
mockGetClaimState,
144-
mockGetBlockFinality
145-
);
114+
mockDeps.transactionHandler = mockTransactionHandler;
115+
mockDeps.fetchClaimResolveState = mockGetClaimState;
116+
mockDeps.fetchClaim = mockGetClaim;
117+
const updatedTransactionHandler = await challengeAndResolveClaim(mockDeps);
146118
expect(updatedTransactionHandler.transactions.sendSnapshotTxn).toEqual("0x123");
147119
expect(mockTransactionHandler.sendSnapshot).toHaveBeenCalled();
148120
expect(updatedTransactionHandler.claim).toEqual(mockClaim);
@@ -164,19 +136,10 @@ describe("validator", () => {
164136
executeSnapshotTxn: "0x0",
165137
},
166138
};
167-
const updatedTransactionHandler = await challengeAndResolveClaim(
168-
0,
169-
10,
170-
veaInbox,
171-
veaInboxProvider,
172-
veaOutbox,
173-
veaOutboxProvider,
174-
mockTransactionHandler as any,
175-
emitter,
176-
mockGetClaim,
177-
mockGetClaimState,
178-
mockGetBlockFinality
179-
);
139+
mockDeps.transactionHandler = mockTransactionHandler;
140+
mockDeps.fetchClaimResolveState = mockGetClaimState;
141+
mockDeps.fetchClaim = mockGetClaim;
142+
const updatedTransactionHandler = await challengeAndResolveClaim(mockDeps);
180143
expect(updatedTransactionHandler.transactions.executeSnapshotTxn).toEqual("0x123");
181144
expect(mockTransactionHandler.resolveChallengedClaim).toHaveBeenCalled();
182145
expect(updatedTransactionHandler.claim).toEqual(mockClaim);
@@ -199,19 +162,10 @@ describe("validator", () => {
199162
withdrawChallengeDepositTxn: "0x0",
200163
},
201164
};
202-
const updatedTransactionHandler = await challengeAndResolveClaim(
203-
0,
204-
10,
205-
veaInbox,
206-
veaInboxProvider,
207-
veaOutbox,
208-
veaOutboxProvider,
209-
mockTransactionHandler as any,
210-
emitter,
211-
mockGetClaim,
212-
mockGetClaimState,
213-
mockGetBlockFinality
214-
);
165+
mockDeps.transactionHandler = mockTransactionHandler;
166+
mockDeps.fetchClaimResolveState = mockGetClaimState;
167+
mockDeps.fetchClaim = mockGetClaim;
168+
const updatedTransactionHandler = await challengeAndResolveClaim(mockDeps);
215169
expect(updatedTransactionHandler.transactions.withdrawChallengeDepositTxn).toEqual("0x1234");
216170
expect(mockTransactionHandler.withdrawChallengeDeposit).toHaveBeenCalled();
217171
expect(updatedTransactionHandler.claim).toEqual(mockClaim);

validator-cli/src/ArbToEth/validator.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,33 @@ import { getBlocksAndCheckFinality } from "../utils/arbToEthState";
1010
// https://github.com/prysmaticlabs/prysm/blob/493905ee9e33a64293b66823e69704f012b39627/config/params/mainnet_config.go#L103
1111
const secondsPerSlotEth = 12;
1212

13-
export async function challengeAndResolveClaim(
14-
epoch: number,
15-
epochPeriod: number,
16-
veaInbox: VeaInboxArbToEth,
17-
veaInboxProvider: JsonRpcProvider,
18-
veaOutboxProvider: JsonRpcProvider,
19-
veaOutbox: VeaOutboxArbToEth,
20-
transactionHandler: ArbToEthTransactionHandler | null,
21-
emitter: typeof defaultEmitter = defaultEmitter,
22-
fetchClaim: typeof getClaim = getClaim,
23-
fetchClaimResolveState: typeof getClaimResolveState = getClaimResolveState,
24-
fetchBlocksAndCheckFinality: typeof getBlocksAndCheckFinality = getBlocksAndCheckFinality
25-
): Promise<ArbToEthTransactionHandler | null> {
13+
export interface ChallengeAndResolveClaimParams {
14+
epoch: number;
15+
epochPeriod: number;
16+
veaInbox: any;
17+
veaInboxProvider: JsonRpcProvider;
18+
veaOutboxProvider: JsonRpcProvider;
19+
veaOutbox: any;
20+
transactionHandler: ArbToEthTransactionHandler | null;
21+
emitter?: typeof defaultEmitter;
22+
fetchClaim?: typeof getClaim;
23+
fetchClaimResolveState?: typeof getClaimResolveState;
24+
fetchBlocksAndCheckFinality?: typeof getBlocksAndCheckFinality;
25+
}
26+
27+
export async function challengeAndResolveClaim({
28+
epoch,
29+
epochPeriod,
30+
veaInbox,
31+
veaInboxProvider,
32+
veaOutboxProvider,
33+
veaOutbox,
34+
transactionHandler,
35+
emitter = defaultEmitter,
36+
fetchClaim = getClaim,
37+
fetchClaimResolveState = getClaimResolveState,
38+
fetchBlocksAndCheckFinality = getBlocksAndCheckFinality,
39+
}: ChallengeAndResolveClaimParams): Promise<ArbToEthTransactionHandler | null> {
2640
const [arbitrumBlock, ethFinalizedBlock, finalityIssueFlagEth] = await fetchBlocksAndCheckFinality(
2741
veaOutboxProvider,
2842
veaInboxProvider,

validator-cli/src/utils/claim.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import { getMessageStatus } from "./arbMsgExecutor";
1010
* @param epoch epoch number of the claim to be fetched
1111
* @returns claim type of ClaimStruct
1212
*/
13+
14+
enum ClaimHonestState {
15+
NONE = 0,
16+
CLAIMER = 1,
17+
CHALLENGER = 2,
18+
}
1319
const getClaim = async (
1420
veaOutbox: any,
1521
veaOutboxProvider: JsonRpcProvider,
@@ -52,11 +58,11 @@ const getClaim = async (
5258
if (hashClaim(claim) == claimHash) {
5359
return claim;
5460
}
55-
claim.honest = 1; // Assuming claimer is honest
61+
claim.honest = ClaimHonestState.CLAIMER; // Assuming claimer is honest
5662
if (hashClaim(claim) == claimHash) {
5763
return claim;
5864
}
59-
claim.honest = 2; // Assuming challenger is honest
65+
claim.honest = ClaimHonestState.CHALLENGER; // Assuming challenger is honest
6066
if (hashClaim(claim) == claimHash) {
6167
return claim;
6268
}
@@ -91,10 +97,9 @@ const getClaimResolveState = async (
9197
};
9298

9399
if (sentSnapshotLogs.length === 0) return claimResolveState;
94-
else {
95-
claimResolveState.sendSnapshot.status = true;
96-
claimResolveState.sendSnapshot.txHash = sentSnapshotLogs[0].transactionHash;
97-
}
100+
101+
claimResolveState.sendSnapshot.status = true;
102+
claimResolveState.sendSnapshot.txHash = sentSnapshotLogs[0].transactionHash;
98103

99104
const status = await fetchMessageStatus(sentSnapshotLogs[0].transactionHash, veaInboxProvider, veaOutboxProvider);
100105
claimResolveState.execution.status = status;

validator-cli/src/utils/epochHandler.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ const getLatestChallengeableEpoch = (
5757
now: number = Date.now(),
5858
fetchBridgeConfig: typeof getBridgeConfig = getBridgeConfig
5959
): number => {
60-
// NOTE: Add logic to check if claim was made here or in main function?
6160
const { epochPeriod } = fetchBridgeConfig(chainId);
6261
return Math.floor(now / 1000 / epochPeriod) - 2;
6362
};

0 commit comments

Comments
 (0)