Esempio n. 1
0
 def test_settle_channel(self):
     # self.block = ''
     self.logger.info('Waiting for channel settlement to end')
     time.sleep(15 * (challenge_period + 3))
     self.logger.info('Lifetime should have ended')
     tx_hash = self.channel.transact({
         'from': self.sender
     }).settle(self.sender, self.open_block)
     try:
         self.logger.info(
             self.channel.call({
                 'from': self.sender
             }).settle(self.sender, self.open_block))
     except:
         self.logger.warning('SETTLEMENT FAILED ×')
     check_successful_tx(self.web3, tx_hash, txn_wait)
     try:
         self.logger.warning('Channel info: {}'.format(
             self.channel.call().getChannelInfo(self.sender,
                                                self.open_block)))
     except:
         self.logger.info('Channel info failed in settle ✓')
     try:
         self.logger.warning('Closing request: {}'.format(
             self.channel.call().getClosingRequestInfo(
                 self.sender, self.open_block)))
     except:
         self.logger.info('Closing request info failed is settle ✓')
     try:
         self.logger.info('Balance: {}'.format(
             self.channel.call({
                 'from': addresses[1]
             }).checkBalance()))
     except:
         self.logger.warning('Balance failed in settle ×')
Esempio n. 2
0
    def test_overspend(self):
        self.set_up()
        self.test_create_channel()
        self.get_all_events()
        data = []
        amount = 142871500000000000  # obviously much more that we've sent initially
        address = int(addresses[1], 0)
        data.append(D160 * amount + address)

        # Trying to close the channel with an overspend
        balance_msg = self.channel.call().getBalanceMessage(
            self.sender, self.open_block, data)
        balance_msg_sig, _ = sign.check(
            balance_msg, binascii.unhexlify(self.sender_key[2:]))
        tx_hash = self.channel.transact({
            'from': self.sender
        }).close(self.sender, self.open_block, data, balance_msg_sig)
        check_successful_tx(self.web3, tx_hash, txn_wait)
        try:
            self.logger.info('Channel info: {}'.format(
                self.channel.call().getChannelInfo(self.sender,
                                                   self.open_block)))
        except:
            self.logger.warning('Channel info failed in overspend. ×')
        try:
            self.logger.warning('Closing request: {}'.format(
                self.channel.call().getClosingRequestInfo(
                    self.sender, self.open_block)))
        except:
            self.logger.info('Closing request info failed in overspend. ✓')

        # Checking if we've overspent
        self.logger.info(
            self.channel.call({
                'from': self.sender
            }).checkOverspend(self.key, data))

        # Reporting cheating
        right_data = []
        amount = 1428715
        address = int(addresses[1], 0)
        right_data.append(D160 * amount + address)
        right_balance_msg = self.channel.call().getBalanceMessage(
            self.sender, self.open_block, right_data)
        right_balance_msg_sig, _ = sign.check(
            right_balance_msg, binascii.unhexlify(self.sender_key[2:]))
        tx_hash = self.channel.transact({
            'from': self.web3.eth.accounts[1]
        }).reportCheating(self.sender, self.open_block, right_data,
                          right_balance_msg_sig, data, balance_msg_sig)
        check_successful_tx(self.web3, tx_hash, txn_wait)
        try:
            self.logger.info('Closing request: {}'.format(
                self.channel.call().getClosingRequestInfo(
                    self.sender, self.open_block)))
        except:
            self.logger.warning('Closing request info failed in close. ×')
Esempio n. 3
0
 def test_withdraw(self):
     self.logger.info('Balance before: {}'.format(
         self.token.call({
             'from': self.web3.eth.accounts[1]
         }).balanceOf(self.web3.eth.accounts[1])))
     tx_hash = self.channel.transact({
         'from': self.web3.eth.accounts[1]
     }).withdraw()
     check_successful_tx(self.web3, tx_hash, txn_wait)
     self.logger.info('Balance after: {}'.format(
         self.token.call({
             'from': self.web3.eth.accounts[1]
         }).balanceOf(self.web3.eth.accounts[1])))
Esempio n. 4
0
 def test_create_channel(self):
     self.logger.info('Creating channel')
     data_bytes = b'\x00\x00\x00\x00\x10'
     tx_hash = self.token.transact({'from': self.sender}) \
         .transfer(self.project_info['channels_address'], 1428715233280, data_bytes)
     # print(tx_hash)
     receipt = check_successful_tx(self.web3, tx_hash, txn_wait)
     self.open_block = receipt['blockNumber']
     self.logger.info('Channel should be created')
     tx_hash = self.channel.transact({'from': self.sender}) \
         .registerMaintainer(self.sender, self.open_block)
     check_successful_tx(self.web3, tx_hash, txn_wait)
     info = self.channel.call().getChannelInfo(self.sender, self.open_block)
     self.key = info[0]
     self.logger.info(info)
Esempio n. 5
0
def deploy_contracts():
    if os.path.isfile(FILE_PATH) and \
                    os.path.getmtime(FILE_PATH) > os.path.getmtime('../../contracts/MicroTransferChannels.sol'):
        return

    logging.info('Deploying new contracts')
    project = Project()
    with project.get_chain(CHAIN_NAME) as chain:
        web3 = chain.web3
        owner = web3.eth.accounts[0]

        token_factory = chain.provider.get_contract_factory('GEXToken')  # This will be the abi of the token
        tx_hash = token_factory.deploy(transaction={'from': owner})  # the way we deploy contracts
        receipt = check_successful_tx(chain.web3, tx_hash, txn_wait)
        token_address = receipt['contractAddress']
        logging.info('{} address is {}'.format(token_name, token_address))

        channel_factory = chain.provider.get_contract_factory('MicroTransferChannels')
        tx_hash = channel_factory.deploy(
            args=[token_address, challenge_period, channel_lifetime],
            transaction={'from': owner}
        )
        receipt = check_successful_tx(chain.web3, tx_hash, txn_wait)
        channels_address = receipt['contractAddress']
        logging.info('MicroTransferChannels address is {}'.format(channels_address))

        node_factory = chain.provider.get_contract_factory('NodeContract')
        tx_hash = node_factory.deploy(transaction={'from': owner})
        receipt = check_successful_tx(chain.web3, tx_hash, txn_wait)
        node_address = receipt['contractAddress']
        logging.info('NodeContract address is {}'.format(node_address))

        tx_hash = node_factory(node_address).transact({'from': owner}).addNode(owner, '10.1.0.56', 1337, 'public_key')
        check_successful_tx(chain.web3, tx_hash, txn_wait)

    write_to_file(
        channels_abi=channel_factory.abi,
        token_abi=token_factory.abi,
        node_abi=node_factory.abi,
        token_address=token_address,
        channels_address=channels_address,
        node_address=node_address
    )
    print('Deployed')
Esempio n. 6
0
 def test_send_new_transaction(self):
     data = []
     amount = 1528715
     address = int(self.web3.eth.accounts[1], 0)
     data.append(D160 * amount + address)
     balance_msg = self.channel.call().getBalanceMessage(
         self.sender, self.open_block, data)
     balance_msg_sig, _ = sign.check(
         balance_msg, binascii.unhexlify(self.sender_key[2:]))
     tx_hash = self.channel.transact({
         'from': self.sender
     }).submitLaterTransaction(self.sender, self.open_block, data,
                               balance_msg_sig)
     check_successful_tx(self.web3, tx_hash, txn_wait)
     try:
         self.logger.info('Closing request: {}'.format(
             self.channel.call().getClosingRequestInfo(
                 self.sender, self.open_block)))
     except:
         self.logger.warning(
             'Closing request info failed in send_new_transaction ×')
Esempio n. 7
0
 def test_top_up(self):
     data_bytes = b'\x01\x00\x00\x00\x00'
     data_int = int.from_bytes(data_bytes,
                               byteorder='big') + self.open_block
     data_bytes = (data_int).to_bytes(5, byteorder='big')
     assert len(data_bytes) == 5
     tx_hash = self.token.transact({'from': self.sender}) \
         .transfer(self.project_info['channels_address'], 1428715233280, data_bytes)
     receipt = check_successful_tx(self.web3, tx_hash, txn_wait)
     self.logger.info('Channel should be topped up')
     info = self.channel.call().getChannelInfo(self.sender, self.open_block)
     self.key = info[0]
     self.logger.info(info)
Esempio n. 8
0
 def test_ten_payee_close(self):
     self.set_up()
     self.get_all_events()
     self.test_create_channel()
     self.logger.info('Waiting for channel lifetime to end')
     time.sleep(15 * (channel_lifetime + 1))
     self.logger.info('Lifetime should have ended')
     data = generate_data([1000] * 10)
     balance_msg = self.channel.call().getBalanceMessage(
         self.sender, self.open_block, data)
     balance_msg_sig, _ = sign.check(
         balance_msg, binascii.unhexlify(self.sender_key[2:]))
     try:
         self.logger.info(
             self.channel.call({
                 'from': self.sender
             }).close(self.sender, self.open_block, data, balance_msg_sig))
     except:
         self.logger.warning('CLOSE REQUEST WILL FAIL')
     tx_hash = self.channel.transact({
         'from': self.sender
     }).close(self.sender, self.open_block, data, balance_msg_sig)
     check_successful_tx(self.web3, tx_hash, txn_wait)
     self.logger.info('BLOCK {}'.format(
         self.open_block))  # TODO write this to the data.json as well?
     try:
         self.logger.info('Channel info: {}'.format(
             self.channel.call().getChannelInfo(self.sender,
                                                self.open_block)))
     except:
         self.logger.warning('Channel info failed in close ×')
     try:
         self.logger.info('Closing request: {}'.format(
             self.channel.call().getClosingRequestInfo(
                 self.sender, self.open_block)))
     except:
         self.logger.warning('Closing request info failed in close ×')
     self.test_settle_channel()
Esempio n. 9
0
 def test_close_channel(self):
     self.logger.info('Waiting for channel lifetime to end')
     time.sleep(15 * (channel_lifetime + 1))
     self.logger.info('Lifetime should have ended')
     data = []
     amount = 1428715
     address = int(self.web3.eth.accounts[1], 0)
     data.append(D160 * amount + address)
     balance_msg = self.channel.call().getBalanceMessage(
         self.sender, self.open_block, data)
     balance_msg_sig, _ = sign.check(
         balance_msg, binascii.unhexlify(self.sender_key[2:]))
     try:
         self.logger.info(
             self.channel.call({
                 'from': self.sender
             }).close(self.sender, self.open_block, data, balance_msg_sig))
     except:
         self.logger.warning('CLOSE REQUEST WILL FAIL')
     tx_hash = self.channel.transact({
         'from': self.sender
     }).close(self.sender, self.open_block, data, balance_msg_sig)
     check_successful_tx(self.web3, tx_hash, txn_wait)
     self.logger.info('BLOCK {}'.format(
         self.open_block))  # TODO write this to the data.json as well?
     try:
         self.logger.info('Channel info: {}'.format(
             self.channel.call().getChannelInfo(self.sender,
                                                self.open_block)))
     except:
         self.logger.warning('Channel info failed in close ×')
     try:
         self.logger.info('Closing request: {}'.format(
             self.channel.call().getClosingRequestInfo(
                 self.sender, self.open_block)))
     except:
         self.logger.warning('Closing request info failed in close ×')
Esempio n. 10
0
 def cheating_template(self, case_n, good_data, bad_data):
     self.set_up()
     self.test_create_channel()
     self.logger.info('Case#: {}, Good data: {}'.format(case_n, good_data))
     bad_balance_msg = self.channel.call().getBalanceMessage(
         self.sender, self.open_block, bad_data)
     bad_balance_msg_sig, _ = sign.check(
         bad_balance_msg, binascii.unhexlify(self.sender_key[2:]))
     right_balance_msg = self.channel.call().getBalanceMessage(
         self.sender, self.open_block, good_data)
     right_balance_msg_sig, _ = sign.check(
         right_balance_msg, binascii.unhexlify(self.sender_key[2:]))
     tx_hash = self.channel.transact({
         'from': self.web3.eth.accounts[1]
     }).reportCheating(self.sender, self.open_block, good_data,
                       right_balance_msg_sig, bad_data, bad_balance_msg_sig)
     check_successful_tx(self.web3, tx_hash, txn_wait)
     try:
         self.logger.info('Closing request: {}'.format(
             self.channel.call().getClosingRequestInfo(
                 self.sender, self.open_block)))
     except:
         self.logger.warning(
             'Closing request info failed in cheating {}. ×'.format(case_n))