Skip to content

Commit

Permalink
Update FDG functions
Browse files Browse the repository at this point in the history
  • Loading branch information
boyuan-chen committed Aug 12, 2024
1 parent fe36dbf commit c813833
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 134 deletions.
136 changes: 39 additions & 97 deletions src/anchorage/anchorage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,22 +419,24 @@ export class AnchorageGraphQLService extends GraphQLService {
return withdrawalTransactions
}

async getFDGSubmissions(chainId: string | number, index: null | number = null, first: number = 1) {
// Fraud proofing specific functions
// If the block number is not found, return 0
async getLatestFDGSubmittedBlock(
chainId: string | number
) {
try {
const whereClause = index !== null
? `{ resolvedStatus: 2, index_lte: ${index} }`
: `{ resolvedStatus: 2 }`;
const qry = gql`
query GetDisputeGameCreateds {
disputeGameCreateds(
orderBy: index,
orderDirection: desc,
first: ${first},
where: ${whereClause}
first: 1,
where: { resolvedStatus: 2 }
) {
id
index
rootClaim
l2BlockNumber
}
}
`
Expand All @@ -444,39 +446,10 @@ export class AnchorageGraphQLService extends GraphQLService {
chainId,
EGraphQLService.DisputeGameFactory
)
const submissions = []
const rpcEndpoint = this.getRpcEndpoint(Number(chainId))
const provider = new JsonRpcProvider(rpcEndpoint)
if (result?.data?.disputeGameCreateds?.length) {
for (const submission of result.data.disputeGameCreateds) {
const FDGAddress = submission.id
const FDGContract = new ethers.Contract(FDGAddress, FDGABI, provider)
const l2BlockNumber = await FDGContract.l2BlockNumber()
submissions.push({
id: FDGAddress,
index: Number(submission.index),
l2BlockNumber: l2BlockNumber.toNumber(),
rootClaim: submission.rootClaim,
})
}
return submissions
return Number(result.data.disputeGameCreateds[0].l2BlockNumber)
}
console.log('graph-utils: getFDGSubmitted: No FDG found')
return [{ index: 0, l2BlockNumber: 0, rootClaim: '0x', id: ethers.constants.AddressZero }]
} catch (e) {
console.log('graph-utils: getFDGSubmitted: Error', e)
return [{ index: 0, l2BlockNumber: 0, rootClaim: '0x', id: ethers.constants.AddressZero }]
}
}

// Fraud proofing specific functions
// If the block number is not found, return 0
async getLatestFDGSubmittedBlock(
chainId: string | number
) {
try {
const result = await this.getFDGSubmissions(chainId)
return result[0].l2BlockNumber
return 0
} catch (e) {
console.log('graph-utils: getLatestFDGSubmittedBlock: Error', e)
return 0
Expand All @@ -488,72 +461,41 @@ export class AnchorageGraphQLService extends GraphQLService {
blockNumber: number
) {
try {
// get latest FDG submission
const latestFDGSubmission = await this.getFDGSubmissions(chainId)
const latestSubmittedIndex = latestFDGSubmission[0].index
const latestSubmittedBlockNumber = latestFDGSubmission[0].l2BlockNumber

// get first FDG submission
const firstFDGSubmission = await this.getFDGSubmissions(chainId, 0)
const firstSubmittedBlockNumber = firstFDGSubmission[0].l2BlockNumber

// Neeeds double check
// You need to resubmit the proof if the block number is less than the first submission
// Or you can claim the reward directly
if (blockNumber < firstSubmittedBlockNumber) {
return {
status: 'failure',
error: 'Less than first submission'
const qry = gql`
query GetDisputeGameCreateds {
disputeGameCreateds(
orderBy: index,
orderDirection: asc,
first: 1,
where: { resolvedStatus: 2, l2BlockNumber_gte: ${blockNumber} }
) {
id
index
rootClaim
l2BlockNumber
}
}
}
if (blockNumber > latestSubmittedBlockNumber) {
`
const result = await this.conductQuery(
qry,
undefined,
chainId,
EGraphQLService.DisputeGameFactory
)
if (result?.data?.disputeGameCreateds?.length) {
return {
status: 'failure',
error: 'Greater than latest submission',
status: 'success',
id: result.data.disputeGameCreateds[0].id,
rootClaim: result.data.disputeGameCreateds[0].rootClaim,
l2BlockNumber: Number(result.data.disputeGameCreateds[0].l2BlockNumber),
index: Number(result.data.disputeGameCreateds[0].index),
}
}

const step = 10
let prevSubmittedIndex = latestSubmittedIndex
let prevSubmittedBlockNumber = latestSubmittedBlockNumber
let prevRootClaim = latestFDGSubmission[0].rootClaim
while (prevSubmittedBlockNumber > blockNumber) {
const prevFDGSubmissions = await this.getFDGSubmissions(chainId, prevSubmittedIndex, step)
for (const prevFDGSubmission of prevFDGSubmissions) {
if (prevFDGSubmission.l2BlockNumber < blockNumber) {
return {
status: 'success',
index: prevSubmittedIndex,
rootClaim: prevRootClaim,
l2BlockNumber: prevSubmittedBlockNumber,
}
}
// special case
if (prevFDGSubmission.l2BlockNumber === blockNumber) {
return {
status: 'success',
index: prevFDGSubmission.index,
rootClaim: prevFDGSubmission.rootClaim,
l2BlockNumber: prevFDGSubmission.l2BlockNumber,
}
}
prevSubmittedIndex = prevFDGSubmission.index
prevSubmittedBlockNumber = prevFDGSubmission.l2BlockNumber
prevRootClaim = prevFDGSubmission.rootClaim
}
}

console.log('graph-utils: getStateRootOfFDGSubmission: Cannot find the root claim')
return {
status: 'failure',
error: 'Unknown error - cannot find the root claim',
} else {
return { status: 'error', error: 'No submission found for the given block number' }
}
} catch (e) {
console.log('graph-utils: getStateRootOfFDGSubmission: Error', e)
return {
status: 'failure',
error: 'Unknown error',
}
return { status: 'error', error: 'Unknown reason' }
}
}
}
Expand Down
61 changes: 24 additions & 37 deletions tests/integration/dispute-game-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,32 @@ describe('Anchorage: Integration Test', function () {

it('Submitted Block reachable on: Sepolia', async () => {
const chainId = 11155111;
const latestBlockNumber = await anchorageGraphQLService.getLatestFDGSubmittedBlock(chainId);
const latestSubmission = await anchorageGraphQLService.getFDGSubmissions(chainId);
expect(latestSubmission.length).toEqual(1);
expect(latestSubmission[0].l2BlockNumber).toEqual(latestBlockNumber)

const submissions = await anchorageGraphQLService.getFDGSubmissions(chainId, latestSubmission[0].index, 5);
expect(submissions.length).toEqual(5);
expect(submissions[0].l2BlockNumber).toEqual(latestBlockNumber);
expect(submissions[0].l2BlockNumber).toBeGreaterThan(submissions[1].l2BlockNumber);
expect(submissions[0].index).toEqual(submissions[1].index + 1);

const prevSubmissions = await anchorageGraphQLService.getFDGSubmissions(chainId, submissions[4].index, 5);
expect(prevSubmissions.length).toEqual(5);
expect(prevSubmissions[0].l2BlockNumber).toEqual(submissions[4].l2BlockNumber);
expect(prevSubmissions[0].index).toEqual(submissions[4].index);
const latestL2BlockNumber = await anchorageGraphQLService.getLatestFDGSubmittedBlock(chainId);
expect(latestL2BlockNumber).toBeGreaterThan(0)
});

it('Get root claim from submissions', async () => {
const chainId = 11155111;

const latestSubmissions = await anchorageGraphQLService.getFDGSubmissions(chainId, null, 20);
const FDGAddress = latestSubmissions[4].id;
const rpcEndpoint = anchorageGraphQLService.getRpcEndpoint(Number(chainId));
const provider = new ethers.providers.JsonRpcProvider(rpcEndpoint);
const FDGContract = new ethers.Contract(FDGAddress, FDGABI, provider);
const l2BlockNumber = (await FDGContract.l2BlockNumber()).toNumber();

const testingL2BlockNumber = l2BlockNumber - 10;

const submission = await anchorageGraphQLService.getRootClaimOfFDGSubmission(chainId, testingL2BlockNumber);
expect(submission.status).toEqual("success");
expect(submission.rootClaim).toEqual(latestSubmissions[4].rootClaim);
expect (submission.l2BlockNumber).toEqual(latestSubmissions[4].l2BlockNumber);
expect(submission.index).toEqual(latestSubmissions[4].index);

const submission2 = await anchorageGraphQLService.getRootClaimOfFDGSubmission(chainId, l2BlockNumber);
expect(submission2.status).toEqual("success");
expect(submission2.rootClaim).toEqual(latestSubmissions[4].rootClaim);
expect (submission2.l2BlockNumber).toEqual(latestSubmissions[4].l2BlockNumber);
expect(submission2.index).toEqual(latestSubmissions[4].index);
})
const testingL2BlockNumber = 8790790;

const case1 = await anchorageGraphQLService.getRootClaimOfFDGSubmission(chainId, testingL2BlockNumber);
expect(case1.status).toEqual("success");
expect(case1.id.toLowerCase()).toEqual(("0x83Ef33E9Ada93ef0040c8C550195fB1018128c9d").toLowerCase());
expect(case1.l2BlockNumber).toEqual(8790794);
expect(case1.rootClaim.toLowerCase()).toEqual("0xce9110ddcada4c1df37bcdf398d048f0af92da59eee205c506726264e5aeecd3");
expect(case1.index).toEqual(596);

const testingL2BlockNumber2 = 8790794;
const case2 = await anchorageGraphQLService.getRootClaimOfFDGSubmission(chainId, testingL2BlockNumber2);
expect(case2.status).toEqual("success");
expect(case2.id.toLowerCase()).toEqual(("0x83Ef33E9Ada93ef0040c8C550195fB1018128c9d").toLowerCase());
expect(case2.l2BlockNumber).toEqual(8790794);
expect(case2.rootClaim.toLowerCase()).toEqual("0xce9110ddcada4c1df37bcdf398d048f0af92da59eee205c506726264e5aeecd3");
expect(case2.index).toEqual(596);

const latestL2BlockNumber = await anchorageGraphQLService.getLatestFDGSubmittedBlock(chainId);
const case3 = await anchorageGraphQLService.getRootClaimOfFDGSubmission(chainId, latestL2BlockNumber + 10);
expect(case3.status).toEqual("error");
expect(case3.error).toEqual("No submission found for the given block number");
});
});

0 comments on commit c813833

Please sign in to comment.