Skip to content

Commit

Permalink
#86dthh08k - Add currentHash method to the native LedgerContract
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirella de Medeiros committed May 24, 2024
1 parent 49731b6 commit 7994610
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
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)

0 comments on commit 7994610

Please sign in to comment.