Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hathor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
NCActionType,
NCArgs,
NCDepositAction,
NCFee,
NCGrantAuthorityAction,
NCParsedArgs,
NCRawArgs,
Expand Down
21 changes: 21 additions & 0 deletions hathor/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ class HathorSettings(NamedTuple):
# Fee rate settings
FEE_PER_OUTPUT: int = 1

@property
def FEE_DIVISOR(self) -> int:
"""Divisor used for evaluating fee amounts"""
result = 1 / self.TOKEN_DEPOSIT_PERCENTAGE
assert result.is_integer()
return int(result)

# To disable reward halving, just set this to `None` and make sure that INITIAL_TOKEN_UNITS_PER_BLOCK is equal to
# MINIMUM_TOKEN_UNITS_PER_BLOCK.
BLOCKS_PER_HALVING: Optional[int] = 2 * 60 * 24 * 365 # 1051200, every 365 days
Expand Down Expand Up @@ -582,6 +589,17 @@ def _validate_tokens(genesis_tokens: int, values: dict[str, Any]) -> int:
return genesis_tokens


def _validate_token_deposit_percentage(token_deposit_percentage: float) -> float:
"""Validate that TOKEN_DEPOSIT_PERCENTAGE results in an integer FEE_DIVISOR."""
result = 1 / token_deposit_percentage
if not result.is_integer():
raise ValueError(
f'TOKEN_DEPOSIT_PERCENTAGE must result in an integer FEE_DIVISOR. '
f'Got TOKEN_DEPOSIT_PERCENTAGE={token_deposit_percentage}, FEE_DIVISOR={result}'
)
return token_deposit_percentage


_VALIDATORS = dict(
_parse_hex_str=pydantic.validator(
'P2PKH_VERSION_BYTE',
Expand Down Expand Up @@ -619,4 +637,7 @@ def _validate_tokens(genesis_tokens: int, values: dict[str, Any]) -> int:
_validate_tokens=pydantic.validator(
'GENESIS_TOKENS'
)(_validate_tokens),
_validate_token_deposit_percentage=pydantic.validator(
'TOKEN_DEPOSIT_PERCENTAGE'
)(_validate_token_deposit_percentage),
)
1 change: 1 addition & 0 deletions hathor/nanocontracts/allowed_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
Context=hathor.Context,
NCFail=hathor.NCFail,
NCAction=hathor.NCAction,
NCFee=hathor.NCFee,
NCActionType=hathor.NCActionType,
SignedData=hathor.SignedData,
public=hathor.public,
Expand Down
26 changes: 22 additions & 4 deletions hathor/nanocontracts/blueprint_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from hathor.conf.settings import HATHOR_TOKEN_UID
from hathor.nanocontracts.storage import NCContractStorage
from hathor.nanocontracts.types import Amount, BlueprintId, ContractId, NCAction, TokenUid
from hathor.nanocontracts.types import Amount, BlueprintId, ContractId, NCAction, NCFee, TokenUid

if TYPE_CHECKING:
from hathor.nanocontracts.contract_accessor import ContractAccessor
Expand Down Expand Up @@ -172,6 +172,7 @@ def call_public_method(
nc_id: ContractId,
method_name: str,
actions: Sequence[NCAction],
fees: Sequence[NCFee] | None = None,
*args: Any,
**kwargs: Any,
) -> Any:
Expand All @@ -183,6 +184,7 @@ def call_public_method(
args,
kwargs,
forbid_fallback=False,
fees=fees or []
)

@final
Expand All @@ -191,22 +193,37 @@ def proxy_call_public_method(
blueprint_id: BlueprintId,
method_name: str,
actions: Sequence[NCAction],
fees: Sequence[NCFee],
*args: Any,
**kwargs: Any,
) -> Any:
"""Execute a proxy call to a public method of another blueprint."""
return self.__runner.syscall_proxy_call_public_method(blueprint_id, method_name, actions, args, kwargs)
return self.__runner.syscall_proxy_call_public_method(
blueprint_id=blueprint_id,
method_name=method_name,
actions=actions,
fees=fees,
args=args,
kwargs=kwargs
)

@final
def proxy_call_public_method_nc_args(
self,
blueprint_id: BlueprintId,
method_name: str,
actions: Sequence[NCAction],
fees: Sequence[NCFee],
nc_args: NCArgs,
) -> Any:
"""Execute a proxy call to a public method of another blueprint."""
return self.__runner.syscall_proxy_call_public_method_nc_args(blueprint_id, method_name, actions, nc_args)
return self.__runner.syscall_proxy_call_public_method_nc_args(
blueprint_id=blueprint_id,
method_name=method_name,
actions=actions,
fees=fees,
nc_args=nc_args
)

@final
def call_view_method(self, nc_id: ContractId, method_name: str, *args: Any, **kwargs: Any) -> Any:
Expand Down Expand Up @@ -246,11 +263,12 @@ def create_contract(
blueprint_id: BlueprintId,
salt: bytes,
actions: Sequence[NCAction],
fees: Sequence[NCFee] | None,
*args: Any,
**kwargs: Any,
) -> tuple[ContractId, Any]:
"""Create a new contract."""
return self.__runner.syscall_create_another_contract(blueprint_id, salt, actions, args, kwargs)
return self.__runner.syscall_create_another_contract(blueprint_id, salt, actions, args, kwargs, fees or [])

@final
def emit_event(self, data: bytes) -> None:
Expand Down
24 changes: 21 additions & 3 deletions hathor/nanocontracts/contract_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import TYPE_CHECKING, Any, Collection, Sequence, final

from hathor.nanocontracts.faux_immutable import FauxImmutable, __set_faux_immutable__
from hathor.nanocontracts.types import BlueprintId, ContractId, NCAction
from hathor.nanocontracts.types import BlueprintId, ContractId, NCAction, NCFee

if TYPE_CHECKING:
from hathor.nanocontracts import Runner
Expand Down Expand Up @@ -62,12 +62,13 @@ def view(self) -> Any:
blueprint_ids=self.__blueprint_ids,
)

def public(self, *actions: NCAction, forbid_fallback: bool = False) -> Any:
def public(self, *actions: NCAction, fees: Sequence[NCFee] | None = None, forbid_fallback: bool = False) -> Any:
return PreparedPublicCall(
runner=self.__runner,
contract_id=self.__contract_id,
blueprint_ids=self.__blueprint_ids,
actions=actions,
fees=fees,
forbid_fallback=forbid_fallback,
)

Expand Down Expand Up @@ -103,7 +104,15 @@ def __getattr__(self, method_name: str) -> ViewMethodAccessor:

@final
class PreparedPublicCall(FauxImmutable):
__slots__ = ('__runner', '__contract_id', '__blueprint_ids', '__actions', '__forbid_fallback', '__is_dirty')
__slots__ = (
'__runner',
'__contract_id',
'__blueprint_ids',
'__actions',
'__forbid_fallback',
'__is_dirty',
'__fees'
)
__skip_faux_immutability_validation__ = True # Needed to implement __getattr__

def __init__(
Expand All @@ -113,6 +122,7 @@ def __init__(
contract_id: ContractId,
blueprint_ids: frozenset[BlueprintId] | None,
actions: Sequence[NCAction],
fees: Sequence[NCFee] | None,
forbid_fallback: bool,
) -> None:
self.__runner: Runner
Expand All @@ -121,13 +131,15 @@ def __init__(
self.__actions: Sequence[NCAction]
self.__forbid_fallback: bool
self.__is_dirty: bool
self.__fees: Sequence[NCFee]

__set_faux_immutable__(self, '__runner', runner)
__set_faux_immutable__(self, '__contract_id', contract_id)
__set_faux_immutable__(self, '__blueprint_ids', blueprint_ids)
__set_faux_immutable__(self, '__actions', actions)
__set_faux_immutable__(self, '__forbid_fallback', forbid_fallback)
__set_faux_immutable__(self, '__is_dirty', False)
__set_faux_immutable__(self, '__fees', fees)

def __getattr__(self, method_name: str) -> PublicMethodAccessor:
from hathor.nanocontracts import NCFail
Expand All @@ -146,6 +158,7 @@ def __getattr__(self, method_name: str) -> PublicMethodAccessor:
method_name=method_name,
actions=self.__actions,
forbid_fallback=self.__forbid_fallback,
fees=self.__fees,
)


Expand Down Expand Up @@ -206,6 +219,7 @@ class PublicMethodAccessor(FauxImmutable):
'__actions',
'__forbid_fallback',
'__is_dirty',
'__fees'
)

def __init__(
Expand All @@ -217,6 +231,7 @@ def __init__(
method_name: str,
actions: Sequence[NCAction],
forbid_fallback: bool,
fees: Sequence[NCFee] | None,
) -> None:
self.__runner: Runner
self.__contract_id: ContractId
Expand All @@ -225,6 +240,7 @@ def __init__(
self.__actions: Sequence[NCAction]
self.__forbid_fallback: bool
self.__is_dirty: bool
self.__fees: Sequence[NCFee]

__set_faux_immutable__(self, '__runner', runner)
__set_faux_immutable__(self, '__contract_id', contract_id)
Expand All @@ -233,6 +249,7 @@ def __init__(
__set_faux_immutable__(self, '__actions', actions)
__set_faux_immutable__(self, '__forbid_fallback', forbid_fallback)
__set_faux_immutable__(self, '__is_dirty', False)
__set_faux_immutable__(self, '__fees', fees)

def __call__(self, *args: Any, **kwargs: Any) -> object:
from hathor.nanocontracts import NCFail
Expand All @@ -254,6 +271,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> object:
contract_id=self.__contract_id,
method_name=self.__method_name,
actions=self.__actions,
fees=self.__fees or [],
args=args,
kwargs=kwargs,
forbid_fallback=self.__forbid_fallback,
Expand Down
5 changes: 5 additions & 0 deletions hathor/nanocontracts/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ class NCInvalidAction(NCFail):
pass


class NCInvalidFee(NCFail):
"""Raised when a fee is invalid."""
pass


class NCInvalidSyscall(NCFail):
"""Raised when a syscall is invalid."""
pass
Expand Down
Loading