def test_store_block(): from bigchaindb.backend import connect, query from bigchaindb.tendermint.lib import Block conn = connect() block = Block(app_hash='random_utxo', height=3, transactions=[]) query.store_block(conn, block._asdict()) cursor = conn.db.blocks.find({}, projection={'_id': False}) assert cursor.count() == 1
def test_get_block(): from bigchaindb.backend import connect, query from bigchaindb.tendermint.lib import Block conn = connect() block = Block(app_hash='random_utxo', height=3, transactions=[]) conn.db.blocks.insert_one(block._asdict()) block = dict(query.get_block(conn, 3)) assert block['height'] == 3
def commit(self): """Store the new height and along with block hash.""" # register a new block only when new transactions are received if self.block_txn_ids: self.bigchaindb.store_bulk_transactions(self.block_transactions) block = Block(app_hash=self.block_txn_hash, height=self.new_height, transactions=self.block_txn_ids) self.bigchaindb.store_block(block._asdict()) data = self.block_txn_hash.encode('utf-8') return Result.ok(data=data)
def test_get_block_containing_transaction(tb, client): b = tb tx = Transaction.create([b.me], [([b.me], 1)], asset={'cycle': 'hero'}) tx = tx.sign([b.me_private]) b.store_transaction(tx) block = Block(app_hash='random_utxo', height=13, transactions=[tx.id]) b.store_block(block._asdict()) res = client.get('{}?transaction_id={}'.format(BLOCKS_ENDPOINT, tx.id)) expected_response = [block.height] assert res.json == expected_response assert res.status_code == 200
def inputs(user_pk, b, alice): from bigchaindb.models import Transaction # create blocks with transactions for `USER` to spend for block in range(4): transactions = [ Transaction.create( [alice_pubkey(alice)], [([user_pk], 1)], metadata={'msg': random.random()}, ).sign([alice_privkey(alice)]).to_dict() for _ in range(10) ] block = Block(app_hash='', height=_get_height(b), transactions=transactions) b.store_block(block._asdict())
def test_delete_latest_block(signed_create_tx, signed_transfer_tx): from bigchaindb.backend import connect, query from bigchaindb.tendermint.lib import Block conn = connect() conn.db.transactions.insert_one(signed_create_tx.to_dict()) query.store_asset(conn, {'id': signed_create_tx.id}) block = Block(app_hash='random_utxo', height=51, transactions=[signed_create_tx.id]) query.store_block(conn, block._asdict()) query.delete_latest_block(conn) assert query.get_transaction(conn, signed_create_tx.id) is None assert query.get_asset(conn, signed_create_tx.id) is None assert query.get_block(conn, 51) is None
def commit(self): """Store the new height and along with block hash.""" data = self.block_txn_hash.encode('utf-8') # register a new block only when new transactions are received if self.block_txn_ids: self.bigchaindb.store_bulk_transactions(self.block_transactions) block = Block(app_hash=self.block_txn_hash, height=self.new_height, transactions=self.block_txn_ids) # NOTE: storing the block should be the last operation during commit # this effects crash recovery. Refer BEP#8 for details self.bigchaindb.store_block(block._asdict()) return Result.ok(data=data)
def test_get_block_endpoint(tb, client): b = tb tx = Transaction.create([b.me], [([b.me], 1)], asset={'cycle': 'hero'}) tx = tx.sign([b.me_private]) b.store_transaction(tx) block = Block(app_hash='random_utxo', height=31, transactions=[tx.id]) b.store_block(block._asdict()) res = client.get(BLOCKS_ENDPOINT + str(block.height)) expected_response = { 'height': block.height, 'transactions': [tx.to_dict()] } assert res.json == expected_response assert res.status_code == 200
def inputs_shared(user_pk, user2_pk): from bigchaindb.models import Transaction # create blocks with transactions for `USER` to spend for block in range(4): transactions = [ Transaction.create( [b.me], [user_pk, user2_pk], metadata={ 'msg': random.random() }, ).sign([b.me_private]).to_dict() for _ in range(10) ] block = Block(app_hash='', height=_get_height(b), transaction=transactions) b.store_block(block._asdict())
def commit(self): """Store the new height and along with block hash.""" data = self.block_txn_hash.encode('utf-8') # register a new block only when new transactions are received if self.block_txn_ids: self.bigchaindb.store_bulk_transactions(self.block_transactions) block = Block(app_hash=self.block_txn_hash, height=self.new_height, transactions=self.block_txn_ids) # NOTE: storing the block should be the last operation during commit # this effects crash recovery. Refer BEP#8 for details self.bigchaindb.store_block(block._asdict()) logger.debug( 'Commit-ing new block with hash: apphash=%s ,' 'height=%s, txn ids=%s', data, self.new_height, self.block_txn_ids) logger.benchmark('COMMIT_BLOCK, height:%s', self.new_height) return data
def init_chain(self, validators): """Initialize chain with block of height 0""" block = Block(app_hash='', height=0, transactions=[]) self.bigchaindb.store_block(block._asdict())