Esempio n. 1
0
    def newFilter(self, filter_dict):
        if not isinstance(filter_dict, dict):
            raise BadRequestError('Filter must be an object')
        b0 = self.json_rpc_server.get_block(
            block_id_decoder(filter_dict.get('fromBlock', 'latest')))
        b1 = self.json_rpc_server.get_block(
            block_id_decoder(filter_dict.get('toBlock', 'latest')))
        if b1.number < b0.number:
            raise BadRequestError('fromBlock must be prior or equal to toBlock')
        address = filter_dict.get('address', None)
        if is_string(address):
            addresses = [address_decoder(address)]
        elif isinstance(address, Iterable):
            addresses = [address_decoder(addr) for addr in address]
        elif address is None:
            addresses = None
        else:
            raise JSONRPCInvalidParamsError('Parameter must be address or list of addresses')
        if 'topics' in filter_dict:
            topics = []
            for topic in filter_dict['topics']:
                if topic is not None:
                    topics.append(big_endian_to_int(data_decoder(topic)))
                else:
                    topics.append(None)
        else:
            topics = None

        blocks = [b1]
        while blocks[-1] != b0:
            blocks.append(blocks[-1].get_parent())
        filter_ = Filter(self.chain.chain, reversed(blocks), addresses, topics)
        self.filters[self.next_id] = filter_
        self.next_id += 1
        return self.next_id - 1
Esempio n. 2
0
    def newFilter(self, filter_dict):
        if not isinstance(filter_dict, dict):
            raise BadRequestError('Filter must be an object')
        required_keys = set(['fromBlock', 'toBlock'])
        if not required_keys.issubset(set(filter_dict.keys())):
            raise BadRequestError('Invalid filter object')

        b0 = self.json_rpc_server.get_block(block_id_decoder(filter_dict['fromBlock']))
        b1 = self.json_rpc_server.get_block(block_id_decoder(filter_dict['toBlock']))
        if b1.number < b0.number:
            raise BadRequestError('fromBlock must be prior or equal to toBlock')
        address = filter_dict.get('address', None)
        if is_string(address):
            addresses = [address_decoder(address)]
        elif isinstance(address, Iterable):
            addresses = [address_decoder(addr) for addr in address]
        elif address is None:
            addresses = None
        else:
            raise JSONRPCInvalidParamsError('Parameter must be address or list of addresses')
        topics = [data_decoder(topic) for topic in filter_dict.get('topics', [])]

        blocks = [b1]
        while blocks[-1] != b1:
            blocks.append(blocks[-1].get_parent())
        filter_ = Filter(self.chain.chain, reversed(blocks), addresses, topics)
        self.filters[self.next_id] = filter_
        self.next_id += 1
        return self.next_id - 1
Esempio n. 3
0
    def newFilter(self, filter_dict):
        log.debug('in newFilter', filter_dict=filter_dict)
        if not isinstance(filter_dict, dict):
            raise BadRequestError('Filter must be an object')
        address = filter_dict.get('address', None)
        if is_string(address):
            addresses = [address_decoder(address)]
        elif isinstance(address, Iterable):
            addresses = [address_decoder(addr) for addr in address]
        elif address is None:
            addresses = None
        else:
            raise JSONRPCInvalidParamsError('Parameter must be address or list of addresses')
        if 'topics' in filter_dict:
            topics = []
            for topic in filter_dict['topics']:
                if topic is not None:
                    log.debug('with topic', topic=topic)
                    log.debug('decoded', topic=data_decoder(topic))
                    log.debug('int', topic=big_endian_to_int(data_decoder(topic)))
                    topics.append(big_endian_to_int(data_decoder(topic)))
                else:
                    topics.append(None)
        else:
            topics = None


        fromBlock = filter_dict.get('fromBlock')
        toBlock = filter_dict.get('toBlock') or 'latest'

        if toBlock in ('latest', 'pending'):
            assert fromBlock in (None, toBlock) , 'latest/pending does not support ranges'
            filter_ = Filter(self.chain.chain, blocks=[], addresses=addresses, topics=topics,
                pending=bool(toBlock==b'pending'), latest=bool(toBlock==b'latest'))
        else:
            assert fromBlock not in ('latest', 'pending')
            b0 = self.json_rpc_server.get_block(block_id_decoder(fromBlock))
            b1 = self.json_rpc_server.get_block(block_id_decoder(toBlock))
            if b1.number < b0.number:
                raise BadRequestError('fromBlock must be prior or equal to toBlock')
            blocks = [b1]
            while blocks[-1] != b0:
                blocks.append(blocks[-1].get_parent())
            filter_ = Filter(self.chain.chain, blocks=list(
                reversed(blocks)), addresses=addresses, topics=topics)
        self.filters[self.next_id] = filter_
        self.next_id += 1
        return self.next_id - 1
Esempio n. 4
0
 def get_chain(self, start='', count=10):
     "return 'count' blocks starting from head or start"
     log.debug("get_chain", start=encode_hex(start), count=count)
     blocks = []
     block = self.head
     if start:
         if start not in self.index.db:
             return []
         block = self.get(start)
         if not self.in_main_branch(block):
             return []
     for i in range(count):
         blocks.append(block)
         if block.is_genesis():
             break
         block = block.get_parent()
     return blocks
Esempio n. 5
0
 def get_chain(self, start='', count=10):
     "return 'count' blocks starting from head or start"
     log.debug("get_chain", start=encode_hex(start), count=count)
     blocks = []
     block = self.head
     if start:
         if start not in self.index.db:
             return []
         block = self.get(start)
         if not self.in_main_branch(block):
             return []
     for i in range(count):
         blocks.append(block)
         if block.is_genesis():
             break
         block = block.get_parent()
     return blocks
Esempio n. 6
0
    def newFilter(self, filter_dict):
        if not isinstance(filter_dict, dict):
            raise BadRequestError('Filter must be an object')
        b0 = self.json_rpc_server.get_block(
            block_id_decoder(filter_dict.get('fromBlock', 'latest')))
        b1 = self.json_rpc_server.get_block(
            block_id_decoder(filter_dict.get('toBlock', 'latest')))
        if b1.number < b0.number:
            raise BadRequestError(
                'fromBlock must be prior or equal to toBlock')
        address = filter_dict.get('address', None)
        if is_string(address):
            addresses = [address_decoder(address)]
        elif isinstance(address, Iterable):
            addresses = [address_decoder(addr) for addr in address]
        elif address is None:
            addresses = None
        else:
            raise JSONRPCInvalidParamsError(
                'Parameter must be address or list of addresses')
        if 'topics' in filter_dict:
            topics = []
            for topic in filter_dict['topics']:
                if topic is not None:
                    topics.append(big_endian_to_int(data_decoder(topic)))
                else:
                    topics.append(None)
        else:
            topics = None

        blocks = [b1]
        while blocks[-1] != b0:
            blocks.append(blocks[-1].get_parent())
        filter_ = Filter(self.chain.chain, reversed(blocks), addresses, topics)
        self.filters[self.next_id] = filter_
        self.next_id += 1
        return self.next_id - 1