|
|
@@ -508,13 +508,13 @@ def check_orphan_tx_from_pool(peer):
|
|
|
# =============================================================================
|
|
|
|
|
|
def broadcast_winner_block(peers,block):
|
|
|
- number_of_verification = 0
|
|
|
+ num_of_verification = 0
|
|
|
for peer in peers:
|
|
|
if peer.verify_block(block):
|
|
|
try_to_add_block(peer,block)
|
|
|
- number_of_verification += 1
|
|
|
+ num_of_verification += 1
|
|
|
|
|
|
- return number_of_verification
|
|
|
+ return num_of_verification
|
|
|
|
|
|
|
|
|
# =============================================================================
|
|
|
@@ -539,18 +539,6 @@ def find_vout_pointer_from_txs(txs):
|
|
|
def find_vin_pointer_from_txs(txs):
|
|
|
return [vin.to_spend for tx in txs for vin in tx.tx_in]
|
|
|
|
|
|
-##def confirm_utxos_from_txs(utxo_set,txs,allow_utxo_from_pool):
|
|
|
-## if allow_utxo_from_pool:
|
|
|
-## utxos = find_utxos_from_txs(txs[1:])
|
|
|
-## add_utxos_from_tx_to_set(utxo_set,txs[0])
|
|
|
-## pointers = find_vout_pointer_from_txs(txs)
|
|
|
-## confirm_utxo_by_pointers(utxo_set,pointers)
|
|
|
-## return pointers,utxos
|
|
|
-## else:
|
|
|
-## utxos = find_utxos_from_block(txs)
|
|
|
-## pointers = find_vout_pointer_from_txs(txs)
|
|
|
-## add_utxos_to_set(utxo_set,utxos)
|
|
|
-## return pointers,utxos
|
|
|
|
|
|
def confirm_utxos_from_txs(utxo_set,txs,allow_utxo_from_pool):
|
|
|
if allow_utxo_from_pool:
|
|
|
@@ -570,15 +558,14 @@ def remove_spent_utxo_from_txs(utxo_set,txs):
|
|
|
return utxos
|
|
|
|
|
|
def delete_utxo_by_pointers(utxo_set,pointers):
|
|
|
- utxos_from_vins = []
|
|
|
+ utxos_to_delete = []
|
|
|
for pointer in pointers:
|
|
|
if pointer in utxo_set:
|
|
|
- utxos_from_vins.append(utxo_set[pointer])
|
|
|
+ utxos_to_delete.append(utxo_set[pointer])
|
|
|
del utxo_set[pointer]
|
|
|
- return utxos_from_vins
|
|
|
+ return utxos_to_delete
|
|
|
|
|
|
|
|
|
-
|
|
|
def sign_utxo_from_tx(utxo_set,tx):
|
|
|
for vin in tx.tx_in:
|
|
|
pointer = vin.to_spend
|
|
|
@@ -626,7 +613,7 @@ def verify_tx(peer,tx,pool = {}):
|
|
|
logger.info('{0} double payment'.format(tx))
|
|
|
return False
|
|
|
|
|
|
- available_value = 0
|
|
|
+ accout_available_value = 0
|
|
|
|
|
|
for vin in tx.tx_in:
|
|
|
utxo = peer.utxo_set.get(vin.to_spend)
|
|
|
@@ -642,9 +629,9 @@ def verify_tx(peer,tx,pool = {}):
|
|
|
logger.info('singature does not math for {0}'.format(tx))
|
|
|
return False
|
|
|
|
|
|
- available_value += utxo.vout.value
|
|
|
+ accout_available_value += utxo.vout.value
|
|
|
|
|
|
- if available_value < sum(vout.value for vout in tx.tx_out):
|
|
|
+ if accout_available_value < sum(vout.value for vout in tx.tx_out):
|
|
|
logger.info(
|
|
|
'{0} no enought available value to spend by {1}(pid={2})'.format(tx,peer,peer.pid)
|
|
|
)
|
|
|
@@ -656,39 +643,42 @@ def verify_signature(peer,vin,utxo,tx_out):
|
|
|
script = check_script_for_vin(vin,utxo,peer.key_base_len)
|
|
|
if not script:
|
|
|
return False
|
|
|
- string = str(vin.to_spend) + str(vin.pubkey) + str(tx_out)
|
|
|
- message = build_message(string)
|
|
|
+ tem_s= str(vin.to_spend) + str(vin.pubkey) + str(tx_out)
|
|
|
+ message = build_message(tem_s)
|
|
|
peer.machine.set_script(script,message)
|
|
|
return peer.machine.run()
|
|
|
|
|
|
|
|
|
def check_script_for_vin(vin,utxo,baselen):
|
|
|
- sig_script,pubkey_script = vin.sig_script,utxo.pubkey_script
|
|
|
- double,fourfold = int(baselen*2),int(baselen*4)
|
|
|
- if len(sig_script) != fourfold:
|
|
|
+ sign_script = vin.sig_script
|
|
|
+ pubkey_script = utxo.pubkey_script
|
|
|
+ twofold = int(2*baselen)
|
|
|
+ fourfold = int(4*baselen)
|
|
|
+ if len(sign_script) != fourfold:
|
|
|
return False
|
|
|
- sig_scrip = [sig_script[:double],sig_script[double:]]
|
|
|
+ sig_scrip = [sign_script[:twofold],sign_script[twofold:]]
|
|
|
try:
|
|
|
pubkey_script = pubkey_script.split(' ')
|
|
|
except Exception:
|
|
|
return False
|
|
|
-
|
|
|
- return sig_scrip+pubkey_script
|
|
|
+ result=sig_scrip+pubkey_script
|
|
|
+ return result
|
|
|
|
|
|
|
|
|
def verify_signature_for_vin(vin,utxo,tx_out):
|
|
|
- pk_str,signature = vin.pubkey,vin.signature
|
|
|
+ pk_str= vin.pubkey
|
|
|
+ signature = vin.signature
|
|
|
to_addr = utxo.vout.to_addr
|
|
|
- string = str(vin.to_spend) + str(pk_str) + str(tx_out)
|
|
|
- message = build_message(string)
|
|
|
+ tem_s = str(vin.to_spend) + str(pk_str) + str(tx_out)
|
|
|
+ message = build_message(tem_s)
|
|
|
pubkey_as_addr = convert_pubkey_to_addr(pk_str)
|
|
|
- verifying_key = VerifyingKey.from_bytes(pk_str)
|
|
|
+ verify_key = VerifyingKey.from_bytes(pk_str)
|
|
|
|
|
|
if pubkey_as_addr != to_addr:
|
|
|
- return Fasle
|
|
|
+ return False
|
|
|
|
|
|
- if not verifying_key.verify(signature, message):
|
|
|
- return Fasle
|
|
|
+ if not verify_key.verify(signature, message):
|
|
|
+ return False
|
|
|
|
|
|
return True
|
|
|
|
|
|
@@ -715,7 +705,7 @@ def verify_coinbase(tx,reward):
|
|
|
return False
|
|
|
if not tx.is_coinbase:
|
|
|
return False
|
|
|
- if (not (len(tx.tx_out) ==1)) or (tx.tx_out[0].value != reward):
|
|
|
+ if ( len(tx.tx_out) !=1) or (tx.tx_out[0].value != reward):
|
|
|
return False
|
|
|
return True
|
|
|
|
|
|
@@ -743,8 +733,8 @@ def verify_winner_block(peer,block):
|
|
|
return False
|
|
|
|
|
|
block_txs = txs[1:]
|
|
|
- rewards = peer.get_block_reward()+peer.calculate_fees(block_txs)
|
|
|
- if not verify_coinbase(block.txs[0],rewards):
|
|
|
+ reward = peer.get_block_reward()+peer.calculate_fees(block_txs)
|
|
|
+ if not verify_coinbase(block.txs[0],reward):
|
|
|
logger.info('{0} coinbase incorrect'.format(block))
|
|
|
return False
|
|
|
|
|
|
@@ -761,7 +751,7 @@ def verify_winner_block(peer,block):
|
|
|
def double_payment_in_block_txs(txs):
|
|
|
a = {vin.to_spend for tx in txs for vin in tx.tx_in}
|
|
|
b = [vin.to_spend for tx in txs for vin in tx.tx_in]
|
|
|
- return len(a) != len(b)
|
|
|
+ return len(b) != len(a)
|
|
|
|
|
|
|
|
|
# =============================================================================
|
|
|
@@ -788,8 +778,10 @@ def try_to_add_block(peer,block):
|
|
|
return True
|
|
|
|
|
|
elif height == peer.get_height()-1:
|
|
|
- b1,b2 = peer.blockchain[-1],block
|
|
|
- a,b = int(b1.hash,16),int(b2.hash,16)
|
|
|
+ b1= peer.blockchain[-1]
|
|
|
+ b2 =block
|
|
|
+ a= int(b1.hash,16)
|
|
|
+ b = int(b2.hash,16)
|
|
|
if a < b:
|
|
|
return False
|
|
|
else:
|
|
|
@@ -803,7 +795,8 @@ def check_orphan_block(peer):
|
|
|
pass
|
|
|
|
|
|
def recieve_new_prev_hash_block(peer,txs):
|
|
|
- utxo_set,pool = peer.utxo_set,peer.mem_pool
|
|
|
+ utxo_set= peer.utxo_set
|
|
|
+ pool = peer.mem_pool
|
|
|
allow_utxo_from_pool = peer.allow_utxo_from_pool
|
|
|
peer._utxos_from_vins = remove_spent_utxo_from_txs(utxo_set,txs)
|
|
|
peer._pointers_from_vouts,peer._utxos_from_vouts = confirm_utxos_from_txs(
|
|
|
@@ -834,11 +827,11 @@ def compare_block_by_hash(a,b):
|
|
|
# =============================================================================
|
|
|
|
|
|
def get_unknown_txs_from_block(mem_pool,txs):
|
|
|
- substraction = {}
|
|
|
+ candidates = {}
|
|
|
for tx in txs:
|
|
|
if tx not in mem_pool.values():
|
|
|
- substraction[tx.id] = tx
|
|
|
- return substraction
|
|
|
+ candidates[tx.id] = tx
|
|
|
+ return candidates
|
|
|
|
|
|
|
|
|
def fill_mem_pool(peer):
|
|
|
@@ -864,12 +857,12 @@ def add_tx_to_mem_pool(peer,tx):
|
|
|
add_utxos_from_tx_to_set(peer.utxo_set,tx)
|
|
|
|
|
|
def calculate_next_block_bits(local_time,prev_height,prev_bits):
|
|
|
- flag = (prev_height + 1) % Params.TOTAL_BLOCKS
|
|
|
+ flag = (1+prev_height) % Params.TOTAL_BLOCKS
|
|
|
if flag != 0:
|
|
|
return prev_bits
|
|
|
|
|
|
- count = ((prev_height + 1)//Params.TOTAL_BLOCKS)*Params.TOTAL_BLOCKS
|
|
|
- actual_time_taken = local_time[:prev_height] - local_time[:count]
|
|
|
+ high_index = ((prev_height + 1)//Params.TOTAL_BLOCKS)*Params.TOTAL_BLOCKS
|
|
|
+ actual_time_taken = local_time[:prev_height] - local_time[:high_index]
|
|
|
|
|
|
if actual_time_taken < Params.PERIOD_FOR_TOTAL_BLOCKS:
|
|
|
return prev_bits + 1
|