full node

This commit is contained in:
Yostra 2020-10-22 16:10:31 -07:00
parent db1cdf5056
commit 99fa3af1a3
2 changed files with 71 additions and 56 deletions

View File

@ -79,9 +79,9 @@ class ChiaServer:
web.get("/ws", self.incoming_connection),
]
self.app.add_routes(routes)
runner = web.AppRunner(self.app, access_log=None)
await runner.setup()
self.site = web.TCPSite(runner, port=self._port)
self.runner = web.AppRunner(self.app, access_log=None)
await self.runner.setup()
self.site = web.TCPSite(self.runner, port=self._port, shutdown_timeout=3)
await self.site.start()
self.log.info(f"Started listening on port: {self._port}")
@ -293,9 +293,8 @@ class ChiaServer:
self.log.error(f"exeption while closing connection {e}")
def close_all(self):
asyncio.ensure_future(self.close_all_connections())
self.site_shutdown_task = asyncio.create_task(self.site.stop())
self.connection_close_taks = asyncio.create_task(self.close_all_connections())
self.site_shutdown_task = asyncio.create_task(self.runner.cleanup())
self.app_shut_down_task = asyncio.create_task(self.app.shutdown())
self.shut_down_event.set()
@ -305,6 +304,7 @@ class ChiaServer:
async def await_closed(self):
self.log.info("Await Closed")
await self.shut_down_event.wait()
await self.connection_close_taks
await self.app_shut_down_task
await self.site_shutdown_task

View File

@ -86,10 +86,11 @@ class TestFullNodeProtocol:
@pytest.mark.asyncio
async def test_request_peers(self, two_nodes, wallet_blocks):
full_node_1, full_node_2, server_1, server_2 = two_nodes
await server_2.start_client(PeerInfo("127.0.0.1", uint16(server_1._port)), None)
async def have_msgs():
await full_node_1.full_node.full_node_peers.address_manager.add_to_new_table(
await full_node_2.full_node.full_node_peers.address_manager.add_to_new_table(
[
TimestampedPeerInfo("127.0.0.1", uint16(1000), uint64(int(time.time())) - 1000),
],
@ -98,15 +99,15 @@ class TestFullNodeProtocol:
msg = await full_node_2.full_node.full_node_peers.request_peers(
PeerInfo("[::1]", server_2._port)
)
if not (len(msg.data.peer_list) == 1):
return False
for peer in msg.data.peer_list:
return peer.host == "127.0.0.1" and peer.port == 1000
peer = msg.data.peer_list[0]
return peer.host == "127.0.0.1" and peer.port == 1000
await time_out_assert_custom_interval(10, 3, have_msgs, True)
await time_out_assert_custom_interval(10, 1, have_msgs, True)
full_node_1.full_node.full_node_peers.address_manager = AddressManager()
"""
@pytest.mark.asyncio
async def test_new_tip(self, two_nodes, wallet_blocks):
full_node_1, full_node_2, server_1, server_2 = two_nodes
@ -121,16 +122,16 @@ class TestFullNodeProtocol:
await time_out_assert(10, num_connections, 1)
new_tip_1 = fnp.NewTip(blocks[-1].height, blocks[-1].weight, blocks[-1].header_hash)
msgs_1 = [x async for x in full_node_1.new_tip(new_tip_1)]
new_tip_1 = fnp.NewTip(
blocks[-1].height, blocks[-1].weight, blocks[-1].header_hash
)
msg_1 = await full_node_1.new_tip(new_tip_1, None)
assert msg_1.data == fnp.RequestBlock(uint32(3), blocks[-1].header_hash)
assert len(msgs_1) == 1
assert msgs_1[0].message.data == fnp.RequestBlock(uint32(3), blocks[-1].header_hash)
new_tip_2 = fnp.NewTip(blocks[2].height, blocks[2].weight, blocks[2].header_hash)
msgs_2 = [x async for x in full_node_1.new_tip(new_tip_2)]
assert len(msgs_2) == 0
@pytest.mark.asyncio
async def test_new_transaction(self, two_nodes, wallet_blocks_five):
full_node_1, full_node_2, server_1, server_2 = two_nodes
@ -153,14 +154,15 @@ class TestFullNodeProtocol:
)
assert spend_bundle is not None
new_transaction = fnp.NewTransaction(spend_bundle.get_hash(), uint64(100), uint64(100))
# Not seen
msgs = [x async for x in full_node_1.new_transaction(new_transaction)]
assert len(msgs) == 1
assert msgs[0].message.data == fnp.RequestTransaction(spend_bundle.get_hash())
new_transaction = fnp.NewTransaction(
spend_bundle.get_hash(), uint64(100), uint64(100)
)
msg = await full_node_1.new_transaction(new_transaction, None)
assert msg.data == fnp.RequestTransaction(spend_bundle.get_hash())
respond_transaction_2 = fnp.RespondTransaction(spend_bundle)
[x async for x in full_node_1.respond_transaction(respond_transaction_2)]
await full_node_1.respond_transaction(respond_transaction_2, None)
program = best_solution_program(spend_bundle)
aggsig = spend_bundle.aggregated_signature
@ -174,19 +176,20 @@ class TestFullNodeProtocol:
transaction_data_at_height=dic_h,
)
# Already seen
msgs = [x async for x in full_node_1.new_transaction(new_transaction)]
assert len(msgs) == 0
msg = await full_node_1.new_transaction(new_transaction, None)
assert msg is None
# Farm one block
for block in blocks_new:
[_ async for _ in full_node_1.respond_block(fnp.RespondBlock(block))]
await full_node_1.respond_block(fnp.RespondBlock(block), None)
spend_bundles = []
total_fee = 0
# Fill mempool
for puzzle_hash in puzzle_hashes:
coin_record = (
await full_node_1.coin_store.get_coin_records_by_puzzle_hash(puzzle_hash, blocks_new[-3].header)
await full_node_1.full_node.coin_store.get_coin_records_by_puzzle_hash(
puzzle_hash, blocks_new[-3].header
)
)[0]
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
fee = random.randint(2, 499)
@ -194,17 +197,20 @@ class TestFullNodeProtocol:
500, receiver_puzzlehash, coin_record.coin, fee=fee
)
respond_transaction = fnp.RespondTransaction(spend_bundle)
res = [x async for x in full_node_1.respond_transaction(respond_transaction)]
await full_node_1.respond_transaction(respond_transaction, None)
# Added to mempool
if len(res) > 0:
request = fnp.RequestTransaction(spend_bundle.get_hash())
req = await full_node_1.request_transaction(request, None)
if req.data == fnp.RespondTransaction(spend_bundle):
total_fee += fee
spend_bundles.append(spend_bundle)
# Mempool is full
new_transaction = fnp.NewTransaction(token_bytes(32), uint64(1000000), uint64(1))
msgs = [x async for x in full_node_1.new_transaction(new_transaction)]
assert len(msgs) == 0
new_transaction = fnp.NewTransaction(
token_bytes(32), uint64(1000000), uint64(1)
)
msg = await full_node_1.new_transaction(new_transaction, None)
assert msg is None
agg = SpendBundle.aggregate(spend_bundles)
program = best_solution_program(agg)
@ -221,7 +227,7 @@ class TestFullNodeProtocol:
fees=uint64(total_fee),
)
# Farm one block to clear mempool
[_ async for _ in full_node_1.respond_block(fnp.RespondBlock(blocks_new[-1]))]
await full_node_1.respond_block(fnp.RespondBlock(blocks_new[-1]), None)
@pytest.mark.asyncio
async def test_request_respond_transaction(self, two_nodes, wallet_blocks_five):
@ -230,9 +236,9 @@ class TestFullNodeProtocol:
tx_id = token_bytes(32)
request_transaction = fnp.RequestTransaction(tx_id)
msgs = [x async for x in full_node_1.request_transaction(request_transaction)]
assert len(msgs) == 1
assert msgs[0].message.data == fnp.RejectTransactionRequest(tx_id)
msg = await full_node_1.request_transaction(request_transaction, None)
assert msg is not None
assert msg.data == fnp.RejectTransactionRequest(tx_id)
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
spend_bundle = wallet_a.generate_signed_transaction(
@ -242,14 +248,12 @@ class TestFullNodeProtocol:
)
assert spend_bundle is not None
respond_transaction = fnp.RespondTransaction(spend_bundle)
prop = [x async for x in full_node_1.respond_transaction(respond_transaction)]
assert len(prop) == 1
assert isinstance(prop[0].message.data, fnp.NewTransaction)
await full_node_1.respond_transaction(respond_transaction, None)
request_transaction = fnp.RequestTransaction(spend_bundle.get_hash())
msgs = [x async for x in full_node_1.request_transaction(request_transaction)]
assert len(msgs) == 1
assert msgs[0].message.data == fnp.RespondTransaction(spend_bundle)
msg = await full_node_1.request_transaction(request_transaction, None)
assert msg is not None
assert msg.data == fnp.RespondTransaction(spend_bundle)
@pytest.mark.asyncio
async def test_respond_transaction_fail(self, two_nodes, wallet_blocks):
@ -258,9 +262,9 @@ class TestFullNodeProtocol:
tx_id = token_bytes(32)
request_transaction = fnp.RequestTransaction(tx_id)
msgs = [x async for x in full_node_1.request_transaction(request_transaction)]
assert len(msgs) == 1
assert msgs[0].message.data == fnp.RejectTransactionRequest(tx_id)
msg = await full_node_1.request_transaction(request_transaction, None)
assert msg is not None
assert msg.data == fnp.RejectTransactionRequest(tx_id)
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
@ -272,17 +276,21 @@ class TestFullNodeProtocol:
)
assert spend_bundle is not None
respond_transaction = fnp.RespondTransaction(spend_bundle)
assert len([x async for x in full_node_1.respond_transaction(respond_transaction)]) == 0
msg = await full_node_1.respond_transaction(respond_transaction, None)
assert msg is None
@pytest.mark.asyncio
async def test_new_pot(self, two_nodes, wallet_blocks):
full_node_1, full_node_2, server_1, server_2 = two_nodes
wallet_a, wallet_receiver, _ = wallet_blocks
no_unf_block = fnp.NewProofOfTime(uint32(5), bytes(32 * [1]), uint64(124512), uint8(2))
assert len([x async for x in full_node_1.new_proof_of_time(no_unf_block)]) == 0
no_unf_block = fnp.NewProofOfTime(
uint32(5), bytes(32 * [1]), uint64(124512), uint8(2)
)
msg = await full_node_1.new_proof_of_time(no_unf_block, None)
assert msg is None
blocks = await get_block_path(full_node_1)
blocks = await get_block_path(full_node_1.full_node)
blocks_new = bt.get_consecutive_blocks(
test_constants,
@ -300,17 +308,22 @@ class TestFullNodeProtocol:
blocks_new[-1].transactions_filter,
)
unf_block_req = fnp.RespondUnfinishedBlock(unf_block)
res = [x async for x in full_node_1.respond_unfinished_block(unf_block_req)]
await full_node_1.respond_unfinished_block(unf_block_req, None)
dont_have = fnp.NewProofOfTime(
unf_block.height,
<<<<<<< HEAD
unf_block.proof_of_space.challenge,
res[0].message.data.iterations_needed,
=======
unf_block.proof_of_space.challenge_hash,
blocks_new[-1].proof_of_time.number_of_iterations,
>>>>>>> full node
uint8(2),
)
assert len([x async for x in full_node_1.new_proof_of_time(dont_have)]) == 1
[x async for x in full_node_1.respond_block(fnp.RespondBlock(blocks_new[-1]))]
msg = await full_node_1.new_proof_of_time(dont_have, None)
assert msg is not None
await full_node_1.respond_block(fnp.RespondBlock(blocks_new[-1]), None)
assert blocks_new[-1].proof_of_time is not None
already_have = fnp.NewProofOfTime(
unf_block.height,
@ -318,8 +331,10 @@ class TestFullNodeProtocol:
res[0].message.data.iterations_needed,
blocks_new[-1].proof_of_time.witness_type,
)
assert len([x async for x in full_node_1.new_proof_of_time(already_have)]) == 0
msg = await full_node_1.new_proof_of_time(already_have, None)
assert msg is None
"""
@pytest.mark.asyncio
async def test_request_pot(self, two_nodes, wallet_blocks):
full_node_1, full_node_2, server_1, server_2 = two_nodes