# Resilience Analysis for Double Spending via Sequential Decision Optimization
## 7 Attack Planning as an Optimal Switching Problem  
Given the number \(n=1,2,\ldots\) of required confirmations, we consider \(n+2\) operation modes \(p\in P=\{1,2,\ldots,n+2\}\) which are interpreted as \(p=1\) \"mining abandoned\", \(p=2\) \"mining continues\", \(p=k+2\) \"attack launched and confirmation block \(k=1,\ldots,n\) completed\". Introduce three actions \(a\in A=\{1,2,3\}\) with switching matrix  
\[(\alpha(p,a))_{p,a=1}^{n+23}=\left[ \begin{array}{cccc}\alpha(1,1)&\alpha(1, 2)&\alpha(1,3)\\ \alpha(2,1)&\alpha(2,2)&\alpha(2,3)\\ \alpha(3,1)&\alpha(3,2)&\alpha(3,3)\\ \vdots&\vdots&\vdots\\ \alpha(n+2,1)&\alpha(n+2,2)&\alpha(n+2,3)\end{array} \right]=\left[ \begin{array} []{cccc}1&1&1\\ 1&2&3\\ 1&4&4\\ \vdots&\vdots&\vdots\\ 1&n+2&n+2\end{array} \right] \tag{39}\]  
whose interpretation is context-dependent and is given as follows: If secret mining is abandoned (mode \(p=1\)), then there is no return to any other mode. If the attacker mines on a secret chain without having launched an attack yet (mode \(p=2\), second row), then the mining can be abandoned by \(a=1\) as \(\alpha(2,1)=1\), continued by \(a=2\) as \(\alpha(2,2)=2\), or the attack can be launched by \(a=3\) as \(\alpha(2,3)=3\). If the attack is already launched and \(k=1,\ldots,n\) confirmation blocks are received (mode \(p=k+2\)) then there are two possibilities: either to continue secret mining by \(a=2,3\) as \(\alpha(p,2)=\alpha(p,3)=(p+1)\wedge(n+2)\) (giving next confirmation block) or to abandon by \(a=1\) as \(\alpha(k,1)=1\). Next, let us use the secret branch length \((\hat{Z}_{t})_{t=0}^{T+1}\) from (8) as a state process and introduce control costs as function of this state. If mining is abandoned (\(p=1\)), then there are no costs  
\[r_{t}(1,z,a)=0,\ \ \text{for all}\ z\in\mathbb{N},\,a\in A,\,t=0,\ldots,T.\]  
If mining continues (\(p=2\)), then the mining costs \(m\geq 0\) must be paid, thus  
\[r_{t}(2,z,2)=r_{t}(2,z,3)=-m,\ \ \ \ \text{for all}\ z\in\mathbb{N},\,t=0, \ldots,T.\]  
In this mode \(p=2\), abandoning secret mining by \(a=1\) has two interpretations. If the attacker is ahead of the official chain (\(\hat{Z}_{t}=z>t\)), then the secret chain will be published and the attacker receives a reward \(\rho\geq 0\) for all blocks mined so far  
\[r_{t}(2,z,1) = \rho\cdot z1_{\{z>t\}},\ \ \text{for all}\ z\in\mathbb{N},\,t=0, \ldots,T.\]  
Similarly, if the attack is launched (\(p>2\)) then again, mining costs must be paid:  
\[r_{t}(p,z,2)=r_{t}(p,z,3)=-m,\ \ \ \ \ z\in\mathbb{N},\ \ t=0,\ldots,T.\]  
In this mode (\(p>2\)) abandoning secret mining has again two interpretations. If the attacker is ahead of the official chain (\(\hat{Z}_{t}=z>t\)), then the secret chain will be published and the attacker receives a reward for each block mined so far. Furthermore, if the official chain was overtaken and at least \(n\) confirmation blocks are received (which corresponds to \(\hat{Z}_{t}=z>0\) and \(p=2+n\)), then also a revenue \(C>0\) from a successful double spending is collected. However, if there are not enough confirmation blocks (\(2<p<2+n\)) then the attack was unsuccessful which causes a loss \(c>0\):  
\[r_{t}(p,z,1) = \rho\cdot z1_{\{z>t\}}+C1_{\{z>t\}}1_{\{p=2+n\}}-c1_{\{2<p<2+n\}},\ \ z\in\mathbb{N},\,t=0,\ldots,T.\]  
At the end \(t=T+1\) of the time horizon, if an attack has been launched but the secret chain was not published (\(2<p<2+n\)), then the attack was unsuccessful which yields a loss \(c>0\):  
\[r_{T+1}(p,z)=-c1_{\{2<p<2+n\}},\ \text{for all}\ z\in\mathbb{N},\,p\in P.\]  
With the above specifications, we introduce the double-spending problem in the optimal switching formulation as follows:  
\[\left. \begin{array}{l}\text{given}\ (p_{0},z_{0})\in E\text{ determine the maximum and a maximizer to}\\ \pi\mapsto v_{0}^{\pi}(p_{0},z_{0})=\mathbb{E}\left(\sum_{t=0}^{T}r_{t}(p_{t}^{ \pi},\hat{Z}_{t},a_{t}^{\pi})+r_{T+1}(p_{T+1}^{\pi},\hat{Z}_{T+1})\right)\\ \text{over all control policies}\ \pi.\end{array}