Merge bitcoin/bitcoin#25878: tests: Use mocktime for wallet encryption timeout

02dea9a47f tests: Use mocktime for wallet encryption timeout (Andrew Chow)

Pull request description:

  The intermittent wallet_encryption.py failures are related to differences in time between python and std::chrono. We can avoid this entirely by using mocktime. This also allows us to test for the exact unlocking time rather than that it is greater than expected.

  Fixes #25482

ACKs for top commit:
  MarcoFalke:
    review ACK 02dea9a47f
  vasild:
    ACK 02dea9a47f

Tree-SHA512: 5a5489f5cd2569c824bf5b3d839be0c632ed27627c0eff65dda63c143a8d1174fe3252acba8102b4242a9ddf42d82bfe79babad68f1beeb83eb251386058e039
This commit is contained in:
MacroFake 2022-08-20 08:50:16 +02:00
commit c73c8d53fe
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

@ -9,8 +9,7 @@ import time
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_raises_rpc_error,
assert_greater_than,
assert_greater_than_or_equal,
assert_equal,
)
@ -76,21 +75,18 @@ class WalletEncryptionTest(BitcoinTestFramework):
self.log.info('Check a timeout less than the limit')
MAX_VALUE = 100000000
expected_time = int(time.time()) + MAX_VALUE - 600
now = int(time.time())
self.nodes[0].setmocktime(now)
expected_time = now + MAX_VALUE - 600
self.nodes[0].walletpassphrase(passphrase2, MAX_VALUE - 600)
# give buffer for walletpassphrase, since it iterates over all encrypted keys
expected_time_with_buffer = time.time() + MAX_VALUE - 600
actual_time = self.nodes[0].getwalletinfo()['unlocked_until']
assert_greater_than_or_equal(actual_time, expected_time)
assert_greater_than(expected_time_with_buffer, actual_time)
assert_equal(actual_time, expected_time)
self.log.info('Check a timeout greater than the limit')
expected_time = int(time.time()) + MAX_VALUE - 1
expected_time = now + MAX_VALUE
self.nodes[0].walletpassphrase(passphrase2, MAX_VALUE + 1000)
expected_time_with_buffer = time.time() + MAX_VALUE
actual_time = self.nodes[0].getwalletinfo()['unlocked_until']
assert_greater_than_or_equal(actual_time, expected_time)
assert_greater_than(expected_time_with_buffer, actual_time)
assert_equal(actual_time, expected_time)
if __name__ == '__main__':