common.py (2055B)
1 # standard imports 2 import hashlib 3 4 # local imports 5 from chainqueue.cache import ( 6 Cache, 7 CacheTokenTx, 8 ) 9 10 11 class MockCounter: 12 13 def __init__(self): 14 self.c = 0 15 16 17 def next(self): 18 c = self.c 19 self.c += 1 20 return c 21 22 23 class MockTokenCache(Cache): 24 25 def __init__(self): 26 self.db = {} 27 self.last_filter = None 28 29 def put(self, chain_spec, cache_tx): 30 self.db[cache_tx.hash] = cache_tx 31 32 33 def get(self, chain_spec, tx_hash): 34 return self.db[tx_hash] 35 36 37 def by_nonce(self, cache_filter): 38 self.last_filter = cache_filter 39 40 41 def by_date(self, cache_filter=None): 42 self.last_filter = cache_filter 43 44 45 def count(self, cache_filter): 46 self.last_filter = cache_filter 47 48 49 class MockCacheTokenTx(CacheTokenTx): 50 51 def deserialize(self, signed_tx): 52 h = hashlib.sha1() 53 try: 54 h.update(signed_tx + b'\x01') 55 except TypeError: 56 h.update(signed_tx.encode('utf-8') + b'\x01') 57 z = h.digest() 58 nonce = int.from_bytes(z[:4], 'big') 59 token_value = int.from_bytes(z[4:8], 'big') 60 value = int.from_bytes(z[8:12], 'big') 61 62 h = hashlib.sha1() 63 h.update(z) 64 z = h.digest() 65 sender = z.hex() 66 67 h = hashlib.sha1() 68 h.update(z) 69 z = h.digest() 70 recipient = z.hex() 71 72 h = hashlib.sha1() 73 h.update(z) 74 z = h.digest() 75 token = z.hex() 76 77 h = hashlib.sha256() 78 h.update(z) 79 z = h.digest() 80 tx_hash = z.hex() 81 82 self.init(tx_hash, nonce, sender, recipient, value) 83 self.set('src_token', token) 84 self.set('dst_token', token) 85 self.set('src_value', token_value) 86 self.set('dst_value', token_value) 87 self.confirm(42, 13, 1024000) 88 89 return self 90 91 92 class MockContentStore: 93 94 def __init__(self): 95 self.store = {} 96 97 98 def put(self, k, v): 99 self.store[k] = v 100 101 102 def get(self, k): 103 return self.store.get(k)