Esempio n. 1
0
    async def getNewestBlocks(self, num_to_return = 10, start_idx = 0, after_hash = b'', chain_address = b'', include_transactions: bool = False):
        '''
        Returns list of block dicts
        :param start_idx:
        :param end_idx:
        :param chain_address:
        :return:
        '''
        # block = chain.get_block_by_hash(block_hash)
        # return block_to_dict(block, include_transactions, chain)
        if num_to_return is None:
            num_to_return = 10
        if start_idx is None:
            start_idx = 0
        num_to_return = min([10, num_to_return])
        block_dicts_to_return = []

        if chain_address != b'' and chain_address is not None:
            chain = self.get_new_chain(chain_address)
            try:
                canonical_header = chain.chaindb.get_canonical_head(chain_address)
                start = canonical_header.block_number-start_idx
                if start >= 0:
                    end = max([-1, start-num_to_return])
                    for i in range(start, end, -1):
                        block = chain.get_block_by_number(i, chain_address)
                        if block.hash == after_hash:
                            break
                        block_dicts_to_return.append(block_to_dict(block, include_transactions, chain))
                    
            except CanonicalHeadNotFound:
                return []
        else:
            chain = self.get_new_chain()
            at_block_index = -1
            current_window = int(time.time() / TIME_BETWEEN_HEAD_HASH_SAVE) * TIME_BETWEEN_HEAD_HASH_SAVE
            for timestamp in range(current_window, current_window-(NUMBER_OF_HEAD_HASH_TO_SAVE*TIME_BETWEEN_HEAD_HASH_SAVE), -1*TIME_BETWEEN_HEAD_HASH_SAVE):
                chronological_blocks = chain.chain_head_db.load_chronological_block_window(Timestamp(timestamp))
                if chronological_blocks is None:
                    continue
                chronological_blocks.reverse()

                for block_timestamp_block_hash in chronological_blocks:
                    at_block_index += 1
                    if at_block_index < start_idx:
                        continue

                    block = chain.get_block_by_hash(block_timestamp_block_hash[1])
                    if block.hash == after_hash:
                        return block_dicts_to_return

                    block_dicts_to_return.append(block_to_dict(block, include_transactions, chain))

                    if len(block_dicts_to_return) >= num_to_return:
                        return block_dicts_to_return

            
        return block_dicts_to_return
Esempio n. 2
0
 async def getBlockByNumber(self,
                            at_block,
                            chain_address,
                            include_transactions: bool = False):
     chain = self.get_new_chain(chain_address)
     block = chain.get_block_by_number(at_block,
                                       chain_address=chain_address)
     return block_to_dict(block, include_transactions, chain)
Esempio n. 3
0
def print_blockchain_database(base_db):
    node_1 = TestnetChain(base_db, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address(), TESTNET_GENESIS_PRIVATE_KEY)

    chain_head_hashes = node_1.chain_head_db.get_head_block_hashes_list()

    i = 0
    for head_hash in chain_head_hashes:
        print("Chain number {}".format(i))
        chain = node_1.get_all_blocks_on_chain_by_head_block_hash(head_hash)

        j = 0
        for block in chain:
            print("Block number {}".format(i))
            print(block_to_dict(block, True, node_1))
            j += 1
        i += 1
Esempio n. 4
0
    async def getBlockchainDatabase(self):
        chain_object = self.get_new_chain()

        chain_head_hashes = chain_object.chain_head_db.get_head_block_hashes_list(
        )

        chains_dict = []
        for head_hash in chain_head_hashes:
            chain = chain_object.get_all_blocks_on_chain_by_head_block_hash(
                head_hash)

            blocks_dict = []
            for block in chain:
                blocks_dict.append(block_to_dict(block, True, chain_object))

            chains_dict.append(blocks_dict)

        return chains_dict
Esempio n. 5
0
 async def getBlockByHash(self,
                          block_hash: Hash32,
                          include_transactions: bool = False):
     chain = self.get_new_chain()
     block = chain.get_block_by_hash(block_hash)
     return block_to_dict(block, include_transactions, chain)
Esempio n. 6
0
 async def getBlockByNumber(
         self, at_block: Union[str, int],
         include_transactions: bool) -> Dict[str, Union[str, List[str]]]:
     block = await get_block_at_number(self._chain, at_block)
     return block_to_dict(block, self._chain, include_transactions)
Esempio n. 7
0
 async def getBlockByHash(
         self, block_hash: Hash32,
         include_transactions: bool) -> Dict[str, Union[str, List[str]]]:
     block = await self._chain.coro_get_block_by_hash(block_hash)
     return block_to_dict(block, self._chain, include_transactions)
log_level = getattr(logging, 'DEBUG')
#log_level = getattr(logging, 'INFO')
logger, log_queue, listener = setup_helios_logging(log_level)
logger.propagate = False
#logger.info(HELIOS_HEADER)
from helios.rpc.format import block_to_dict

print("Instance 1 wallet address = {}".format(
    RECEIVER.public_key.to_address()))
base_db = LevelDB('/home/tommy/.local/share/helios/mainnet/chain/full')
#base_db = LevelDB('/WWW/.local/share/helios/mainnet/chain/full')

node_1 = MainnetChain(base_db, RECEIVER.public_key.to_canonical_address(),
                      RECEIVER)

queue_block = node_1.queue_block

chain_head_hashes = node_1.chain_head_db.get_head_block_hashes_list()

i = 0
for head_hash in chain_head_hashes:
    print("Chain number {}".format(i))
    chain = node_1.get_all_blocks_on_chain_by_head_block_hash(head_hash)

    j = 0
    for block in chain:
        print("Block number {}".format(i))
        print(block_to_dict(block, False, node_1))
        j += 1
    i += 1