Skip to content

Commit

Permalink
feat: 0xSpaceShard#347: add fork_status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ptisserand committed Nov 25, 2022
1 parent 8f172a1 commit 5d8d109
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
18 changes: 18 additions & 0 deletions page/docs/guide/fork.md
Expand Up @@ -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:
```
{}
```
9 changes: 9 additions & 0 deletions starknet_devnet/blueprints/base.py
Expand Up @@ -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 bool(config.fork_network):
return jsonify({"url": config.fork_network.url, "block": config.fork_block})
return jsonify({})
33 changes: 33 additions & 0 deletions test/test_fork.py
Expand Up @@ -4,13 +4,15 @@
"""

import pytest
import requests

from starknet_devnet.constants import DEFAULT_INITIAL_BALANCE

from .account import get_nonce, invoke
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,
Expand Down Expand Up @@ -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 len(data.keys()) == 0

0 comments on commit 5d8d109

Please sign in to comment.