def run_test(self): self.reset() daemon = Daemon() wallet = Wallet() # close the wallet if any, will throw if none is loaded try: wallet.close_wallet() except: pass wallet.restore_deterministic_wallet( 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted' ) destinations = [] for i in range(3): destinations.append({ "amount": 1, "address": '888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H' }) self._test_speed_generateblocks(daemon=daemon, blocks=70) for i in range(1, 10): while wallet.get_balance().unlocked_balance == 0: print('Waiting for wallet to refresh...') sleep(1) self._test_speed_transfer_split(wallet=wallet) self._test_speed_generateblocks(daemon=daemon, blocks=10)
def run_test(self): self.reset() daemon = Daemon() wallet = Wallet() # close the wallet if any, will throw if none is loaded try: wallet.close_wallet() except: pass wallet.restore_deterministic_wallet( 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted' ) destinations = [] for i in range(3): destinations.append({ "amount": 1, "address": '44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A' }) self._test_speed_generateblocks(daemon=daemon, blocks=70) for i in range(1, 10): while wallet.get_balance().unlocked_balance == 0: print('Waiting for wallet to refresh...') sleep(1) self._test_speed_transfer_split(wallet=wallet) self._test_speed_generateblocks(daemon=daemon, blocks=10)
def run_test(self): daemon = Daemon() wallet = Wallet() destinations = wallet.make_uniform_destinations('44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A',1,3) self._test_speed_generateblocks(daemon=daemon, blocks=70) for i in range(1, 10): while wallet.get_balance().unlocked_balance == 0: print('Waiting for wallet to refresh...') sleep(1) self._test_speed_transfer_split(wallet=wallet) self._test_speed_generateblocks(daemon=daemon, blocks=10)
def run_test(self): daemon = Daemon() wallet = Wallet() destinations = wallet.make_uniform_destinations('44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A',1,3) self._test_speed_generateblocks(daemon=daemon, blocks=70) for i in range(1, 10): while wallet.get_balance().unlocked_balance == 0: print('Waiting for wallet to refresh...') sleep(1) self._test_speed_transfer_split(wallet=wallet) self._test_speed_generateblocks(daemon=daemon, blocks=10)
def mine(self, via_daemon): print("Test mining via " + ("daemon" if via_daemon else "wallet")) cores_init = multiprocessing.cpu_count() # RX init uses all cores cores_mine = 1 # Mining uses a parametric number of cores is_mining_measurent = 'MINING_NO_MEASUREMENT' not in os.environ if is_mining_measurent: # A dynamic calculation of the CPU power requested time_pi_single_cpu = self.measure_cpu_power_get_time(cores_mine) time_pi_all_cores = self.measure_cpu_power_get_time(cores_init) # This is the last measurement, since it takes very little time and can be placed timewise-closer to the mining itself. available_ram = self.get_available_ram( ) # So far no ideas how to use this var, other than printing it start = monotonic.monotonic() daemon = Daemon() wallet = Wallet() # check info/height/balance before generating blocks res_info = daemon.get_info() initial_height = res_info.height res_getbalance = wallet.get_balance() prev_balance = res_getbalance.balance res_status = daemon.mining_status() if via_daemon: res = daemon.start_mining( '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', threads_count=1) else: res = wallet.start_mining(threads_count=cores_mine) res_status = daemon.mining_status() assert res_status.active == True assert res_status.threads_count == cores_mine assert res_status.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' assert res_status.is_background_mining_enabled == False assert res_status.block_reward >= 600000000000 # wait till we mined a few of them target_height = initial_height + 5 height = initial_height if not is_mining_measurent: timeout_init = 600 timeout_mine = 300 else: """ Randomx init has high variance on CI machines due to noisy neighbors, taking up resources in parallel (including by our own jobs). Mining is organized in the following scheme: 1) first loop's pass: RandomX init and mining 2) every next pass: only mining Pass 1) takes much more time than pass 2) Pass 1) uses all cores, pass 2) just one (currently) For the above reasons both passes need separate timeouts and adjustments. After the first pass, the timeout is being reset to a lower value. """ def calc_timeout(seconds_constant, time_pi, cores): """ The time it took to calculate pi under certain conditions is proportional to the time it will take to calculate the real job. The number of cores used decreases the time almost linearly. """ timeout = float(seconds_constant) * time_pi / float(cores) return timeout timeout_base_init = 60 # RX init needs more time timeout_base_mine = 20 timeout_init = calc_timeout(timeout_base_init, time_pi_all_cores, cores_init) timeout_mine = calc_timeout(timeout_base_mine, time_pi_single_cpu, cores_mine) msg_timeout_src = "adjusted for the currently available CPU power" if is_mining_measurent else "selected to have the default value" msg = "Timeout for {} {}, is {:.1f} s" self.print_mining_info( msg.format("init, ", msg_timeout_src, timeout_init)) self.print_mining_info( msg.format("mining,", msg_timeout_src, timeout_mine)) timeout = timeout_init rx_inited = False # Gets initialized in the first pass of the below loop while height < target_height: seen_height = height for _ in range(int(math.ceil(timeout))): time.sleep(1) seconds_passed = monotonic.monotonic() - start height = daemon.get_info().height if height > seen_height: break else: assert False, 'Failed to mine successor to block %d (initial block = %d) after %d s. RX initialized = %r' % ( seen_height, initial_height, round(seconds_passed), rx_inited) if not rx_inited: rx_inited = True timeout = timeout_mine # Resetting the timeout after first mined block and RX init self.print_time_taken(start, "RX init + mining 1st block") else: self.print_time_taken(start, "mining iteration") self.print_time_taken(start, "mining total") if via_daemon: res = daemon.stop_mining() else: res = wallet.stop_mining() res_status = daemon.mining_status() assert res_status.active == False res_info = daemon.get_info() new_height = res_info.height wallet.refresh() res_getbalance = wallet.get_balance() balance = res_getbalance.balance assert balance >= prev_balance + (new_height - initial_height) * 600000000000 if via_daemon: res = daemon.start_mining( '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', threads_count=1, do_background_mining=True) else: res = wallet.start_mining(threads_count=1, do_background_mining=True) res_status = daemon.mining_status() assert res_status.active == True assert res_status.threads_count == 1 assert res_status.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' assert res_status.is_background_mining_enabled == True assert res_status.block_reward >= 600000000000 # don't wait, might be a while if the machine is busy, which it probably is if via_daemon: res = daemon.stop_mining() else: res = wallet.stop_mining() res_status = daemon.mining_status() assert res_status.active == False
def mine(self, via_daemon): print("Test mining via " + ("daemon" if via_daemon else "wallet")) daemon = Daemon() wallet = Wallet() # check info/height/balance before generating blocks res_info = daemon.get_info() prev_height = res_info.height res_getbalance = wallet.get_balance() prev_balance = res_getbalance.balance res_status = daemon.mining_status() if via_daemon: res = daemon.start_mining('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', threads_count = 1) else: res = wallet.start_mining(threads_count = 1) res_status = daemon.mining_status() assert res_status.active == True assert res_status.threads_count == 1 assert res_status.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' assert res_status.is_background_mining_enabled == False assert res_status.block_reward >= 600000000000 # wait till we mined a few of them target_height = prev_height + 5 height = prev_height timeout = 60 # randomx is slow to init while height < target_height: seen_height = height for _ in range(timeout): time.sleep(1) height = daemon.get_info().height if height > seen_height: break else: assert False, 'Failed to mine successor to block %d (initial block = %d)' % (seen_height, prev_height) timeout = 5 if via_daemon: res = daemon.stop_mining() else: res = wallet.stop_mining() res_status = daemon.mining_status() assert res_status.active == False res_info = daemon.get_info() new_height = res_info.height wallet.refresh() res_getbalance = wallet.get_balance() balance = res_getbalance.balance assert balance >= prev_balance + (new_height - prev_height) * 600000000000 if via_daemon: res = daemon.start_mining('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', threads_count = 1, do_background_mining = True) else: res = wallet.start_mining(threads_count = 1, do_background_mining = True) res_status = daemon.mining_status() assert res_status.active == True assert res_status.threads_count == 1 assert res_status.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' assert res_status.is_background_mining_enabled == True assert res_status.block_reward >= 600000000000 # don't wait, might be a while if the machine is busy, which it probably is if via_daemon: res = daemon.stop_mining() else: res = wallet.stop_mining() res_status = daemon.mining_status() assert res_status.active == False
class P2PTest(): def run_test(self): self.reset() self.create() self.mine(80) self.test_p2p_reorg() self.test_p2p_tx_propagation() def reset(self): print('Resetting blockchain') daemon = Daemon() res = daemon.get_height() daemon.pop_blocks(res.height - 1) daemon.flush_txpool() def create(self): print('Creating wallet') seed = 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted' self.wallet = Wallet(idx=4) # close the wallet if any, will throw if none is loaded try: self.wallet.close_wallet() except: pass res = self.wallet.restore_deterministic_wallet(seed=seed) def mine(self, blocks): assert blocks >= 1 print("Generating", blocks, 'blocks') daemon = Daemon(idx=2) # generate blocks res_generateblocks = daemon.generateblocks( '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', blocks) def test_p2p_reorg(self): print('Testing P2P reorg') daemon2 = Daemon(idx=2) daemon3 = Daemon(idx=3) # give sync some time time.sleep(1) res = daemon2.get_info() height = res.height assert height > 0 top_block_hash = res.top_block_hash assert len(top_block_hash) == 64 res = daemon3.get_info() assert res.height == height assert res.top_block_hash == top_block_hash # disconnect daemons and mine separately on both daemon2.out_peers(0) daemon3.out_peers(0) res = daemon2.generateblocks( '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 2) res = daemon3.generateblocks( '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 3) res = daemon2.get_info() assert res.height == height + 2 daemon2_top_block_hash = res.top_block_hash assert daemon2_top_block_hash != top_block_hash res = daemon3.get_info() assert res.height == height + 3 daemon3_top_block_hash = res.top_block_hash assert daemon3_top_block_hash != top_block_hash assert daemon3_top_block_hash != daemon2_top_block_hash # reconnect, daemon2 will now switch to daemon3's chain daemon2.out_peers(8) daemon3.out_peers(8) time.sleep(10) res = daemon2.get_info() assert res.height == height + 3 assert res.top_block_hash == daemon3_top_block_hash # disconect, mine on daemon2 again more than daemon3 daemon2.out_peers(0) daemon3.out_peers(0) res = daemon2.generateblocks( '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 3) res = daemon3.generateblocks( '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 2) res = daemon2.get_info() assert res.height == height + 6 daemon2_top_block_hash = res.top_block_hash assert daemon2_top_block_hash != top_block_hash res = daemon3.get_info() assert res.height == height + 5 daemon3_top_block_hash = res.top_block_hash assert daemon3_top_block_hash != top_block_hash assert daemon3_top_block_hash != daemon2_top_block_hash # reconnect, daemon3 will now switch to daemon2's chain daemon2.out_peers(8) daemon3.out_peers(8) time.sleep(5) res = daemon3.get_info() assert res.height == height + 6 assert res.top_block_hash == daemon2_top_block_hash # disconnect and mine a lot on daemon3 daemon2.out_peers(0) daemon3.out_peers(0) res = daemon3.generateblocks( '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 500) # reconnect and wait for sync daemon2.out_peers(8) daemon3.out_peers(8) loops = 100 while True: res2 = daemon2.get_info() res3 = daemon3.get_info() if res2.top_block_hash == res3.top_block_hash: break time.sleep(10) loops -= 1 assert loops >= 0 def test_p2p_tx_propagation(self): print('Testing P2P tx propagation') daemon2 = Daemon(idx=2) daemon3 = Daemon(idx=3) for daemon in [daemon2, daemon3]: res = daemon.get_transaction_pool_hashes() assert not 'tx_hashes' in res or len(res.tx_hashes) == 0 self.wallet.refresh() res = self.wallet.get_balance() dst = { 'address': '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 'amount': 1000000000000 } res = self.wallet.transfer([dst]) assert len(res.tx_hash) == 32 * 2 txid = res.tx_hash time.sleep(5) for daemon in [daemon2, daemon3]: res = daemon.get_transaction_pool_hashes() assert len(res.tx_hashes) == 1 assert res.tx_hashes[0] == txid
def mine(self): print("Test mining") daemon = Daemon() wallet = Wallet() # check info/height/balance before generating blocks res_info = daemon.get_info() prev_height = res_info.height res_getbalance = wallet.get_balance() prev_balance = res_getbalance.balance res_status = daemon.mining_status() res = daemon.start_mining( '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', threads_count=1) res_status = daemon.mining_status() assert res_status.active == True assert res_status.threads_count == 1 assert res_status.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' assert res_status.is_background_mining_enabled == False assert res_status.block_reward >= 600000000000 # wait till we mined a few of them timeout = 5 timeout_height = prev_height while True: time.sleep(1) res_info = daemon.get_info() height = res_info.height if height >= prev_height + 5: break if height > timeout_height: timeout = 5 timeout_height = height else: timeout -= 1 assert timeout >= 0 res = daemon.stop_mining() res_status = daemon.mining_status() assert res_status.active == False res_info = daemon.get_info() new_height = res_info.height wallet.refresh() res_getbalance = wallet.get_balance() balance = res_getbalance.balance assert balance >= prev_balance + (new_height - prev_height) * 600000000000 res = daemon.start_mining( '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', threads_count=1, do_background_mining=True) res_status = daemon.mining_status() assert res_status.active == True assert res_status.threads_count == 1 assert res_status.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' assert res_status.is_background_mining_enabled == True assert res_status.block_reward >= 600000000000 # don't wait, might be a while if the machine is busy, which it probably is res = daemon.stop_mining() res_status = daemon.mining_status() assert res_status.active == False
def mine(self): print("Test mining") daemon = Daemon() wallet = Wallet() # check info/height/balance before generating blocks res_info = daemon.get_info() prev_height = res_info.height res_getbalance = wallet.get_balance() prev_balance = res_getbalance.balance res_status = daemon.mining_status() res = daemon.start_mining('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', threads_count = 1) res_status = daemon.mining_status() assert res_status.active == True assert res_status.threads_count == 1 assert res_status.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' assert res_status.is_background_mining_enabled == False assert res_status.block_reward >= 600000000000 # wait till we mined a few of them timeout = 5 timeout_height = prev_height while True: time.sleep(1) res_info = daemon.get_info() height = res_info.height if height >= prev_height + 5: break if height > timeout_height: timeout = 5 timeout_height = height else: timeout -= 1 assert timeout >= 0 res = daemon.stop_mining() res_status = daemon.mining_status() assert res_status.active == False res_info = daemon.get_info() new_height = res_info.height wallet.refresh() res_getbalance = wallet.get_balance() balance = res_getbalance.balance assert balance >= prev_balance + (new_height - prev_height) * 600000000000 res = daemon.start_mining('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', threads_count = 1, do_background_mining = True) res_status = daemon.mining_status() assert res_status.active == True assert res_status.threads_count == 1 assert res_status.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' assert res_status.is_background_mining_enabled == True assert res_status.block_reward >= 600000000000 # don't wait, might be a while if the machine is busy, which it probably is res = daemon.stop_mining() res_status = daemon.mining_status() assert res_status.active == False