def get_work_rpc( shard: Optional[int], host: str = "localhost", jrpc_port: int = 38391 ) -> MiningWork: json_rpc_url = "http://{}:{}".format(host, jrpc_port) header_hash, height, diff = jsonrpcclient.request( json_rpc_url, "getWork", hex(shard) if shard is not None else None ) return MiningWork(bytes.fromhex(header_hash[2:]), int(height, 16), int(diff, 16))
def test_getWork_and_submitWork(self): id1 = Identity.create_random_identity() acc1 = Address.create_from_identity(id1, full_shard_key=0) with ClusterContext( 1, acc1, remote_mining=True, shard_size=1, small_coinbase=True) as clusters, jrpc_server_context( clusters[0].master): master = clusters[0].master slaves = clusters[0].slave_list tx = create_transfer_transaction( shard_state=clusters[0].get_shard_state(1 | 0), key=id1.get_key(), from_address=acc1, to_address=acc1, value=0, gas_price=12, ) self.assertTrue(slaves[0].add_tx(tx)) for shard_id in ["0x0", None]: # shard, then root resp = send_request("getWork", [shard_id]) self.assertEqual(resp[1:], ["0x1", "0xa"]) # height and diff header_hash_hex = resp[0] if shard_id is not None: # shard 0 miner_address = Address.create_from( master.env.quark_chain_config.shards[1]. COINBASE_ADDRESS) else: # root miner_address = Address.create_from( master.env.quark_chain_config.ROOT.COINBASE_ADDRESS) block = call_async( master.get_next_block_to_mine(address=miner_address, branch_value=shard_id and 0b01)) # solve it and submit work = MiningWork(bytes.fromhex(header_hash_hex[2:]), 1, 10) solver = DoubleSHA256(work) nonce = solver.mine(0, 10000).nonce mixhash = "0x" + sha3_256(b"").hex() resp = send_request( "submitWork", [ shard_id, header_hash_hex, hex(nonce), mixhash, "0x" + bytes(65).hex(), ], ) self.assertTrue(resp) # show progress on shard 0 self.assertEqual( clusters[0].get_shard_state(1 | 0).get_tip().header.height, 1)
async def get_work(self, branch: Branch) -> Optional[MiningWork]: try: shard = self.shards[branch] work, block = await shard.miner.get_work() if shard.state.shard_config.POSW_CONFIG.ENABLED: check(isinstance(block, MinorBlock)) diff = shard.state.posw_diff_adjust(block) work = MiningWork(work.hash, work.height, diff) return work except Exception: Logger.log_exception() return None
def test_getWork_and_submitWork(self): id1 = Identity.create_random_identity() acc1 = Address.create_from_identity(id1, full_shard_id=0) with ClusterContext( 1, acc1, remote_mining=True, shard_size=1 ) as clusters, jrpc_server_context(clusters[0].master): master = clusters[0].master slaves = clusters[0].slave_list branch = Branch.create(1, 0) tx = create_transfer_transaction( shard_state=slaves[0].shards[branch].state, key=id1.get_key(), from_address=acc1, to_address=acc1, value=0, gas_price=12, ) self.assertTrue(slaves[0].add_tx(tx)) for shard_id in ["0x0", None]: # shard, then root resp = send_request("getWork", shard_id) self.assertEqual(resp[1:], ["0x1", "0xa"]) # height and diff header_hash_hex = resp[0] _, block = call_async( master.get_next_block_to_mine( address=master.env.quark_chain_config.miner_address, prefer_root=shard_id is None, ) ) self.assertEqual( header_hash_hex[2:], block.header.get_hash_for_mining().hex() ) # solve it and submit work = MiningWork(bytes.fromhex(resp[0][2:]), 1, 10) solver = DoubleSHA256(work) nonce = solver.mine(0, 10000).nonce mixhash = "0x" + sha3_256(b"").hex() resp = send_request( "submitWork", shard_id, header_hash_hex, hex(nonce), mixhash ) self.assertTrue(resp) # show progress _, new_block = call_async(master.get_next_block_to_mine(address=acc1)) self.assertIsInstance(new_block, MinorBlock) self.assertEqual(new_block.header.height, 2)
def get_work_rpc( full_shard_id: Optional[int], host: str = "localhost", jrpc_port: int = 38391, timeout=TIMEOUT, ) -> MiningWork: jrpc_url = "http://{}:{}".format(host, jrpc_port) cli = get_jsonrpc_cli(jrpc_url) header_hash, height, diff = cli.send( jsonrpcclient.Request( "getWork", hex(full_shard_id) if full_shard_id is not None else None ), timeout=timeout, ) return MiningWork(bytes.fromhex(header_hash[2:]), int(height, 16), int(diff, 16))
def test_sha3sha3(self): miner = self.miner_gen(ConsensusType.POW_SHA3SHA3, None, None) block = RootBlock( RootBlockHeader(create_time=42, difficulty=5), tracking_data="{}".encode("utf-8"), ) work = MiningWork(block.header.get_hash_for_mining(), 42, 5) # only process one block, which is passed in. `None` means termination right after miner.input_q.put((None, {})) miner._mine_loop( ConsensusType.POW_SHA3SHA3, work, {}, miner.input_q, miner.output_q ) mined_res = miner.output_q.get() self.assertEqual(mined_res.nonce, 8) block.header.nonce = mined_res.nonce validate_seal(block.header, ConsensusType.POW_SHA3SHA3)
def test_qkchash(self): miner = self.miner_gen(ConsensusType.POW_QKCHASH, None, None) block = RootBlock( RootBlockHeader(create_time=42, difficulty=5), tracking_data="{}".encode("utf-8"), ) work = MiningWork(block.header.get_hash_for_mining(), 42, 5) # only process one block, which is passed in. `None` means termination right after miner.input_q.put((None, {})) miner.mine_loop( work, {"consensus_type": ConsensusType.POW_QKCHASH}, miner.input_q, miner.output_q, ) mined_res = miner.output_q.get() block.header.nonce = mined_res.nonce block.header.mixhash = mined_res.mixhash validate_seal(block.header, ConsensusType.POW_QKCHASH)