From 7994610e53360bf4fef84c1c851d788e16e6271d Mon Sep 17 00:00:00 2001 From: Mirella de Medeiros Date: Fri, 24 May 2024 12:22:58 -0300 Subject: [PATCH] #86dthh08k - Add currentHash method to the native LedgerContract --- boa3/builtin/nativecontract/ledger.py | 13 +++++++++++++ boa3/internal/model/builtin/native/ledgerclass.py | 1 + boa3/sc/contracts/ledgercontract.py | 13 +++++++++++++ .../test_sc/native_test/ledger/GetCurrentHash.py | 8 ++++++++ .../tests/compiler_tests/test_native/test_ledger.py | 8 ++++++++ 5 files changed, 43 insertions(+) create mode 100644 boa3_test/test_sc/native_test/ledger/GetCurrentHash.py diff --git a/boa3/builtin/nativecontract/ledger.py b/boa3/builtin/nativecontract/ledger.py index 7c93711b..1d2bd937 100644 --- a/boa3/builtin/nativecontract/ledger.py +++ b/boa3/builtin/nativecontract/ledger.py @@ -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: """ diff --git a/boa3/internal/model/builtin/native/ledgerclass.py b/boa3/internal/model/builtin/native/ledgerclass.py index 0817534c..ead808c4 100644 --- a/boa3/internal/model/builtin/native/ledgerclass.py +++ b/boa3/internal/model/builtin/native/ledgerclass.py @@ -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, diff --git a/boa3/sc/contracts/ledgercontract.py b/boa3/sc/contracts/ledgercontract.py index 87a06843..53f8318f 100644 --- a/boa3/sc/contracts/ledgercontract.py +++ b/boa3/sc/contracts/ledgercontract.py @@ -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: """ diff --git a/boa3_test/test_sc/native_test/ledger/GetCurrentHash.py b/boa3_test/test_sc/native_test/ledger/GetCurrentHash.py new file mode 100644 index 00000000..b63a44a5 --- /dev/null +++ b/boa3_test/test_sc/native_test/ledger/GetCurrentHash.py @@ -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() diff --git a/boa3_test/tests/compiler_tests/test_native/test_ledger.py b/boa3_test/tests/compiler_tests/test_native/test_ledger.py index e2186675..d37648e8 100644 --- a/boa3_test/tests/compiler_tests/test_native/test_ledger.py +++ b/boa3_test/tests/compiler_tests/test_native/test_ledger.py @@ -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)