forked from qwerwon/SGBL_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmining.py
179 lines (136 loc) · 6.19 KB
/
mining.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import base64
import time
from Crypto.Hash import keccak
from blkutils import get_difficulty, getLatestBlock, get_candidateblock, Block_Validation
from block import Block
from key import Key
from transaction import Transaction
from utxo import UTXOset
# Class for Mining
class Mining(object):
# class variables
_MiningFlag = False
# Function for mining start
@classmethod
def mineStart(cls):
publicKey = Key._publicKey
publicKey_ser = publicKey.serialize(compressed=False)
if(cls._MiningFlag):
return True
cls.flagup()
# Mining start
while Mining._MiningFlag:
previous_block = getLatestBlock()
target_diff = get_difficulty(Block._BlockHeight,
getLatestBlock().difficulty)
print('target difficulty :', target_diff)
candidate_block = get_candidateblock()
blockData = str(candidate_block.previous_block) + \
str(candidate_block.merkle_root) + \
str(target_diff)
(targetNonce, time) = Mining.proofofwork(blockData, target_diff)
if targetNonce == False:
print('Failed to get golden nonce')
continue
else:
keccak_hash = keccak.new(digest_bits=256)
blockData = blockData + str(targetNonce)
keccak_hash.update(blockData.encode('ascii'))
candidate_block.block_hash = keccak_hash.hexdigest()
candidate_block.difficulty = target_diff
candidate_block.nonce = targetNonce
candidate_block.timestamp = time
# Add to RawBlock and _Blockchain
Block.Insert_RawBlock(candidate_block.block_index,
candidate_block.block_hash,
candidate_block.previous_block,
candidate_block.merkle_root,
candidate_block.difficulty,
candidate_block.timestamp,
candidate_block.nonce,
candidate_block.tx_set)
Block.insert_blockchain(candidate_block.block_index,
candidate_block.block_hash,
candidate_block.previous_block,
candidate_block.merkle_root,
candidate_block.difficulty,
candidate_block.timestamp,
candidate_block.nonce,
candidate_block.tx_set)
print('successfully mined new block#' + str(Block._BlockHeight))
if candidate_block.block_index != 0:
is_valid_flag = Block_Validation(candidate_block.block_index,
candidate_block.block_hash,
candidate_block.previous_block,
candidate_block.merkle_root,
candidate_block.difficulty,
candidate_block.timestamp,
candidate_block.nonce,
candidate_block.tx_set,
previous_block.difficulty)
if is_valid_flag == True :
print("True")
if is_valid_flag == False :
print("False")
# Add to UTXOsets and myUTXOsets(for coinbase transaction only)
tx_length=candidate_block.tx_set.length()
for i in range(tx_length):
tx=candidate_block.tx_set[i]
tx_id=tx.tx_id
for j in range(tx.in_num):
vin=tx.vin[j]
UTXOset.Pop_UTXO(vin.tx_id, vin.index)
UTXOset.Pop_myUTXO(vin.tx_id, vin.index)
for j in range(tx.out_num):
vout=tx.vout[j]
UTXOset.Insert_UTXO(tx_id, j, vout.lock, vout.value)
address_str=base64.b64encode(vout.lock).decode('utf-8')
if address_str==Key._publicKey:
UTXOset.Insert_myUTXO(tx_id,j,vout.lock,vout.value)
"""
coinbase = candidate_block.tx_set[0]
coinbase_data = coinbase.vout[0]
coinbase_tx_id = coinbase.tx_id
UTXOset.Insert_UTXO(coinbase_tx_id,
0,
coinbase_data.lock,
coinbase_data.value)
UTXOset.Insert_myUTXO(coinbase_tx_id,
0,
coinbase_data.lock,
coinbase_data.value)
"""
# Delete from MemoryPool
for tx in candidate_block.tx_set:
Transaction.Pop_MemoryPool(base64.b64decode(tx.tx_id))
# Proof of work
"""
parameter: blockData, targetValue
return: nonce, current_time
"""
@classmethod
def proofofwork(cls, blockData, targetValue):
nonce = 0
start = int(time.time())
while(Mining._MiningFlag):
keccak_hash = keccak.new(digest_bits=256)
current_time = int(time.time())
SumString = blockData + str(nonce) + str(current_time)
elaped_time = int(current_time - start)
keccak_hash.update(SumString.encode(('ascii')))
if int('0x' + keccak_hash.hexdigest(), 0) < targetValue:
print('target nonce :' + str(nonce))
print('elapsed time: '+ str(elaped_time))
return (nonce, current_time)
nonce += 1
return (False,0)
# Flag value management
@classmethod
def flagup(cls):
cls._MiningFlag = True
@classmethod
def flagdown(cls):
cls._MiningFlag = False
@classmethod
def miningflag(cls):
return cls._MiningFlag