ForkDec: Accurate Detection for Selfish Mining Attacks

Incentive mechanism is the key to the success of the Bitcoin system as a permissionless blockchain. It encourages participants to contribute their computing resources to ensure the correctness and consistency of user transaction records. Selfish mining attacks, however, prove that Bitcoin’s incentive mechanism is not incentive-compatible, which is contrary to traditional views. Selfish mining attacks may cause the loss of mining power, especially those of honest participants, which brings great security challenges to the Bitcoin system. Although there are a series of studies against selfish mining behaviors, these works have certain limitations: either the existing protocol needs to be modified or the detection effect for attacks is not satisfactory. We propose the ForkDec, a high-accuracy system for selfish mining detection based on the fully connected neural network, for the purpose of effectively deterring selfish attackers.,e neural network contains a total of 100 neurons (10 hidden layers and 10 neurons per layer), learned on a training set containing about 200,000 fork samples. ,e data set, used to train the model, is generated by a Bitcoin mining simulator that we preconstructed. We also applied ForkDec to the test set to evaluate the attack detection and achieved a detection accuracy of 99.03%. ,e evaluation experiment demonstrates that ForkDec has certain application value and excellent research prospects.


Introduction
Bitcoin is essentially a decentralized, distributed public ledger, which allows anyone or institution to participate in publishing transactions in a client-side manner [1]. e transaction will be collected by the participants (called miners) in the network and then added to the public ledger through a consensus protocol. e consensus protocol adopted by Bitcoin is called Proof-of-Work. All miners compete to solve a difficult-to-solve but easy-to-verify cryptographic puzzle. e miner who successfully solves the puzzle first is allowed to add transactions to the ledger and receive Bitcoin rewards [2]. Incentive mechanism is central to the functionality of Bitcoin, which ensures the security and liveness of Bitcoin by encouraging a large number of honest miners to participate in the consensus process [3]. Traditionally, it is believed that Bitcoin's incentive mechanism is incentive-compatible, but the emergence of selfish mining proves that this opinion is inaccurate [2]. By strategically publishing previously withholding blocks to invalidate blocks mined by honest miners, selfish attackers can collect additional reward shares that should belong to honest miners. e harm of selfish mining attacks is not limited to this. Unfair reward distribution will induce some rational participants to be selfish. A large number of selfish participants may also launch collusive attacks to infringe the revenue of other honest participants, which will seriously damage Bitcoin's reputation. Resulting in plenty of honest miners quitting will weaken the security significantly and give other attacks (e.g., double-spending attacks) an opportunity to take advantage of. Although selfish mining attacks have not been discovered in the real world, with the continuous improvement of potential attackers' computing power and the iterative upgrade of attack algorithms [4][5][6][7][8][9][10], the possibility of this attack is gradually increasing. We consequently must attach great importance to the detection of this attack to ensure that it can be discovered and countermeasures are taken as soon as possible when an attack occurs.

Related Works. Ethan
Heilman proposed a method based on unforgeable timestamps against selfish mining [11], called Freshness Preferred. It requires miners to add unforgeable timestamps to blocks, and it invalidates the blocks withheld by attackers by encouraging honest miners to choose blocks with the latest timestamp. e disadvantage of this method, however, is that it requires a credible timestamp agency to generate unforgeable timestamps and requires honest miners to record all recent timestamp release logs. Solat et al. [12] proposed a new solution that does not use unforgeable timestamps, called the ZeroBlock. e idea is that if selfish miners withhold blocks for more than a preset time interval, all honest miners will directly reject the block.
e ZeroBlock scheme forces the selfish attacker to be unable to withhold blocks for a long time. Zhang et al. proposed the Weighted Fork-Resolving Policy. When a fork occurs, a weight is calculated for each branch. And, it is recommended that honest miners no longer simply rely on the length of the branch when determining the main chain but choose the branch with the largest weight [13]. Saad et al. [14] assigned an expected confirmation height (i.e., the expected height of the block containing the specified transaction) to each transaction by measuring the transaction size, transaction fee, and other factors. e smaller the gap between the actual confirmation height and the expected height, the lower the possibility of selfish mining behavior. Lee et al. increased the profit threshold of selfish mining from 25% to 33% by adding transaction creation time to the transaction data structure [15]. Chicarino et al. [16] analyzed the impact of selfish mining on Bitcoin's fork height and judged whether a selfish mining attack occurred by monitoring the abnormal changes in the fork height.

Motivation and Contribution. Since Eyal and Sirer
proposed the concept of selfish mining and pointed out its harmfulness; a series of studies on this attack have appeared [4,10,17,18,19,20]. e main focus of most research, however, is to increase the attacker's rewards or reduce the mining power threshold. By contrast, there are relatively few research studies on selfish mining defense measures [11][12][13][14][15][16], and many works require upgrading the existing protocol, which is costly to implement. e selfish mining detector [16] proposed by Chicarino et al. realized the detection of selfish mining without modifying the Bitcoin protocol. However, it only considers the factor of fork height and does not take other factors into consideration, which leads to a certain misjudgment rate. To improve the detection accuracy, in this work, we propose a selfish mining attack detection system based on a machine learning classification model, called ForkDec. e system can detect selfish mining attacks in the Bitcoin network with an accuracy rate of 99.03%. Our primary contributions are threefold as follows: (1) We construct a data set containing approximately 200,000 fork samples. Considering that selfish mining has not been discovered in reality, we build a Bitcoin mining simulator to simulate the Bitcoin mining process in the presence of propagation delays and selfish miners. In the simulation process, the simulator records all the fork features, and then the feature extractor extracts feature vectors based on the fork features to construct fork samples. (2) We present ForkDec as an accurate detection system for detecting selfish mining attacks in Bitcoin. To accurately detect selfish mining, we trained a classification model based on logistic regression and a fully connected neural network (with 10 hidden layers and 10 neurons per layer) on the training set, respectively, and then applied the learned model to ForkDec for attack detection. (3) We applied ForkDec to the test set to evaluate its performance. e evaluation results show that ForkDec is better than the selfish mining detector [16] in accuracy. In addition, we also found that the classification model based on the fully connected neural network has a better overall performance.

Roadmap of is Paper.
e rest of the paper is organized as follows. In Section 2, we introduce the details of the ForkDec system, including the construction of the data set and the selection of the classification model. In Section 3, the evaluation results and discussion of the proposed system are given. Finally, we conclude this work in Section 4. Figure 1 presents the basic architecture of the ForkDec system. It mainly includes three modules: data set construction, model training, and attack detection. Firstly, we built a simulator to simulate the Bitcoin network with selfish attackers. e simulator will record the information of each block (block height, miner, and timestamp), especially fork features, and then each fork will be delivered to the feature extractor to extract the feature vector to construct the fork data set. We, subsequently, use the built training set to train the classification model and embed the learned model into ForkDec for attack detection.

Data Set Construction.
e classification model relies on the training set to learn sample features and to identify unknown samples. To get an excellent attack detection model, we must have a training set with abundant selfish mining samples. Since machine learning has not been applied to selfish mining detection before, there is no existing data set available. To solve this issue, we constructed a data set containing 200,000 fork samples for model training, in which the ratio of natural fork samples to attacking fork samples is 3 : 7.

Feature Vector Extraction.
All miners in the Bitcoin network utilize Proof-of-Work to compete for accounting rights to create new blocks at an average rate of 10 minutes. After being created, the new block will be broadcast immediately by all honest miners in the Peer-to-Peer network. Unlike honest miners, the selfish attacker will secretly withhold newly mined blocks to create conflicting branches. en, the attacker invalidates the blocks mined by honest counterparts through strategically publishing the withheld blocks. In this way, the attacker could increase his proportion of rewards distribution. By studying the strategy of the selfish attacker, it can be known that the attacker carries out attacks by making forks. erefore, the key to detecting this type of attack is to track the fork data in the blockchain. Based on this, we construct a feature extractor to represent the fork data as a feature vector. e classification model learns the characteristics of the selfish mining attack through the feature vector, thereby detecting the attacks. In the Bitcoin, we define the structure of the feature vector as follows: h, l, i b , i t . e meaning of each feature is as follows: (i) h is the block height of the fork (ii) l is the length of the fork, i.e., the number of blocks on the conflicting branch (iii) i b is the number of blocks between this fork and the previous fork (iv) i t is the absolute value of the difference between the timestamps of the first block of each branch Subsequently, we use an example to present the general process of feature vector extraction, as illustrated in Figure 2. For simplicity and without loss of generality, we assume that b 0 is the first block after the previous fork is resolved, and its timestamp is t 0 . After b 1 is accepted by all participants, two valid blocks b 2 (with timestamp t 2 ) and b 2 ′ (with timestamp t 2 ′ ) are propagated in the P2P network. Consequently, the blockchain makes a fork since b 2 and b 2 ′ have the same block height, i.e., h(b 2 ). Note that we utilize h(x) to indicate the height of block x. e Bitcoin mining simulator will capture and record information about this fork. en, the extractor converts this information into a 4dimensional vector, which is the feature vector on the farright side of Figure 2.

Fork Sample Generation.
Under the setting that only considers selfish mining attacks, there are two types of forks in the Bitcoin network: natural forks and attacking forks. Natural fork means that when a block is propagated in the network, other miners create and broadcast a block with the same height, which leads to inconsistencies in the distributed ledger. is inconsistency is not caused by the attack but by network propagation delays [21]. Christian Decker and Roger Wattenhofer pointed out that the average delay of a block in Bitcoin is 12.6 seconds, and after the new block is broadcast for 40 seconds, 95% of the nodes have received the block [21]. In other words, the timestamp difference between most conflicting blocks in the Bitcoin network is close to the average propagation delay. Based on this, we adopt an exponential distribution with the expected value of 12.6 seconds to approximate the block propagation delay distribution, as shown in Figure 3. e simulator then randomly samples based on the distribution to simulate the timestamp interval of a natural fork.  Security and Communication Networks e opposite is the attacking fork which is caused by a malicious attack. Figure 4 shows the formation of an attacking fork. Assume that the selfish attacker firstly mines block A 1 at time t. According to the SM1 strategy [2], the attacker will secretly withhold block A 1 . Since honest miners will not perceive the existence of A 1 until it is published, the honest miners may mine a new block A 2 at any time t ′ after time t. en, there is 0 < t ′ − t; considering the average block creation time is 600 seconds (10 minutes), we set 0 < t ′ − t ≤ 600. at is when the simulator is simulating an attacking fork, the timestamp interval of conflicting blocks is randomly sampled between 0 and 600 seconds.

Classification Model.
e selection of the learning algorithm is another key point for ForkDec to realize highaccurate detection. It is impossible to get an efficient model if the learning algorithm is not well selected and even if there are rich sample data sets to utilize. We, respectively, test the detection effect of ForkDec when logistic regression and a fully connected neural network are used as the classification model. Among them, the logistic regression features a faster model convergence rate while the fully connected neural network performs better in accuracy rate.

Logistic Regression.
Logistic regression is a classification model that utilizes a linear model to predict binary classification problems. e idea is to map the output of the linear model (any continuous value) to a value between 0 and 1 by adding the sigmoid function after the linear model. Equation (1) presents the mathematical expression of logistic regression, where x T represents the sample to be classified, (w, b) represents the model parameter, and y represents the prediction results of the model (also called the confidence level): By setting the threshold to 0.5, the ForkDec classifies fork samples with a confidence level of more than 0.5 as attacking forks, otherwise as natural forks. In addition, to prevent overfitting, we use minimizing the cost function (with the L2 penalty term) as the optimization problem during model training and then apply the L-BFGS algorithm, a kind of quasi-Newton method, to solve the optimization problem.

Fully Connected Neural Network.
Logistic regression has the characteristics of clear structure and simplicity. However, on the other hand, a simple model may not be able to make full use of the rich training samples and cannot achieve top-notch detection results. To further improve the accuracy in attack detection, we additionally consider the use of fully connected neural network, also known as multilayer perceptron, as the classification model. Figure 5 presents the structure of the fully connected neural network. e input layer on the far left is composed of a group of neurons x i |x 1 , x 2 , . . . , x m representing the characteristics of the sample. Unlike logistic regression, there can be one or more nonlinear layers between the input layer and output layer of a neural network, called hidden layers. e neurons in each hidden layer perform a weighted linear summation conversion on the values of the previous layer. e converted value firstly passes through the activation function and then is delivered to the next layer until the final output layer. In the ForkDec system, we utilize backpropagation to train the neural network, and finally, we get a fully connected neural network with 10 hidden layers and 10 neurons in each layer.

Evaluation
In this section, we evaluate the performance of ForkDec in detecting selfish mining attacks. e ForkDec system utilizes Scikit-learn (version 1.0) to implement the model training. Scikit-learn, an open-source and efficient machine learning tool library, is implemented based on the Python program language. Subsequently, we embed the trained model into the ForkDec system and test it on a test set containing 76,686 samples. e test results show that the ForkDec system can achieve a detection accuracy of 99.03% when the fully connected neural network is used as the classification model and 98.76% when using logistic regression. We additionally compare the performance of the ForkDec detection system with the selfish mining detector (hereinafter referred to as SM detector) proposed in [16]. We also train the fully connected neural networks under different hyperparameters to find the optimal model and then detect selfish attackers with different abilities.

Comprehensive Performance.
By applying ForkDec to a test set containing 76,686 samples, the confusion matrix of ForkDec in detecting selfish mining attacks can be obtained, which is presented in Table 1. In the confusion matrix, the classification results of ForkDec and the real distribution of the samples are shown, where positive represents the attacking fork category and negative represents the natural fork category. To facilitate the description, we name the ForkDec system, respectively, according to the different classification models: (i) ForkDec-DNN is the ForkDec system with the fully connected neural network as the classification model (ii) ForkDec-LR is the ForkDec system with logistic regression as the classification model (iii) ForkDec is the collective name of ForkDec-DNN and ForkDec-LR From Table 1, we can see that the advantage of ForkDec-DNN is that it does not misclassify natural forks as attacking forks, while ForkDec-LR misclassifies 542 natural forks as attacking forks. However, ForkDec-DNN also has its disadvantages; that is, 745 attacking forks are misidentified as natural forks by ForkDec-DNN, while this value is only 407 for ForkDec-LR.
To more intuitively evaluate the performance of Fork-Dec, we present the accuracy, precision, recall, and F 1 value of ForkDec on the test set in Figure 6. e meanings of these indicators are as follows: (i) Accuracy: it is the proportion of correctly classified samples to the total sample. (ii) Precision: among all attacking fork samples detected by the model, precision is the proportion of real attacking samples. (iii) Recall: among all the attacking samples, recall is the proportion detected by the model. (iv) F 1 : the F 1 value, shown in (2), can be used to measure the comprehensive performance of the model in terms of precision and recall. e reason is that the F 1 value is only high when both precision and recall are high: It can be found in Figure 6 that ForkDec-DNN and SM Detector both have top scores in precision rate, which

e Optimal Model.
To find the optimal model, we train the fully connected neural networks under different hyperparameters. en, we apply these trained models to the test set. e performance of these models on the test set is shown in Table 2. It can be concluded from Table 2 that a neural network with 10 hidden layers and 10 neurons per layer has the best performance. And, more neurons do not mean better classification performance. It is worth mentioning that a neural network with 10 hidden layers and 10 neurons per layer may not be optimal, but its performance is close to the optimal model.

Detection for Attacker with Varying Power.
In order to fully evaluate the detection effect of ForkDec, we additionally considered the detection of selfish attackers under specific mining power. We first utilize α to represent the fraction of attacker's mining power in the power of the entire Bitcoin network. Figure 7 presents the detection effect of ForkDec against different power attackers. We notice that the accuracy rate, recall rate, and F 1 value drop rapidly when α > 0. 25. is is because, as the attacker's mining power increases, the frequency of the selfish mining attack is getting higher and higher, resulting in a large number of forks with close timestamps in the blockchain. Many of these forks are not correctly detected by the model, leading to a drop in recall rate, as the characteristics of such attacking forks are very similar to natural forks. en, the accuracy rate and F 1

Input Layer
Hidden Layer Output Layer ŷ Figure 5: e example of a fully connected neural network with 4 hidden layers and 4 neurons per layer.  value also drop. However, even in the face of powerful attackers, ForkDec still maintains a very high-accuracy rate. It can still ensure that there are almost no false positives during the detection process.

Conclusion
In this work, we propose a detection system for selfish mining attacks in Bitcoin, called ForkDec. e system is based on the machine learning classification model to realize intelligent detection of attacks. To ensure that ForkDec has a high detection accuracy, we construct a data set containing about 200,000 Bitcoin fork samples for model training. We then apply ForkDec to the test set for evaluation. e evaluation results show that ForkDec can achieve an accuracy of 99.03% for the detection of selfish mining in Bitcoin. What needs to be clear is that ForkDec can only detect the presence of an attack but cannot identify the miner who launched the attack. In future work, we will further analyze the attacker's strategy and improve ForkDec to accurately locate the attacker. In addition, the blockchain also applies in the fields of privacy protection [22] and data traceability. Attackers may use other methods to attack the blockchain. Hence, we also have to study the application of ForkDec to the detection of other attacks, e.g., double-spending attacks [23], time-bandit attacks [24], and blockchain DoS attacks [25].

Data Availability
e data used to support the findings of this study are available from the corresponding author upon request.

Conflicts of Interest
e authors declare that there are no conflicts of interest regarding the publication of this paper.  Security and Communication Networks 7