github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/test/test_combinators.py (about)

     1  
     2  import testlib
     3  
     4  fee = 20
     5  
     6  initialsend = 200000
     7  capacity = 1000000
     8  
     9  pushsend = 250000
    10  
    11  def run_pushclose_test(env, initiator, target, closer):
    12      bc = env.bitcoind
    13      initiator = env.lits[0]
    14      target = env.lits[1]
    15  
    16      # Connect the nodes.
    17      initiator.connect_to_peer(target)
    18  
    19      # First figure out where we should send the money.
    20      addr1 = initiator.make_new_addr()
    21      print('Got initiator address:', addr1)
    22  
    23      # Send a bitcoin.
    24      bc.rpc.sendtoaddress(addr1, 1)
    25      env.generate_block()
    26  
    27      # Log it to make sure we got it.
    28      bal1 = initiator.get_balance_info()['TxoTotal']
    29      print('initial initiator balance:', bal1)
    30  
    31      # Set the fee so we know what's going on.
    32      initiator.rpc.SetFee(Fee=fee, CoinType=testlib.REGTEST_COINTYPE)
    33      target.rpc.SetFee(Fee=fee, CoinType=testlib.REGTEST_COINTYPE)
    34      print('fees set to', fee, '(per byte)')
    35  
    36      # Now actually do the funding.
    37      cid = initiator.open_channel(target, capacity, initialsend)
    38      print('Created channel:', cid)
    39  
    40      # Now we confirm the block.
    41      env.generate_block()
    42  
    43      # Figure out if it's actually open now.
    44      res = initiator.rpc.ChannelList(ChanIdx=cid)
    45      cinfo = res['Channels'][0]
    46      assert cinfo['Height'] == env.get_height(), "Channel height doesn't match new block."
    47  
    48      # Send the money through the channel.
    49      ct0initiator = initiator.get_balance_info()['ChanTotal']
    50      ct0target = target.get_balance_info()['ChanTotal']
    51      initiator.rpc.Push(ChanIdx=cid, Amt=pushsend, Data=None)
    52      ct1initiator = initiator.get_balance_info()['ChanTotal']
    53      ct1target = target.get_balance_info()['ChanTotal']
    54      assert ct1initiator == ct0initiator - pushsend, "channel balances don't match up"
    55      assert ct1target == ct0target + pushsend, "channel balances don't match up"
    56  
    57      # Close it, but Alice be the initiator.
    58      print('Closing channel... (with Alice)')
    59      tt0 = target.get_balance_info()['TxoTotal']
    60      res = closer.rpc.CloseChannel(ChanIdx=cid)
    61      print('Status:', res['Status'])
    62      print('Mining new block(s) to confirm closure...')
    63      env.generate_block(count=20)
    64      tt1 = target.get_balance_info()['TxoTotal']
    65  
    66      # Now report the difference in channel balance.
    67      print('Target:', tt0, '->', tt1, '( expected:', initialsend + pushsend - 200, ')')
    68      assert tt1 == tt0 + initialsend + pushsend - 200, "final balance doesn't match"
    69  
    70      # 200 = Fee() * consts.QcStateFee
    71  
    72  def run_pushbreak_test(env, initiator, target, breaker):
    73      bc = env.bitcoind
    74  
    75      # Connect the nodes.
    76      initiator.connect_to_peer(target)
    77  
    78      # First figure out where we should send the money.
    79      addr1 = initiator.make_new_addr()
    80      print('Got initiator address:', addr1)
    81  
    82      # Send a bitcoin.
    83      bc.rpc.sendtoaddress(addr1, 1)
    84      env.generate_block()
    85  
    86      # Log it to make sure we got it.
    87      bal1 = initiator.get_balance_info()['TxoTotal']
    88      print('initial initiator balance:', bal1)
    89  
    90      # Set the fee so we know what's going on.
    91      initiator.rpc.SetFee(Fee=fee, CoinType=testlib.REGTEST_COINTYPE)
    92      target.rpc.SetFee(Fee=fee, CoinType=testlib.REGTEST_COINTYPE)
    93      print('fees set to', fee, '(per byte)')
    94  
    95      # Now actually do the funding.
    96      cid = initiator.open_channel(target, capacity, initialsend)
    97      print('Created channel:', cid)
    98  
    99      # Now we confirm the block.
   100      env.generate_block()
   101  
   102      # Figure out if it's actually open now.
   103      res = initiator.rpc.ChannelList(ChanIdx=cid)
   104      cinfo = res['Channels'][0]
   105      assert cinfo['Height'] == env.get_height(), "Channel height doesn't match new block."
   106  
   107      # Send the money through the channel.
   108      ct0initiator = initiator.get_balance_info()['ChanTotal']
   109      ct0target = target.get_balance_info()['ChanTotal']
   110      initiator.rpc.Push(ChanIdx=cid, Amt=pushsend, Data=None)
   111      ct1initiator = initiator.get_balance_info()['ChanTotal']
   112      ct1target = target.get_balance_info()['ChanTotal']
   113      assert ct1initiator == ct0initiator - pushsend, "channel balances don't match up"
   114      assert ct1target == ct0target + pushsend, "channel balances don't match up"
   115  
   116      # Close it, but Bob be the initiator.
   117      print('Breaking channel... (with Bob)')
   118      tt0 = target.get_balance_info()['TxoTotal']
   119      res = breaker.rpc.BreakChannel(ChanIdx=cid)
   120      print('Status:', str(res))
   121      print('Mining new block(s) to confirm closure...')
   122      env.generate_block(count=20)
   123      tt1 = target.get_balance_info()['TxoTotal']
   124  
   125      # Now report the difference in channel balance.
   126      print('Target:', tt0, '->', tt1, '( expected:', initialsend + pushsend - 200, ')')
   127      assert tt1 == tt0 + initialsend + pushsend - 200, "final balance doesn't match"
   128  
   129      # 200 = Fee() * consts.QcStateFee
   130  
   131  def run_close_test(env, initiator, target, closer):
   132      bc = env.bitcoind
   133  
   134      # Connect the nodes.
   135      initiator.connect_to_peer(target)
   136  
   137      # First figure out where we should send the money.
   138      addr1 = initiator.make_new_addr()
   139      print('Got initiator address:', addr1)
   140  
   141      # Send a bitcoin.
   142      bc.rpc.sendtoaddress(addr1, 1)
   143      env.generate_block()
   144  
   145      # Log it to make sure we got it.
   146      bal1 = initiator.get_balance_info()['TxoTotal']
   147      print('initial initiator balance:', bal1)
   148  
   149      # Set the fee so we know what's going on.
   150      initiator.rpc.SetFee(Fee=fee, CoinType=testlib.REGTEST_COINTYPE)
   151      target.rpc.SetFee(Fee=fee, CoinType=testlib.REGTEST_COINTYPE)
   152  
   153      # Now actually do the funding.
   154      cid = initiator.open_channel(target, capacity, initialsend)
   155      print('Created channel:', cid)
   156  
   157      # Now we confirm the block.
   158      env.generate_block()
   159  
   160      # Now close the channel.
   161      print('Now closing...')
   162      res = closer.rpc.CloseChannel(ChanIdx=cid)
   163      print('Status:', res['Status'])
   164      env.generate_block()
   165  
   166      # Check balances.
   167      bals = initiator.get_balance_info()
   168      fbal = bals['TxoTotal']
   169      print('final balance:', fbal)
   170      expected = bal1 - initialsend - 3560
   171      print('expected:', expected)
   172      print('diff:', expected - fbal)
   173  
   174      assert bals['ChanTotal'] == 0, "channel balance isn't zero!"
   175  
   176  def run_break_test(env, initiator, target, breaker):
   177      bc = env.bitcoind
   178  
   179      # Connect the nodes.
   180      initiator.connect_to_peer(target)
   181  
   182      # First figure out where we should send the money.
   183      addr1 = initiator.make_new_addr()
   184      print('Got initiator address:', addr1)
   185  
   186      # Send a bitcoin.
   187      bc.rpc.sendtoaddress(addr1, 1)
   188      env.generate_block()
   189  
   190      # Log it to make sure we got it.
   191      bal1 = initiator.get_balance_info()['TxoTotal']
   192      print('initial initiator balance:', bal1)
   193  
   194      # Set the fee so we know what's going on.
   195      initiator.rpc.SetFee(Fee=fee, CoinType=testlib.REGTEST_COINTYPE)
   196      target.rpc.SetFee(Fee=fee, CoinType=testlib.REGTEST_COINTYPE)
   197  
   198      # Now actually do the funding.
   199      cid = initiator.open_channel(target, capacity, initialsend)
   200      print('Created channel:', cid)
   201  
   202      # Now we confirm the block.
   203      env.generate_block()
   204  
   205      # Now we confirm the block.
   206      env.generate_block(count=5)
   207  
   208      # Now close the channel.
   209      print('Now breaking channel...')
   210      res = breaker.rpc.BreakChannel(ChanIdx=cid)
   211      print('Status:', str(res))
   212  
   213      # Now we figure out the balances at 2 points in time.
   214      print(str(initiator.get_balance_info()))
   215      print('Fast-forwarding time...')
   216      env.generate_block(count=5) # Just to escape the locktime to make sure we get our money.
   217      bi2 = initiator.get_balance_info()
   218      print(str(bi2))
   219  
   220      print(str(initiator.rpc.ChannelList(ChanIdx=cid)['Channels']))
   221      assert bi2['ChanTotal'] == 0, "channel balance isn't zero!"
   222      # TODO Make sure the channel actually gets broken.