# XIV. Work Packages and Work Reports
## 14.1. Honest Behavior
為鏈下活動定義 "誠實行為" Honest Behavior,預期至少 $\frac{2}{3}$ 驗證者表現誠實
- work package 的 guarantee & report 行為 (詳見 15 章)
- 收到資料後確保 work package 可用性
- 決定要對哪個 work report 做 audit,包含 audit & judge 行為
- 提交其他驗證者執行的正確 auditing work (詳見 13 章)
## 14.2. Segments and the Manifest
基本 erasure-coding segment 大小 $\mathsf{W}_E$ = 684 (byte) octets
:::info
**erasure code 簡介 (Appendix H)**
- 作用
- 把 684 octects blob (342 octect pairs) 分成 1023個 octect pairs
- 任意取 342 個 octect pairs 還有他們的 index 就可以重建 data
- 大小為 684 octect 倍數的資料,剩下補 0
- JAM 會 erasure coding 到下面兩個地方
- Audit DA: 資料比較大,大小會變化
- Import DA: 資料較小且是固定,input 4104 octects data segments 可以 6 degree 平行處理
:::
- 1023 個參與者的 $\frac{2}{3}$ 是惡意或是壞的還可以重建
- erasure code (Galois field) : 16 bit [name=nu1lspaxe]
- support encoding data >= 4KB
work package 通常不大,Extrinsic data 用 reference 方式作為 argument 進入 refine logic
work package 有兩種外部關聯:
- 對每個 imported segment 的 cryptographic commitment
- exported segment 的數量
### 14.2.1. Segments, Imports and Exports
把大量資料從一個 work package 傳給某個後續 work package 是 JAM availability system 的功能
(14.1)
$$
\mathbb{G} \equiv \mathbb{Y}_{\mathsf{W}_G}
$$
:::info
- $\mathbb{G}$: export segment,是一個 octect sequence
- $\mathsf{W}_G$: 固定長度 4104 bytes
:::
work package 在 refine 時 import/export segment from/to Imports DA,因為大小 684 倍數所以效率高
export segment: 只要知道 export segment 總數和 index 就能操作
import segment: 需要當時所有 import segment root 和 index

### 14.2.2. Data Collection and Justification
- guarantor 要負責從足夠數量的 unique validators(至少 342) 取得所需 segment 的 erasure-coded chunks,用來重建所有 imported segments
- 為了避免重建有錯的 chunk,imported segment Merkle root and index 要用另外的驗證 justification,放進 imported DA,segment 可能被需要的時間裡,大家都要能驗證
- guarantor 會生一個 Auditable Work Package,包含原始 workpackage, extrinsic data, imported data and a concise proof of correctness of imported data,並放到 Audit DA 裡面。雖然在 Imports DA 與 Audits DA 之間部分 data 重複儲存,但可接受的,因為 audit 通常在每個 work package 上平均進行 30 次,而 guaranteeing 只發生兩到三次
==why 30???== (audit 要 2/3 共識,guarantor 不用)
## 14.3. Packages and Items
work package
(14.2)
$$
\mathbb{P} \equiv (\mathbf{j}\in\mathbb{Y},\ \mathcal{h}\in\mathbb{N}_S,\ \mathcal{u}\in\mathbb{H},\ \mathbf{p}\in\mathbb{Y},\ \mathbf{x}\in\mathbb{X},\ \mathbf{w}\in⟦\mathbb{I}⟧_{1:\mathsf{I}})
$$
:::info
- $\mathbb{P}$: work package
- $\mathbf{j}$: authorization token
- $\mathbb{Y}$: The set of octet strings/“blobs”
- $\mathcal{h}$: 有 authorization code 的 service 的 index
- $\mathbb{N}_S$: service identifier (index) 詳見第 9 章
- $\mathcal{u}$: authorization code hash
- $\mathbf{p}$: parameterization blob
- $\mathbf{x}$: context
- $\mathbb{X}$: The set of refinement contexts
- $\mathbf{w}$: sequence of work items
- $\mathbb{I}$: work items (是一個 sequence, 長度最小為 1, 最大為 4)
- $\mathsf{I} = 4$ (The maximum amount of work items in a package.)
:::
work items
(14.3)
$$
\mathbb{I} \equiv (\mathcal{s}\in\mathbb{N}_S,\ \mathcal{c}\in\mathbb{H},\ \mathcal{y}\in\mathbb{Y},\ \mathcal{g}\in\mathbb{N}_G,\ \mathcal{a}\in\mathbb{N}_G,\ \mathcal{e}\in\mathbb{N},\ \mathbf{i}\in⟦(\mathbb{H}\cup(\mathbb{H}^⊞),\mathbb{N})⟧,\ \mathbf{x}\in⟦(\mathbb{H},\mathbb{N})⟧)
$$
:::info
$\mathbb{I}$: work items
- $\mathcal{s}$: service index
- $\mathbb{N}_S$: 拿 service indices 的集合,等於 $N_{2^{32}}$ 詳見 9.1
- $\mathcal{c}$: reporting 時 service 的 hash (對 lookup anchor block 來說,preimage 要 available)
- $\mathbf{y}$: payload blob
- $\mathcal{g}$ & $\mathcal{a}$: gas limits for Refinement and Accumulation
- $\mathbb{N}_G$: unsigned gas values 的集合,等於 $N_{2^{64}}$ 詳見 4.23
- $\mathcal{e}$: work item export 的 segment 數量
- $\mathbf{i}$: sequence of imported data segments
- work package export identity
- segment index
- $\mathbb{H}$: segment root 的 hash
- $\mathbb{H}^⊞$: exporting work package 的 hash
- $\mathbf{x}$: a sequence of blob hashes and lengths to be introduced in this block
- 貌似是 extrinsic data?
:::
imported data segment’s work-package 是屬於 $\mathbb{H}$ 和 $\mathbb{H}^⊞$ 的聯集
- 如果從 $\mathbb{H}$ 來表示已經知道 segment root,可以直接從 DA 拿資料
- 如果從 $\mathbb{H}^⊞$ 來表示只有 export work package hash,沒有 segment root,這樣 guarantor 要先把他轉成 segment root 且要在 work report 紀錄給鏈上驗證用
```
i = [
{ "identifier": "0x..abc", "index": 0 }, // 是已知的 segment root (in H)
{ "identifier": "0x..def", "index": 2 } // 從H田來的 work package hash,需要後續轉換為segment-root
]
```
限制 imported 和 exported items 的總數為 $\mathsf{W}_M$ = 2^11



:::info
- $\mathcal{p}$: work package
- $\mathcal{p}_\mathbf{w}$: work items
- $\mathcal{w}$: work item
- $\mathcal{w}_e$: exported work item ($\mathcal{e}$ 為 exported 數量)
- $\mathcal{w}_\mathbf{i}$: imported work item ($\mathbf{i}$ 為 imported segments 集合)
:::
假設 guarantor 知道 preimage to each extrinsic hash in each work items,這些資料會和 work package 一起給 guarantor
為了確保每個核心約有 2MB/s 的 throughput,限制 import and extrinsic items, all payloads, authorizer parameter, authorization token 總大小不得超過 12MB

:::info
- $\mathcal{p}_\mathbf{j}$: authorization token
- $\mathcal{p}_\mathbf{p}$: parameterized blob
- $\mathcal{S}$: work item 的內容大小
- $\mathcal{w}$: work item
- $\mathcal{w}_\mathbf{y}$: work item payload
- $\mathcal{w}_\mathbf{i}$: work item imported segment
- $\mathcal{w}_\mathbf{x}$: a sequence of blob hashes and lengths
- $\mathsf{W}_B$: encoded work-package 和他 extrinsic data 的最大 size = 12MB
:::
限制 gas limit 總和不能分別超過分配給 core 進行 accumulate 和 refine 的量


:::info
- $\mathsf{G}_A$: work-report Accumulation 需要的 gas = 100,000
- $\mathsf{G}_R$: work-package Refine 需要的 gas = 500,000,000
:::
item-to-result function $\mathcal{C}$




:::info
- $\mathcal{o}$: worker item result
- $\mathbb{J}$: work execution errors 的集合
- $\mathbb{L}$: work results 的集合 詳見 11 章
- $\mathcal{H}$: Keccak 256-bit hash function
- $\mathcal{g}$ & $\mathcal{a}$: gas limits for Refinement and Accumulation
==兩個小箭頭?==
:::


:::info
- $\mathcal{p}_a$: work-package’s implied authorizer
- $\mathcal{p}_\mathbf{p}$: parameterization
- $\mathcal{p}_c$: authorization code (a hash value)
- 用 service index (前面的 h) 歷史查詢查到的
> [name=yu2C]: p_c 移除了
- $\Lambda$: historical lookup function 在公式 9.7 定義
- $\mathcal{p}_h$: 有 authorization code 的 service 的 index
- $\mathcal{p}_\mathbf{x}$: context
- $\mathcal{p}_u$: authorization code hash
:::
### 14.3.1. Exporting
work package 的任意 work items 都可以 export segments,並在 work report 中放一個 segments root ,這些 root 會依照 export 順序進行排列
(會形成固定深度的 Merkle tree 詳見 E.4)
guarantors 要對兩組資料進行 erasure-code 和 distribute :
1. auditable work-package,其中包含 encoded work-package, extrinsic data 以及 self- justifying imported segments,這些資料放在 Audit DA
2. exported segments 和對應的 Paged-Proofs metadata
在第一組資料是短期的,assurers 只需在獲得最終性(finality)的區塊出現前保存這些資料。
在第二組資料是長期性的,work report reporting 之後至少要保存 28 天(672 epochs)
定義一個 paged-proofs 函數 ${P}$ ,以一系列 exported segments 為輸入,結果為 pages of hash 和 subtree proofs,可以證明 segment 的正確性
page proof 數量和 segment 數量一樣,
- 每頁 64 段 hashes (64 個 segment)
- Merkle proof from root to 64 個 segment 的 subtree root (segment root)

:::info
- segment 的層級是 work-item:
- 一個 work-item 一個 segment root
- 一個 segment root 會有 多個 subtree roots
- 一個 subtree root 代表一頁 (另外限制頁數上限)
- 一頁會有 64 segments
- $\mathcal{J}_6$: constant-depth Merkle tree 的 justification path 詳見公式 E.5
- 2^6 size page
- $\mathcal{L}_6$: constant-depth Merkle tree 的 page function 詳見公式 E.6
- 2^6 size page
- $\mathcal{E}$: serialization codec
- $\updownarrow{x} \equiv (|x|, x)$, $\updownarrow$ 表達 x 的長度與數值,使用於序列化時,遇到動態的資料型別(sequence), 將其資料長度與數值進行序列化。
- $\mathcal{P}_n$ : The octet-array zero-padding function 詳見公式 14.16
:::
## 14.4. Computation of Work Results
工作結果計算的程序與功能(稱為 $\Xi$),這個計算結果可能是錯誤(∇),也可能是正確的work result,並包含series of exported segments。
* 可確定性的(deterministic): 相同的輸入,計算結果永遠一致。
* 歷史查詢(historical lookup): 要求是在最近完成定案(finalized)的區塊( block )的八個時代(epochs)內對工作結果進行計算。
* 目的是確保任何在審核期間( auditing period )的節點(node)都能輕鬆地對該計算進行驗證,即使他們的同步狀態並非完美無瑕,也不會妨礙整個計算和驗證程序。換句話說,系統透過歷史查詢功能與明確的時間限制,讓所有節點在合理的時限內都能重新執行並確認這些計算的正確性,確保整個流程的可信度與可審計性。
---




> [name=yu2C] v0.5.3 有修改 $I$
> [name=yu2C] v0.6.0 有新增 $\bar{\mathbf{i}}$ (PVM 會用到)
:::info
$\mathbb{P}$: Set of Work Packages。
$\mathbb{N}_\mathsf{C}$: Set of the total number of cores。
$\mathbb{W}$: Set of Work Results。
$\mathbb{Y}$: The set of octet strings/“blobs”。
$s$: data availability specification
$p_\mathbf{x}$: work-package context
$p_\mathbf{a}$: work-package authorizer
$\mathbf{o}$: authorization output, the result of the Is-Authorized function.
$\mathbf{r}$: work-item results
:::

:::info
$\mathcal{K}$: set of keys, of a dictionary.
$\mathbf{l}$: segment-root dictionary
$h$: 有 authorization code 的 service 的 index
$p_\mathbf{w}$: worker-package 的 work-items
$w$: worker-item
$n$: segment count
$w_\mathbf{i}$: work-item 的 import segments
$|\mathbf{l}| \le 8$: $\mathbf{l}$ 必須小於八個時代(epochs)
:::

:::info
$\mathbf{o}$: authorization output, the result of the Is-Authorized function.
Is-Authorized
擔保人(guarantor)必須按照以下順序執行驗證步驟:
1. 第一步
- 確保 work-packages 具備所需的核心時間(core-time)
2. 第二步,擔保人(guarantor)確認所有匯入 segment 承諾(imported segment commitments)的 segment-tree roots
- 檢查這些 segment-tree roots 是否已知
- 驗證這些 segment-tree roots 是否尚未過期
3. 最後,確保可以獲取所有 extrinsic segments 的 原像數據(preimage data)
- 這些原像數據是 extrinsic segments 承諾的基礎
- 擔保人必須能夠成功提取這些數據
$\Psi_I$: The Is-Authorized pvm invocation function. See appendix B.。
$p$: work-package。
$c$: 特定的核心(nominated core)。
:::

:::info
$\mathbf{r}$: work-item results
$r$: work-item result
$\overline{\mathbf{e}}$: 由 Work Item 產生每個有序的 export segment
$\mathbf{T}$: transposition operator
$C$: erasure-coding function appendix H.
$p_\mathbf{w}$: worker-package 的 work items
$j$: work-items 的 index
$I$: ordered accumulation (i.e. counter),the total number of exports made from the work-package
$\mathbf{e}$: export segment
:::

:::info
$\Psi_R$: The Refine pvm invocation function. See appendix B
$w_c$: work-item 的 core
$w_g$: work-item 的 gas limit
$w_s$: work-item 的 service index
$h$: work-package hash
$w_\mathbf{y}$: work-item 的 payload blob
$p_\mathbf{x}$: work-package 的 context
$p_\mathbf{a}$: work-package 的 authorizer
$\mathbf{o}$: authorization output
$S$: the import segment data for some work-item
- $w$: work-item
- $\mathbf{l}$: segment-root dictionary
$X$: the extrinsic data for some work-item
- $w$: work-item
$\mathcal{H}$: The Blake 2b 256-bit hash function
$\mathbf{e}$: export segment
:::

:::info
$\mathbf{l}$: segment-root dictionary
* 包含所有獨特的 work-package hashes,這些 segments 並非直接通過 segment-root 識別,而是透過 work-package hashes 來識別。
$L$: segment-root lookup function
* segment-roots 與 work-package hashes 的聯集整合(collapse)成 segment-roots
* 利用 $\mathbf{l}$ 整合成 segment-roots
$\mathbb{H}$: hash value is of the segment-root containing the export
$\mathbb{H}^⊞$: the hash of the exporting work-package
$r$: segment-roots
$h$: work-package hash
$h^⊞$: exporting segment-root
:::
在工作報告 ( worker report ) 補償中,擔保人(guarantors)必須構建一個 $\mathbf{l}$ 值:
* 滿足先前的條件
* 確保所有 work-package hashes 和 segment-roots 存在適當的對應關係

:::info
$h$: work-package hash
$\mathcal{e}$: work-item export segment
$\mathbf{l}$: segment-root dictionary
$p$: work package
$c$: 特定的核心(nominated core)
$\mathbb{P}$: set of work-packages。
$\mathbb{N}_\mathsf{C}$: Set of the total number of cores。
$\mathcal{H}$: The Blake 2b 256-bit hash function
$s$: work-package 規範(specification)。
:::


:::info
$X$: the extrinsic data for some work-item
$w$: work item
$\mathbb{I}$: set of work items
$\mathbf{d}$: original data
$\mathcal{H}$: The Blake 2b 256-bit hash function
$w_\mathbf{x}$: a sequence of blob hashes and lengths
:::

:::info
$S$: the import segment data for some work-item
$w$: work item
$\mathbb{I}$: set of work items
$\mathbf{s}$: all segments exported
$n$: segment count
$\mathcal{M}$: The constant-depth binary Merklization function. See appendix E.4
$r$: segment-roots
$w_\mathbf{i}$: import segment of work-item
::::

:::info
$J$: justifications of segment data
$w$: work item
$\mathbb{I}$: set of work items
$\mathcal{J}_x$ The justification path to a specific $2^x$ size page of a constant-depth Merkle tree. See equation E.5.
$\mathbf{s}$: all segments exported
$n$: segment count
$\mathcal{M}$: The constant-depth binary Merklization function. See appendix E.4
$L$: segment-root lookup function
$r$: segment-roots
$w_\mathbf{i}$: import segment of work-item
::::

:::info
$s$: data availability specification
$A$: Availability Specifier function (see section 14.4.1)
$\mathcal{H}$: The Blake 2b 256-bit hash function
$\mathcal{E}$: The octet-sequence encode function. Superscripted${}^{-1}$ to denote the inverse. See appendix C.
$p$: work-packagee。
$X^\#$: the extrinsic data for some work-item ( sequence )
$S^\#$: the import segment data for some work-item ( sequence )
$J^\#$: justifications of segment data ( sequence )
: join all export segment
$p_w$: work-item of work-package
:::
雖然 $S$ 和 $J$ 都使用 $\mathbf{s}$ (all segments exported by all work-packages exporting a segment to be imported),但實際上不需要如此龐大的數據量。原因是可以通過單頁證明(single paged-proof)來推導出正當性。
這種方法的主要優點是:
* 大幅減少擔保人(guarantor)需要提取的數據量
* 最壞的情況下,對於每個人要匯入的 segments ,擔保人只需獲取兩個 segments 的數據
* 對於連續匯出的 segments(假設這是常見情況),
* 單個證明頁(single proof-page)就足以證明多個匯入 segments 的正當性
## 14.4.1. Availability Specifier
1. **作用:**
- Availability Specifier 是一個函數 $A$,用於生成 **可用性規範**,該規範是基於:
- 工作包的雜湊值(work package hash)。
- 包含完整數據的八位元序列(octet sequence)。
- 導出的段(exported segments)的序列。
2. **目標:**
- 確保數據包的整體完整性和正確性。
- 生成一個統一的規範,幫助系統檢查和驗證數據的可用性。
---

:::info
$\mathbb{H}$: hash value is of the segment-root containing the export
$\mathbb{Y}$: The set of octet strings/“blobs”。
$\mathbb{G}$: The set of data segments, equivalent to octet sequences of length $W_G$. See equation 14.1.
$\mathbb{S}$: The set of availability specifications.
${h}$: work-item hash
$\mathcal{e}$: work-item export segment
$n$: segment count
$\mathbf{x}$: context
$\mathbf{s}$: all segments exported
$\mathcal{H}$: The Blake 2b 256-bit hash function
$\mathsf{b}$: Service / Accumulate hash
$\mathsf{W}_E$: 684 The basic size of erasure-coded pieces in octets. See equation H.6.
$\mathcal{M}$: constant-depth binary Merkle function E.4
$\mathcal{M_B}$: well-balanced binary Merkle function E.3
$C$: erasure-coding function appendix H.
:::
### paged-proofs function
1. **paged-proofs 函數 $P$:**
- $P$是一個函數,接受一個segments的序列作為輸入,返回一組「分頁證明」(paged-proofs)。
- **目標:**
- 為每個segments生成足夠的證明,以證明它們的正確性。
2. **分頁證明(paged-proofs):**
- **結構:**
- 每個分頁包含 64 個 segments heashes。
- 附帶一個從 Merkle root 到包含這 64 個 segments 的subtree-root 的 Merkle 證明。

:::info
$\mathcal{P}$: the zero-padding function to take an octet array to some multiple of n in length.
$\mathbb{Y}$: The set of octet strings/“blobs”。
:::
### work package 流程
1. 分配 Work package
- Builder to Guarantor (Guarantor 是一個 validator 角色)
- 一個 work package 會分給一組三個 guarantor
- 只給先建立 CE 133 連線的 guarantor 送 package,其他斷掉
- 
2. work package sharing
- 拿到 work package 的 guarantor 先做兩件事
- authorization
- 拿到所有 import segments 的資料 (包含 work package hash to segment roots)
- 把 wp hash 換成 segment root
- 
- 用 local 的 segment root to erasure root map 把 segment root 換成 erasure root
- 用 erasure root 和 CE 139/140 去 DA 拿資料
- 
- 用 CE 134 送給同一組的另外兩個 guarantor,包含 work package hash to segment root map 還有 work package bundle (這個動作可以和自己的 refine 平行)
- bundle
- 
- 進行 refine,一邊等另外兩個 guarantor 回傳的結果
3. guarantor 要在 local 維護兩個東西
- work package hash to segment root map
- 因為 work item 裡面的 import segments 包含了兩種: segment root 和 work package hash (H, H田)
- segment root 表示算完已經上鏈的 work report 裡面的 segment root
- work package hash 表示還沒上鏈的,讓 builder 可以安排連續相關的 work package (一次安排 a, b, c wp, b 要用到 a 結果,c 要用到 a,b 結果,三個的 import 可能就是 [], [a-wp-hash], [a-wp-hash, b-wp-hash])(連續 work package 應該都會安排給同一組 guarantor)
- 這個 map 可以用來分辨是否是 work package hash,然後取出對應的 segment root
- 
- 運算完 refine 後更新 map
- 
- 這個 map 有長度限制,最多八個,所以存最新 8 個 work package hash
- guarantor 因為安全性會輪換,輪換後 map 也會消失 (guarantor 怎麼輪換?)
- segment root to erasure root map
- 獲得 segment root 後,要去 DA 拿資料是要用 erasure root 的,所以需要這個 map
- 需要 monitor on chain work report 來更新 segment root 到 erasure root 的關係 (How to monitor?)
- work report 有 segment root & erasure root
- 
- 參照 DA 時間,資料存 28 天