# Resilience Analysis for Double Spending via Sequential Decision Optimization
## 7 Attack Planning as an Optimal Switching Problem
### Numerical Illustration of Optimal Switching  
We illustrate the algorithm presented above by an implementation in R. First let us define the matrix for calculation (47) of conditional expectation by the same code as in the second listing from Section 8.1. Figure 2: Solution to optimal stopping problem (18). The continuation region is depicted by dashed lines, while and the stopping regions by solid lines. and introduce a routine for generation of switching matrix (39):
```
1 make_switch_matrix<-function(n){
2 mat<-matrix(data=0,mrow=-2:n,ncol-3)
3 mat[2,]<-c(1,1,1)
4 mat[2,]<-c(1,2,3)
5 for(iin3:(mrow(mat)-1)){mat[i,]<-c(1,i+1,i+1)}
6 mat[mrow(mat),]<-c(1,n^2,n^2)
7 return(mat) }
```
Next define functions for (42) and (43)-(45)
```
1 asympt_valuefet<-function(t,p,z){
2 stopfront((1<-p)h(p<-n^2)k(m>T))
3(1<p)*rho<(T+1)+(2<p)*(C*(C*(T-n-p-2)-c*(t>T-n-p-2))*(p==2)*C*(t<-T-n))
4##########
5 reward_fct<-function(t,p,z,a){
5 stopfront((1<-p)h(p<-n^2)k((m==-1)|(m==2)|(m==3))k(0<-t)k(t<-T))
7 result<-0}
8 if(a==-1){
9 result<-rho*z*(z+t)+(1>p)+C*(z+t)+(p-2>n)-c*(2<p)*(p<2==)
10 if((a==-2)|(a==3)){
11 result<-m-1(p)}
12 return(result) }
13######
13 scrap_fct<-function(p,z){return(-c*(2<p)*(p<2==n))}
```
The functions for generation of containers for rewards and scrap values are
```
1 make_reward<-function(t){
2 result<-array(data=0,dim=c(T+1,n^2,3))
3 for(zin(1:dim(result)[1]))
4 for(pin1:(dim(result)[2]))
5 for(ain(1:dim(result)[3])){
6 result[z,p,a]<-reward_fct(t,p,z-1,a)}
7 return(result) }
8######
9 make_scrap>-function(){
10 result<-array(data=0,dim=c(T+1,n^2))
11 for(zin1:(dim(result)[1]))
12 for(pin1:(dim(result)[2])){
13 result[z,p]<-scrap_fct(p,z-1)}
14 return(result) }
```
The conditional expectation calculation (47) is implemented as
```
1 cond_exp<-function(t,valuefunc,tr_mat){
2 lastrow<-matrix(data=0,mrow=-1,ncol-n^2)
3 for(pin1:(a=2))lastrow[p]<-asympt_valuefet(t,p,T+1)
4 exppvaluefunc<-tr_mat%>%rbind(valuefunc,lastrow)
5 return(exppvaluefunc))
```
wheresas (46) is realized by
```
1 maximize_action<-function(t,evalfunc,alfa){
2 container<-array(data=0,dim=c(T+1,n^2,3)) reward<-make_reward(t)
4 for(pin1:(1:m=2))
5 for(ain1:(1:3)){
6 container[,p,a]-reward[,p,a]+evalfunc[,alfa[p,a]]}
7 result<-function(apply(FUM=N,X-container,MAGIN=c(1,2)), apply(FUM=Nhich.max,X-container,MAGIN=c(1,2))) names(result)<-c(\"opt_val\",opt_act\") return(result))
```
which yields beyond value function maximization also the maximizing actions. ```
1 bellman<-function(timepoint)
2{stopinfnot((0<-timepoint)&(timepoint<-T))
3 tr_mat<-make_tr_matrix(q-q,d-T+2)
4 alfa<-make_switch_matrix(n)
5 valfuncfunc<-make_scraps()
6 evalfunc<-valfunc
7t=T
8################
9 while(t>-timepoint)
10{
11 ren<-maximize_action(t,evalfunc,alfa)
12 evalfunc<-cond_exp(t,res$opt_val,tr_mat)
13t-t-1
14}
15################
16 return(res)
17}
```
As an illustration, define the model parameters and run strategy optimization:
```
1T<-100;m<-6;q<-0.1;C<-100;ec<-5;rhc<-0.1;m=0.07
2################
3 ren<-bellman(0)
4 res$opt_act[1:5,]
5 round(res$opt_val[1:5,],digits=3)
6 res$opt_val[1,2]
```
which returns
```
1 res$opt_act[1:5,]
2[,1][,2][,3][,4][,5][,6][,7][,8]
3[,1]112222221
4[2,1]11222221
5[3,1]1222221
6[4,1]13222221
7[5,1]13222221
8 round(res$opt_val[1:5,],digits=3)
9[,1][,2][,3][,4][,5][,6][,7][,8]
10[1,1]00.000-0.334-0.234-0.0730.2641.0681.068
11[2,2]00.100-0.2550.0110.7062.87810.913100.100
12[3,1]00.200.1821.3955.30819.787100.141100.200
13[4,1]00.4562.3438.21927.786100.182100.241100.300
14[5,1]00.35111.49234.996100.223100.282100.341100.400
15 res$opt_val[1,2]
```
Note in the last line that the result \(\
u_{0}^{*}(0,2)=0\) is as in (41), ensuring that the block chain is resilient to the double-spending attacks. When examining the strategy, we note that the optimal action is to quit (line 3, column \([,2]\)) right at the beginning, even if the block difference was one and two (line 3 and 5, column \([,2]\)). However, for a block difference greater than two, the double-spending attack shall be launched (line 6 and 7, column \([,2]\)).