# Resilience Analysis for Double Spending via Sequential Decision Optimization
## 7 Attack Planning as an Optimal Switching Problem
#### Attack Optimization in the Optimal Switching Formulation  
Given the state process \((\hat{Z}_{t})_{t=0}^{T+1}\) from (8) and switching matrix (39) the optimal control problem (40) is solved via backward induction (13), (14). However, (14) requires determining an expectation with respect to a geometric distribution, which involves an infinite number of summations. Still, this calculation can be reduced to a finite number of operations since our value functions are constant, starting from a sufficiently large state variable. More precisely, we verify below that our assumption that there is a maximal chain length \(T\) which can be abandoned implies that \(\
u_{t}^{*}(p,z)=\
u_{t}^{*}(p,T+1)\) for all \(z\in\mathbb{N}\) with \(z>T\). Specifically, the value functions \(\
u_{t}^{*}(p,z)\) of the optimal policy can be explicitly calculated for large values \(z>T\) as  
\[ \begin{array}{lll}\
u_{t}^{*}(p,z)&=&1_{\{1<p\}}\rho(T+1)+1_{\{2<p\}}(C1_{\{ t\leq T-n+p-2\}}-c1_{\{t>T-n+p-2\}})\\ &&+1_{\{p=2\}}C1_{\{t\leq T-n\}}\;\;\mbox{for all}\;z\in\mathbb{N},z>T,\,p\in P,\,t=0,\ldots,T.\end{array}  \tag{42}\]  
Indeed, if the secret chain exceeds \(z>T\) the maximal branch length which can be abandoned then further mining is not profitable and the gain from publishing a longer branch is \(1_{\{1<p\}}\rho(T+1)\). Furthermore, having a secret branch of this length \(z>T\), there is a good chance of the double-spending attack succeeding. For this, the publication of the chain should be postponed until the last confirmation block is received. Please note that the number of confirmation blocks required is \(n-(p-2)\) whereas \(T-t\) is the number of official blocks to be received until the time horizon ends. Suppose that the attack is already launched \(p>2\) then it succeeds if \(n-(p-2)\leq T-t\) with the publication of the secret branch (until block \(T+1\)) after the official block \(t+n-(p-2)\leq T\), immediately after the last confirmation block is received. This explains the term \(C1_{\{t\leq T-n+p-2\}}\) in the expression (42). Otherwise, if \(n-(p-2)>T-t\) then the last confirmation block arrives after the official block \(T\), thus the attack does not succeed which gives a loss term \(-c1_{\{t>T-n+p-2\}}\) in the formula (42). If the network is not attacked yet \(p=2\), then the attack shall be launched immediately if \(n\leq T-t\), otherwise there will be no attack. This ensures that the last confirmation block is received at \(t+n\leq T\) before the end of the time horizon, giving the gain term \(1_{\{p=2\}}C1_{\{t\leq T-n\}}\) in (42). Let us summarize the control costs formulated in Section 7 as  
\[r_{t}(p,z,1) = \rho\cdot z1_{\{z>t\}}1_{\{1<p\}}+C1_{\{z>t\}}1_{\{p=2+n\}}-c1_{ \{2<p<2+n\}}, \tag{43}\] \[r_{t}(p,z,2) = r_{t}(p,z,3)=-m1_{\{1<p\}},\] (44) \[r_{T+1}(p,z) = -c1_{\{2<p\leq 2+n\}}, \tag{45}\]  
for \(t=0,\ldots,T\), \(p\in P\) and \(z\in\mathbb{N}\) and provide a solution to the stochastic switching problem (40) in terms of the following algorithm:  
* **Step 1:** Calculate the expected value function using scrap values from (45): \[\
u_{T+1}^{E}(p,z)=r_{T+1}(p,z)\quad\mbox{for $p\in P,z=0,\ldots,T$, set $t:=T$.}\]
* **Step 2:** Given \(t\in\{0,\ldots,T\}\)  
and the regions are plotted
```
1 plot(x=0,y-0,xlim=c(0,tildeT),ylim=c(-n,tildeT),type=\"-\",xlab=\"time\",ylab=\"blockdifference\")
2 for(t in arguments){
3 points(x=rep(t,length(stopping_points[[t+1]]),y=stopping_points[[t+1]],type=\"-\"1\",col=\"red\")
4 points(x=rep(t,length(continuation_points[[t+1]])),y=continuation_points[[t+1]]),y=continuation_points[[t+1]],type=\"-\"1\",col=\"black\",lty=\"-danhed\") }
```
The result of this calculation is illustrated in Figure 2 which depicts the continuation and the stopping regions by dashed lines (in black) and by solid lines (in red) respectively. Recall that we agreed to consider the states visited by \((Z_{t})_{t=0}^{T}\) for block difference not greater than \(n\) due to (19). Hence, the graph of the relevant states forms a triangle-type figure whose bottom range turns out to be the stopping region.