# The Fast Probabilistic Consensus simulator note
## Summary
In FPC, nodes vote on whether they like or dislike a transaction. This vote is represented by a 1 or 0. When all honest nodes reach the same voting opinion, consensus is reached.
在 FPC 中,節點對自己喜歡還是不喜歡交易進行投票。該投票用 1 或 0 表示。當所有誠實節點都得出相同的投票意見時,就達成共識。
To simulate an attack on the protocol, you can set a strategy for the adversary nodes in the Adv_Strategy parameter.
要模擬對協議的攻擊,可以在 Adv_Strategy 參數中為對手節點設置策略。
Nodes vote in rounds. You can define the average opinion of the honest nodes (defined in the p0 parameter) for the initial round.
節點輪次投票。您可以為初始回合定義誠實節點(在 p0 參數中定義)的平均意見。例如:p0 = 0.9 為有 90% 的誠實節點投票 1 選項。
For a given round each node calculates the average opinion of k randomly queried nodes and this value is denoted as eta.
對於給定的回合,每個節點計算 k 個隨機查詢的節點的平均意見,該值表示為 eta。
If eta is larger than the threshold, the opinion of that node is set 1, or 0 otherwise.
如果 eta 大於閾值,則將該節點的意見設置為 1,否則設置為 0。
The threshold is between [a,b] in the first round, and between [beta,1-beta] in the following rounds.
該閾值在第一輪中在 [a,b] 之間,在隨後的輪中在 [beta,1-beta] 之間。
For a given node the protocol terminates if the node has the same opinion for l (but at least l+m) consecutive rounds. Unless the round reaches round maxTermRound, for which the simulation stops with a termination failure.
對於給定的節點,如果該節點對 l 個連續(但至少為l + m)個回合具有相同意見,則協議終止。 除非該回合達到 maxTermRound 回合,否則模擬會因終止失敗而停止。
- FPC
- FPC 為交易收到 3 sec 內有出現衝突交易的解決方法,然而 3 sec 內該節點沒有發現衝突交易的話,直接是 like
- threshold 解釋
假設某次 query 的結果為 50% ,以 threadhold 為 60% 例子,那麼結果為 dislike. 以 threadhold 為 40% 為例子,那麼結果便為 like. 而 threadhold 在每一輪的值不為固定。使得攻擊者無法預測每一輪的攻擊難度。
## HowTo
#### Environment:
- host: node0.puyuma.org
- path: /home/ubuntu/workspace
#### Prerequisites:
- Clone the repository
``` shell
$ git clone https://github.com/iotaledger/fpc-sim.git
```
- Build the executable file
``` shell
$ cd fpc-sim
$ go build -o sim
```
#### How to start simualate:
- Start to simulate
```shell
$ ./sim
```

#### The results of the simulation:
- AgreementRate:所有誠實節點得出相同意見的比率
- IntegrityRate:所有誠實節點得出相同意見且最終意見與原始多數相同的比率
- TerminationRate:所有誠實節點在 maxTermRound
- MeanTerminationRound:FPC 終止時的平均回合
- MedianTerminationRound:FPC 終止時的中位數
- TerminationRound:完成 FPC(直方圖)需要幾輪
- MeanLastRound:所有節點的平均最後一輪
- OnesProportion:協議終止後 1s 的比例
- OnesPropEvolution:1s 比例的演變
- EtaEvolution:每一輪誠實 eta 的直方圖
#### 資料視覺化:
- Install the dependencies
``` shell
$ pip install dash numpy matplotlib
```
- For a single-vector-parameter input, edit the output setting in the plot.py file to read the correct csv column
``` shell
$ python plot.py
```
- output

- 解讀


#### Parameters:
input.txt:
| **Parameter** | **Type** | **Description** |
|-------------------|:-------------:|:--------------|
| `nRun` | int | Number of runs for each simulation. Each run is a new voting object and it can be, for example, understood as a new transaction. |
| `N` | int | Number of nodes |
| `a` | float64 | Lower threshold limit in the first round |
| `deltaab` | float64 | Difference between a and and upper first threshold limit b <br>[0.5 < a ≤ b < 1], [deltaab < 1-a ] |
| `beta` | float64 | Threshold limits in the subsequent rounds [0 ≤ beta ≤ 0.5] |
| `p0` | float64 | Proportion of nodes that have initial opinion 1, or 0 otherwise [0 ≤ p0 ≤ 1] |
| `q` | float64 | Proportion of adversaries [0 ≤ q < 1] |
| `k` | int | Amount of nodes each node queries |
| `m` | int | The cooling-off period |
| `l` | int | The required consecutive rounds for a node to finalize its opinion |
| `maxTermRound` | int | Maximum number of rounds before terminating the protocol |
| `Adv_Strategy` | int | Adversary strategy: `Adv_Strategy=1` is where adversary nodes always vote the opposite to whatever the value of the `p0` parameter is. `Adv_Strategy=2` is where adversary nodes vote with the minority opinion of the honest nodes from the previous round |
| `enableWS` | bool | Enable [Watts-Strogatz graph](https://blog.iota.org/the-fast-probabilistic-consensus-simulator-d5963c558b6e)|
| `deltaWS` | float64 | Watts-Strogatz parameter - Proportion of network that can be queried <br>[0 ≤ deltaWS < 1] |
| `gammaWS` | float64 | Watts-Strogatz paramter - Rewiring probability [0 ≤ gammaWS < 1] |
| `rateRandomness` | float64 | Average rate at which a random number is created ([see this blog post for more details](https://blog.iota.org/the-fast-probabilistic-consensus-simulator-d5963c558b6e)) |
| `etaAgreement` | float64 | Proportion of nodes ignored for agreement failure |
| `enableSaveEta` | bool | Save etas; this does not work if a vector input is provided |
| `enableExtremeBeta` | bool | Experimental: beta switches between min and max threshold |
| `enableRandN_adv` | bool | Turn nodes adversarial with probability q, otherwise assign by index |