Skip to content

Commit

Permalink
Renamed SubstrateNodeExtension
Browse files Browse the repository at this point in the history
Updated extensions docs
  • Loading branch information
arjanz committed Jun 7, 2023
1 parent da6ee57 commit 0a9ee73
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
13 changes: 6 additions & 7 deletions docs/extensions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ It allows for the integration of third-party search indices, which can be easily
other data sources that provide the same functionality, as long as they adhere to standardized naming conventions in
the extension registry.

## Search extensions
## Available extensions

At present, the only type of extension that has been implemented is the search extension. While other types of
extensions may be developed in the future, the current implementation provides a fallback option that uses only
existing Substrate RPC methods.
| Name | Maintained by | Code |
|------------------------------------------------|----------------------|-----------------------------------------------------------------------------------|
| [SubstrateNodeExtension](./substrate-node-extension) | Polkascan Foundation | [Github](https://github.com/polkascan/py-substrate-interface) |
| [PolkascanExtension](./polkascan-extension.md) | Polkascan Foundation | [Github](https://github.com/polkascan/py-substrate-interface-extension-polkascan) |

However, it is important to note that this fallback implementation is significantly
inefficient, and it is encouraged to utilize third-party search indices where possible for optimal search performance.

### Available extension calls
## Available extension calls

| | |
|-----------------|-------------------------------------------------------------------------------------------------------------------------|
Expand Down
6 changes: 3 additions & 3 deletions docs/extensions/polkascan-extension.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PolkascanSearchExtension
# PolkascanExtension

This extension enables indexes provided by [Polkascan Explorer API](https://github.com/polkascan/explorer#explorer-api-component).

Expand All @@ -13,11 +13,11 @@ pip install substrate-interface-polkascan

```python
from substrateinterface import SubstrateInterface
from substrateinterface_polkascan.extensions import PolkascanSearchExtension
from substrateinterface_polkascan.extensions import PolkascanExtension

substrate = SubstrateInterface(url="ws://127.0.0.1:9944")

substrate.register_extension(PolkascanSearchExtension(url='http://127.0.0.1:8000/graphql/'))
substrate.register_extension(PolkascanExtension(url='http://127.0.0.1:8000/graphql/'))
```

## Implemented extension calls
Expand Down
11 changes: 9 additions & 2 deletions docs/extensions/substrate-node-extension.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SubstrateNodeSearchExtension
# SubstrateNodeExtension

This extensions is meant as a fallback option that uses only existing Substrate RPC methods.
However, it is important to note that this fallback implementation is significantly inefficient, and it is encouraged to utilize third-party search indices where possible for optimal search performance.
Expand All @@ -8,7 +8,7 @@ However, it is important to note that this fallback implementation is significan
```python
substrate = SubstrateInterface(url="ws://127.0.0.1:9944")
# Provide maximum block range (bigger range descreases performance)
substrate.register_extension(SubstrateNodeSearchExtension(max_block_range=100))
substrate.register_extension(SubstrateNodeExtension(max_block_range=100))
```

## Implemented extension calls
Expand All @@ -34,3 +34,10 @@ block_datetime = datetime(2020, 7, 12, 0, 0, 0)

block_number = substrate.extensions.search_block_number(block_datetime=block_datetime)
```

### get_block_timestamp

```python
# Get timestamp for specific block number
block_timestamp = substrate.extensions.get_block_timestamp(block_number)
```
2 changes: 1 addition & 1 deletion docs/usage/extensions.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Extensions

[Moved to extensions section](/extensions/)
Moved to main section "Extensions"
4 changes: 2 additions & 2 deletions examples/extensions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from datetime import datetime, timedelta

from substrateinterface import SubstrateInterface
from substrateinterface.extensions import SubstrateNodeSearchExtension
from substrateinterface.extensions import SubstrateNodeExtension

import logging
logging.basicConfig(level=logging.DEBUG)

substrate = SubstrateInterface(url="wss://rpc.polkadot.io")

substrate.register_extension(SubstrateNodeSearchExtension(max_block_range=100))
substrate.register_extension(SubstrateNodeExtension(max_block_range=100))

# Search for block number corresponding a specific datetime
block_datetime = datetime(2022, 1, 1, 0, 0, 0)
Expand Down
9 changes: 7 additions & 2 deletions substrateinterface/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import math
from datetime import datetime, timedelta

__all__ = ['Extension', 'SearchExtension', 'SubstrateNodeSearchExtension']
__all__ = ['Extension', 'SearchExtension', 'SubstrateNodeExtension']

if TYPE_CHECKING:
from .base import SubstrateInterface
Expand Down Expand Up @@ -135,7 +135,7 @@ def get_block_timestamp(self, block_number: int) -> int:
raise NotImplementedError()


class SubstrateNodeSearchExtension(SearchExtension):
class SubstrateNodeExtension(SearchExtension):
"""
Implementation of `SearchExtension` using only Substrate RPC methods. Could be significant inefficient.
"""
Expand Down Expand Up @@ -268,3 +268,8 @@ def search_block_number(self, block_datetime: datetime, block_time: int = 6, **k
self.debug_message(f"Accepted prediction #{predicted_block_number}")

return predicted_block_number


# Backwards compatibility
class SubstrateNodeSearchExtension(SubstrateNodeExtension):
pass
8 changes: 6 additions & 2 deletions test/test_extension_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from substrateinterface import SubstrateInterface
from substrateinterface.exceptions import ExtensionCallNotFound
from substrateinterface.extensions import SubstrateNodeSearchExtension
from substrateinterface.extensions import SubstrateNodeExtension
from test import settings


Expand All @@ -31,7 +31,7 @@ def setUpClass(cls):
cls.substrate = SubstrateInterface(
url=settings.POLKADOT_NODE_URL
)
cls.substrate.register_extension(SubstrateNodeSearchExtension(max_block_range=100))
cls.substrate.register_extension(SubstrateNodeExtension(max_block_range=100))

def test_search_block_number(self):
block_datetime = datetime(2020, 7, 12, 0, 0, 0, tzinfo=timezone.utc)
Expand All @@ -41,6 +41,10 @@ def test_search_block_number(self):
self.assertGreaterEqual(block_number, 665270)
self.assertLessEqual(block_number, 665280)

def test_search_block_timestamp(self):
block_timestamp = self.substrate.extensions.get_block_timestamp(1000)
self.assertEqual(1590513426, block_timestamp)

def test_unsupported_extension_call(self):
with self.assertRaises(ExtensionCallNotFound):
self.substrate.extensions.unknown()
Expand Down

0 comments on commit 0a9ee73

Please sign in to comment.