Beispiel #1
0
    def __init__(self,
                 web3: Web3,
                 channel_manager_contract: Contract,
                 token_contract: Contract,
                 private_key: str,
                 state_filename: str = None,
                 n_confirmations=1) -> None:
        gevent.Greenlet.__init__(self)
        self.blockchain = Blockchain(web3,
                                     channel_manager_contract,
                                     self,
                                     n_confirmations=n_confirmations)
        self.receiver = privkey_to_addr(private_key)
        self.private_key = private_key
        self.channel_manager_contract = channel_manager_contract
        self.token_contract = token_contract
        self.n_confirmations = n_confirmations
        self.log = logging.getLogger('channel_manager')
        network_id = int(web3.version.network)
        assert is_same_address(privkey_to_addr(self.private_key),
                               self.receiver)

        # check contract version
        self.check_contract_version()

        if state_filename not in (
                None, ':memory:') and os.path.isfile(state_filename):
            self.state = ChannelManagerState.load(state_filename)
        else:
            self.state = ChannelManagerState(state_filename)
            self.state.setup_db(network_id, channel_manager_contract.address,
                                self.receiver)

        assert self.state is not None
        if state_filename not in (None, ':memory:'):
            self.lock_state = filelock.FileLock(state_filename + '.lock')
            try:
                self.lock_state.acquire(timeout=0)
            except filelock.Timeout:
                raise StateFileLocked(
                    "state file %s is locked by another process" %
                    state_filename)

        if network_id != self.state.network_id:
            raise NetworkIdMismatch(
                "Network id mismatch: state=%d, backend=%d" %
                (self.state.network_id, network_id))

        if not is_same_address(self.receiver, self.state.receiver):
            raise StateReceiverAddrMismatch(
                '%s != %s' % (self.receiver, self.state.receiver))
        if not is_same_address(self.state.contract_address,
                               channel_manager_contract.address):
            raise StateContractAddrMismatch('%s != %s' %
                                            (channel_manager_contract.address,
                                             self.state.contract_address))

        self.log.debug(
            'setting up channel manager, receiver=%s channel_contract=%s' %
            (self.receiver, channel_manager_contract.address))
    def __init__(self,
                 web3,
                 contract_proxy,
                 token_contract,
                 private_key: str,
                 state_filename=None,
                 n_confirmations=1) -> None:
        gevent.Greenlet.__init__(self)
        self.blockchain = Blockchain(web3,
                                     contract_proxy,
                                     self,
                                     n_confirmations=n_confirmations)
        self.receiver = privkey_to_addr(private_key)
        self.private_key = private_key
        self.contract_proxy = contract_proxy
        self.token_contract = token_contract
        self.n_confirmations = n_confirmations
        self.log = logging.getLogger('channel_manager')
        network_id = web3.version.network
        assert privkey_to_addr(self.private_key) == self.receiver.lower()

        channel_contract_address = contract_proxy.contract.address
        if state_filename is not None and os.path.isfile(state_filename):
            self.state = ChannelManagerState.load(state_filename)
        else:
            self.state = ChannelManagerState(channel_contract_address,
                                             self.receiver,
                                             network_id,
                                             filename=state_filename)

        assert self.state is not None
        if state_filename is not None:
            self.lock_state = filelock.FileLock(state_filename)
            try:
                self.lock_state.acquire(timeout=0)
            except filelock.Timeout:
                raise StateFileLocked(
                    "state file %s is locked by another process" %
                    state_filename)

        if network_id != self.state.network_id:
            raise NetworkIdMismatch(
                "Network id mismatch: state=%d, backend=%d" %
                (self.state.network_id, network_id))

        if not is_same_address(self.receiver, self.state.receiver):
            raise StateReceiverAddrMismatch(
                '%s != %s' % (self.receiver.lower(), self.state.receiver))
        if not is_same_address(self.state.contract_address,
                               channel_contract_address):
            raise StateContractAddrMismatch(
                '%s != %s' % (channel_contract_address.lower(),
                              self.state.contract_address.lower()))

        self.log.debug(
            'setting up channel manager, receiver=%s channel_contract=%s' %
            (self.receiver, channel_contract_address))
Beispiel #3
0
    def __init__(self,
                 web3: Web3,
                 channel_manager_contract: Contract,
                 receiver: str,
                 private_key: str,
                 state_filename: str = None,
                 n_confirmations=1) -> None:
        gevent.Greenlet.__init__(self)
        self.blockchain = Blockchain(web3,
                                     channel_manager_contract,
                                     self,
                                     n_confirmations=n_confirmations)
        pk_address = privkey_to_addr(private_key)
        bytecode = web3.eth.getCode(Web3.toChecksumAddress(receiver))

        if bytecode:
            # TODO check if contract.getOwner() return same address with private_key
            #  fn_hash = web3.sha3(text='getOwner()').hex()
            #  fn_hash = "63" + fn_hash[2:10] # 0x63 is PUSH4
            #  if fn_hash.encode('utf-8') in bytecode:
            #      log.warning("has getOwner()")
            #  else:
            #      log.warning("No getOwner()")
            log.info(
                "The channel's receiver {} is a contract, the close_signature can be signed by it's owner"
                .format(receiver))
            self.receiver = receiver
        else:
            if not is_same_address(pk_address, receiver):
                raise ReceiverAddrMismatchPrivateKey(
                    "Receiver address {} not match with the address({}) from provided private_key"
                    .format(receiver, pk_address))
            self.receiver = pk_address
        self.private_key = private_key
        self.channel_manager_contract = channel_manager_contract
        self.n_confirmations = n_confirmations
        self.log = logging.getLogger('channel_manager')
        network_id = int(web3.version.network)

        if state_filename not in (
                None, ':memory:') and os.path.isfile(state_filename):
            self.state = ChannelManagerState.load(state_filename)
        else:
            self.state = ChannelManagerState(state_filename)
            self.state.setup_db(network_id, channel_manager_contract.address,
                                self.receiver)

        assert self.state is not None
        if state_filename not in (None, ':memory:'):
            self.lock_state = filelock.FileLock(state_filename + '.lock')
            try:
                self.lock_state.acquire(timeout=0)
            except filelock.Timeout:
                raise StateFileLocked(
                    "state file %s is locked by another process" %
                    state_filename)

        if network_id != self.state.network_id:
            raise NetworkIdMismatch(
                "Network id mismatch: state=%d, backend=%d" %
                (self.state.network_id, network_id))

        if not is_same_address(self.receiver, self.state.receiver):
            raise StateReceiverAddrMismatch(
                '%s != %s' % (self.receiver, self.state.receiver))
        if not is_same_address(self.state.contract_address,
                               channel_manager_contract.address):
            raise StateContractAddrMismatch('%s != %s' %
                                            (channel_manager_contract.address,
                                             self.state.contract_address))

        self.log.debug(
            'setting up channel manager, receiver=%s channel_contract=%s' %
            (self.receiver, channel_manager_contract.address))