Skip to content

Commit

Permalink
Add compressProof
Browse files Browse the repository at this point in the history
  • Loading branch information
makoto committed Apr 29, 2024
1 parent ea52ed7 commit fa73e0e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
Binary file modified bun.lockb
Binary file not shown.
22 changes: 8 additions & 14 deletions scroll-gateway/src/ScrollProofService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable prettier/prettier */
import { EVMProofHelper, type IProofService } from '@ensdomains/evm-gateway';
import { AbiCoder, Contract, ethers, type AddressLike, } from 'ethers';
import { concat } from "ethers";

import rollupAbi from "./abi/rollupABI.js";
import type { IBlockCache } from './blockCache/IBlockCache.js';
Expand Down Expand Up @@ -62,32 +61,28 @@ export class ScrollProofService implements IProofService<ScrollProvableBlock> {
const obj:any = await resp.json()
const batchIndex = obj.batch_index
const proof = await this.helper.getProofs(Number(block.number), address, slots)
const accountProof: Array<string> = proof.stateTrieWitness;
const storageProof: Array<string> = proof.storageProofs[0];

const compressedProof = concat([
`0x${accountProof.length.toString(16).padStart(2, "0")}`,
...accountProof,
`0x${storageProof.length.toString(16).padStart(2, "0")}`,
...storageProof,
]);
console.log(JSON.stringify(proof, null, 2))
console.log('account', proof.stateTrieWitness.length)
for (let index = 0; index < proof.storageProofs.length; index++) {
const element = proof.storageProofs[index];
console.log('storage', index, element.length)
}
const res:any = AbiCoder.defaultAbiCoder().encode(
[
'tuple(uint256 batchIndex, uint256[] storageKeys, bytes compressedProof)',
'tuple(uint256 batchIndex, uint256[] storageKeys)',
'tuple(bytes[] stateTrieWitness, bytes[][] storageProofs)',
],
[
{
batchIndex,
storageKeys:slots, // Check how to handle multiple storage
compressedProof
},
proof,
]
);
console.log({
blockNumber:Number(block.number), address,
batchIndex, slots, compressedProof, proof,
batchIndex, slots, proof,
res
})
return res;
Expand All @@ -97,7 +92,6 @@ export class ScrollProofService implements IProofService<ScrollProvableBlock> {
*/
public async getProvableBlock(): Promise<ScrollProvableBlock> {
const block = await this.l2Provider.send("eth_getBlockByNumber", ["finalized", false]);
console.log('***getProvableBlock', block)
if (!block) throw new Error('No block found');
return {
number: block.number
Expand Down
24 changes: 20 additions & 4 deletions scroll-verifier/contracts/ScrollVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ interface IScrollChainCommitmentVerifier {
struct ScrollWitnessData {
uint256 batchIndex;
bytes32[] storageKeys;
bytes compressedProof;
}

contract ScrollVerifier is IEVMVerifier {
Expand All @@ -43,6 +42,22 @@ contract ScrollVerifier is IEVMVerifier {
return _gatewayURLs;
}

function compressProof(
bytes[] memory stateTrieWitness,
bytes[][] memory storageProofs,
uint256 storageIndex
) public pure returns (bytes memory output) {
output = abi.encodePacked(uint8(stateTrieWitness.length));
for (uint256 i = 0; i < stateTrieWitness.length; i++) {
output = abi.encodePacked(output, stateTrieWitness[i]);
}
output = abi.encodePacked(output, uint8(storageProofs[storageIndex].length));
for (uint256 i = 0; i < storageProofs[storageIndex].length; i++) {
output = abi.encodePacked(output, storageProofs[storageIndex][i]);
}
return output;
}

/*
* Retrieves storage values from the specified target address
*
Expand All @@ -63,16 +78,17 @@ contract ScrollVerifier is IEVMVerifier {

(bool isDynamic, uint256 slot) = EVMProofHelper2.computeFirstSlot(command, constants, values);
(ScrollWitnessData memory scrollData, StateProof memory stateProof) = abi.decode(proof, (ScrollWitnessData, StateProof));
(bytes32 stateRoot, bytes32 storageValue) = verifier.verifyZkTrieProof(target, scrollData.storageKeys[i], scrollData.compressedProof);
bytes memory compressedProof = compressProof(stateProof.stateTrieWitness, stateProof.storageProofs, i);
(bytes32 stateRoot, bytes32 storageValue) = verifier.verifyZkTrieProof(target, scrollData.storageKeys[i], compressedProof);
if(!isDynamic) {
values[i] = abi.encodePacked(storageValue);
if(values[i].length > 32) {
revert InvalidSlotSize(values[i].length);
}
} else {
// TODO
values[i] = abi.encodePacked(storageValue);
// (values[i], proofIdx) = getDynamicValue(storageRoot, slot, proof, proofIdx);
// TODO
// (values[i], proofIdx) = getDynamicValue(storageRoot, slot, proof, proofIdx);
}
}
}
Expand Down

0 comments on commit fa73e0e

Please sign in to comment.