Example #1
0
 def sign_close(self, sender, open_block_number, balance):
     """Sign an agreement for a channel closing."""
     assert is_checksum_address(sender)
     if (sender, open_block_number) not in self.channels:
         raise NoOpenChannel('Channel does not exist or has been closed'
                             '(sender=%s, open_block_number=%d)' %
                             (sender, open_block_number))
     c = self.channels[sender, open_block_number]
     if c.is_closed:
         raise NoOpenChannel('Channel closing has been requested already.')
     assert balance is not None
     if c.last_signature is None:
         raise NoBalanceProofReceived('Payment has not been registered.')
     if balance != c.balance:
         raise InvalidBalanceProof(
             'Requested closing balance does not match latest one.')
     c.is_closed = True
     c.mtime = time.time()
     receiver_sig = sign_close(self.private_key, sender, open_block_number,
                               c.balance,
                               self.channel_manager_contract.address)
     self.state.set_channel(c)
     self.log.info(
         'signed cooperative closing message (sender %s, block number %s)',
         sender, open_block_number)
     return receiver_sig
    def close_channel(self, sender, open_block_number):
        """Close and settle a channel."""
        if not (sender, open_block_number) in self.channels:
            self.log.warn(
                "attempt to close a non-registered channel (sender=%s open_block=%s"
                % (sender, open_block_number))
            return
        c = self.channels[sender, open_block_number]
        if c.last_signature is None:
            raise NoBalanceProofReceived(
                'Cannot close a channel without a balance proof.')
        # send closing tx
        tx_params = [
            self.state.receiver, open_block_number, c.balance,
            decode_hex(c.last_signature)
        ]
        raw_tx = self.contract_proxy.create_signed_transaction(
            'close', tx_params)

        txid = self.blockchain.web3.eth.sendRawTransaction(raw_tx)
        self.log.info('sent channel close(sender %s, block number %s, tx %s)',
                      sender, open_block_number, txid)
        # update local state
        c.is_closed = True
        c.mtime = time.time()
        self.state.store()
Example #3
0
    def close_channel(self, sender, open_block_number):
        """Close and settle a channel."""
        if not (sender, open_block_number) in self.channels:
            self.log.warning(
                "attempt to close a non-registered channel (sender=%s open_block=%s"
                % (sender, open_block_number))
            return
        c = self.channels[sender, open_block_number]
        if c.last_signature is None:
            raise NoBalanceProofReceived(
                'Cannot close a channel without a balance proof.')
        # send closing tx
        raw_tx = create_signed_contract_transaction(
            self.private_key, self.channel_manager_contract,
            'uncooperativeClose', [
                self.state.receiver, open_block_number, c.balance,
                decode_hex(c.last_signature)
            ])

        # update local state
        c.is_closed = True
        c.mtime = time.time()
        self.state.set_channel(c)

        try:
            txid = self.blockchain.web3.eth.sendRawTransaction(raw_tx)
            self.log.info(
                'sent channel close(sender %s, block number %s, tx %s)',
                sender, open_block_number, txid)
        except InsufficientBalance:
            c.state = ChannelState.CLOSE_PENDING
            self.state.set_channel(c)
            raise