Skip to content

Commit

Permalink
Merge pull request #1244 from CityOfZion/CU-86dt6db2x
Browse files Browse the repository at this point in the history
#86dt6db2x - Improve new get methods to avoid returning misleading sentinel values
  • Loading branch information
luc10921 authored May 2, 2024
2 parents 1ebbe22 + 441cd41 commit c71affa
Show file tree
Hide file tree
Showing 20 changed files with 744 additions and 22 deletions.
173 changes: 167 additions & 6 deletions boa3/builtin/interop/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
'get_uint160',
'get_uint256',
'get_ecpoint',
'try_get',
'try_get_int',
'try_get_bool',
'try_get_str',
'try_get_uint160',
'try_get_uint256',
'try_get_ecpoint',
'get_context',
'get_read_only_context',
'put',
Expand Down Expand Up @@ -55,7 +62,7 @@ def get(key: bytes, context: StorageContext = get_context()) -> bytes:
b'test'
>>> get(b'fake_key')
''
b''
:param key: value identifier in the store
:type key: bytes
Expand All @@ -67,6 +74,28 @@ def get(key: bytes, context: StorageContext = get_context()) -> bytes:
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def try_get(key: bytes, context: StorageContext = get_context()) -> tuple[bytes, bool]:
"""
Gets a value from the persistent store based on the given key and returns whether the value is stored.
>>> put(b'unit', 'test')
... try_get(b'unit')
(b'test', True)
>>> try_get(b'fake_key')
(b'', False)
:param key: value identifier in the store
:type key: bytes
:param context: storage context to be used
:type context: StorageContext
:return: the value corresponding to given key for current storage context and whether it was actually stored
:rtype: tuple[bytes, bool]
"""
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def get_int(key: bytes, context: StorageContext = get_context()) -> int:
"""
Expand All @@ -78,7 +107,7 @@ def get_int(key: bytes, context: StorageContext = get_context()) -> int:
5
>>> get_int(b'fake_key')
''
0
:param key: value identifier in the store
:type key: bytes
Expand All @@ -90,6 +119,28 @@ def get_int(key: bytes, context: StorageContext = get_context()) -> int:
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def try_get_int(key: bytes, context: StorageContext = get_context()) -> tuple[int, bool]:
"""
Gets a value as integer from the persistent store based on the given key and returns whether the value is stored.
>>> put_int(b'unit', 5)
... try_get_int(b'unit')
(5, True)
>>> try_get_int(b'fake_key')
(0, False)
:param key: value identifier in the store
:type key: bytes
:param context: storage context to be used
:type context: StorageContext
:return: the value corresponding to given key for current storage context and whether it was actually stored
:rtype: tuple[int, bool]
"""
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def get_bool(key: bytes, context: StorageContext = get_context()) -> bool:
"""
Expand All @@ -101,7 +152,7 @@ def get_bool(key: bytes, context: StorageContext = get_context()) -> bool:
True
>>> get_bool(b'fake_key')
''
False
:param key: value identifier in the store
:type key: bytes
Expand All @@ -113,6 +164,28 @@ def get_bool(key: bytes, context: StorageContext = get_context()) -> bool:
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def try_get_bool(key: bytes, context: StorageContext = get_context()) -> tuple[bool, bool]:
"""
Gets a value as boolean from the persistent store based on the given key and returns whether the value is stored.
>>> put_bool(b'unit', False)
... try_get_bool(b'unit')
(False, True)
>>> try_get_bool(b'fake_key')
(False, False)
:param key: value identifier in the store
:type key: bytes
:param context: storage context to be used
:type context: StorageContext
:return: the value corresponding to given key for current storage context and whether it was actually stored
:rtype: tuple[bool, bool]
"""
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def get_str(key: bytes, context: StorageContext = get_context()) -> str:
"""
Expand All @@ -136,6 +209,28 @@ def get_str(key: bytes, context: StorageContext = get_context()) -> str:
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def try_get_str(key: bytes, context: StorageContext = get_context()) -> tuple[str, bool]:
"""
Gets a value as string from the persistent store based on the given key and returns whether the value is stored.
>>> put_str(b'unit', 'test')
... try_get_str(b'unit')
('test', True)
>>> try_get_str(b'fake_key')
('', False)
:param key: value identifier in the store
:type key: bytes
:param context: storage context to be used
:type context: StorageContext
:return: the value corresponding to given key for current storage context and whether it was actually stored
:rtype: tuple[str, bool]
"""
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def get_uint160(key: bytes, context: StorageContext = get_context()) -> UInt160:
"""
Expand All @@ -147,7 +242,7 @@ def get_uint160(key: bytes, context: StorageContext = get_context()) -> UInt160:
UInt160(0x4a49484746454443424139383736353433323130)
>>> get_uint160(b'fake_key')
''
UInt160(0x0000000000000000000000000000000000000000)
:param key: value identifier in the store
:type key: bytes
Expand All @@ -159,6 +254,28 @@ def get_uint160(key: bytes, context: StorageContext = get_context()) -> UInt160:
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def try_get_uint160(key: bytes, context: StorageContext = get_context()) -> tuple[UInt160, bool]:
"""
Gets a value as UInt160 from the persistent store based on the given key and returns whether the value is stored.
>>> put_uint160(b'unit', UInt160(b'0123456789ABCDEFGHIJ'))
... try_get_uint160(b'unit')
(UInt160(0x4a49484746454443424139383736353433323130), True)
>>> get_uint160(b'fake_key')
(UInt160(0x0000000000000000000000000000000000000000), False)
:param key: value identifier in the store
:type key: bytes
:param context: storage context to be used
:type context: StorageContext
:return: the value corresponding to given key for current storage context and whether it was actually stored
:rtype: tuple[UInt160, bool]
"""
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def get_uint256(key: bytes, context: StorageContext = get_context()) -> UInt256:
"""
Expand All @@ -170,7 +287,7 @@ def get_uint256(key: bytes, context: StorageContext = get_context()) -> UInt256:
UInt256(0x565554535251504f4e4d4c4b4a49484746454443424139383736353433323130)
>>> get_uint160(b'fake_key')
''
UInt256(0x0000000000000000000000000000000000000000000000000000000000000000)
:param key: value identifier in the store
:type key: bytes
Expand All @@ -182,6 +299,28 @@ def get_uint256(key: bytes, context: StorageContext = get_context()) -> UInt256:
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def try_get_uint256(key: bytes, context: StorageContext = get_context()) -> tuple[UInt256, bool]:
"""
Gets a value as UInt256 from the persistent store based on the given key and returns whether the value is stored.
>>> put_uint256(b'unit', UInt256(b'0123456789ABCDEFGHIJKLMNOPQRSTUV'))
... get_uint256(b'unit')
(UInt256(0x565554535251504f4e4d4c4b4a49484746454443424139383736353433323130), True)
>>> get_uint160(b'fake_key')
(UInt256(0x0000000000000000000000000000000000000000000000000000000000000000), False)
:param key: value identifier in the store
:type key: bytes
:param context: storage context to be used
:type context: StorageContext
:return: the value corresponding to given key for current storage context and whether it was actually stored
:rtype: tuple[UInt256, bool]
"""
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def get_ecpoint(key: bytes, context: StorageContext = get_context()) -> ECPoint:
"""
Expand All @@ -193,7 +332,7 @@ def get_ecpoint(key: bytes, context: StorageContext = get_context()) -> ECPoint:
ECPoint(0x57565554535251504f4e4d4c4b4a49484746454443424139383736353433323130)
>>> get_ecpoint(b'fake_key')
''
ECPoint(0x000000000000000000000000000000000000000000000000000000000000000000)
:param key: value identifier in the store
:type key: bytes
Expand All @@ -205,6 +344,28 @@ def get_ecpoint(key: bytes, context: StorageContext = get_context()) -> ECPoint:
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def try_get_ecpoint(key: bytes, context: StorageContext = get_context()) -> tuple[ECPoint, bool]:
"""
Gets a value as ECPoint from the persistent store based on the given key and returns whether the value is stored.
>>> put_ecpoint(b'unit', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'))
... try_get_ecpoint(b'unit')
(ECPoint(0x57565554535251504f4e4d4c4b4a49484746454443424139383736353433323130), True)
>>> try_get_ecpoint(b'fake_key')
(ECPoint(0x000000000000000000000000000000000000000000000000000000000000000000), False)
:param key: value identifier in the store
:type key: bytes
:param context: storage context to be used
:type context: StorageContext
:return: the value corresponding to given key for current storage context and whether it was actually stored
:rtype: tuple[ECPoint, bool]
"""
pass


@deprecated(details='This module is deprecated. Use boa3.sc.storage instead')
def get_read_only_context() -> StorageContext:
"""
Expand Down
14 changes: 14 additions & 0 deletions boa3/internal/model/builtin/interop/interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ def interop_events(cls) -> list[Event]:
StorageGetUInt160 = StorageGetUInt160Method()
StorageGetUInt256 = StorageGetUInt256Method()
StorageGetECPoint = StorageGetECPointMethod()
StorageGetCheck = StorageTryGetBytesMethod()
StorageGetCheckInt = StorageTryGetIntMethod()
StorageGetCheckBool = StorageTryGetBoolMethod()
StorageGetCheckStr = StorageTryGetStrMethod()
StorageGetCheckUInt160 = StorageTryGetUInt160Method()
StorageGetCheckUInt256 = StorageTryGetUInt256Method()
StorageGetCheckECPoint = StorageTryGetECPointMethod()
StoragePut = StoragePutBytesMethod()
StoragePutInt = StoragePutIntMethod()
StoragePutBool = StoragePutBoolMethod()
Expand Down Expand Up @@ -453,6 +460,13 @@ def interop_events(cls) -> list[Event]:
StorageGetUInt160,
StorageGetUInt256,
StorageGetECPoint,
StorageGetCheck,
StorageGetCheckInt,
StorageGetCheckBool,
StorageGetCheckStr,
StorageGetCheckUInt160,
StorageGetCheckUInt256,
StorageGetCheckECPoint,
StorageGetContext,
StorageGetReadOnlyContext,
StoragePut,
Expand Down
10 changes: 9 additions & 1 deletion boa3/internal/model/builtin/interop/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@
'StoragePutIntMethod',
'StoragePutStrMethod',
'StoragePutUInt160Method',
'StoragePutUInt256Method'
'StoragePutUInt256Method',
'StorageTryGetBoolMethod',
'StorageTryGetBytesMethod',
'StorageTryGetECPointMethod',
'StorageTryGetIntMethod',
'StorageTryGetStrMethod',
'StorageTryGetUInt160Method',
'StorageTryGetUInt256Method',
]

from boa3.internal.model.builtin.interop.storage.findoptionstype import FindOptionsType
from boa3.internal.model.builtin.interop.storage.get import *
from boa3.internal.model.builtin.interop.storage.getcheck import *
from boa3.internal.model.builtin.interop.storage.put import *
from boa3.internal.model.builtin.interop.storage.storagecontext import StorageContextType
from boa3.internal.model.builtin.interop.storage.storagedeletemethod import StorageDeleteMethod
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from boa3.internal.model.builtin.interop.storage.getcheck.storagegetboolmethod import StorageTryGetBoolMethod
from boa3.internal.model.builtin.interop.storage.getcheck.storagegetbytesmethod import StorageTryGetBytesMethod
from boa3.internal.model.builtin.interop.storage.getcheck.storagegetecpointmethod import StorageTryGetECPointMethod
from boa3.internal.model.builtin.interop.storage.getcheck.storagegetintmethod import StorageTryGetIntMethod
from boa3.internal.model.builtin.interop.storage.getcheck.storagegetstrmethod import StorageTryGetStrMethod
from boa3.internal.model.builtin.interop.storage.getcheck.storagegetuint160method import StorageTryGetUInt160Method
from boa3.internal.model.builtin.interop.storage.getcheck.storagegetuint256method import StorageTryGetUInt256Method
Loading

0 comments on commit c71affa

Please sign in to comment.