Skip to content

Commit

Permalink
feat: raise JSONRPCError when VITE_IS_HOSTED is True for blocked endp…
Browse files Browse the repository at this point in the history
…oints (#826)

* feat: raise JSONRPCError when VITE_IS_HOSTED is True for blocked endpoints

* fix: typos

* refactor: change wrapper name
  • Loading branch information
kstroobants authored Jan 18, 2025
1 parent b9a9226 commit aabe5ea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion backend/database_handler/transactions_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ def set_transaction_appeal(self, transaction_hash: str, appeal: bool):
transaction = (
self.session.query(Transactions).filter_by(hash=transaction_hash).one()
)
transaction.appealed = appeal
if (transaction.status == TransactionStatus.ACCEPTED.value) or (
transaction.status == TransactionStatus.UNDETERMINED.value
):
transaction.appealed = appeal

def set_transaction_timestamp_accepted(
self, transaction_hash: str, timestamp_accepted: int = None
Expand Down
30 changes: 29 additions & 1 deletion backend/protocol_rpc/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import random
import json
import eth_utils
from functools import partial
from functools import partial, wraps
from typing import Any
from flask_jsonrpc import JSONRPC
from flask_jsonrpc.exceptions import JSONRPCError
Expand Down Expand Up @@ -52,6 +52,22 @@
from flask import request
from flask_jsonrpc.exceptions import JSONRPCError
import base64
import os


####### WRAPPER TO BLOCK ENDPOINTS FOR HOSTED ENVIRONMENT #######
def check_forbidden_method_in_hosted_studio(func):
@wraps(func)
def wrapper(*args, **kwargs):
if os.getenv("VITE_IS_HOSTED") == "true":
raise JSONRPCError(
code=-32000,
message="Non-allowed operation",
data={},
)
return func(*args, **kwargs)

return wrapper


####### HELPER ENDPOINTS #######
Expand All @@ -60,6 +76,7 @@ def ping() -> str:


####### SIMULATOR ENDPOINTS #######
@check_forbidden_method_in_hosted_studio
def clear_db_tables(session: Session, tables: list) -> None:
for table_name in tables:
table = Table(
Expand All @@ -84,6 +101,7 @@ def fund_account(
return transaction_hash


@check_forbidden_method_in_hosted_studio
def reset_defaults_llm_providers(llm_provider_registry: LLMProviderRegistry) -> None:
llm_provider_registry.reset_defaults()

Expand All @@ -94,6 +112,7 @@ async def get_providers_and_models(
return await llm_provider_registry.get_all_dict()


@check_forbidden_method_in_hosted_studio
def add_provider(llm_provider_registry: LLMProviderRegistry, params: dict) -> int:
provider = LLMProvider(
provider=params["provider"],
Expand All @@ -108,6 +127,7 @@ def add_provider(llm_provider_registry: LLMProviderRegistry, params: dict) -> in
return llm_provider_registry.add(provider)


@check_forbidden_method_in_hosted_studio
def update_provider(
llm_provider_registry: LLMProviderRegistry, id: int, params: dict
) -> None:
Expand All @@ -123,10 +143,12 @@ def update_provider(
llm_provider_registry.update(id, provider)


@check_forbidden_method_in_hosted_studio
def delete_provider(llm_provider_registry: LLMProviderRegistry, id: int) -> None:
llm_provider_registry.delete(id)


@check_forbidden_method_in_hosted_studio
def create_validator(
validators_registry: ValidatorsRegistry,
accounts_manager: AccountsManager,
Expand Down Expand Up @@ -162,6 +184,7 @@ def create_validator(
)


@check_forbidden_method_in_hosted_studio
async def create_random_validator(
validators_registry: ValidatorsRegistry,
accounts_manager: AccountsManager,
Expand All @@ -180,6 +203,7 @@ async def create_random_validator(
)[0]


@check_forbidden_method_in_hosted_studio
async def create_random_validators(
validators_registry: ValidatorsRegistry,
accounts_manager: AccountsManager,
Expand Down Expand Up @@ -214,6 +238,7 @@ async def create_random_validators(
return response


@check_forbidden_method_in_hosted_studio
def update_validator(
validators_registry: ValidatorsRegistry,
accounts_manager: AccountsManager,
Expand Down Expand Up @@ -254,6 +279,7 @@ def update_validator(
return validators_registry.update_validator(validator)


@check_forbidden_method_in_hosted_studio
def delete_validator(
validators_registry: ValidatorsRegistry,
accounts_manager: AccountsManager,
Expand All @@ -267,6 +293,7 @@ def delete_validator(
return validator_address


@check_forbidden_method_in_hosted_studio
def delete_all_validators(
validators_registry: ValidatorsRegistry,
) -> list:
Expand Down Expand Up @@ -530,6 +557,7 @@ def set_transaction_appeal(
transactions_processor.set_transaction_appeal(transaction_hash, True)


@check_forbidden_method_in_hosted_studio
def set_finality_window_time(consensus: ConsensusAlgorithm, time: int) -> None:
consensus.set_finality_window_time(time)

Expand Down

0 comments on commit aabe5ea

Please sign in to comment.