Repository: 0xSpaceShard/starknet-devnet (language: python)

Issue:
Improvement : add endpoint to return fork status
It could be useful to be able to request to devnet the fork info.
For example, with a such command :
`curl -X GET http://127.0.0.1:5050/fork-status -H "Content-Type:application/json"`
and have in return some infos :
- forked or not.
- source network of the fork (alpha-goerli, alpha-goerli2, alpha-mainnet, or URL).


Discussion / hints:
Hi, can I work on this issue?

@ptisserand Yes you can!

 Although I believe first we would need to decide on the API. Here's my suggestion, let me know what you think:
- Introduce a new endpoint `/fork_status` (underscore to be in sync with other endpoints and the original gateway interface)
- If forking, it could return an object with (at least) two keys: forking url and forking block
- If not forking, it could return an empty object

You would need to create at least two tests (asserting response when forking and not)

@ivpavici Do we have to do anything onlydust-wise?
@FabijanC I agree with your suggestion
For returned JSON object, is `{"url": "https://.....", "block": 437771}`  ok for you ?
Yeah, looks good!
@FabijanC we did not add any new tasks on OnlyDust, but we could I guess
For endpoint documentation, should I add it in `page/docs/guide/fork.md` ?
@ptisserand yes. You can also create a PR (or a PR draft) so we can discuss there

Fix patch:
diff --git a/page/docs/guide/fork.md b/page/docs/guide/fork.md
index 757023169..b87886a99 100644
--- a/page/docs/guide/fork.md
+++ b/page/docs/guide/fork.md
@@ -17,3 +17,21 @@ The `--fork-block` parameter is optional and its value should be the block numbe
 All calls will first try Devnet's state and then fall back to the forking block.
 
 If you are forking another Devnet instance, keep in mind that it doesn't support polling specific blocks, but will always fall back to the currently latest block.
+
+## Get fork status
+```
+GET /fork_status
+```
+
+Response when in fork mode:
+```
+{
+    "url": "https://alpha4.starknet.io",
+    "block": 438839
+}
+```
+
+Response when not in fork mode:
+```
+{}
+```
diff --git a/starknet_devnet/blueprints/base.py b/starknet_devnet/blueprints/base.py
index 57794ef12..a61097fd3 100644
--- a/starknet_devnet/blueprints/base.py
+++ b/starknet_devnet/blueprints/base.py
@@ -189,3 +189,12 @@ async def create_block():
     """Create empty block"""
     block = await state.starknet_wrapper.create_empty_block()
     return Response(block.dumps(), status=200, mimetype="application/json")
+
+
+@base.route("/fork_status", methods=["GET"])
+async def fork_status():
+    """Get fork status"""
+    config = state.starknet_wrapper.config
+    if config.fork_network:
+        return jsonify({"url": config.fork_network.url, "block": config.fork_block})
+    return jsonify({})


Test patch:
diff --git a/test/test_fork.py b/test/test_fork.py
index c6ac00153..5a569474c 100644
--- a/test/test_fork.py
+++ b/test/test_fork.py
@@ -4,6 +4,7 @@
 """
 
 import pytest
+import requests
 
 from starknet_devnet.constants import DEFAULT_INITIAL_BALANCE
 
@@ -11,6 +12,7 @@
 from .settings import APP_URL, HOST, bind_free_port
 from .shared import (
     ABI_PATH,
+    ALPHA_GOERLI2_URL,
     ALPHA_MAINNET_URL,
     CONTRACT_PATH,
     PREDEPLOY_ACCOUNT_CLI_ARGS,
@@ -300,3 +302,34 @@ def test_deploy_account():
 def test_deploy_with_udc():
     """Test that deploying with udc works when forking"""
     deploy_with_udc_test_body()
+
+
+@devnet_in_background(*TESTNET_FORK_PARAMS)
+def test_fork_status_forked():
+    """Test GET on /fork_status when forking"""
+    resp = requests.get(f"{APP_URL}/fork_status")
+    assert resp.status_code == 200
+    data = resp.json()
+    assert data.get("url") == ALPHA_GOERLI2_URL
+    assert data.get("block") is not None
+
+
+@devnet_in_background(
+    *TESTNET_FORK_PARAMS, "--fork-block", str(TESTNET_DEPLOYMENT_BLOCK)
+)
+def test_fork_status_forked_at_block():
+    """Test GET on /fork_status when forking a given block"""
+    resp = requests.get(f"{APP_URL}/fork_status")
+    assert resp.status_code == 200
+    data = resp.json()
+    assert data.get("url") == ALPHA_GOERLI2_URL
+    assert data.get("block") == TESTNET_DEPLOYMENT_BLOCK
+
+
+@devnet_in_background()
+def test_fork_status_not_forked():
+    """Test GET on /fork_status when not forking"""
+    resp = requests.get(f"{APP_URL}/fork_status")
+    assert resp.status_code == 200
+    data = resp.json()
+    assert data == {}