Ejemplo n.º 1
0
def waitForXMRNode(rpc_offset, max_tries=7):
    for i in range(max_tries + 1):
        try:
            callrpc_xmr_na(XMR_BASE_RPC_PORT + rpc_offset, 'get_block_count')
            return
        except Exception as ex:
            if i < max_tries:
                logging.warning('Can\'t connect to XMR RPC: %s. Retrying in %d second/s.', str(ex), (i + 1))
                time.sleep(i + 1)
    raise ValueError('waitForXMRNode failed')
Ejemplo n.º 2
0
def run_loop(cls):
    while not cls.stop_nodes:
        if cls.btc_addr is not None:
            callnoderpc(0, 'generatetoaddress', [1, cls.btc_addr])

        if cls.xmr_addr is not None:
            callrpc_xmr_na(XMR_BASE_RPC_PORT + 0, 'generateblocks', {
                'wallet_address': cls.xmr_addr,
                'amount_of_blocks': 1
            })
        time.sleep(0.5)
Ejemplo n.º 3
0
def run_loop(cls):
    while not cls.stop_nodes:
        try:
            if cls.xmr_addr is not None:
                callrpc_xmr_na(XMR_BASE_RPC_PORT + 0, 'generateblocks', {
                    'wallet_address': cls.xmr_addr,
                    'amount_of_blocks': 1
                })

            part_height = callnoderpc(0, 'getblockchaininfo')['blocks']
            if cls.part_stakelimit <= part_height:
                cls.part_stakelimit += 1
                callnoderpc(0, 'walletsettings',
                            ['stakelimit', {
                                'height': cls.part_stakelimit
                            }])
        except Exception as e:
            logging.error('Update thread: %s', str(e))
        time.sleep(0.5)
Ejemplo n.º 4
0
    def test_01_swap_successful(self):
        ID_ALICE_SWAP = os.path.join(TEST_DIR,
                                     'test_01_alice_swap_state') + '.json'
        ID_BOB_SWAP = os.path.join(TEST_DIR,
                                   'test_01_bob_swap_state') + '.json'

        self.startSwap(ID_ALICE_SWAP, ID_BOB_SWAP, 1, 2)

        logging.info(
            'Alice creates the script-chain lock and refund txns and signs the refund tx, sends to Bob.'
        )
        msg2f = callSwapTool(ID_ALICE_SWAP, 'msg2f')

        logging.info(
            'Bob verifies the txns and signs the refund tx and creates an encrypted signature for the refund spend tx encumbered by Alice\'s coin B key share.'
        )
        callSwapTool(ID_BOB_SWAP, 'processmsg', str_param=msg2f)
        msg3l = callSwapTool(ID_BOB_SWAP, 'msg3l')

        logging.info(
            'Alice verifies the signature and encrypted signature from Bob.')
        callSwapTool(ID_ALICE_SWAP, 'processmsg', str_param=msg3l)

        logging.info(
            'Creates the lock spend tx and signs an encrypted signature encumbered by Bob\'s coin B key share'
        )
        msg4f = callSwapTool(ID_ALICE_SWAP, 'msg4f')

        logging.info('Publishes the script-chain lock tx.')
        a_lock_txid = callSwapTool(ID_ALICE_SWAP, 'publishalocktx')

        # Check that the script-chain lock refund tx isn't mineable yet
        try:
            rv = callSwapTool(ID_ALICE_SWAP, 'publishalockrefundtx')
            assert (False)
        except Exception as e:
            assert ('non-BIP68-final' in str(e))

        logging.info(
            'Bob verifies the lock spend tx and encrypted signature from Alice.'
        )
        callSwapTool(ID_BOB_SWAP, 'processmsg', str_param=msg4f)

        logging.info('Bob waits for the script-chain lock tx to confirm.')

        num_tries = 30
        for i in range(1 + num_tries):
            rv = callSwapTool(ID_BOB_SWAP, 'confirmalocktx')
            print('confirmalocktx', rv)
            if rv.strip() == 'True':
                break

            if i >= num_tries:
                raise ValueError(
                    'Timed out waiting for script-chain lock tx to confirm.')

        logging.info('Then publishes the second-chain lock tx.')
        b_lock_txid = callSwapTool(ID_BOB_SWAP, 'publishblocktx')

        logging.info(
            'Alice waits for the scriptless-chain lock tx to confirm.')

        num_tries = 120
        for i in range(1 + num_tries):
            rv = callSwapTool(ID_ALICE_SWAP, 'confirmblocktx')
            print('confirmblocktx', rv)
            if rv.strip() == 'True':
                break

            if i >= num_tries:
                raise ValueError(
                    'Timed out waiting for scriptless-chain lock tx to confirm.'
                )
            time.sleep(2)

        logging.info(
            'Alice shares the secret value with Bob, allowing the script-chain lock tx to be spent'
        )
        msg5f = callSwapTool(ID_ALICE_SWAP, 'msg5f')
        callSwapTool(ID_BOB_SWAP, 'processmsg', str_param=msg5f)

        logging.info('Bob spends from the script-chain lock tx')
        alockspendtxid = callSwapTool(ID_BOB_SWAP, 'publishalockspendtx')
        logging.info('alockspendtxid %s', alockspendtxid)

        logging.info(
            'Alice looks for Bob\'s script-chain lock spend tx and extracts the sig'
        )

        num_tries = 20
        for i in range(1 + num_tries):
            rv = callSwapTool(ID_ALICE_SWAP, 'findalockspendtx')
            print('findalockspendtx', rv)
            if rv.strip() == 'True':
                break

            if i >= num_tries:
                raise ValueError(
                    'Timed out waiting for script-chain lock spend tx to confirm.'
                )
            time.sleep(1)

        self.callxmrnodewallet(ID_ALICE_XMR, 'open_wallet',
                               {'filename': 'testwallet'})
        xmr_addr_alice1 = self.callxmrnodewallet(ID_ALICE_XMR,
                                                 'get_address')['address']

        logging.info(
            'Alice redeems the scriptless-chain lock tx to her address: %s',
            xmr_addr_alice1)
        rv = callSwapTool(ID_ALICE_SWAP,
                          'redeemblocktx',
                          str_param=xmr_addr_alice1)
        print('redeemblocktx', rv)

        self.callxmrnodewallet(ID_ALICE_XMR, 'close_wallet')
        self.callxmrnodewallet(ID_ALICE_XMR, 'open_wallet',
                               {'filename': 'testwallet'})

        logging.info('Waiting for Alice\'s XMR to confirm...')
        num_tries = 120
        for i in range(num_tries + 1):
            rv = self.callxmrnodewallet(ID_ALICE_XMR, 'get_balance')
            if rv['balance'] > 0 and rv['blocks_to_unlock'] == 0:
                break

            r = callrpc_xmr_na(XMR_BASE_RPC_PORT + ID_ALICE_XMR,
                               'get_block_count')
            print('XMR blocks', r['count'])

            if i >= num_tries:
                raise ValueError(
                    'Balance not confirming on node {}'.format(ID_ALICE_XMR))
            time.sleep(2)

        logging.info('Waiting for Bob\'s BTC to confirm...')
        for i in range(num_tries + 1):
            rv = callnoderpc(ID_BOB_PART, 'getbalances')
            if rv['mine']['trusted'] > 0:
                break
            print('btc height', i,
                  callnoderpc(ID_BOB_PART, 'getblockchaininfo')['blocks'])
            if i >= num_tries:
                raise ValueError(
                    'Balance not confirming on node {}'.format(ID_ALICE_XMR))
            time.sleep(1)
Ejemplo n.º 5
0
    def initialiseTestState(self):
        # Called from a classmethod, seems to poison all method calls
        logging.info('Initialising chain states')

        for i in range(NUM_NODES):
            # Disable staking
            callnoderpc(i, 'reservebalance', [True, 1000000])

            # Lower output split threshold for more stakeable outputs
            callnoderpc(i, 'walletsettings', [
                'stakingoptions', {
                    'stakecombinethreshold': 100,
                    'stakesplitthreshold': 200
                }
            ])

        # Add regtest coins and allow staking for node 0
        callnoderpc(0, 'extkeyimportmaster', [
            'abandon baby cabbage dad eager fabric gadget habit ice kangaroo lab absorb'
        ])
        assert (callnoderpc(0, 'getwalletinfo')['total_balance'] == 100000)
        callnoderpc(0, 'reservebalance', [False])
        callnoderpc(0, 'walletsettings', ['stakelimit', {'height': 0}])

        # Import coin for Alice
        callnoderpc(ID_ALICE_PART, 'extkeyimportmaster', [
            'pact mammal barrel matrix local final lecture chunk wasp survey bid various book strong spread fall ozone daring like topple door fatigue limb olympic',
            '', 'true'
        ])
        callnoderpc(ID_ALICE_PART, 'getnewextaddress', ['lblExtTest'])
        callnoderpc(ID_ALICE_PART, 'rescanblockchain')
        assert (callnoderpc(ID_ALICE_PART,
                            'getwalletinfo')['total_balance'] == 25000)

        # Import account for Bob
        callnoderpc(ID_BOB_PART, 'extkeyimportmaster', [
            'sección grito médula hecho pauta posada nueve ebrio bruto buceo baúl mitad'
        ])

        # Why so many blocks?
        num_blocks = 500
        if callrpc_xmr_na(XMR_BASE_RPC_PORT + 0,
                          'get_block_count')['count'] < num_blocks:
            logging.info('Mining %d Monero blocks.', num_blocks)
            callrpc_xmr_na(XMR_BASE_RPC_PORT + 0, 'generateblocks', {
                'wallet_address': self.xmr_addr,
                'amount_of_blocks': num_blocks
            })
        rv = callrpc_xmr_na(XMR_BASE_RPC_PORT + 0, 'get_block_count')
        logging.info('XMR blocks: %d', rv['count'])

        rv = callnoderpc(0, 'getblockchaininfo')
        logging.info('PART blocks: %d', rv['blocks'])

        xmr_addr_bob = self.callxmrnodewallet(self, ID_BOB_XMR,
                                              'get_address')['address']
        logging.info('Sending 50 XMR to Bob\'s address %s\n', xmr_addr_bob)
        params = {
            'destinations': [{
                'amount': 50 * XMR_COIN,
                'address': xmr_addr_bob
            }]
        }
        rv = self.callxmrnodewallet(self, 0, 'transfer', params)
        logging.info('Sent initial XMR to Bob: %s', dumpj(rv))

        logging.info('Testing node sync')
        sync_passed = False
        for i in range(20):
            try:
                above_0 = 0
                for i in range(NUM_NODES):
                    r = callnoderpc(i, 'getblockchaininfo')
                    print('PART', i, r['blocks'])
                    if r['blocks'] > 0:
                        above_0 += 1

                xmr_above_1 = 0
                for i in range(XMR_NUM_NODES):
                    r = callrpc_xmr_na(XMR_BASE_RPC_PORT + i,
                                       'get_block_count')
                    print('XMR', i, r['count'])
                    if r['count'] > 2:  # xmr counts genesis block as 1
                        xmr_above_1 += 1

                if above_0 >= NUM_NODES and xmr_above_1 >= XMR_NUM_NODES:
                    logging.info('Node syncing passed.')
                    sync_passed = True
                    break
            except Exception as e:
                print('Error', repr(e))
            time.sleep(1)

        assert (sync_passed), 'Nodes did not sync'

        num_tries = 40
        for i in range(num_tries + 1):
            rv = self.callxmrnodewallet(self, ID_BOB_XMR, 'get_balance')
            if rv['balance'] > 0 and rv['blocks_to_unlock'] == 0:
                break

            r = callrpc_xmr_na(XMR_BASE_RPC_PORT + ID_BOB_XMR,
                               'get_block_count')
            print(r)

            if i >= num_tries:
                raise ValueError(
                    'XMR balance not confirming on node {}'.format(ID_BOB_XMR))
            time.sleep(1)
Ejemplo n.º 6
0
    def initialiseTestState(self):
        # Called from a classmethod, seems to poison all method calls
        logging.info('\nInitialising chain states')

        # Why so many blocks?
        num_blocks = 500
        if callrpc_xmr_na(XMR_BASE_RPC_PORT + 0,
                          'get_block_count')['count'] < num_blocks:
            logging.info('Mining %d Monero blocks.', num_blocks)
            callrpc_xmr_na(XMR_BASE_RPC_PORT + 0, 'generateblocks', {
                'wallet_address': self.xmr_addr,
                'amount_of_blocks': num_blocks
            })
        rv = callrpc_xmr_na(XMR_BASE_RPC_PORT + 0, 'get_block_count')
        logging.info('XMR blocks: %d', rv['count'])

        if callnoderpc(0, 'getblockchaininfo')['blocks'] < num_blocks:
            logging.info('Mining %d Bitcoin blocks to %s', num_blocks,
                         self.btc_addr)
            callnoderpc(0, 'generatetoaddress', [num_blocks, self.btc_addr])
        rv = callnoderpc(0, 'getblockchaininfo')
        logging.info('BTC blocks: %d', rv['blocks'])

        btc_addr_alice1 = callnoderpc(ID_ALICE_BTC, 'getnewaddress',
                                      ['alice\'s main addr', 'bech32'])
        callnoderpc(0, 'sendtoaddress', [btc_addr_alice1, 100])

        xmr_addr_bob1 = self.callxmrnodewallet(self, ID_BOB_XMR,
                                               'get_address')['address']
        params = {
            'destinations': [{
                'amount': 50 * XMR_COIN,
                'address': xmr_addr_bob1
            }]
        }
        rv = self.callxmrnodewallet(self, 0, 'transfer', params)
        logging.info('Sent initial XMR to Bob: %s', dumpj(rv))

        logging.info('Testing node sync')
        sync_passed = False
        for i in range(20):
            try:
                above_0 = 0
                for i in range(NUM_NODES):
                    r = callnoderpc(i, 'getblockchaininfo')
                    print('BTC', i, r['blocks'])
                    if r['blocks'] > 0:
                        above_0 += 1

                xmr_above_1 = 0
                for i in range(XMR_NUM_NODES):
                    r = callrpc_xmr_na(XMR_BASE_RPC_PORT + i,
                                       'get_block_count')
                    print('XMR', i, r['count'])
                    if r['count'] > 2:  # xmr counts genesis block as 1
                        xmr_above_1 += 1

                if above_0 >= NUM_NODES and xmr_above_1 >= XMR_NUM_NODES:
                    logging.info('Node syncing passed.')
                    sync_passed = True
                    break
            except Exception as e:
                print('Error', repr(e))
            time.sleep(1)

        assert (sync_passed), 'Nodes did not sync'

        num_tries = 40
        for i in range(num_tries + 1):
            rv = self.callxmrnodewallet(self, ID_BOB_XMR, 'get_balance')
            if rv['balance'] > 0 and rv['blocks_to_unlock'] == 0:
                break

            r = callrpc_xmr_na(XMR_BASE_RPC_PORT + ID_BOB_XMR,
                               'get_block_count')
            print(r)

            if i >= num_tries:
                raise ValueError(
                    'XMR Balance not confirming on node {}'.format(ID_BOB_XMR))
            time.sleep(1)