forked from godwokenrises/godwoken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
godwoken.mol
405 lines (346 loc) · 9.22 KB
/
godwoken.mol
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import blockchain;
vector Uint32Vec <Uint32>;
struct BlockMerkleState {
merkle_root: Byte32,
count: Uint64,
}
struct AccountMerkleState {
merkle_root: Byte32,
count: Uint32,
}
struct GlobalStateV0 {
rollup_config_hash: Byte32,
account: AccountMerkleState,
block: BlockMerkleState,
reverted_block_root: Byte32,
tip_block_hash: Byte32,
last_finalized_block_number: Uint64,
// 0: running, 1: halting
status: byte,
}
struct GlobalState {
rollup_config_hash: Byte32,
account: AccountMerkleState,
block: BlockMerkleState,
reverted_block_root: Byte32,
tip_block_hash: Byte32,
tip_block_timestamp: Uint64,
last_finalized_block_number: Uint64,
// 0: running, 1: halting
status: byte,
version: byte,
}
struct AllowedTypeHash {
type_: byte,
hash: Byte32,
}
vector AllowedTypeHashVec <AllowedTypeHash>;
// allowed eoa:
// 0: unknown, 1: eth, 2: tron
//
// allowed contract:
// 0: unknown, 1: meta, 2: sudt, 3: polyjuice, 4: eth addr reg
table RollupConfig {
l1_sudt_script_type_hash: Byte32,
custodian_script_type_hash: Byte32,
deposit_script_type_hash: Byte32,
withdrawal_script_type_hash: Byte32,
challenge_script_type_hash: Byte32,
stake_script_type_hash: Byte32,
l2_sudt_validator_script_type_hash: Byte32,
burn_lock_hash: Byte32,
required_staking_capacity: Uint64,
challenge_maturity_blocks: Uint64,
finality_blocks: Uint64,
reward_burn_rate: byte, // * reward_burn_rate / 100
chain_id: Uint64, // chain id
allowed_eoa_type_hashes: AllowedTypeHashVec, // list of script code_hash allowed an EOA(external owned account) to use
allowed_contract_type_hashes: AllowedTypeHashVec, // list of script code_hash allowed a contract account to use
}
table RawL2Transaction {
// chain id
chain_id: Uint64,
from_id: Uint32,
to_id: Uint32,
nonce: Uint32,
args: Bytes,
}
table L2Transaction {
raw: RawL2Transaction,
signature: Bytes,
}
vector L2TransactionVec <L2Transaction>;
struct SubmitTransactions {
tx_witness_root: Byte32,
tx_count: Uint32,
// hash(account_root | account_count) before apply all txs
prev_state_checkpoint: Byte32,
}
struct SubmitWithdrawals {
withdrawal_witness_root: Byte32,
withdrawal_count: Uint32,
}
table RawL2Block {
number: Uint64,
// In registry address format: registry_id (4 bytes) | address len (4 bytes) | address (n bytes)
block_producer: Bytes,
parent_block_hash: Byte32,
stake_cell_owner_lock_hash: Byte32,
timestamp: Uint64,
prev_account: AccountMerkleState,
post_account: AccountMerkleState,
// hash(account_root | account_count) of each withdrawals & transactions
state_checkpoint_list: Byte32Vec,
submit_withdrawals: SubmitWithdrawals,
submit_transactions: SubmitTransactions,
}
vector RawL2BlockVec <RawL2Block>;
table L2Block {
raw: RawL2Block,
kv_state: KVPairVec,
kv_state_proof: Bytes,
transactions: L2TransactionVec,
block_proof: Bytes,
withdrawals: WithdrawalRequestVec,
}
table DepositRequest {
// CKB amount
capacity: Uint64,
// SUDT amount
amount: Uint128,
sudt_script_hash: Byte32,
script: Script,
// Deposit to a Godwoken registry
registry_id: Uint32,
}
vector DepositRequestVec <DepositRequest>;
struct RawWithdrawalRequest {
nonce: Uint32,
// chain id
chain_id: Uint64,
// CKB amount
capacity: Uint64,
// SUDT amount
amount: Uint128,
sudt_script_hash: Byte32,
// layer2 account_script_hash
account_script_hash: Byte32,
// withdrawal registry ID
registry_id: Uint32,
// layer1 lock to withdraw after challenge period
owner_lock_hash: Byte32,
// withdrawal fee, paid to block producer
fee: Uint128,
}
vector WithdrawalRequestVec <WithdrawalRequest>;
table WithdrawalRequest {
raw: RawWithdrawalRequest,
signature: Bytes,
}
// --- contract execution ---
struct KVPair { k: Byte32, v: Byte32, }
vector KVPairVec <KVPair>;
table BlockInfo {
block_producer: Bytes,
number: Uint64,
timestamp: Uint64,
}
// --- end of contract execution ---
// --- deposit lock ---
// a rollup_type_hash exists before this args, to make args friendly to prefix search
table DepositLockArgs {
// layer1 lock hash
owner_lock_hash: Byte32,
layer2_lock: Script,
cancel_timeout: Uint64,
registry_id: Uint32,
}
// --- end of deposit lock ---
// --- custodian lock ---
// a rollup_type_hash exists before this args, to make args friendly to prefix search
table CustodianLockArgs {
deposit_block_hash: Byte32,
deposit_block_number: Uint64,
// used for revert this cell to deposit request cell
// after finalize, this lock is meaningless
deposit_lock_args: DepositLockArgs,
}
struct UnlockCustodianViaRevertWitness {
deposit_lock_hash: Byte32,
}
// --- end of custodian lock ---
// --- withdrawal lock ---
// a rollup_type_hash exists before this args, to make args friendly to prefix search
struct WithdrawalLockArgs {
withdrawal_block_hash: Byte32,
withdrawal_block_number: Uint64,
account_script_hash: Byte32,
// layer1 lock to withdraw after challenge period
owner_lock_hash: Byte32,
}
union UnlockWithdrawalWitness {
UnlockWithdrawalViaFinalize,
UnlockWithdrawalViaRevert,
}
table UnlockWithdrawalViaFinalize {
}
struct UnlockWithdrawalViaRevert {
custodian_lock_hash: Byte32,
}
// --- end of withdrawal lock ---
// --- stake lock ---
// a rollup_type_hash exists before this args, to make args friendly to prefix search
struct StakeLockArgs {
owner_lock_hash: Byte32,
stake_block_number: Uint64,
}
// --- end of stake lock ---
// --- builtin Meta contract ---
union MetaContractArgs {
CreateAccount,
}
struct Fee {
// registry id
registry_id: Uint32,
// amount in CKB
amount: Uint128,
}
table CreateAccount {
script: Script,
fee: Fee,
}
// --- end of Meta contract
// --- layer2 SUDT ---
union SUDTArgs {
SUDTQuery,
SUDTTransfer,
}
table SUDTQuery {
// Godwoken registry address: (registry_id (4 bytes) | address len(4 bytes) | address)
address: Bytes,
}
table SUDTTransfer {
// Godwoken registry address: (registry_id (4 bytes) | address len(4 bytes) | address)
to_address: Bytes,
amount: Uint256,
// paid fee(ckb)
fee: Fee,
}
// --- end of layer2 SUDT ---
// --- challenge ---
struct ChallengeTarget {
block_hash: Byte32,
// index of the challenge target
target_index: Uint32,
// 0: tx execution, 1: tx signature, 2: withdrawal signature
target_type: byte,
}
// a rollup_type_hash exists before this args, to make args friendly to prefix search
table ChallengeLockArgs {
target: ChallengeTarget,
// layer1 lock to receive the rewards
rewards_receiver_lock: Script,
}
// witness to prove the validity of challenge
table ChallengeWitness {
raw_l2block: RawL2Block,
block_proof: Bytes, // block proof
}
vector ScriptVec<Script>;
struct BlockHashEntry {
number: Uint64,
hash: Byte32,
}
vector BlockHashEntryVec <BlockHashEntry>;
// CKB merkle proof
table CKBMerkleProof {
indices: Uint32Vec,
lemmas: Byte32Vec,
}
// cancel challenge by execute the transaction
table CCTransactionWitness {
l2tx: L2Transaction,
raw_l2block: RawL2Block,
tx_proof: CKBMerkleProof,
kv_state_proof: Bytes,
block_hashes_proof: Bytes,
account_count: Uint32,
kv_state: KVPairVec,
load_data: BytesVec,
scripts: ScriptVec,
return_data_hash: Byte32,
block_hashes: BlockHashEntryVec,
}
// cancel challenge by verify tx signature
table CCTransactionSignatureWitness {
raw_l2block: RawL2Block,
l2tx: L2Transaction,
tx_proof: CKBMerkleProof,
kv_state: KVPairVec,
kv_state_proof: Bytes,
account_count: Uint32,
sender: Script,
receiver: Script,
}
// cancel challenge by verify witness signature
table CCWithdrawalWitness {
raw_l2block: RawL2Block,
withdrawal: WithdrawalRequest,
sender: Script,
owner_lock: Script,
withdrawal_proof: CKBMerkleProof,
kv_state_proof: Bytes,
kv_state: KVPairVec,
account_count: Uint32,
}
// --- end of challenge ---
// --- Rollup ---
table RollupSubmitBlock {
block: L2Block,
reverted_block_hashes: Byte32Vec,
reverted_block_proof: Bytes,
}
table RollupEnterChallenge {
witness: ChallengeWitness,
}
table RollupCancelChallenge {}
table RollupRevert {
reverted_blocks: RawL2BlockVec, // sorted by block number
block_proof: Bytes,
reverted_block_proof: Bytes,
new_tip_block: RawL2Block,
}
union RollupAction {
// submit layer2 block
RollupSubmitBlock,
// change rollup status to halting
RollupEnterChallenge,
// change rollup status to running
RollupCancelChallenge,
// revert layer2 blocks and change status to running
RollupRevert,
}
// --- end of Rollup ---
// --- ETH Address Registry ---
array Byte20 [byte; 20];
union ETHAddrRegArgs {
EthToGw,
GwToEth,
SetMapping,
BatchSetMapping,
}
struct EthToGw {
eth_address: Byte20,
}
struct GwToEth {
gw_script_hash: Byte32,
}
struct SetMapping {
gw_script_hash: Byte32,
fee: Fee,
}
table BatchSetMapping {
gw_script_hashes: Byte32Vec,
fee: Fee,
}
// --- end of ETH Address Registry ---