# JAMNPs
## Grid Structure
- Topology of previous/current/next validator set
## [CE 131/132](https://github.com/zdave-parity/jam-np/blob/main/simple.md#ce-131132-safrole-ticket-distribution): Safrole ticket distribution
### CE 131: ticket generating validator -> proxy validator
- proxy validator is selected from previous epoch
- the ticket generating validator wakes up in new epoch applied, and sends ticket to proxy validator after $\mathrm{max}(\lfloor E/60 \rfloor, 1)$ slot
### CE 132: proxy validator -> all current validator (current validator set)
- The distribution(forwarding) should evenly space out from $\mathrm{max}(\lfloor E/20 \rfloor, 1)$ to half-lottery period
## CE133 : Work-package submission
- Goal : builder 發送 work-package 相關資料給 guarantor (總共發送兩筆 messages)
- 第一個 message 發送 Core Index, Work-package
- 第二個 message 發送 Work-Package 參照的 Extrinsic data
- **Imported segments** 的內容不會由 builder 上傳,而是由接收到 work-package 的 guarantor 從 DA 系統 fetch.
```
Work-Package = As in GP
Extrinsic = [u8]
Builder -> Guarantor
--> Core Index ++ Work-Package
--> [Extrinsic] (Message size should equal sum of extrinsic data lengths)
--> FIN
<-- FIN
```
## CE134 : Work-package sharing
- Goal : 收到 work-package 的 guarantor 要向其他的 guarantors 分享 work-package
- 與 CE133 不同處:會發送 full work-package bundle,連同 work package hash to segments-root mappings
- bundle 會包含 imported data segments, justification, work-package 和 extrinsic data
- 收到 bundle 後需要做驗證才能執行 refine logic,並回傳 work-report hash 和 guaranteed 簽章
- 確認 authorization 有效性
- 確認 work-package hash 到 segment-root 的 mapping
- 若 authorization 無法被驗證,guarantor 可以自行決定允許或拒絕 refine work package
- 必須確保以下兩點才可以 share :
- 已經可以產生 work-report,這會牽涉到 work package 的 authorization
- 所有的 import segment 已經被回傳了(會牽涉到 work-package hash mapping 到 segment-roots)
- refine 需要在 share 之後才被執行。最理想的狀況是,refine 已經完成,正在等待其他 guarantors 的回應
```
Segments-Root = [u8; 32]
Segments-Root Mappings = len++[Work-Package Hash ++ Segments-Root]
Work-Package Bundle = As in GP
Guarantor -> Guarantor
--> Core Index ++ Segments-Root Mappings
--> Work-Package Bundle
--> FIN
<-- Work-Report Hash ++ Ed25519 Signature
<-- FIN
```
## CE133 ~ CE134 細節[WIP]
- work package bundle 需要的資料
- extrinsic data
- import segments
- justification (merkle proof)
### Main Step:
1. CE133 : receive work-package
2. CE134 : work-package (bundle) sharing
- Prerequisites
3. bundle erasure-coding & distribute
---
### 1. CE133 : builder 發送 work-package 以及 extrinsic data 給 guarantor
除此之外,因為 builder 知道 core index,所以他需要針對 core index 的 validator set (guarantor) 發送 data
:::spoiler CE133 Spec
Work-Package = As in GP
Extrinsic = [u8]
Builder -> Guarantor
--> Core Index ++ Work-Package
--> [Extrinsic] (Message size should equal sum of extrinsic data lengths)
--> FIN
<-- FIN
:::
三個 guarantor 中的一位收到 work-package 和 extrinsic data
:::spoiler work-package struct :
$\mathbb{P}\equiv( \mathbf{j}\in\mathbb{Y}, h\in\mathbb{N}_S, u\in\mathbb{H}, \mathbf{p}\in\mathbb{Y}, \mathbf{x}\in\mathbb{X}, \mathbb{w}\in\left[ \! \left[\mathbb{I} \right] \! \right]_{1:\mathrm{l}} )$
- $\mathbf{j}$ : authorization token
- $h$ : authorization code
- $u$ : authorization code hash
- $\mathbf{p}$ : configuration blob
- $\mathbf{x}$ : context
- $\mathbf{w}$ : work items
- $\mathbb{I} \equiv (s \in \mathbf{N}_{s}, h \in \mathbb{H}, \mathbf{y} \in \mathbb{Y}, g \in \mathbb{N}_{G}, a \in \mathbb{N}_{G}, e \in \mathbb{N}, \mathbf{i}\in \left[ \! \left[(\mathbb{H} \cup (\mathbb{H}^{田}), \mathbb{N}) \right] \! \right], \mathbf{x} \in \left[ \! \left[\mathbb{H}, \mathbb{N} \right] \! \right])$
- $s$ : service identifier
- $h$ : the code hash of the service
- $\mathbf{y}$ : payload blob
- $g$ : gas limit for Refinement
- $a$ : gas limit for accumulation
- $e$ : the number of data segments exported by the work-item
$\mathbf{i}$ : imported segments
$\mathbf{x}$ : extrinsic data hash and length (assume the validator know)
:::
此時,guarantor 應該收到這些 elements data,除了 $\mathbf{x}$ (imported segments) 以外
imported segments 需要由接收到 work package 的 guarantor 去 fetch 而來
### 2. CE134 : work-package (bundle) sharing
:::spoiler CE134 Spec
```
Segments-Root = [u8; 32]
Segments-Root Mappings = len++[Work-Package Hash ++ Segments-Root]
Work-Package Bundle = As in GP
Guarantor -> Guarantor
--> Core Index ++ Segments-Root Mappings
--> Work-Package Bundle
--> FIN
<-- Work-Report Hash ++ Ed25519 Signature
<-- FIN
```
:::
### work-package bundle distribution prerequisities
---
#### 1. fetch imported segments : CE139/CE140
為了拿到 import segments 需要向 assurers 請求 segment shards
worst case : 向 342 位 assurers 請求 segment shards
```
Segment Index = u16
Justification = [0 ++ Hash OR 1 ++ Hash ++ Hash OR 2 ++ Segment Shard] (Each discriminator is a single byte)
Guarantor -> Assurer
--> [Erasure-Root ++ Shard Index ++ len++[Segment Index]]
--> FIN
<-- [Segment Shard]
[Protocol 140 only] for each segment shard {
[Protocol 140 only] <-- Justification
[Protocol 140 only] }
<-- FIN
```
發送 work-report 的 erasure-root (所以會有一個 work-report hash -> erasure root mapping)
- guarantor 或 validators 會需要維護 work-report hash -> erasure-root mapping
- assurers 會需要保留 28天的 erasure-root
- erasure-root : 需要知道是哪個 work-report
- shard index : 需要知道某一個片段? (任意 342 個片段就可以重組為何需要 index) (跟 paged-proof 有關 ?)
- segment index : 需要知道哪一個 work report 的 segment
#### 2. 用 justification 驗證 correctness
如果 guarantor 本身有 justification : CE139,否則 CE140
#### 3. 製作 work-package bundle (v0.6.5 Eq. 14.16)
- Extrinsic data : $X(w \in \mathbb{I}) \equiv [\mathbf{d} | \mathcal{H}(\mathrm{d}), |\mathrm{d}|]$
- Import Segments : $S(w \in \mathbb{I}) \equiv [\mathbf{b}[n] | \mathcal{M}(b)= L(r)]$
- Justification : $J(w \in \mathbb{I}) \equiv ) [\updownarrow \mathcal{J}_0(\mathbf{b}, n) | \mathcal{M}(\mathbf{b})=L(r)]$
會遇到的問題[WIP]:
---
##### imported segments 該如何分辨 $\mathbb{H} \cup \mathbb{H}^{田}$ ?
- $\mathbb{H}$ : hash value of the segment root
- $\mathbb{H}^{田}$ : hash value of the work-package
- $\mathbb{H}$ 怎麼來的 ?
- 每一個 work-package 在 export 的時候都會 export segments 和 一個 segment-root 會包含在 work-report
- 必須要把 work-package 轉為 segment root,所以需要一個 work-package -> segment root mapping,這個 conversion 需要被寫入 work-report 給鏈上驗證
- how ? 每一個 validator 都需要自己維護一個 (但是會不會發生 inconsistent 的情況 ?)
- inconsistent 的情況下,work-report 鏈上驗證就不會通過