def test_cpu_bound_fork_operator_error(): print('##### test_cpu_bound_fork_operator_error #####') try: x = 1 + fork(operator_error_cpu) print(x) except: given_traceback = traceback.format_exc() if re.match(wanted_operator_traceback.strip(), given_traceback.strip()): print('Traceback looks as desired.') else: print('Traceback do not look as desired:') print(given_traceback.strip())
def test_io_bound_fork_runtime_error(): print('##### test_io_bound_fork_runtime_error #####') try: x = fork(runtime_error_io) print(x) except: given_traceback = traceback.format_exc() if re.match(wanted_runtime_traceback.strip(), given_traceback.strip()): print('Traceback looks as desired.') else: print('Traceback do not look as desired:') print(given_traceback.strip())
def test_cpu_bound_fork_contagious(n): print('##### test_cpu_bound_fork_contagious #####') start = time.time() seq_results = 0 for i in [20]*n: seq_results += fib(i) str(seq_results) end = time.time() print('sequential:', end-start) start = time.time() par_results = 0 for i in [20]*n: par_results += fork(fib, i) str(par_results) end = time.time() print('parallel: ', end-start) if seq_results == par_results: print('results are equal') else: print('results are unequal') print('sequential:', seq_results) print('parallel: ', par_results)
def test_io_bound_fork_contagious(n): print('##### test_io_bound_fork_contagious #####') start = time.time() seq_results = '' for i in range(n): seq_results += webservice() str(seq_results) end = time.time() print('sequential:', end-start) start = time.time() par_results = '' for i in range(n): par_results += fork(webservice) str(par_results) end = time.time() print('parallel: ', end-start) if seq_results == par_results: print('results are equal') else: print('results are unequal') print('sequential:', seq_results) print('parallel: ', par_results)
def test_cpu_bound_fork(n): print('##### test_cpu_bound_fork #####') start = time.time() seq_results = [] for i in range(n): seq_results += [fib(i)] str(seq_results) end = time.time() print('sequential:', end-start) start = time.time() par_results = [] for i in range(n): par_results += [fork(fib, i)] str(par_results) end = time.time() print('parallel: ', end-start) if seq_results == par_results: print('results are equal') else: print('results are unequal') print('sequential:', seq_results) print('parallel: ', par_results)
def difficulty(node, db_handler): try: POW_FORK, FORK_AHEAD, FORK_DIFF = fork() db_handler.execute( db_handler.c, "SELECT * FROM transactions WHERE reward != 0 ORDER BY block_height DESC LIMIT 2" ) result = db_handler.c.fetchone() timestamp_last = Decimal(result[1]) block_height = int(result[0]) node.last_block_timestamp = timestamp_last #node.last_block = block_height do not fetch this here, could interfere with block saving previous = db_handler.c.fetchone() node.last_block_ago = int(time.time() - int(timestamp_last)) # Failsafe for regtest starting at block 1} timestamp_before_last = timestamp_last if previous is None else Decimal( previous[1]) db_handler.execute_param(db_handler.c, ( "SELECT timestamp FROM transactions WHERE block_height > ? AND reward != 0 ORDER BY timestamp ASC LIMIT 2" ), (block_height - 1441, )) timestamp_1441 = Decimal(db_handler.c.fetchone()[0]) block_time_prev = (timestamp_before_last - timestamp_1441) / 1440 temp = db_handler.c.fetchone() timestamp_1440 = timestamp_1441 if temp is None else Decimal(temp[0]) block_time = Decimal(timestamp_last - timestamp_1440) / 1440 db_handler.execute( db_handler.c, "SELECT difficulty FROM misc ORDER BY block_height DESC LIMIT 1") diff_block_previous = Decimal(db_handler.c.fetchone()[0]) time_to_generate = timestamp_last - timestamp_before_last if node.is_regnet: return (float('%.10f' % regnet.REGNET_DIFF), float('%.10f' % (regnet.REGNET_DIFF - 8)), float(time_to_generate), float(regnet.REGNET_DIFF), float(block_time), float(0), float(0), block_height) hashrate = pow(2, diff_block_previous / Decimal(2.0)) / ( block_time * math.ceil(28 - diff_block_previous / Decimal(16.0))) # Calculate new difficulty for desired blocktime of 60 seconds target = Decimal(60.00) ##D0 = diff_block_previous difficulty_new = Decimal( (2 / math.log(2)) * math.log(hashrate * target * math.ceil(28 - diff_block_previous / Decimal(16.0)))) # Feedback controller Kd = 10 difficulty_new = difficulty_new - Kd * (block_time - block_time_prev) diff_adjustment = (difficulty_new - diff_block_previous ) / 720 # reduce by factor of 720 if diff_adjustment > Decimal(1.0): diff_adjustment = Decimal(1.0) difficulty_new_adjusted = quantize_ten(diff_block_previous + diff_adjustment) difficulty = difficulty_new_adjusted if node.is_mainnet: if block_height == POW_FORK - FORK_AHEAD: limit_version(node) if block_height == POW_FORK - 1: difficulty = FORK_DIFF if block_height == POW_FORK: difficulty = FORK_DIFF # Remove mainnet0018 from allowed limit_version(node) # disconnect our outgoing connections diff_drop_time = Decimal(180) if Decimal(time.time()) > Decimal(timestamp_last) + Decimal( 2 * diff_drop_time): # Emergency diff drop time_difference = quantize_two( time.time()) - quantize_two(timestamp_last) diff_dropped = quantize_ten(difficulty) - quantize_ten(1) \ - quantize_ten(10 * (time_difference - 2 * diff_drop_time) / diff_drop_time) elif Decimal(time.time() ) > Decimal(timestamp_last) + Decimal(diff_drop_time): time_difference = quantize_two( time.time()) - quantize_two(timestamp_last) diff_dropped = quantize_ten(difficulty) + quantize_ten( 1) - quantize_ten(time_difference / diff_drop_time) else: diff_dropped = difficulty if difficulty < 50: difficulty = 50 if diff_dropped < 50: diff_dropped = 50 return (float('%.10f' % difficulty), float('%.10f' % diff_dropped), float(time_to_generate), float(diff_block_previous), float(block_time), float(hashrate), float(diff_adjustment), block_height ) # need to keep float here for database inserts support except: #new chain or regnet difficulty = [24, 24, 0, 0, 0, 0, 0, 0] return difficulty
import base64 import essentials import hashlib import staking from fork import * import mining import mining_heavy3 import regnet import mempool as mp from essentials import db_to_drive #rework from essentials import checkpoint_set #rework from essentials import ledger_balance3 #rework from difficulty import * POW_FORK, FORK_AHEAD, FORK_DIFF = fork() from Cryptodome.Hash import SHA from Cryptodome.PublicKey import RSA from Cryptodome.Signature import PKCS1_v1_5 def digest_block(node, data, sdef, peer_ip, db_handler): """node param for imports""" class Transaction(): def __init__(self): self.start_time_tx = 0 self.q_received_timestamp = 0 self.received_timestamp = "0.00" self.received_address = None self.received_recipient = None
# linux 才能运行fork import os import fork import time print('--尝试fork()进程--') pid = fork() # 返回0 代表子进程, 反之代表主进程 print('进程ID', os.getpid(), 'hello...')
def cpu_a(): print('cpu_a') for _ in range(30): fork(cpu_b) print('cpu_a end')
def io_b(): print('io_b') for _ in range(30): fork(io_c) print('io_b end')
def io_a(): print('io_a') for _ in range(30): fork(io_b) print('io_a end')
def cpu_b(): print('cpu_b') for _ in range(30): fork(cpu_c) print('cpu_b end')
print('cpu_c') pass @io_bound def io_a(): print('io_a') for _ in range(30): fork(io_b) print('io_a end') @io_bound def io_b(): print('io_b') for _ in range(30): fork(io_c) print('io_b end') @io_bound def io_c(): print('io_c') pass fork(cpu_a) fork(io_a) fork(fork, fork, fork, fork, fork, cpu_a) fork(fork, fork, fork, fork, fork, io_a)