def test_casting_to_binary():
    bloom = BloomFilter()

    assert bin(bloom) == '0b0'

    bloom.add(b'value 1')
    bloom.add(b'value 2')

    assert bin(bloom) == (
        '0b1000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000001000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000001001000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000001000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000010000000000000000000000000000000000000'
        '00000000000000000')
Beispiel #2
0
    def setUp(self):
        self._icon_service_engine = generate_service_engine()
        self._icon_service_engine._icon_score_deploy_engine = \
            Mock(spec=IconScoreDeployEngine)

        self._icon_service_engine._icon_score_engine = \
            Mock(spec=IconScoreEngine)

        self._icon_service_engine._charge_transaction_fee = \
            Mock(return_value=(0, 0))

        step_counter_factory = IconScoreStepCounterFactory()
        step_counter_factory.get_step_cost = Mock(return_value=6000)
        self._icon_service_engine._step_counter_factory = step_counter_factory
        self._icon_service_engine._icon_pre_validator = \
            Mock(spec=IconPreValidator)

        self._mock_context = Mock(spec=IconScoreContext)
        self._mock_context.tx = Mock(spec=Transaction)
        self._mock_context.block = Mock(spec=Block)
        self._mock_context.readonly = False
        self._mock_context.event_logs = []
        self._mock_context.logs_bloom = BloomFilter()
        self._mock_context.traces = []
        self._mock_context.cumulative_step_used = Mock(spec=int)
        self._mock_context.cumulative_step_used.attach_mock(Mock(), "__add__")
        self._mock_context.step_counter = step_counter_factory.create(5000000)
        self._mock_context.current_address = Mock(spec=Address)
Beispiel #3
0
    def test_sample_result(self):
        from_ = Address.from_data(AddressPrefix.EOA, b'from')
        to_ = Address.from_data(AddressPrefix.CONTRACT, b'to')
        self._mock_context.tx = Transaction(os.urandom(32), 1234, from_, to_,
                                            0)
        self._mock_context.msg = Message(from_)

        IconScoreContext.engine.deploy.invoke = Mock()
        self._icon_service_engine._process_transaction = Mock(return_value=to_)

        tx_result = self._icon_service_engine._handle_icx_send_transaction(
            self._mock_context, {
                'from': from_,
                'to': to_
            })

        tx_result.score_address = to_
        tx_result.event_logs = [
            EventLog(
                Address.from_data(AddressPrefix.CONTRACT, b'addr_to'),
                [b'indexed',
                 Address.from_data(AddressPrefix.EOA, b'index')],
                [True, 1234, 'str', None, b'test'])
        ]
        tx_result.logs_bloom = BloomFilter()
        tx_result.logs_bloom.add(b'1')
        tx_result.logs_bloom.add(b'2')
        tx_result.logs_bloom.add(b'3')
        tx_result.block = Block(123,
                                hashlib.sha3_256(b'block').digest(), 1, None,
                                0)

        camel_dict = tx_result.to_dict(to_camel_case)

        self.assertIn('txHash', camel_dict)
        self.assertIn('blockHeight', camel_dict)
        self.assertIn('txIndex', camel_dict)
        self.assertIn('to', camel_dict)
        self.assertIn('scoreAddress', camel_dict)
        self.assertIn('stepUsed', camel_dict)
        self.assertIn('stepPrice', camel_dict)
        self.assertIn('eventLogs', camel_dict)
        self.assertIn('logsBloom', camel_dict)
        self.assertIn('status', camel_dict)
        self.assertEqual(1, len(camel_dict['eventLogs']))
        self.assertIn('scoreAddress', camel_dict['eventLogs'][0])
        self.assertIn('indexed', camel_dict['eventLogs'][0])
        self.assertIn('data', camel_dict['eventLogs'][0])
        self.assertEqual(256, len(camel_dict['logsBloom']))

        converted_result = TypeConverter.convert_type_reverse(camel_dict)
        self.assertFalse(converted_result['txHash'].startswith('0x'))
        self.assertTrue(converted_result['blockHeight'].startswith('0x'))
        self.assertTrue(converted_result['txIndex'].startswith('0x'))
        self.assertTrue(converted_result['to'].startswith('cx'))
        self.assertTrue(converted_result['scoreAddress'].startswith('cx'))
        self.assertTrue(converted_result['stepUsed'].startswith('0x'))
        self.assertTrue(converted_result['logsBloom'].startswith('0x'))
        self.assertTrue(converted_result['status'].startswith('0x'))
def test_bloom_filter_extend_method(log_entries):
    bloom = BloomFilter()

    for address, topics in log_entries:
        bloom.extend([address])
        bloom.extend(topics)

    check_bloom(bloom, log_entries)
def test_bloom_filter_add_method(log_entries):
    bloom = BloomFilter()

    for address, topics in log_entries:
        bloom.add(address)
        for topic in topics:
            bloom.add(topic)

    check_bloom(bloom, log_entries)
def test_casting_to_integer():
    bloom = BloomFilter()

    assert int(bloom) == 0

    bloom.add(b'value 1')
    bloom.add(b'value 2')

    assert int(
        bloom
    ) == 21710164295456076474617584992928400544833610601629804355184679244996915561410724725836732565769488746136311050148073673265964181449023170224217591735796544712419156303622933453295521514824262282891245002098040635984515316506723798044184739318854384899770043742390234112500473883275684478937626267252490137704059428574657614238265964572574441396059283455118115364729586499643146391191744523298143676480447917059831750842466827858731528231787130418055540865561637277942991003058176
Beispiel #7
0
def test_casting_to_integer():
    bloom = BloomFilter()

    assert int(bloom) == 0

    bloom.add(b'value 1')
    bloom.add(b'value 2')

    assert int(
        bloom
    ) == 3944947030189331945888046470784906245171399251674253421890911307070880754273777782025227273955532066346428157183861919152855317645418835483482967962240252952857754628365121144784192001997419273756295453946726208769629092975984643729129571930661032646249421007454383641782705799176256002506374622796611822848119642151539915419994976560620428097444674106905443161454585490253858703654494390107576530610849917073078768083510119535265208359501478408998313536528623559776266571916699099350169806925278534145817311643230845850981927611352983742062763555974677561326077180327181727032341773782404312126519432624394469376
Beispiel #8
0
    def test_sample_result(self):
        from_ = Address.from_data(AddressPrefix.EOA, b'from')
        to_ = Address.from_data(AddressPrefix.CONTRACT, b'to')
        self._mock_context.tx.index = 1234
        self._mock_context.tx.hash = hashlib.sha256(b'hash').digest()
        self._icon_service_engine._icon_score_deploy_engine.attach_mock(
            Mock(return_value=False), 'is_data_type_supported')

        tx_result = self._icon_service_engine._handle_icx_send_transaction(
            self._mock_context, {'from': from_, 'to': to_})

        tx_result.score_address = \
            Address.from_data(AddressPrefix.CONTRACT, b'score_address')
        tx_result.event_logs = [
            EventLog(
                Address.from_data(AddressPrefix.CONTRACT, b'addr_to'),
                [b'indexed', Address.from_data(AddressPrefix.EOA, b'index')],
                [True, 1234, 'str', None, b'test']
            )
        ]
        tx_result.logs_bloom = BloomFilter()
        tx_result.logs_bloom.add(b'1')
        tx_result.logs_bloom.add(b'2')
        tx_result.logs_bloom.add(b'3')
        tx_result.block = Block(123, hashlib.sha256(b'block').digest(), 1, None)

        camel_dict = tx_result.to_dict(to_camel_case)

        self.assertIn('txHash', camel_dict)
        self.assertIn('blockHeight', camel_dict)
        self.assertIn('txIndex', camel_dict)
        self.assertIn('to', camel_dict)
        self.assertIn('scoreAddress', camel_dict)
        self.assertIn('stepUsed', camel_dict)
        self.assertIn('stepPrice', camel_dict)
        self.assertIn('eventLogs', camel_dict)
        self.assertIn('logsBloom', camel_dict)
        self.assertIn('status', camel_dict)
        self.assertEqual(1, len(camel_dict['eventLogs']))
        self.assertIn('scoreAddress', camel_dict['eventLogs'][0])
        self.assertIn('indexed', camel_dict['eventLogs'][0])
        self.assertIn('data', camel_dict['eventLogs'][0])
        self.assertEqual(256, len(camel_dict['logsBloom']))

        converted_result = TypeConverter.convert_type_reverse(camel_dict)
        self.assertFalse(converted_result['txHash'].startswith('0x'))
        self.assertTrue(converted_result['blockHeight'].startswith('0x'))
        self.assertTrue(converted_result['txIndex'].startswith('0x'))
        self.assertTrue(converted_result['to'].startswith('cx'))
        self.assertTrue(converted_result['scoreAddress'].startswith('cx'))
        self.assertTrue(converted_result['stepUsed'].startswith('0x'))
        self.assertTrue(converted_result['logsBloom'].startswith('0x'))
        self.assertTrue(converted_result['status'].startswith('0x'))
def test_icon_bloom():
    b1 = BloomFilter()

    keys = ["func_name", "index1", "index2", "index3"]

    # make key from string
    i: int = 0
    for key in keys:
        # add to bloom filter as bytes type
        item = key + str(i)
        b1.add(item.encode())

    # backup bloom filter with int casting
    b1_int = int(b1)

    # load bloom filter from backup
    b2 = BloomFilter(b1_int)

    # check bloom filter has key value
    item = keys[0] + str(0)
    assert item.encode() in b2
Beispiel #10
0
    def _make_block_data(self, block_hash: str, tx: Union[list, dict], timestamp: int, invoke_response: dict):
        is_genesis = isinstance(tx, dict)
        tx_list = []
        tx_results = invoke_response['txResults']
        logs_bloom = BloomFilter()
        if is_genesis:
            tx_list.append(tx)
            tx_results = list(tx_results.values())
        else:
            tx_list = tx
            for tx_result in tx_results:
                if "logsBloom" in tx_result.keys():
                    logs_bloom = logs_bloom + int(tx_result['logsBloom'], 16)

        block_height = self.block.block_height + 1

        preps = [] if self._prep_manager.prep_list is None else self._prep_manager.prep_list
        preps = [prep['id'] for prep in preps]
        preps = [f'00{prep[2:]}'.encode() for prep in preps]
        prev_preps = [] if self._prep_manager.prev_preps is None else self._prep_manager.prev_preps
        prev_preps = [prep['id'] for prep in prev_preps]
        prev_preps = [f'00{prep[2:]}'.encode() for prep in prev_preps]

        transactions_hash = generate_hash(tx_list)
        receipts_hash = generate_hash(tx_results)
        pre_votes_hash = generate_hash(self._prep_manager.prev_votes, "icx_vote")
        reps_hash = generate_hash(prev_preps)
        next_reps_hash = generate_hash(preps)
        block = {
            "version": "tbears",
            "prevHash": self.block.prev_block_hash if not is_genesis else "",
            "transactionsHash": transactions_hash,
            "stateHash": invoke_response['stateRootHash'],
            "receiptsHash": receipts_hash,
            "repsHash": reps_hash,
            "nextRepsHash": next_reps_hash,
            "leaderVotesHash": '0'*64,
            "prevVotesHash": pre_votes_hash,
            "logsBloom": hex(logs_bloom.value),
            "timestamp": timestamp,
            "transactions": tx_list,
            "leaderVotes": [],
            "prevVotes": self._prep_manager.prev_votes,
            "hash": block_hash,
            "height": block_height,
            "leader": self._prep_manager.prev_generator,
            "signature": "tbears_block_manager_does_not_support_block_signature" if not is_genesis else "",
            "nextLeader": self._prep_manager.generator
        }
        return block
def test_combining_filters():
    b1 = BloomFilter()
    b2 = BloomFilter()

    b1.add(b'a')
    b1.add(b'b')
    b1.add(b'c')

    b2.add(b'd')
    b2.add(b'e')
    b2.add(b'f')

    b1.add(b'common')
    b2.add(b'common')

    assert b'a' in b1
    assert b'b' in b1
    assert b'c' in b1

    assert b'a' not in b2
    assert b'b' not in b2
    assert b'c' not in b2

    assert b'd' in b2
    assert b'e' in b2
    assert b'f' in b2

    assert b'd' not in b1
    assert b'e' not in b1
    assert b'f' not in b1

    assert b'common' in b1
    assert b'common' in b2

    b3 = b1 | b2

    assert b'a' in b3
    assert b'b' in b3
    assert b'c' in b3
    assert b'd' in b3
    assert b'e' in b3
    assert b'f' in b3
    assert b'common' in b3

    b4 = b1 + b2

    assert b'a' in b4
    assert b'b' in b4
    assert b'c' in b4
    assert b'd' in b4
    assert b'e' in b4
    assert b'f' in b4
    assert b'common' in b4

    b5 = BloomFilter(int(b1))
    b5 |= b2

    assert b'a' in b5
    assert b'b' in b5
    assert b'c' in b5
    assert b'd' in b5
    assert b'e' in b5
    assert b'f' in b5
    assert b'common' in b5

    b6 = BloomFilter(int(b1))
    b6 += b2

    assert b'a' in b6
    assert b'b' in b6
    assert b'c' in b6
    assert b'd' in b6
    assert b'e' in b6
    assert b'f' in b6
    assert b'common' in b6