def test_channel_sync(monkeypatch): """Test ability to sync the status of all channels.""" channel_server._db = DatabaseSQLite3(':memory:', db_dir='') # Seed the database with activity in Channel A test_client_a = _create_client_txs() deposit_txid_a = channel_server.open(test_client_a.deposit_tx, test_client_a.redeem_script) payment_txid = channel_server.receive_payment(deposit_txid_a, test_client_a.payment_tx) amount = channel_server.redeem(payment_txid) assert amount == TEST_PMT_AMOUNT # Seed the database with activity in Channel B cust_wallet._private_key = PrivateKey.from_random() test_client_b = _create_client_txs() deposit_txid_b = channel_server.open(test_client_b.deposit_tx, test_client_b.redeem_script) payment_txid = channel_server.receive_payment(deposit_txid_b, test_client_b.payment_tx) amount = channel_server.redeem(payment_txid) payment_tx1 = _create_client_payment(test_client_b, 2) payment_tx2 = _create_client_payment(test_client_b, 3) payment_tx3 = _create_client_payment(test_client_b, 4) payment_txid1 = channel_server.receive_payment(deposit_txid_b, payment_tx1) payment_txid2 = channel_server.receive_payment(deposit_txid_b, payment_tx2) payment_txid3 = channel_server.receive_payment(deposit_txid_b, payment_tx3) amount1 = channel_server.redeem(payment_txid1) amount2 = channel_server.redeem(payment_txid3) amount3 = channel_server.redeem(payment_txid2) assert amount1 == TEST_PMT_AMOUNT assert amount2 == TEST_PMT_AMOUNT assert amount3 == TEST_PMT_AMOUNT # Both channels should be `ready` since our channel is zeroconf by default channels = channel_server._db.pc.lookup() assert channels, 'Channel lookup with no args should return a list of all channels.' for channel in channels: assert channel.state == ChannelSQLite3.READY, 'Channel should be READY.' # Change Channel A to `confirming` for testing purposes channel_server._db.pc.update_state(deposit_txid_a, ChannelSQLite3.CONFIRMING) test_state = channel_server._db.pc.lookup(deposit_txid_a).state assert test_state == ChannelSQLite3.CONFIRMING, 'Channel should be CONFIRMING' # Change Channel B's expiration to be very close to allowable expiration new_expiry = int(time.time() + 3600) update = 'UPDATE payment_channel SET expires_at=? WHERE deposit_txid=?' channel_server._db.pc.c.execute(update, (new_expiry, deposit_txid_b)) channel_server._db.pc.c.connection.commit() test_expiry = channel_server._db.pc.lookup(deposit_txid_b).expires_at assert test_expiry == new_expiry, 'Channel should closing soon.' # Sync all of the server's payment channels channel_server.sync() # Test that Channel A is `ready` after a sync test_state = channel_server._db.pc.lookup(deposit_txid_a).state assert test_state == ChannelSQLite3.READY, 'Channel should be READY' # Test that Channel B is `closed` after a sync test_state = channel_server._db.pc.lookup(deposit_txid_b).state assert test_state == ChannelSQLite3.CLOSED, 'Channel should be CLOSED' # Test that Channel B payment is fully signed after a sync test_payment = channel_server._db.pc.lookup(deposit_txid_b).payment_tx goodsig_1 = Script.validate_template(test_payment.inputs[0].script, [bytes, bytes, 'OP_1', bytes]) goodsig_true = Script.validate_template(test_payment.inputs[0].script, [bytes, bytes, 'OP_TRUE', bytes]) assert goodsig_1 or goodsig_true, 'Payment should be in a fully signed format' # Test that Channel A remains `ready` after another sync channel_server.sync() test_state = channel_server._db.pc.lookup(deposit_txid_a).state assert test_state == ChannelSQLite3.READY, 'Channel should be READY' # Modify `lookup_spend_txid` to return a txid, as if the tx were spent monkeypatch.setattr(MockBlockchain, 'lookup_spend_txid', mock_lookup_spent_txid) # Test that Channel A is `closed` after a sync where it finds a spent txid channel_server.sync() test_state = channel_server._db.pc.lookup(deposit_txid_a).state assert test_state == ChannelSQLite3.CLOSED, 'Channel should be CLOSED'
def __init__(self): """Initialize the mock wallet with a private key.""" self._private_key = PrivateKey.from_random() self.testnet = False