polygon-cdk代码解读

同步器

数据的查找

  1. 数据来源

    1. 数据是从以太坊L1区块链(L1)中获取的。

    2. 通过多个以太坊客户端(ethermans)并行地请求获取区块数据。

  2. 查找数据的函数

    1. asyncRequestRollupInfoByBlockRange: 异步请求特定区块范围内的汇总信息(rollup info)。

    2. requestLastBlockWithRetries: 重试请求L1上的最新区块,直到成功或达到最大重试次数。

  3. 查找数据的核心逻辑

    1. launchWork 函数:是查找数据的核心函数。它负责获取下一个需要处理的区块范围,并使用asyncRequestRollupInfoByBlockRange来请求这些区块的数据。

    2. renewLastBlockOnL1IfNeeded 函数:用于在必要时刷新L1上的最新区块信息,以确保同步是最新的。

数据的发送

  1. 发送的目标

    1. 获取到的数据会被发送到同步器中,通过 outgoingChannel 进行传递。
  2. 发送数据的函数

    1. sendPackages 函数:将打包好的数据发送到 outgoingChannel

    2. onResponseRollupInfo 函数:处理从L1获取到的汇总信息,并通过 sendPackages 发送给消费者(consumer)。

  3. 数据发送的时机

    1. 当生产者(producer)认为自己已完全同步(通过 IsNodeFullySynchronizedWithL1 判断)时,会发送一个消息到消费者,告知已完成同步。

    2. 在每次获取到新的汇总信息时,也会将信息发送到 outgoingChannel

数据发送的流程

  • 数据首先通过以太坊客户端从L1中获取,然后经过一系列的过滤和处理,最后通过 outgoingChannel 将数据发送到同步器(同步器可能是另一个服务或处理逻辑,用于进一步处理这些数据)。

总结:

  • 数据是从以太坊L1区块链中并行获取的,使用 asyncRequestRollupInfoByBlockRangerequestLastBlockWithRetries 进行请求。

  • 获取到的数据会通过 sendPackages 函数发送到 outgoingChannel,传递给同步器进行进一步处理。

里面的几个方法会去找etherman,然后通过ethclient去交互。
func (w *workerEtherman) executeRequestRollupInfoByBlockRange(ctx contextWithCancel, ch chan responseRollupInfoByBlockRange, request requestRollupInfoByBlockRange) (*rollupInfoByBlockRangeResult, error) {
    resultRollupInfo := rollupInfoByBlockRangeResult{request.blockRange, nil, nil, nil, nil}
    if err := w.fillLastBlock(&resultRollupInfo, ctx, request, false); err != nil {
       return &resultRollupInfo, err
    }
    if err := w.fillRollup(&resultRollupInfo, ctx, request); err != nil {
       return &resultRollupInfo, err
    }
    if err := w.fillLastBlock(&resultRollupInfo, ctx, request, true); err != nil {
       return &resultRollupInfo, err
    }
    if err := w.fillPreviousBlock(&resultRollupInfo, ctx, request); err != nil {
       return &resultRollupInfo, err
    }
    return &resultRollupInfo, nil
}
posted @ 2024-08-13 16:54  范晓刚  阅读(4)  评论(0编辑  收藏  举报