# Stochastic Modelling of Selfish Mining in Proof-of-Work Protocols +
## 3 Agent-Based Model and Simulation
### Simulation  
The ABM evolves over time as events occur. There are two types of events that can occur either (1) block creation: a new block is mined by some miner, or (2) block propagation: a block is propagated from one miner to another. The evolution of events can be modelled very efficiently with the _Gillespie algorithm_[20]: \"It [...] numerically simulates the time evolution of the given [...] system. [...] This algorithm never approximates infinitesimal time increments \(dt\) by finite time steps \(\Delta t\)\" [21]. Initially, at time \(t=0\), miners located in an underlying network are endowed with some computational power. Each miner \(i\) independently mines block at a Poisson rate \(\eta_{i}\). Thus, at the global level new blocks are created at a rate \(\sum_{i}\eta_{i}=\tau_{b}{}^{-1}\). Blocks, on the other hand, are propagated from node \(i\) to her peers at a rate \(\lambda_{nd}\). Importantly, blocks are propagated by both the honest and selfish miners according to rules specified in Section 3.1.3. The ABM keeps track of all nodes that are broadcasting in any given state of the model, as well as all peers to which each node is broadcasting. The latter we define as the number of _active links_\(E_{a}\), again following [14]'s notation. It implies that the rate of block propagation between two nodes connected by an active link occurs at an aggregate rate of \(E_{a}\lambda_{nd}\). All events independently occur following the rate at which _any_ event occurs which is defined as \(\xi=\sum_{i}\eta_{i}+E_{a}\lambda_{nd}\). Effectively, the Gillespie algorithm computes a waiting time such that the event occurrences in the system follow the distributions outlined in Section 3.1.2 for block mining and Section 3.1.4 for block propagation. To compute the waiting time, the next event is selected with probability proportional to its rate of occurrence in the system. Thus,  
\[p^{p}=\frac{E_{a}\lambda_{nd}}{\sum_{i}\eta_{i}+E_{a}\lambda_{nd}} \tag{3}\]  
is the probability that the next event is a propagation event. If the next event is a propagation event, then the probability with which a specific active link is executed is random, since we assume \(\lambda_{nd}\) to be equal for all active links. Similarly,  
\[p^{c}=\frac{\sum_{i}\eta_{i}}{\sum_{i}\eta_{i}+E_{a}\lambda_{nd}} \tag{4}\]  
is the probability that the next event is a block creation event. If the next event is a block creation event, then the probability with which a specific node is picked to be the miner is proportional to \(\eta_{i}/\sum_{j}\eta_{j}\), which in turn is proportional to node \(i\)'s share of the network's hashing power \(c_{i}\). Finally, after every event, time within the model is updated from \(t\) to \((t+t^{\prime})\) with \(t^{\prime}\sim\exp(\xi)\). To further consider the centrality of the selfish miner within the topology, one could assign the selfish miner to various positions in the network using various centrality measures, for example, the betweenness centrality which is given by the expression:  
\[C_{B}(v)=\sum_{s\
eq v\
eq t}\frac{\gamma_{st}(v)}{\gamma_{st}} \tag{5}\]  
where \(\gamma_{st}\) is the total number of shortest paths from node \(s\) to node \(t\) and \(\gamma_{st}(v)\) is the number of those paths that pass through \(v\). Therefore, in the _betweenness centrality method_, we assign the miner \(v\) with the highest betweenness, \(\max\limits_{i\in N}(C_{B}(v))\), as the selfish miner, and then randomly assign all honest miners. In addition, to capture the effect of a correlation between node position and its hashing power (e.g., because nodes with more resources could place themselves better in the overlay network) we introduce the concept of _hashing betweenness_. In this setting, the ranking of hashing powers of honest nodes is the same as the ranking of their betweenness centrality.