-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into COM-253-efficient-receipts-transfer
# Conflicts: # compute_horde/pyproject.toml # compute_horde/tests/settings.py
- Loading branch information
Showing
17 changed files
with
613 additions
and
1,527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ComputeHordeBlockchainConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "compute_horde.blockchain" | ||
verbose_name = "Blockchain" | ||
|
||
def ready(self): | ||
from django.conf import settings | ||
|
||
# Check if a required setting is defined | ||
if not hasattr(settings, "BITTENSOR_NETWORK"): | ||
raise ValueError( | ||
"The setting 'BITTENSOR_NETWORK' is required in your settings.py " | ||
"for 'compute_horde.blockchain' to function properly. Please define it." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import asyncio | ||
import functools | ||
|
||
import bittensor | ||
from asgiref.sync import async_to_sync | ||
from django.conf import settings | ||
from django.core.cache import cache | ||
|
||
_BLOCK_CACHE_KEY = getattr( | ||
settings, "COMPUTE_HORDE_BLOCK_CACHE_KEY", "compute_horde.blockchain.block_cache.current_block" | ||
) | ||
_BLOCK_CACHE_TIMEOUT = getattr(settings, "COMPUTE_HORDE_BLOCK_CACHE_TIMEOUT", 2) | ||
|
||
|
||
class BlockNotInCacheError(KeyError): | ||
pass | ||
|
||
|
||
@functools.cache | ||
def _get_subtensor(network): | ||
return bittensor.subtensor(network) | ||
|
||
|
||
async def aget_current_block(timeout: float = 1.0) -> int: | ||
""" | ||
Gets the current block number from cache. Waits for ``timeout`` seconds if it's not there. | ||
:param timeout: Number of seconds to wait for the block. Pass 0 to return or raise an exception immediately instead. | ||
:raise BlockNotInCacheError: If block was not found in cache. | ||
""" | ||
# Note: cache.get here is not async. | ||
current_block: int = cache.get(_BLOCK_CACHE_KEY) | ||
if current_block is not None: | ||
return current_block | ||
|
||
if timeout <= 0: | ||
raise BlockNotInCacheError(_BLOCK_CACHE_KEY) | ||
|
||
await asyncio.sleep(timeout) | ||
|
||
return await aget_current_block(timeout=0) | ||
|
||
|
||
get_current_block = async_to_sync(aget_current_block) | ||
|
||
|
||
def cache_current_block() -> None: | ||
subtensor = _get_subtensor(network=settings.BITTENSOR_NETWORK) | ||
current_block = subtensor.get_current_block() | ||
|
||
cache.set(_BLOCK_CACHE_KEY, current_block, _BLOCK_CACHE_TIMEOUT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import logging | ||
|
||
from celery import shared_task | ||
|
||
from .block_cache import cache_current_block | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@shared_task() | ||
def update_block_cache(): | ||
logger.info("Updating block cache") | ||
cache_current_block() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
INSTALLED_APPS = ["compute_horde.receipts"] | ||
INSTALLED_APPS = [ | ||
"compute_horde.blockchain", | ||
"compute_horde.receipts", | ||
] | ||
|
||
BITTENSOR_NETWORK = "local" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.