您正在查看: Layer2 分类下的文章

ZK-COUNTERS TABLE

ZK-COUNTERS TABLE

opcode name cnt_arith cnt_binary cnt_mem_align cnt_keccak_f cnt_padding_pg cnt_poseidon_g is_dynamic
0x00 STOP 0 0 0 0 0 0 false
0x01 ADD 0 1 0 0 0 0 false
0x02 MUL 1 0 0 0 0 0 false
0x03 SUB 0 1 0 0 0 0 false
0x04 DIV 1 2 0 0 0 0 false
0x05 SDIV 1 8 0 0 0 0 false
0x06 MOD 1 2 0 0 0 0 false
0x07 SMOD 1 8 0 0 0 0 false
0x08 ADDMOD 1 3 0 0 0 0 false
0x09 MULMOD 2 2 0 0 0 0 false
0x0a EXP 512 1025 0 0 0 0 true
0x0b SIGNEXTEND 0 6 0 0 0 0 false
0x10 LT 0 1 0 0 0 0 false
0x11 GT 0 1 0 0 0 0 false
0x12 SLT 0 1 0 0 0 0 false
0x13 SGT 0 1 0 0 0 0 false
0x14 EQ 0 1 0 0 0 0 false
0x15 ISZERO 0 1 0 0 0 0 false
0x16 AND 0 1 0 0 0 0 false
0x17 OR 0 1 0 0 0 0 false
0x18 XOR 0 1 0 0 0 0 false
0x19 NOT 0 1 0 0 0 0 false
0x1a BYTE 2 4 0 0 0 0 false
0x1b SHL 1 2 0 0 0 0 false
0x1c SHR 1 3 0 0 0 0 false
0x1d SAR 2 10 0 0 0 0 false
0x20 SHA3 192 193 2 2 0 10 true
0x30 ADDRESS 0 0 0 0 0 0 false
0x31 BALANCE 0 0 0 0 0 9 false
0x32 ORIGIN 0 0 0 0 0 0 false
0x33 CALLER 0 0 0 0 0 0 false
0x34 CALLVALUE 0 0 0 0 0 0 false
0x35 CALLDATALOAD 64 66 0 0 0 0 true
0x36 CALLDATASIZE 0 0 0 0 0 0 false
0x37 CALLDATACOPY - - - 0 0 0 true
0x38 CODESIZE 0 0 0 0 0 252 true
0x39 CODECOPY 0 - - 0 0 255 true
0x3a GASPRICE 0 0 0 0 0 0 false
0x3b EXTCODESIZE 0 0 0 0 0 255 true
0x3c EXTCODECOPY 0 - - 0 11 510 true
0x3d RETURNDATASIZE 0 1 0 0 0 0 false
0x3e RETURNDATACOPY - - 2 0 0 0 true
0x3f EXTCODEHASH 0 0 0 0 0 255 true
0x40 BLOCKHASH 0 0 0 1 0 9 false
0x41 COINBASE 0 0 0 0 0 0 false
0x42 TIMESTAMP 0 0 0 0 0 0 false
0x43 NUMBER 0 0 0 0 0 0 false
0x44 DIFFICULTY 0 0 0 0 0 0 false
0x45 GASLIMIT 0 0 0 0 0 0 false
0x46 CHAINID 0 0 0 0 0 0 false
0x47 SELFBALANCE 0 0 0 0 0 255 true
0x50 POP 0 0 0 0 0 0 false
0x51 MLOAD 32 32 1 0 0 255 true
0x52 MSTORE 32 32 1 0 0 255 true
0x53 MSTORE8 32 1 1 0 0 255 false
0x54 SLOAD 0 0 0 0 0 255 true
0x55 SSTORE 0 - 0 0 0 255 true
0x56 JUMP 0 - 0 0 0 0 true
0x57 JUMPI 0 - 0 0 0 0 true
0x59 MSIZE 1 3 0 0 0 0 false
0x5a GAS 0 0 0 0 0 0 false
0x5b JUMPDEST 0 0 0 0 0 0 false
0x60 PUSH1 0 3 0 0 0 0 true
0x61 PUSH2 0 4 0 0 0 0 true
0x62 PUSH3 0 5 0 0 0 0 false
0x63 PUSH4 0 2 0 0 0 0 false
0x64 PUSH5 0 4 0 0 0 0 false
0x65 PUSH6 0 5 0 0 0 0 false
0x66 PUSH7 0 6 0 0 0 0 false
0x67 PUSH8 0 3 0 0 0 0 false
0x68 PUSH9 0 5 0 0 0 0 false
0x69 PUSH10 0 6 0 0 0 0 false
0x6a PUSH11 0 7 0 0 0 0 false
0x6b PUSH12 0 4 0 0 0 0 false
0x6c PUSH13 0 6 0 0 0 0 false
0x6d PUSH14 0 7 0 0 0 0 false
0x6e PUSH15 0 8 0 0 0 0 false
0x6f PUSH16 0 5 0 0 0 0 false
0x70 PUSH17 0 7 0 0 0 0 false
0x71 PUSH18 0 8 0 0 0 0 false
0x72 PUSH19 0 9 0 0 0 0 false
0x73 PUSH20 0 6 0 0 0 0 false
0x74 PUSH21 0 8 0 0 0 0 false
0x75 PUSH22 0 9 0 0 0 0 false
0x76 PUSH23 0 10 0 0 0 0 false
0x77 PUSH24 0 7 0 0 0 0 false
0x78 PUSH25 0 9 0 0 0 0 false
0x79 PUSH26 0 10 0 0 0 0 false
0x7a PUSH27 0 11 0 0 0 0 false
0x7b PUSH28 0 8 0 0 0 0 false
0x7c PUSH29 0 10 0 0 0 0 false
0x7d PUSH30 0 11 0 0 0 0 false
0x7e PUSH31 0 12 0 0 0 0 false
0x7f PUSH32 0 9 0 0 0 0 false
0x80 DUP1 0 0 0 0 0 0 false
0x81 DUP2 0 0 0 0 0 0 false
0x82 DUP3 0 0 0 0 0 0 false
0x83 DUP4 0 0 0 0 0 0 false
0x84 DUP5 0 0 0 0 0 0 false
0x85 DUP6 0 0 0 0 0 0 false
0x86 DUP7 0 0 0 0 0 0 false
0x87 DUP8 0 0 0 0 0 0 false
0x88 DUP9 0 0 0 0 0 0 false
0x90 SWAP1 0 0 0 0 0 0 false
0x91 SWAP2 0 0 0 0 0 0 false
0x92 SWAP3 0 0 0 0 0 0 false
0x93 SWAP4 0 0 0 0 0 0 false
0x94 SWAP5 0 0 0 0 0 0 false
0x95 SWAP6 0 0 0 0 0 0 false
0x96 SWAP7 0 0 0 0 0 0 false
0xa0 LOG0 0 - 0 0 0 0 true
0xa1 LOG1 0 - 0 0 0 0 true
0xa2 LOG2 0 - 0 0 0 0 true
0xa3 LOG3 0 - 0 0 0 0 true
0xa4 LOG4 0 - 0 0 0 0 true
0xf0 CREATE - - 0 - 0 - true
0xf1 CALL - - 0 0 - - true
0xf2 CALLCODE - - 0 0 - - true
0xf3 RETURN 0 0 0 0 0 0 false
0xf4 DELEGATECALL - - 0 0 - - true
0xf5 CREATE2 - - 0 - 0 - true
0xfa STATICCALL - - 0 0 - - true
0xfd REVERT 0 0 0 0 0 0 false
0xfe INVALID 0 1 0 0 0 0 false

Dynamic zk-counters

In the following doc, we calculate the cost of processing the opcode. It's important to also add the cost of calculating the cost

EXP

Inputs:
1- a: integer base.
2- exponent: integer exponent.

dynamic_gas = 50 * exponent_byte_size
We need to calculate the exponent byte size to get the gas cost. The counters cost is dynamic but we can't calculate the cost without consuming counters in a dynamic way. We need to find a way to get the size of the exponent in a constant manner or handle the counters limitations from the zkasm.
Maximum setted: Maxmimun byte syze = 256 bytes. Max counters = 256 * (2A + 4B) + 1B = 512A + 1025B

SHA3

L = input length
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0

cnt_arith = 2 + B6
cnt_binary = 2 + A + B
9
cnt_keccak = 1

CALLDATALOAD

L = byte offset in the calldata.
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0

counters = divARITH + B*(SHLarith + SHRarith)

CALLDATACOPY

L = Length to copy.
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0

counters = A(divARITH + SHLarith + SHRarith + MSTORE32) + SHLarith2 + SHRarith + MSTOREX

CODESIZE

counters = SLOAD

CODECOPY

L = bytes to copy.
if is createContract -> counters = CALLDATACOPY
else -> counters = LT + L*(LT + MEM_ALIGN_WR8)

EXTCODESIZE

counters = SLOAD

EXTCODECOPY

L = bytes to copy.
if is createContract -> counters = CALLDATACOPY
else -> counters = LT + L*(LT + MEM_ALIGN_WR8)

RETURNDATACOPY

L = Length to copy.
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0
counters = 2EQ + LT + divARITH + mulARITH + A(MLOAD32 + MSTORE32) + B*(MLOADX + MSTOREX)

EXTCODEHASH

counters = SLOAD

SELFBALANCE

counters = SLOAD

MLOAD

counters = MLOAD32

MSTORE

counters = MSTORE32

SLOAD

counters = SLOAD

SSTORE

Cant calculate

JUMPI

isCreateContract ? true -> A = 1, false -> A = 0
isCreate ? true -> B = 1, false -> B = 0

counters = EQ + A(B(MLOADX + SHRarith) + (1-B)(EQ)) + (1-A)(EQ)

JUMP

isCreateContract ? true -> A = 1, false -> A = 0
isCreate ? true -> B = 1, false -> B = 0

counters = EQ + A(B(MLOADX + SHRarith) + (1-B)(EQ)) + (1-A)(EQ)

LOG

L = byte size to copy.
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0
counters = AMLOAD32 + BMLOADX

CREATE

counters = computeGasSendCall + copySP + SLOAD + SSTORE + getLenBytes

CALL

argsLengthCall + retLength == 0 ? true -> A = 1, false -> A = 0
argsOffsetCall > memLength ? true -> B = 1, false -> B = 0
counters = addARITH + EQ + (1-A)(LT +BsaveMem ) + LT + isEmptyAccount + computeGasSendCall + copySP

CALLCODE

counters = 2EQ + LT2 + computeGasSendCall + copySP

DELEGATECALL

counters = 2EQ + LT2 + computeGasSendCall + copySP

CREATE2

counters = computeGasSendCall + copySP + SLOAD + SSTORE + getLenBytes

STATICCALL

counters = 2EQ + LT2 + computeGasSendCall + copySP

REGS TABLE

REG Name cnt_arith cnt_binary cnt_mem_align cnt_keccak_f cnt_padding_pg cnt_poseidon_g is_dynamic
LT 0 1 0 0 0 0 false
EQ 0 1 0 0 0 0 false
ARITH 1 0 0 0 0 0 false
SLOAD 0 0 0 0 0 11 true
SSTORE 0 0 0 0 0 11 true
MEM_ALIGN_WR8 0 0 1 0 0 0 false

Dynamic regs

SSTORE

SLOAD

Should check how SLOAD is implemented

Functions TABLE

FUNC Name cnt_arith cnt_binary cnt_mem_align cnt_keccak_f cnt_padding_pg cnt_poseidon_g is_dynamic
addARITH 0 1 0 0 0 0 false
divARITH 1 2 0 0 0 0 false
subARITH 0 1 0 0 0 0 false
mulARITH 1 0 0 0 0 0 false
saveMem 0 0 0 0 0 0 false
computeGasSendCall 0 1 0 0 0 0 false
copySP - - 0 0 0 0 true
MLOAD32 0 0 0 0 0 0 true
MLOADX 0 0 0 0 0 0 true
MSTORE32 0 0 0 0 0 0 true
MSTOREX 0 0 0 0 0 0 true
sliceA 0 0 0 0 0 0 false
SHRarith - - 0 0 0 0 true
SHLarith - - 0 0 0 0 true
opCODECOPYLoadBytes 0 1 0 0 0 0 false
isEmptyAccount - - 0 0 0 0 true

Dynamic functions

copySP

It depends on the stack size.
L = stack length
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0
counters = MLOAD32 * (A + B)

MLOAD32

MSTORE32

MSTOREX

MLOADX

L = bytes length
L > 0 ? true -> A = 1, false -> A = 0
isMSTOREX ? true -> B = 1, false -> B = 0
counters = LT + B(2SHRarith + 2SHLarith) + (1-B)(C(2SHLarith + 4SHRarith) + (1-C)(2SHRarith + 2SHLarith) + MEM_ALIGN_WR)
MAX:
counters = 192A + 193B + 2MA

SHRarith

SHLarith

A -> bytes to shift
D -> times to shift (A << D)
E -> D > 256? true = 1, false = 0
counters= ARITH + EQ + (1-E) (D(LT + ARITH)) = 1A + 1B + (1-E)(D(1A + 1B)
MAX:
counters = 32A + 32B

isEmptyAccount

isNotPrecompiled ? true -> A = 1, false -> A = 0
zeroBalance ? true -> B = 1, false -> B = 0
zeroNonce ? true -> C = 1, false -> C = 0

counters = LT + A(SLOAD + LT + B(SLOAD + LT + C*(SLOAD + LT)))

https://github.com/0xPolygonHermez/zkevm-rom/blob/main/docs/opcode-cost-zk-counters.md

Polygon zkEvm链区块不可逆判断

区块会经过两个步骤
eth_blockNumber->zkevm_isBlockVirtualized->zkevm_isBlockConsolidated

zkevm_isBlockVirtualized 到达该阶段说明数据已提交到L1层,当到达后,出现不一致概率极低,比如版本升级异常等,才会出现数据不一致
zkevm_isBlockConsolidated zkp 证明已生成,不会再发生数据不一致

每个阶段具体时间隔间,要看具体节点配置,比如当前测试环境
zkevm_isBlockVirtualized:15s
zkevm_isBlockConsolidated:10-20分 (算力充足情况下)

查询RPC
https://www.quicknode.com/docs/polygon-zkevm/zkevm_isBlockVirtualized
https://www.quicknode.com/docs/polygon-zkevm/zkevm_isBlockConsolidated

zkSync 根据l2到l1跨链hash查询CommitBlocks,PublishProofBlocksOnchain,ExecuteBlocks各个L1层交易hash

跨链交易发起:0x0bbbcf153f17ec0e1f12d698bbc64f9d242bc1cd1312dd5f34febf0e6cb6601a

  1. 根据tx_hash查看表l2_to_l1_logs,得到所在的miniblock_number15841
  2. 根据上面的miniblock_number,根据number查看表miniblocks
    select * from miniblocks where number=15841;
  3. 得到当前跨链交易所在l1_batch_number为7670
  4. 根据l1_batch_number,以number查看l1_batches
    select number,is_finished,eth_commit_tx_id,eth_prove_tx_id,eth_execute_tx_id from l1_batches where number=7670;

    得到

    number | is_finished | eth_commit_tx_id | eth_prove_tx_id | eth_execute_tx_id 
    --------+-------------+------------------+-----------------+-------------------
    7670 | t           |            41073 |           41148 |             41151
  5. 根据eth_commit_tx_id查询eth_txs,得到CommitBlocks对应交易信息
    select nonce,contract_address,tx_type,has_failed,sent_at_block,tx_status,confirmed_eth_tx_history_id from eth_txs where id=41073 ORDER BY updated_at DESC limit 1;
    nonce |              contract_address              |   tx_type    | has_failed | sent_at_block | tx_status | confirmed_eth_tx_history_id 
     -------+--------------------------------------------+--------------+------------+---------------+-----------+-----------------------------
      41091 | 0x5e3e5f6ef0e21f0cf5b4c3acd3cf29740b1cbbd8 | CommitBlocks | f          |               | Done      |                       43194
  6. 根据confirmed_eth_tx_history_id得到CommitBlocks对应交易hash
    select eth_tx_id,tx_hash,confirmed_at from eth_txs_history where id=43194  ORDER BY updated_at DESC limit 10;
    eth_tx_id |                              tx_hash                               |        confirmed_at        
     -----------+--------------------------------------------------------------------+----------------------------
          41073 | 0x0b01e199877faef52b95477119f53bf546a2915bc903132331f41542e58da53d | 2023-09-18 03:50:19.059576
  7. 同理查询eth_prove_tx_ideth_execute_tx_id得到对应的交易hash

zkSync Era宕机问题排查

问题背景

从一些外部消息得知

9月12日消息,据zkSync Era区块链浏览器显示,zkSync Era主网疑似出现宕机情况,zkSync Era提交给以太坊的最新批次为#208455,时间为14:14,区块高度已暂停于#13641404,已暂停出块37分钟。

跟进缘由

由于现有部分项目基于zkSync Era,所以需要确认下问题起因是什么,是否存在官方新版修复,目前现有版本会不会同样存在问题

确认问题

先从浏览器数据,确认下,是否存在消息描述问题,以及分析下问题位置(区块浏览器/链节点)

确认下Batch高度时间

Batch高度 区块时间 链接 位置
208455 2023-09-12 14:14 https://explorer.zksync.io/batch/208455
208456 2023-09-12 14:14 https://explorer.zksync.io/batch/208456
208457 2023-09-12 14:15 https://explorer.zksync.io/batch/208457 后+1

208455 与 208456 相差时间符合预期

确认下Block高度时间

Block高度 Batch高度 Committed时间 链接 位置
13641404 208456 2023-09-12 14:14 https://explorer.zksync.io/block/13641404
13641405 208456 2023-09-12 14:14 https://explorer.zksync.io/block/13641405
13641406 208456 2023-09-12 14:14 https://explorer.zksync.io/block/13641406 后+1

确认下Batch Commit时间

Batch高度 Commit时间 Commit tx hash 位置
208455 Sep-12-2023 06:15:59 AM +UTC https://etherscan.io/tx/0x369446bc9d99087aa1160d426b7af372dce91bb7d372724b7c529f2e3ff30ecd
208456 Sep-12-2023 06:16:35 AM +UTC https://etherscan.io/tx/0x902b3b0eee2e82ef048e8de8ec0417d7875c0930b5b0b893de48f8b5b59f8944
208457 Sep-12-2023 06:17:59 AM +UTC https://etherscan.io/tx/0x7ce6d03ead9117a0a7268042c6e19637c702df2382070f04442df75602461661 后+1

分析结果

从节点Batch和Block生成时间,以及Batch Commit,对比消息中的#208455前后时间差,综合来看,链方面数据无宕机,
大概率是当时区块浏览器服务方面,或者连接的某些提供数据RPC节点,出现了区块同步不及时问题。

如何避免

浏览器和提供查询的RPC节点做多个主备灾备, 实时高度状态检查和线路自动切换

显卡驱动与CUDA版本对照

Driver Version CUDA Version docker image
510 11.6 docker pull nvidia/cuda:11.6.2-runtime-ubuntu20.04
525 12.0 docker pull nvidia/cuda:12.0.0-runtime-ubuntu20.04
530 12.1 docker pull nvidia/cuda:12.1.0-runtime-ubuntu20.04
535 12.2 docker pull nvidia/cuda:12.2.0-runtime-ubuntu20.04

所在服务器安装对应的显卡驱动(一般都有了),需要使用对应的 cuda image与其对应

对外prover docker统一使用ubuntu 20.04

  • base:从 CUDA 9.0 开始,包含部署预构建 CUDA 应用程序的最低限度(libcudart)。如果您想手动选择要安装的 CUDA 软件包,请使用此映像。
  • runtime:通过添加 CUDA 工具包中的所有共享库来扩展基础映像。如果您有使用多个 CUDA 库的预构建应用程序,请使用此映像。
  • devel:通过添加编译器工具链、调试工具、标头和静态库来扩展运行时映像。使用此映像从源代码编译 CUDA 应用程序

数据来源:https://hub.docker.com/r/nvidia/cuda/tags