def test_get_more_then_thousand_utxos(helpers, addresses, nodeSetWithIntegratedTokenPlugin): """ test if we send more have more than a 1000 UTXO's we still receive a response. """ _, address_2 = addresses states = [ n.db_manager.get_state(TOKEN_LEDGER_ID) for n in nodeSetWithIntegratedTokenPlugin ] utxos = [] for i in range(UTXO_LIMIT + 200): amount = randint(1, 5) key = TokenStaticHelper.create_state_key( libsovtoken_address_to_address(address_2), i + 5) utxos.append((key, amount)) for state in states: state.set(key, str(amount).encode()) request = helpers.request.get_utxo(address_2) responses = helpers.sdk.send_and_check_request_objects([request]) for response in responses: result = response[1]['result'] assert len(result[OUTPUTS]) == UTXO_LIMIT for output in result[OUTPUTS]: assert (TokenStaticHelper.create_state_key(output[ADDRESS], output[SEQNO]), output[AMOUNT]) in utxos assert result.get(NEXT_SEQNO, None)
def insert_over_thousand_utxos(db_manager, payment_address): token_state = db_manager.get_state(TOKEN_LEDGER_ID) for i in range(1200): token_state.set( TokenStaticHelper.create_state_key( libsovtoken_address_to_address(payment_address), i), str(i).encode())
def insert_utxos_after_gap(db_manager, payment_address): token_state = db_manager.get_state(TOKEN_LEDGER_ID) for i in range(1300, 2400): token_state.set( TokenStaticHelper.create_state_key( libsovtoken_address_to_address(payment_address), i), str(i).encode()) return 1300
def test_get_more_then_thousand_utxos_with_from( helpers, addresses, nodeSetWithIntegratedTokenPlugin): """ test if we send more have more than a thousand of UTXO's we will still receive a response. """ address_1, address_2 = addresses states = [ n.db_manager.get_state(TOKEN_LEDGER_ID) for n in nodeSetWithIntegratedTokenPlugin ] utxos = [] for i in range(UTXO_LIMIT + 200): amount = randint(1, 5) seq_no = i + 5 key = TokenStaticHelper.create_state_key( libsovtoken_address_to_address(address_2), seq_no) utxos.append((key, amount, seq_no)) for state in states: state.set(key, str(amount).encode()) # NB: this transaction is needed just to update bls_store with new root hash total = 1000 outputs = [{"address": address_1, "amount": total}] mint_result = helpers.general.do_mint(outputs) shift = 50 request = helpers.request.get_utxo(address_2, utxos[shift][2]) responses = helpers.sdk.send_and_check_request_objects([request]) utxos = utxos[shift:shift + UTXO_LIMIT] for response in responses: result = response[1]['result'] assert result[STATE_PROOF] assert len(result[OUTPUTS]) == UTXO_LIMIT for output in result[OUTPUTS]: assert (TokenStaticHelper.create_state_key(output[ADDRESS], output[SEQNO]), output[AMOUNT], output[SEQNO]) in utxos assert result.get(NEXT_SEQNO)
def make_tokens(helpers, nodeSetWithIntegratedTokenPlugin, addresses): address, address_2 = addresses states = [n.db_manager.get_state(TOKEN_LEDGER_ID) for n in nodeSetWithIntegratedTokenPlugin] utxos = [] for i in range(UTXO_LIMIT+200): amount = randint(1, 5) seq_no = i+5 key = TokenStaticHelper.create_state_key(libsovtoken_address_to_address(address), seq_no) utxos.append((key, amount, seq_no)) for state in states: state.set(key, str(amount).encode()) total = 1000 outputs = [{"address": address_2, "amount": total}] helpers.general.do_mint(outputs)
def test_state_after_xfer(helpers, initial_mint, addresses, nodeSetWithIntegratedTokenPlugin): mint_seq_no = get_seq_no(initial_mint) [address1, address2, *_] = addresses inputs = helpers.general.get_utxo_addresses([address1]) inputs = [utxo for utxos in inputs for utxo in utxos] outputs = [{"address": address2, "amount": 100}] helpers.general.do_transfer(inputs, outputs) key = TokenStaticHelper.create_state_key( libsovtoken_address_to_address(address1), mint_seq_no) for n in nodeSetWithIntegratedTokenPlugin: res = n.db_manager.get_state(TOKEN_LEDGER_ID).get(key) assert not res
def test_state_proof(public_minting, looper, # noqa sdk_pool_handle, sdk_wallet_client, seller_token_wallet, seller_address, user1_token_wallet, user1_address): res = send_get_utxo(looper, seller_address, sdk_wallet_client, sdk_pool_handle) update_token_wallet_with_result(seller_token_wallet, res) # Do 20 XFER txns for _ in range(20): utxos = [_ for lst in seller_token_wallet.get_all_wallet_utxos().values() for _ in lst] seq_no, amount = utxos[0] inputs = [[seller_token_wallet, seller_address, seq_no]] outputs = [{"address": user1_address, "amount": 1}, {"address": seller_address, "amount": amount-1}] res = send_xfer(looper, inputs, outputs, sdk_pool_handle) update_token_wallet_with_result(seller_token_wallet, res) res = send_get_utxo(looper, seller_address, sdk_wallet_client, sdk_pool_handle) update_token_wallet_with_result(seller_token_wallet, res) res = send_get_utxo(looper, user1_address, sdk_wallet_client, sdk_pool_handle) update_token_wallet_with_result(user1_token_wallet, res) res = send_get_utxo(looper, user1_address, sdk_wallet_client, sdk_pool_handle) # Check presence of state proof assert res[STATE_PROOF] encoded = {} outputs = res[OUTPUTS] for out in outputs: state_key = TokenStaticHelper.create_state_key(out["address"], out["seqNo"]) encoded[state_key] = rlp_encode([str(out["amount"])]) proof_nodes = decode_proof(res[STATE_PROOF][PROOF_NODES]) client_trie = Trie(PersistentDB(KeyValueStorageInMemory())) assert client_trie.verify_spv_proof_multi( state_roots_serializer.deserialize(res[STATE_PROOF][ROOT_HASH]), encoded, proof_nodes)
def test_token_req_handler_update_state_XFER_PUBLIC_success( helpers, addresses, xfer_handler_a): [address1, address2] = addresses seq_no = 1 inputs = [{"source": utxo_from_addr_and_seq_no(address1, seq_no)}] outputs = [{"address": address2, "amount": 40}] request = helpers.request.transfer(inputs, outputs) txn = reqToTxn(request) append_txn_metadata(txn, seq_no=seq_no) xfer_handler_a.dynamic_validation(request) xfer_handler_a.update_state(txn, None, request) state_key = TokenStaticHelper.create_state_key( libsovtoken_address_to_address(address1), seq_no) utxo_cache = xfer_handler_a.database_manager.get_store(UTXO_CACHE_LABEL) key = utxo_cache._create_key( Output(libsovtoken_address_to_address(address1), seq_no, 60)) assert utxo_cache._store._has_key(key) try: xfer_handler_a.state.get(state_key, False) except Exception: pytest.fail("This state key isn't in the state")
def add_utxo(payment_address, get_utxo_handler): get_utxo_handler.state.set( TokenStaticHelper.create_state_key( libsovtoken_address_to_address(payment_address), 1), "3".encode())
def create_state_key(address: str, seq_no: int) -> bytes: return TokenStaticHelper.create_state_key(address=address, seq_no=seq_no)