-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_analytic.py
More file actions
executable file
·59 lines (51 loc) · 2.65 KB
/
Copy pathdata_analytic.py
File metadata and controls
executable file
·59 lines (51 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from main import Block
import numpy as np
import matplotlib.pyplot as plt
import time
import random as rand
from function import chain_function, proof_of_work as PoW
def create_pow_blockchain(num_blocks, difficulty, blockchain_pow, previous_block, nonce_length, print_data=1):
hash_array = []
time_array = []
for i in range(0, num_blocks):
time_taken, hashes_tried, block_to_add = PoW.find_next_block(previous_block, difficulty, nonce_length)
blockchain_pow.append(block_to_add)
previous_block = block_to_add
hash_array.append(hashes_tried)
time_array.append(time_taken)
# Tell everyone about it!
if print_data:
print("Block #{} has been added to the blockchain!".format(block_to_add.index))
print("{} Hashes Tried!".format(hashes_tried))
print("Time taken to find block: {}".format(time_taken))
print("Hash: {}\n".format(block_to_add.hash))
return(hash_array, time_array)
blockchain = [chain_function.create_genesis_block()]
previous_block = blockchain[0]
num_blocks = 10
#3 different types of difficulty to analyze
difficulty_0 = 1
difficulty_1 = 2
difficulty_2 = 3
difficulty_3 = 4
nonce_length = 20
hash_array_0, time_array_0 = create_pow_blockchain(num_blocks, difficulty_0, blockchain, previous_block, nonce_length, 0)
print("Difficulty Level: {} complete".format(difficulty_0))
hash_array_1, time_array_1 = create_pow_blockchain(num_blocks, difficulty_1, blockchain, previous_block, nonce_length, 0)
print("Difficulty Level: {} complete".format(difficulty_1))
hash_array_2, time_array_2 = create_pow_blockchain(num_blocks, difficulty_2, blockchain, previous_block, nonce_length, 0)
print("Difficulty Level: {} complete".format(difficulty_2))
hash_array_3, time_array_3 = create_pow_blockchain(num_blocks, difficulty_3, blockchain, previous_block, nonce_length, 0)
print("Difficulty Level: {} complete".format(difficulty_3))
mean_arr_hash = [np.mean(hash_array_0), np.mean(hash_array_1), np.mean(hash_array_2), np.mean(hash_array_3)]
mean_arr_time = [np.mean(time_array_0), np.mean(time_array_1), np.mean(time_array_2), np.mean(time_array_3)]
plt.plot(mean_arr_hash)
plt.show()
plt.plot(mean_arr_time)
plt.show()
diff_factor_1 = np.mean(hash_array_1)/np.mean(hash_array_0)
diff_factor_2 = np.mean(hash_array_2)/np.mean(hash_array_1)
diff_factor_3 = np.mean(hash_array_3)/np.mean(hash_array_2)
print("Factor of difficulty increase from 1 to 2: {}".format(diff_factor_1))
print("Factor of difficulty increase from 2 to 3: {}".format(diff_factor_2))
print("Factor of difficulty increase from 3 to 4: {}".format(diff_factor_3))