Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#86dthh08k - Add currentHash method to the native LedgerContract #1258

Merged
merged 1 commit into from
May 24, 2024
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
13 changes: 13 additions & 0 deletions boa3/builtin/nativecontract/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ def get_current_index(cls) -> int:
"""
pass

@classmethod
def get_current_hash(cls) -> UInt256:
"""
Gets the hash of the current block.

>>> Ledger.get_current_hash()
b'\\x3e\\x65\\xe5\\x4d\\x75\\x5a\\x94\\x90\\xd6\\x98\\x3a\\x77\\xe4\\x82\\xaf\\x7a\\x38\\xc9\\x8c\\x1a\\xc6\\xd9\\xda\\x48\\xbd\\x7c\\x22\\xb3\\x2a\\x9e\\x34\\xea'

:return: the hash of the current block
:rtype: UInt256
"""
pass

@classmethod
def get_transaction(cls, hash_: UInt256) -> Transaction | None:
"""
Expand Down
1 change: 1 addition & 0 deletions boa3/internal/model/builtin/native/ledgerclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def class_methods(self) -> dict[str, Method]:
self._class_methods = {
'get_block': Interop.GetBlock,
'get_current_index': Interop.CurrentIndex.getter,
'get_current_hash': Interop.CurrentHash.getter,
'get_transaction': Interop.GetTransaction,
'get_transaction_from_block': Interop.GetTransactionFromBlock,
'get_transaction_height': Interop.GetTransactionHeight,
Expand Down
13 changes: 13 additions & 0 deletions boa3/sc/contracts/ledgercontract.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ def get_current_index(cls) -> int:
"""
pass

@classmethod
def get_current_hash(cls) -> UInt256:
"""
Gets the hash of the current block.

>>> LedgerContract.get_current_hash()
b'\\x3e\\x65\\xe5\\x4d\\x75\\x5a\\x94\\x90\\xd6\\x98\\x3a\\x77\\xe4\\x82\\xaf\\x7a\\x38\\xc9\\x8c\\x1a\\xc6\\xd9\\xda\\x48\\xbd\\x7c\\x22\\xb3\\x2a\\x9e\\x34\\xea'

:return: the hash of the current block
:rtype: UInt256
"""
pass

@classmethod
def get_transaction(cls, hash_: UInt256) -> Transaction | None:
"""
Expand Down
8 changes: 8 additions & 0 deletions boa3_test/test_sc/native_test/ledger/GetCurrentHash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from boa3.sc.compiletime import public
from boa3.sc.contracts import LedgerContract
from boa3.sc.types import UInt256


@public
def main() -> UInt256:
return LedgerContract.get_current_hash()
8 changes: 8 additions & 0 deletions boa3_test/tests/compiler_tests/test_native/test_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,11 @@ async def test_get_current_index(self):
block_ = await self.get_last_block(self.called_tx)
expected = block_.index
self.assertEqual(expected, result)

async def test_get_current_hash(self):
await self.set_up_contract('GetCurrentHash.py')

result, _ = await self.call('main', [], return_type=types.UInt256, signing_accounts=[self.genesis])
block_ = await self.get_last_block(self.called_tx)
expected = block_.hash()
self.assertEqual(expected, result)
Loading