From 3480cd56c894acc6ee0e15c55091686839b28409 Mon Sep 17 00:00:00 2001 From: Alex Ruzenhack Date: Mon, 3 Apr 2023 16:46:45 +0100 Subject: [PATCH 1/6] feat: add best_block and best_block_tips to dag on status --- hathor/p2p/resources/status.py | 26 +++++++++++++++++++++++++- tests/resources/p2p/test_status.py | 17 +++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/hathor/p2p/resources/status.py b/hathor/p2p/resources/status.py index bcd72826f..dbf442920 100644 --- a/hathor/p2p/resources/status.py +++ b/hathor/p2p/resources/status.py @@ -81,6 +81,15 @@ def render_GET(self, request): }) app = 'Hathor v{}'.format(hathor.__version__) + + best_block_tips = [] + for tip in self.manager.tx_storage.get_best_block_tips(): + tx = self.manager.tx_storage.get_transaction(tip) + meta = tx.get_metadata() + best_block_tips.append({'hash': tx.hash_hex, 'height': meta.height}) + + best_block = self.manager.tx_storage.get_best_block() + data = { 'server': { 'id': self.manager.connections.my_peer.id, @@ -100,6 +109,11 @@ def render_GET(self, request): 'dag': { 'first_timestamp': self.manager.tx_storage.first_timestamp, 'latest_timestamp': self.manager.tx_storage.latest_timestamp, + 'best_block_tips': best_block_tips, + 'best_block': { + 'hash': best_block.hash_hex, + 'height': best_block.get_metadata().height, + }, } } return json_dumpb(data) @@ -170,7 +184,17 @@ def render_GET(self, request): }, 'dag': { 'first_timestamp': 1539271481, - 'latest_timestamp': 1539271483 + 'latest_timestamp': 1539271483, + 'best_block_tips': [ + { + 'hash': '000006c', + 'height': 0 + } + ], + 'best_block': { + 'hash': '000006c', + 'height': 0 + } } } } diff --git a/tests/resources/p2p/test_status.py b/tests/resources/p2p/test_status.py index 6e1e215fc..ca0671e35 100644 --- a/tests/resources/p2p/test_status.py +++ b/tests/resources/p2p/test_status.py @@ -22,11 +22,28 @@ def setUp(self): def test_get(self): response = yield self.web.get("status") data = response.json_value() + server_data = data.get('server') self.assertEqual(server_data['app_version'], 'Hathor v{}'.format(hathor.__version__)) self.assertEqual(server_data['network'], 'testnet') self.assertGreater(server_data['uptime'], 0) + dag_data = data.get('dag') + # We have the genesis block + self.assertEqual(len(dag_data['best_block_tips']), 1) + self.assertIsNotNone(dag_data['best_block_tips'][0]) + # As we don't have a type, we most check if the keys are there, + # and the types are correct + self.assertIn('hash', dag_data['best_block_tips'][0]) + self.assertIn('height', dag_data['best_block_tips'][0]) + self.assertIsInstance(dag_data['best_block_tips'][0]['hash'], str) + self.assertIsInstance(dag_data['best_block_tips'][0]['height'], int) + self.assertIsNotNone(dag_data['best_block']) + self.assertIn('hash', dag_data['best_block']) + self.assertIn('height', dag_data['best_block']) + self.assertIsInstance(dag_data['best_block']['hash'], str) + self.assertIsInstance(dag_data['best_block']['height'], int) + @inlineCallbacks def test_handshaking(self): response = yield self.web.get("status") From 58ce859143ce1b2bbca66d334151f501c7c612fa Mon Sep 17 00:00:00 2001 From: Alex Ruzenhack Date: Tue, 4 Apr 2023 01:12:23 +0100 Subject: [PATCH 2/6] chore: any change to force CI checks --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6ef4ad85f..bb247890d 100644 --- a/README.md +++ b/README.md @@ -199,3 +199,5 @@ redoc-cli bundle hathor/cli/openapi_files/openapi.json --output index.html [open-issue]: https://github.com/HathorNetwork/hathor-core/issues/new [create-pr]: https://github.com/HathorNetwork/hathor-core/compare + +. \ No newline at end of file From 13fa0e5db8179d298ef527fa6e59af83ea28c020 Mon Sep 17 00:00:00 2001 From: Alex Ruzenhack Date: Tue, 4 Apr 2023 02:03:15 +0100 Subject: [PATCH 3/6] chore: fix typo and improve test --- README.md | 2 -- tests/resources/p2p/test_status.py | 12 +++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bb247890d..6ef4ad85f 100644 --- a/README.md +++ b/README.md @@ -199,5 +199,3 @@ redoc-cli bundle hathor/cli/openapi_files/openapi.json --output index.html [open-issue]: https://github.com/HathorNetwork/hathor-core/issues/new [create-pr]: https://github.com/HathorNetwork/hathor-core/compare - -. \ No newline at end of file diff --git a/tests/resources/p2p/test_status.py b/tests/resources/p2p/test_status.py index ca0671e35..595260e98 100644 --- a/tests/resources/p2p/test_status.py +++ b/tests/resources/p2p/test_status.py @@ -32,17 +32,27 @@ def test_get(self): # We have the genesis block self.assertEqual(len(dag_data['best_block_tips']), 1) self.assertIsNotNone(dag_data['best_block_tips'][0]) - # As we don't have a type, we most check if the keys are there, + # As we don't have a type, we must check if the keys are there, # and the types are correct self.assertIn('hash', dag_data['best_block_tips'][0]) self.assertIn('height', dag_data['best_block_tips'][0]) self.assertIsInstance(dag_data['best_block_tips'][0]['hash'], str) self.assertIsInstance(dag_data['best_block_tips'][0]['height'], int) + self.assertEqual( + dag_data['best_block_tips'][0]['hash'], + '339f47da87435842b0b1b528ecd9eac2495ce983b3e9c923a37e1befbe12c792' + ) + self.assertEqual(dag_data['best_block_tips'][0]['height'], 0) self.assertIsNotNone(dag_data['best_block']) self.assertIn('hash', dag_data['best_block']) self.assertIn('height', dag_data['best_block']) self.assertIsInstance(dag_data['best_block']['hash'], str) self.assertIsInstance(dag_data['best_block']['height'], int) + self.assertEqual( + dag_data['best_block']['hash'], + '339f47da87435842b0b1b528ecd9eac2495ce983b3e9c923a37e1befbe12c792' + ) + self.assertEqual(dag_data['best_block']['height'], 0) @inlineCallbacks def test_handshaking(self): From 597bee750efcab78058b2675f290ccbcb2c52dc2 Mon Sep 17 00:00:00 2001 From: Alex Ruzenhack Date: Tue, 4 Apr 2023 02:17:38 +0100 Subject: [PATCH 4/6] chore: change hard coded hash to its SETTINGS reference --- tests/resources/p2p/test_status.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/resources/p2p/test_status.py b/tests/resources/p2p/test_status.py index 595260e98..1466911cb 100644 --- a/tests/resources/p2p/test_status.py +++ b/tests/resources/p2p/test_status.py @@ -6,6 +6,7 @@ from hathor.simulator import FakeConnection from tests import unittest from tests.resources.base_resource import StubSite, _BaseResourceTest +from hathor.conf.unittests import SETTINGS class BaseStatusTest(_BaseResourceTest._ResourceTest): @@ -38,20 +39,14 @@ def test_get(self): self.assertIn('height', dag_data['best_block_tips'][0]) self.assertIsInstance(dag_data['best_block_tips'][0]['hash'], str) self.assertIsInstance(dag_data['best_block_tips'][0]['height'], int) - self.assertEqual( - dag_data['best_block_tips'][0]['hash'], - '339f47da87435842b0b1b528ecd9eac2495ce983b3e9c923a37e1befbe12c792' - ) + self.assertEqual(dag_data['best_block_tips'][0]['hash'], SETTINGS.GENESIS_BLOCK_HASH.hex()) self.assertEqual(dag_data['best_block_tips'][0]['height'], 0) self.assertIsNotNone(dag_data['best_block']) self.assertIn('hash', dag_data['best_block']) self.assertIn('height', dag_data['best_block']) self.assertIsInstance(dag_data['best_block']['hash'], str) self.assertIsInstance(dag_data['best_block']['height'], int) - self.assertEqual( - dag_data['best_block']['hash'], - '339f47da87435842b0b1b528ecd9eac2495ce983b3e9c923a37e1befbe12c792' - ) + self.assertEqual(dag_data['best_block']['hash'], SETTINGS.GENESIS_BLOCK_HASH.hex()) self.assertEqual(dag_data['best_block']['height'], 0) @inlineCallbacks From 24dc93462d692275df5af23746839bc029ed450e Mon Sep 17 00:00:00 2001 From: Alex Ruzenhack Date: Tue, 4 Apr 2023 02:31:53 +0100 Subject: [PATCH 5/6] chore: resolve import order --- tests/resources/p2p/test_status.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/resources/p2p/test_status.py b/tests/resources/p2p/test_status.py index 1466911cb..ea80ece6e 100644 --- a/tests/resources/p2p/test_status.py +++ b/tests/resources/p2p/test_status.py @@ -2,11 +2,11 @@ from twisted.internet.defer import inlineCallbacks import hathor +from hathor.conf.unittests import SETTINGS from hathor.p2p.resources import StatusResource from hathor.simulator import FakeConnection from tests import unittest from tests.resources.base_resource import StubSite, _BaseResourceTest -from hathor.conf.unittests import SETTINGS class BaseStatusTest(_BaseResourceTest._ResourceTest): From fc94a87f00db8fc15384ea407db3b1dd384d70dc Mon Sep 17 00:00:00 2001 From: Alex Ruzenhack Date: Tue, 4 Apr 2023 05:12:02 +0100 Subject: [PATCH 6/6] chore: add hex hash for the genesis block --- hathor/p2p/resources/status.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hathor/p2p/resources/status.py b/hathor/p2p/resources/status.py index dbf442920..fdefb58a5 100644 --- a/hathor/p2p/resources/status.py +++ b/hathor/p2p/resources/status.py @@ -187,12 +187,12 @@ def render_GET(self, request): 'latest_timestamp': 1539271483, 'best_block_tips': [ { - 'hash': '000006c', + 'hash': '000007eb968a6cdf0499e2d033faf1e163e0dc9cf41876acad4d421836972038', # noqa: E501 'height': 0 } ], 'best_block': { - 'hash': '000006c', + 'hash': '000007eb968a6cdf0499e2d033faf1e163e0dc9cf41876acad4d421836972038', # noqa: E501 'height': 0 } }