コード例 #1
0
    def put_block(self, block):
        if block.hash in self.block_cache:
            raise KeyError(
                "[put_block] Block with hash %s already exists in the database"
                % block.hash)

        if self.cache:
            # Write the block to cache
            self.block_cache[block.hash] = BlockSerializer.to_database(block)

            if len(self.block_cache) >= self.cache_size:
                self.flush_cache()
        else:
            self.blocks.insert_one(BlockSerializer.to_database(block))
コード例 #2
0
ファイル: api.py プロジェクト: stassen82/block-exploder
def get_block_by_hash(block_hash):
    if not validate_sha256_hash(block_hash):
        return "Invalid block hash", 400
    try:
        return BlockSerializer.to_web(db.get_block_by_hash(block_hash))
    except KeyError:
        return "Block with given hash doesn't exist", 404
コード例 #3
0
ファイル: gateways.py プロジェクト: stassen82/block-exploder
    def flush_cache(self):
        if self.block_cache:
            self.blocks.insert_many([BlockSerializer.to_database(block) for block in self.block_cache.values()])

        if self.tr_cache:
            self.transactions.insert_many([TransactionSerializer.to_database(tr) for tr in self.tr_cache.values()])

        self.block_cache = {}
        self.tr_cache = {}
コード例 #4
0
    def setUp(self):
        self.db.blocks.insert_many(
            [BlockSerializer.to_database(block) for block in self.blocks])
        self.db.transactions.insert_many([
            TransactionSerializer.to_database(tr) for tr in self.transactions
        ])

        self.db_gateway = MongoDatabaseGateway(database=self.db,
                                               config=self.config)
コード例 #5
0
ファイル: utils.py プロジェクト: esrefozturk/poa
def broadcast_new_block(block):
    for miner in MINERS:
        if miner == IP:
            continue
        try:
            requests.post('http://{m}:8000/consensus/'.format(m=miner),
                          data=BlockSerializer(block).data)
        except:
            pass
コード例 #6
0
ファイル: api.py プロジェクト: stassen82/block-exploder
def get_block_by_height(height):
    # If we don't check whether height is integer
    # MongoDB will throw OverflowError
    # because MongoDB can only handle up to 8-byte ints
    if not isinstance(height, int):
        return "Block height too large", 400
    try:
        return BlockSerializer.to_web(db.get_block_by_height(height))
    except KeyError:
        return "Block with given height doesn't exist", 404
コード例 #7
0
    def setUp(self):
        self.db.blocks.insert_many(
            [BlockSerializer.to_database(block) for block in self.blocks])
        self.db.transactions.insert_many([
            TransactionSerializer.to_database(tr) for tr in self.transactions
        ])

        for tr in self.transactions:
            self.db.vin.insert_many(
                [VinSerializer.to_database(vin, tr.txid) for vin in tr.vin])
            list_of_lists = [
                VoutSerializer.to_database(vout, tr.txid, index)
                for (index, vout) in enumerate(tr.vout)
            ]
            self.db.vout.insert_many(
                [item for sublist in list_of_lists for item in sublist])

        self.db_gateway = MongoDatabaseGateway(database=self.db,
                                               cache=True,
                                               cache_size=5)
コード例 #8
0
ファイル: api.py プロジェクト: stassen82/block-exploder
def get_latest_blocks(limit, offset):
    if not isinstance(offset, int):
        return "Offset too large", 400
    blocks = db.get_latest_blocks(limit, offset)
    return [BlockSerializer.to_web(block) for block in blocks]