forked from ensdomains/evmgateway
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathReverseOPRollup.ts
executable file
·165 lines (152 loc) · 4.78 KB
/
ReverseOPRollup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {
AbstractRollup,
RollupCommit,
type RollupDeployment,
} from '../rollup.js';
import type {
EncodedProof,
HexAddress,
HexString,
ProviderPair,
ProofSequence,
} from '../types.js';
import { CHAINS } from '../chains.js';
import { ABI_CODER, EVM_BLOCKHASH_DEPTH, MAINNET_BLOCK_SEC } from '../utils.js';
import { EthProver } from '../eth/EthProver.js';
import { encodeRlpBlock } from '../rlp.js';
import { dataSlice } from 'ethers/utils';
import { Contract } from 'ethers/contract';
import { Interface } from 'ethers/abi';
export const L1_BLOCK_ABI = new Interface([
`function number() view returns (uint256)`,
]);
export type OPReverseConfig = {
L1Block?: HexAddress;
//storageSlot?: bigint;
};
// https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L1Block.sol
// TODO: should these be settings?
// (the contract needs to know SLOT_HASH)
const SLOT_NUMBER = 0n;
const SLOT_HASH = 2n;
export type ReverseOPCommit = RollupCommit<EthProver> & {
readonly rlpEncodedL1Block: HexString;
readonly rlpEncodedL2Block: HexString;
readonly accountProof: EncodedProof;
readonly storageProof: EncodedProof;
};
const L1Block = '0x4200000000000000000000000000000000000015'; // default deployment
// TODO: switch this to using previousBeaconRoot
// see: test/research/eip-4788/
// im using chain1 as mainnet and chain2 as op
// however the proving is from chain2 to chain1
// either rename chain1/chain2 to chainCall/chainData
// or add direction: 1=>2 or 2=>1
// 20241116: testName() has reverse, but not a feature of the Rollup yet
export class ReverseOPRollup extends AbstractRollup<ReverseOPCommit> {
// https://docs.optimism.io/chain/addresses#op-mainnet-l2
static readonly mainnetConfig: RollupDeployment<OPReverseConfig> = {
chain1: CHAINS.MAINNET,
chain2: CHAINS.OP,
};
// https://docs.base.org/docs/base-contracts#base-mainnet
static readonly baseMainnetConfig: RollupDeployment<OPReverseConfig> = {
chain1: CHAINS.MAINNET,
chain2: CHAINS.BASE,
};
readonly L1Block: Contract;
readonly commitStep: bigint;
//readonly storageSlot: bigint; // using const SLOT_* instead
constructor(
providers: ProviderPair,
config: OPReverseConfig,
commitStep = 1
) {
super(providers);
//this.latestBlockTag = 'latest'; // 20240922: not necessary
this.L1Block = new Contract(
config.L1Block ?? L1Block,
L1_BLOCK_ABI,
this.provider2
);
this.commitStep = BigInt(commitStep);
}
private align(index: bigint) {
return index - (index % this.commitStep);
}
async findL2Block(l1BlockNumber: bigint) {
let b = (await this.provider2.getBlockNumber()) + 1;
let a = Math.max(0, b - EVM_BLOCKHASH_DEPTH);
while (a < b) {
const middle = Math.floor((a + b) / 2);
const value = await this.provider2.getStorage(
this.L1Block.target,
SLOT_NUMBER,
middle
);
const block = BigInt(dataSlice(value, 24, 32)); // uint64
if (block == l1BlockNumber) return BigInt(middle);
if (block > l1BlockNumber) {
b = middle;
} else {
a = middle + 1;
}
}
throw new Error(`unable to find block: ${l1BlockNumber}`);
}
override async fetchLatestCommitIndex(): Promise<bigint> {
return this.align(
await this.L1Block.number({ blockTag: this.latestBlockTag })
);
}
protected override async _fetchParentCommitIndex(
commit: ReverseOPCommit
): Promise<bigint> {
return this.align(commit.index - 1n);
}
protected override async _fetchCommit(
index: bigint
): Promise<ReverseOPCommit> {
const prover = new EthProver(this.provider1, index);
const prover2 = new EthProver(
this.provider2,
await this.findL2Block(index)
);
const [l1BlockInfo, l2BlockInfo, proof] = await Promise.all([
prover.fetchBlock(),
prover2.fetchBlock(),
prover2.fetchProofs(this.L1Block.target as string, [SLOT_HASH]),
]);
return {
index,
rlpEncodedL1Block: encodeRlpBlock(l1BlockInfo),
rlpEncodedL2Block: encodeRlpBlock(l2BlockInfo),
accountProof: EthProver.encodeProof(proof.accountProof),
storageProof: EthProver.encodeProof(proof.storageProof[0].proof),
prover,
};
}
override encodeWitness(
commit: ReverseOPCommit,
proofSeq: ProofSequence
): HexString {
return ABI_CODER.encode(
['(bytes, bytes, bytes, bytes, bytes[], bytes)'],
[
[
commit.rlpEncodedL1Block,
commit.rlpEncodedL2Block,
commit.accountProof,
commit.storageProof,
proofSeq.proofs,
proofSeq.order,
],
]
);
}
override windowFromSec(sec: number): number {
// finalization is not on chain
// L1 block time is 12 sec
return Math.ceil(sec / MAINNET_BLOCK_SEC);
}
}