test_query.py (13814B)
1 # standard imports 2 import os 3 import logging 4 import unittest 5 6 # external imports 7 from hexathon import ( 8 add_0x, 9 strip_0x, 10 ) 11 12 # local imports 13 from chainqueue.sql.query import * 14 from chainqueue.sql.tx import create 15 from chainqueue.sql.state import ( 16 set_waitforgas, 17 set_ready, 18 set_reserved, 19 set_sent, 20 set_final, 21 ) 22 from chainqueue.enum import StatusBits 23 24 # test imports 25 from tests.chainqueue_base import TestTxBase 26 27 logging.basicConfig(level=logging.DEBUG) 28 logg = logging.getLogger() 29 30 31 class TestTxQuery(TestTxBase): 32 33 def test_get_tx(self): 34 tx = get_tx(self.chain_spec, self.tx_hash) 35 expected_keys = [ 36 'otx_id', 37 'status', 38 'signed_tx', 39 'nonce', 40 ] 41 for k in tx.keys(): 42 expected_keys.remove(k) 43 44 self.assertEqual(len(expected_keys), 0) 45 46 47 def test_nonce_tx(self): 48 49 nonce_hashes = [self.tx_hash] 50 tx_hash = add_0x(os.urandom(32).hex()) 51 signed_tx = add_0x(os.urandom(128).hex()) 52 create( 53 self.chain_spec, 54 42, 55 self.alice, 56 tx_hash, 57 signed_tx, 58 session=self.session, 59 ) 60 txc = TxCache( 61 tx_hash, 62 self.alice, 63 self.bob, 64 self.foo_token, 65 self.bar_token, 66 self.from_value, 67 self.to_value, 68 session=self.session, 69 ) 70 self.session.add(txc) 71 self.session.commit() 72 73 nonce_hashes.append(tx_hash) 74 75 tx_hash = add_0x(os.urandom(32).hex()) 76 signed_tx = add_0x(os.urandom(128).hex()) 77 create( 78 self.chain_spec, 79 41, 80 self.alice, 81 tx_hash, 82 signed_tx, 83 session=self.session, 84 ) 85 txc = TxCache( 86 tx_hash, 87 self.alice, 88 self.bob, 89 self.foo_token, 90 self.bar_token, 91 self.from_value, 92 self.to_value, 93 session=self.session, 94 ) 95 self.session.add(txc) 96 97 txs = get_nonce_tx_cache(self.chain_spec, 42, self.alice) 98 self.assertEqual(len(txs.keys()), 2) 99 100 for h in nonce_hashes: 101 self.assertTrue(strip_0x(h) in txs) 102 103 104 def test_paused_tx_cache(self): 105 set_waitforgas(self.chain_spec, self.tx_hash) 106 107 tx_hash = add_0x(os.urandom(32).hex()) 108 signed_tx = add_0x(os.urandom(128).hex()) 109 create( 110 self.chain_spec, 111 43, 112 self.alice, 113 tx_hash, 114 signed_tx, 115 session=self.session, 116 ) 117 txc = TxCache( 118 tx_hash, 119 self.alice, 120 self.bob, 121 self.foo_token, 122 self.bar_token, 123 self.from_value, 124 self.to_value, 125 session=self.session, 126 ) 127 self.session.add(txc) 128 self.session.commit() 129 130 txs = get_paused_tx_cache(self.chain_spec, status=StatusBits.GAS_ISSUES, sender=self.alice, session=self.session) 131 self.assertEqual(len(txs.keys()), 1) 132 133 txs = get_paused_tx_cache(self.chain_spec, status=StatusBits.GAS_ISSUES, session=self.session) 134 self.assertEqual(len(txs.keys()), 1) 135 136 tx_hash = add_0x(os.urandom(32).hex()) 137 signed_tx = add_0x(os.urandom(128).hex()) 138 create( 139 self.chain_spec, 140 42, 141 self.bob, 142 tx_hash, 143 signed_tx, 144 session=self.session, 145 ) 146 txc = TxCache( 147 tx_hash, 148 self.bob, 149 self.alice, 150 self.bar_token, 151 self.foo_token, 152 self.to_value, 153 self.from_value, 154 session=self.session, 155 ) 156 self.session.add(txc) 157 self.session.commit() 158 159 txs = get_paused_tx_cache(self.chain_spec, status=StatusBits.GAS_ISSUES, session=self.session) 160 self.assertEqual(len(txs.keys()), 1) 161 162 set_waitforgas(self.chain_spec, tx_hash) 163 self.session.commit() 164 165 txs = get_paused_tx_cache(self.chain_spec, status=StatusBits.GAS_ISSUES, session=self.session) 166 self.assertEqual(len(txs.keys()), 2) 167 168 txs = get_paused_tx_cache(self.chain_spec, status=StatusBits.GAS_ISSUES, sender=self.bob, session=self.session) 169 self.assertEqual(len(txs.keys()), 1) 170 171 172 def test_count(self): 173 for i in range(3): 174 tx_hash = add_0x(os.urandom(32).hex()) 175 signed_tx = add_0x(os.urandom(128).hex()) 176 create( 177 self.chain_spec, 178 i, 179 self.alice, 180 tx_hash, 181 signed_tx, 182 session=self.session, 183 ) 184 txc = TxCache( 185 tx_hash, 186 self.alice, 187 self.bob, 188 self.foo_token, 189 self.bar_token, 190 self.from_value, 191 self.to_value, 192 session=self.session, 193 ) 194 self.session.add(txc) 195 set_ready(self.chain_spec, tx_hash, session=self.session) 196 set_reserved(self.chain_spec, tx_hash, session=self.session) 197 if i > 0: 198 set_sent(self.chain_spec, tx_hash, session=self.session) 199 if i == 2: 200 set_final(self.chain_spec, tx_hash, session=self.session) 201 202 tx_hash = add_0x(os.urandom(32).hex()) 203 signed_tx = add_0x(os.urandom(128).hex()) 204 create( 205 self.chain_spec, 206 i, 207 self.bob, 208 tx_hash, 209 signed_tx, 210 session=self.session, 211 ) 212 txc = TxCache( 213 tx_hash, 214 self.bob, 215 self.carol, 216 self.foo_token, 217 self.bar_token, 218 self.from_value, 219 self.to_value, 220 session=self.session, 221 ) 222 223 self.session.add(txc) 224 set_ready(self.chain_spec, tx_hash, session=self.session) 225 set_reserved(self.chain_spec, tx_hash, session=self.session) 226 set_sent(self.chain_spec, tx_hash, session=self.session) 227 self.session.commit() 228 229 self.assertEqual(count_tx(self.chain_spec, status=StatusBits.IN_NETWORK | StatusBits.FINAL, status_target=StatusBits.IN_NETWORK), 2) 230 self.assertEqual(count_tx(self.chain_spec, sender=self.alice, status=StatusBits.IN_NETWORK | StatusBits.FINAL, status_target=StatusBits.IN_NETWORK), 1) 231 232 233 def test_account_tx(self): 234 235 nonce_hashes = [self.tx_hash] 236 tx_hash = add_0x(os.urandom(32).hex()) 237 signed_tx = add_0x(os.urandom(128).hex()) 238 create( 239 self.chain_spec, 240 42, 241 self.alice, 242 tx_hash, 243 signed_tx, 244 session=self.session, 245 ) 246 txc = TxCache( 247 tx_hash, 248 self.alice, 249 self.bob, 250 self.foo_token, 251 self.bar_token, 252 self.from_value, 253 self.to_value, 254 session=self.session, 255 ) 256 self.session.add(txc) 257 self.session.commit() 258 259 nonce_hashes.append(tx_hash) 260 261 time_between = datetime.datetime.utcnow() 262 263 tx_hash = add_0x(os.urandom(32).hex()) 264 signed_tx = add_0x(os.urandom(128).hex()) 265 create( 266 self.chain_spec, 267 41, 268 self.alice, 269 tx_hash, 270 signed_tx, 271 session=self.session, 272 ) 273 txc = TxCache( 274 tx_hash, 275 self.alice, 276 self.bob, 277 self.foo_token, 278 self.bar_token, 279 self.from_value, 280 self.to_value, 281 session=self.session, 282 ) 283 self.session.add(txc) 284 285 nonce_hashes.append(tx_hash) 286 287 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session) 288 self.assertEqual(len(txs.keys()), 3) 289 290 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=tx_hash) 291 self.assertEqual(len(txs.keys()), 1) 292 293 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=nonce_hashes[0]) 294 self.assertEqual(len(txs.keys()), 3) 295 296 bogus_hash = add_0x(os.urandom(32).hex()) 297 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=bogus_hash) 298 self.assertEqual(len(txs.keys()), 0) 299 300 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=time_between) 301 self.assertEqual(len(txs.keys()), 1) 302 303 time_before = time_between - datetime.timedelta(hours=1) 304 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=time_before) 305 self.assertEqual(len(txs.keys()), 3) 306 307 time_after = datetime.datetime.utcnow() 308 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=time_after) 309 self.assertEqual(len(txs.keys()), 0) 310 311 312 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=2) 313 self.assertEqual(len(txs.keys()), 2) 314 315 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=1) 316 self.assertEqual(len(txs.keys()), 3) 317 318 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=4) 319 self.assertEqual(len(txs.keys()), 0) 320 321 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=1, until=2) 322 self.assertEqual(len(txs.keys()), 2) 323 324 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=time_before, until=time_between) 325 self.assertEqual(len(txs.keys()), 2) 326 327 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=nonce_hashes[0], until=nonce_hashes[1]) 328 self.assertEqual(len(txs.keys()), 2) 329 330 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, status=StatusBits.QUEUED, session=self.session) 331 self.assertEqual(len(txs.keys()), 0) 332 333 set_ready(self.chain_spec, nonce_hashes[1], session=self.session) 334 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, status=StatusBits.QUEUED, session=self.session) 335 self.assertEqual(len(txs.keys()), 1) 336 337 set_reserved(self.chain_spec, nonce_hashes[1], session=self.session) 338 set_sent(self.chain_spec, nonce_hashes[1], session=self.session) 339 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, status=StatusBits.QUEUED, session=self.session) 340 self.assertEqual(len(txs.keys()), 0) 341 342 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, not_status=StatusBits.QUEUED, session=self.session) 343 self.assertEqual(len(txs.keys()), 3) 344 345 txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, not_status=StatusBits.QUEUED, status=StatusBits.IN_NETWORK, session=self.session) 346 self.assertEqual(len(txs.keys()), 1) 347 348 def test_latest_txs(self): 349 350 nonce_hashes = [self.tx_hash] 351 tx_hash = add_0x(os.urandom(32).hex()) 352 signed_tx = add_0x(os.urandom(128).hex()) 353 create( 354 self.chain_spec, 355 42, 356 self.alice, 357 tx_hash, 358 signed_tx, 359 session=self.session, 360 ) 361 txc = TxCache( 362 tx_hash, 363 self.alice, 364 self.bob, 365 self.foo_token, 366 self.bar_token, 367 self.from_value, 368 self.to_value, 369 session=self.session, 370 ) 371 self.session.add(txc) 372 self.session.commit() 373 374 nonce_hashes.append(tx_hash) 375 376 377 tx_hash = add_0x(os.urandom(32).hex()) 378 signed_tx = add_0x(os.urandom(128).hex()) 379 create( 380 self.chain_spec, 381 41, 382 self.alice, 383 tx_hash, 384 signed_tx, 385 session=self.session, 386 ) 387 txc = TxCache( 388 tx_hash, 389 self.alice, 390 self.bob, 391 self.foo_token, 392 self.bar_token, 393 self.from_value, 394 self.to_value, 395 session=self.session, 396 ) 397 self.session.add(txc) 398 399 nonce_hashes.append(tx_hash) 400 401 txs = get_latest_txs(self.chain_spec, session=self.session) 402 self.assertEqual(len(txs.keys()), 3) 403 404 txs = get_latest_txs(self.chain_spec, count=1, session=self.session) 405 self.assertEqual(len(txs.keys()), 1) 406 407 if __name__ == '__main__': 408 unittest.main()