def _process_new_blocks(self, last_block: BlockNumber) -> None:
        # BCL return a new state and events related to channel lifecycle
        new_chain_state, events = get_blockchain_events(
            web3=self.web3,
            contract_manager=CONTRACT_MANAGER,
            chain_state=self.context.ms_state.blockchain_state,
            to_block=last_block,
        )

        # If a new token network was found we need to write it to the DB, otherwise
        # the constraints for new channels will not be constrained. But only update
        # the network addresses here, all else is done later.
        token_networks_changed = (
            self.context.ms_state.blockchain_state.token_network_addresses !=
            new_chain_state.token_network_addresses)
        if token_networks_changed:
            self.context.ms_state.blockchain_state.token_network_addresses = (
                new_chain_state.token_network_addresses)
            self.context.db.update_blockchain_state(
                self.context.ms_state.blockchain_state)

        # Now set the updated chain state to the context, will be stored later
        self.context.ms_state.blockchain_state = new_chain_state
        for event in events:
            handle_event(event, self.context)

        # check triggered events and trigger the correct ones
        triggered_events = self.context.db.get_scheduled_events(
            max_trigger_block=last_block)
        for scheduled_event in triggered_events:
            event = scheduled_event.event

            handle_event(event, self.context)
            self.context.db.remove_scheduled_event(scheduled_event)
Exemple #2
0
    def _process_new_blocks(self, last_block: BlockNumber) -> None:
        self.last_known_block = last_block

        # BCL return a new state and events related to channel lifecycle
        new_chain_state, events = get_blockchain_events(
            web3=self.web3,
            contract_manager=CONTRACT_MANAGER,
            chain_state=self.blockchain_state,
            to_block=last_block,
            query_ms=False,
        )

        # If a new token network was found we need to write it to the DB, otherwise
        # the constraints for new channels will not be constrained. But only update
        # the network addresses here, all else is done later.
        token_networks_changed = (
            self.blockchain_state.token_network_addresses
            != new_chain_state.token_network_addresses
        )
        if token_networks_changed:
            self.blockchain_state.token_network_addresses = new_chain_state.token_network_addresses
        #     self.context.db.update_state(self.context.ms_state)

        # Now set the updated chain state to the context, will be stored later
        self.blockchain_state = new_chain_state
        for event in events:
            self.handle_channel_event(event)

        self.blockchain_state.latest_known_block = last_block
Exemple #3
0
    def _process_new_blocks(self, last_block: BlockNumber) -> None:
        self.context.last_known_block = last_block

        # BCL return a new state and events related to channel lifecycle
        new_chain_state, events = get_blockchain_events(
            web3=self.web3,
            contract_manager=CONTRACT_MANAGER,
            chain_state=self.context.ms_state.blockchain_state,
            to_block=last_block,
        )

        # If a new token network was found we need to write it to the DB, otherwise
        # the constraints for new channels will not be constrained. But only update
        # the network addresses here, all else is done later.
        token_networks_changed = (
            self.context.ms_state.blockchain_state.token_network_addresses
            != new_chain_state.token_network_addresses
        )
        if token_networks_changed:
            self.context.ms_state.blockchain_state.token_network_addresses = (
                new_chain_state.token_network_addresses
            )
            self.context.db.update_blockchain_state(self.context.ms_state.blockchain_state)

        # Now set the updated chain state to the context, will be stored later
        self.context.ms_state.blockchain_state = new_chain_state
        for event in events:
            handle_event(event, self.context)

        # check triggered events and trigger the correct ones
        triggered_events = self.context.db.get_scheduled_events(max_trigger_block=last_block)
        for scheduled_event in triggered_events:
            event = scheduled_event.event

            handle_event(event, self.context)
            self.context.db.remove_scheduled_event(scheduled_event)

        # check pending transactions
        # this is done here so we don't have to block waiting for receipts in the state machine
        for tx_hash in self.context.db.get_waiting_transactions():
            receipt = self.web3.eth.getTransactionReceipt(tx_hash)

            if receipt is not None:
                self.context.db.remove_waiting_transaction(tx_hash)

                if receipt["status"] == 1:
                    log.info(
                        "Transaction was mined successfully",
                        transaction_hash=tx_hash,
                        receipt=receipt,
                    )
                else:
                    log.error(
                        "Transaction was not mined successfully",
                        transaction_hash=tx_hash,
                        receipt=receipt,
                    )
Exemple #4
0
    def _process_new_blocks(self, last_block: BlockNumber) -> None:
        self.blockchain_state.latest_known_block = self.database.get_latest_known_block(
        )
        self.blockchain_state.token_network_addresses = list(
            self.token_networks.keys())

        _, events = get_blockchain_events(
            web3=self.web3,
            contract_manager=CONTRACT_MANAGER,
            chain_state=self.blockchain_state,
            to_block=last_block,
        )
        for event in events:
            self.handle_event(event)
Exemple #5
0
def test_get_blockchain_events_returns_early_for_invalid_interval(
        web3: Web3, token_network_registry_contract: Contract):
    events = get_blockchain_events(
        web3=web3,
        token_network_addresses=[],
        chain_state=BlockchainState(
            chain_id=ChainID(1),
            token_network_registry_address=to_canonical_address(
                token_network_registry_contract.address),
            latest_committed_block=BlockNumber(4),
        ),
        from_block=BlockNumber(10),
        to_block=BlockNumber(5),
    )

    assert len(events) == 0
Exemple #6
0
 def _process_new_blocks(self, last_block: BlockNumber) -> None:
     _, events = get_blockchain_events(
         web3=self.web3,
         contract_manager=CONTRACT_MANAGER,
         chain_state=BlockchainState(
             latest_known_block=self.database.get_latest_known_block(),
             token_network_addresses=list(self.token_networks.keys()),
             token_network_registry_address=self.registry_address,
             monitor_contract_address=Address(""),  # FIXME
             chain_id=self.chain_id,
         ),
         to_block=last_block,
         query_ms=False,
     )
     for event in events:
         self.handle_event(event)