def score_invoke(self, _block: Block) -> dict or None:
        if conf.USE_EXTERNAL_SCORE:
            method = "icx_sendTransaction"
            transactions = []
            for tx in _block.confirmed_transaction_list:
                data = tx.icx_origin_data
                transaction = {"method": method, "params": data}
                transactions.append(transaction)

            request = {
                'block': {
                    'blockHeight': _block.height,
                    'blockHash': _block.block_hash,
                    'prevBlockHash': _block.prev_block_hash,
                    'timestamp': _block.time_stamp
                },
                'transactions': transactions
            }
            request = convert_params(request, ParamType.invoke)
            stub = StubCollection().icon_score_stubs[ChannelProperty().name]
            response = stub.sync_task().invoke(request)
            response_to_json_query(response)
            _block.commit_state[
                ChannelProperty().name] = response['stateRootHash']
            return response["txResults"]
        else:
            stub = StubCollection().score_stubs[ChannelProperty().name]
            response = stub.sync_task().score_invoke(_block)

            if response.code == message_code.Response.success:
                commit_state = pickle.loads(response.object)
                _block.commit_state = commit_state
                return json.loads(response.meta)

        return None
Exemple #2
0
    def test_block_hash_must_be_the_same_regardless_of_the_commit_state(self):
        # ENGINE-302
        # GIVEN
        block1 = Block(channel_name=conf.LOOPCHAIN_DEFAULT_CHANNEL)
        block2 = Block(channel_name=conf.LOOPCHAIN_DEFAULT_CHANNEL)

        # WHEN
        block1.commit_state = {"TEST": "TEST_VALUE1234"}
        block1.generate_block()
        block2.generate_block()
        util.logger.spam(f"block1 hash({block1.block_hash})")
        util.logger.spam(f"block1 hash({block2.block_hash})")

        # THEN
        self.assertEqual(block1.block_hash, block2.block_hash)
    async def announce_confirmed_block(self,
                                       serialized_block,
                                       commit_state="{}"):
        try:
            confirmed_block = Block(channel_name=ChannelProperty().name)
            confirmed_block.deserialize_block(serialized_block)
            util.logger.spam(
                f"channel_inner_service:announce_confirmed_block\n "
                f"hash({confirmed_block.block_hash}), block_type({confirmed_block.block_type}), "
                f"block height({confirmed_block.height}), "
                f"commit_state({commit_state})")
            try:
                confirmed_block.commit_state = ast.literal_eval(commit_state)
            except Exception as e:
                logging.warning(
                    f"channel_inner_service:announce_confirmed_block FAIL get commit_state_dict, "
                    f"error by : {e}")

            if self._channel_service.block_manager.get_blockchain(
            ).block_height < confirmed_block.height:
                self._channel_service.block_manager.add_confirmed_block(
                    confirmed_block)
            else:
                logging.debug(
                    f"channel_inner_service:announce_confirmed_block "
                    f"already synced block height({confirmed_block.height})")
            response_code = message_code.Response.success
            # stop subscribe timer
            if TimerService.TIMER_KEY_SUBSCRIBE in self._channel_service.timer_service.timer_list.keys(
            ):
                self._channel_service.timer_service.stop_timer(
                    TimerService.TIMER_KEY_SUBSCRIBE)
        except Exception as e:
            logging.error(f"announce confirmed block error : {e}")
            response_code = message_code.Response.fail
        return response_code