def test_bigchain_class_initialization_with_parameters(default_config): from bigchaindb.core import Bigchain bigchain = Bigchain() assert bigchain.consensus != PLUGIN_CLASS bigchain = Bigchain(**{'consensus_plugin': PLUGIN_NAME}) assert bigchain.consensus == PLUGIN_CLASS
def test_get_blocks_status_containing_tx(monkeypatch): from bigchaindb.backend import query as backend_query from bigchaindb.core import Bigchain blocks = [{'id': 1}, {'id': 2}] monkeypatch.setattr(backend_query, 'get_blocks_status_from_transaction', lambda x: blocks) monkeypatch.setattr(Bigchain, 'block_election_status', lambda x, y, z: Bigchain.BLOCK_VALID) bigchain = Bigchain(public_key='pubkey', private_key='privkey') with pytest.raises(Exception): bigchain.get_blocks_status_containing_tx('txid')
def test_bigchain_class_initialization_with_parameters(config): from bigchaindb.core import Bigchain from bigchaindb.backend import connect from bigchaindb.consensus import BaseConsensusRules init_kwargs = { 'public_key': 'white', 'private_key': 'black', 'keyring': ['key_one', 'key_two'], } init_db_kwargs = { 'backend': 'rethinkdb', 'host': 'this_is_the_db_host', 'port': 12345, 'name': 'this_is_the_db_name', } connection = connect(**init_db_kwargs) bigchain = Bigchain(connection=connection, **init_kwargs) assert bigchain.connection == connection assert bigchain.connection.host == init_db_kwargs['host'] assert bigchain.connection.port == init_db_kwargs['port'] assert bigchain.connection.dbname == init_db_kwargs['name'] assert bigchain.me == init_kwargs['public_key'] assert bigchain.me_private == init_kwargs['private_key'] assert bigchain.nodes_except_me == init_kwargs['keyring'] assert bigchain.consensus == BaseConsensusRules
def make_nodes(i): """ make_nodes is a recursive context manager. Essentially it is doing: with f(a[0]) as b0: with f(a[1]) as b1: with f(a[2]) as b2: yield [b0, b1, b2] with an arbitrary depth. It is also temporarily patching global configuration to simulate nodes with separate identities. """ nonlocal keys if i == 0: yield [] else: config = deepcopy(config_orig) keys = [keys[-1]] + keys[:-1] # Rotate keys config['keyring'] = [pub for _, pub in keys[1:]] config['keypair']['private'] = keys[0][0] config['keypair']['public'] = keys[0][1] bigchaindb.config = config stepper = create_stepper() with stepper.start(): node = (Bigchain(), stepper) with make_nodes(i - 1) as rest: yield [node] + rest
def test_count_votes(): class TestVoting(Voting): @classmethod def verify_vote_schema(cls, vote): return vote['node_pubkey'] != 'malformed' voters = (['says invalid', 'malformed'] + ['kosher' + str(i) for i in range(10)]) votes = [Bigchain(v).vote('block', 'a', True) for v in voters] votes[0]['vote']['is_block_valid'] = False # Incorrect previous block subtracts from n_valid and adds to n_invalid votes[-1]['vote']['previous_block'] = 'z' by_voter = dict(enumerate(votes)) assert TestVoting.count_votes(by_voter) == { 'counts': { 'n_valid': 9, # 9 kosher votes 'n_invalid': 3, # 1 invalid, 1 malformed, 1 rogue prev block }, 'malformed': [votes[1]], 'previous_block': 'a', 'other_previous_block': { 'z': 1 }, }
def test_bigchain_class_default_initialization(config): from bigchaindb.core import Bigchain bigchain = Bigchain() assert bigchain.host == config['database']['host'] assert bigchain.port == config['database']['port'] assert bigchain.dbname == config['database']['name'] assert bigchain.me == config['keypair']['public'] assert bigchain.me_private == config['keypair']['private'] assert bigchain.federation_nodes == config['keyring'] assert bigchain._conn is None
def test_bigchain_class_default_initialization(config): from bigchaindb.core import Bigchain from bigchaindb.consensus import BaseConsensusRules from bigchaindb.backend.connection import Connection bigchain = Bigchain() assert isinstance(bigchain.connection, Connection) assert bigchain.connection.host == config['database']['host'] assert bigchain.connection.port == config['database']['port'] assert bigchain.connection.dbname == config['database']['name'] assert bigchain.consensus == BaseConsensusRules
def test_bigchain_class_default_initialization(config): from bigchaindb.core import Bigchain from bigchaindb.consensus import BaseConsensusRules from bigchaindb.backend.connection import Connection bigchain = Bigchain() assert isinstance(bigchain.connection, Connection) assert bigchain.connection.host == config['database']['host'] assert bigchain.connection.port == config['database']['port'] assert bigchain.connection.dbname == config['database']['name'] assert bigchain.me == config['keypair']['public'] assert bigchain.me_private == config['keypair']['private'] assert bigchain.nodes_except_me == config['keyring'] assert bigchain.consensus == BaseConsensusRules
def test_bigchain_class_initialization_with_parameters(config): from bigchaindb.core import Bigchain from bigchaindb.backend import connect from bigchaindb.consensus import BaseConsensusRules init_db_kwargs = { 'backend': 'localmongodb', 'host': 'this_is_the_db_host', 'port': 12345, 'name': 'this_is_the_db_name', } connection = connect(**init_db_kwargs) bigchain = Bigchain(connection=connection, **init_db_kwargs) assert bigchain.connection == connection assert bigchain.connection.host == init_db_kwargs['host'] assert bigchain.connection.port == init_db_kwargs['port'] assert bigchain.connection.dbname == init_db_kwargs['name'] assert bigchain.consensus == BaseConsensusRules
def test_bigchain_class_initialization_with_parameters(config): from bigchaindb.core import Bigchain init_kwargs = { 'host': 'some_node', 'port': '12345', 'dbname': 'atom', 'public_key': 'white', 'private_key': 'black', 'keyring': ['key_one', 'key_two'], } bigchain = Bigchain(**init_kwargs) assert bigchain.host == init_kwargs['host'] assert bigchain.port == init_kwargs['port'] assert bigchain.dbname == init_kwargs['dbname'] assert bigchain.me == init_kwargs['public_key'] assert bigchain.me_private == init_kwargs['private_key'] assert bigchain.federation_nodes == init_kwargs['keyring'] assert bigchain._conn is None
def test_must_agree_prev_block(): class TestVoting(Voting): @classmethod def verify_vote_schema(cls, vote): return True voters = 'abcd' votes = [Bigchain(v).vote('block', 'a', True) for v in voters] votes[0]['vote']['previous_block'] = 'b' votes[1]['vote']['previous_block'] = 'c' by_voter = dict(enumerate(votes)) assert TestVoting.count_votes(by_voter) == { 'counts': { 'n_valid': 2, 'n_invalid': 2, }, 'previous_block': 'a', 'other_previous_block': { 'b': 1, 'c': 1 }, 'malformed': [], }
def load(): from bigchaindb.core import Bigchain from bigchaindb.common.crypto import generate_key_pair from bigchaindb.common.transaction import Transaction def transactions(): priv, pub = generate_key_pair() tx = Transaction.create([pub], [([pub], 1)]) while True: i = yield tx.to_dict() tx.asset = {'data': {'n': i}} tx.sign([priv]) def wait_for_up(): print('Waiting for server to start... ', end='') while True: try: requests.get('http://localhost:9984/') break except requests.ConnectionError: time.sleep(0.1) print('Ok') def post_txs(): txs = transactions() txs.send(None) try: with requests.Session() as session: while True: i = tx_queue.get() if i is None: break tx = txs.send(i) res = session.post('http://localhost:9984/api/v1/transactions/', json=tx) assert res.status_code == 202 except KeyboardInterrupt: pass wait_for_up() num_clients = 30 test_time = 60 tx_queue = multiprocessing.Queue(maxsize=num_clients) txn = 0 b = Bigchain() start_time = time.time() for i in range(num_clients): multiprocessing.Process(target=post_txs).start() print('Sending transactions') while time.time() - start_time < test_time: # Post 500 transactions to the server for i in range(500): tx_queue.put(txn) txn += 1 print(txn) while True: # Wait for the server to reduce the backlog to below # 10000 transactions. The expectation is that 10000 transactions # will not be processed faster than a further 500 transactions can # be posted, but nonetheless will be processed within a few seconds. # This keeps the test from running on and keeps the transactions from # being considered stale. count = b.connection.db.backlog.count() if count > 10000: time.sleep(0.2) else: break for i in range(num_clients): tx_queue.put(None) print('Waiting to clear backlog') while True: bl = b.connection.db.backlog.count() if bl == 0: break print(bl) time.sleep(1) print('Waiting for all votes to come in') while True: blocks = b.connection.db.bigchain.count() votes = b.connection.db.votes.count() if blocks == votes + 1: break print('%s blocks, %s votes' % (blocks, votes)) time.sleep(3) print('Finished')
def test_bigchain_class_file_initialization(plugin_config): from bigchaindb.core import Bigchain bigchain = Bigchain() assert bigchain.consensus == PLUGIN_CLASS
def main(): """ Main function """ ctx = {} def pretty_json(data): return json.dumps(data, indent=2, sort_keys=True) client = server.create_app().test_client() host = 'example.com:9984' # HTTP Index res = client.get('/', environ_overrides={'HTTP_HOST': host}) res_data = json.loads(res.data.decode()) res_data['keyring'] = [ "6qHyZew94NMmUTYyHnkZsB8cxJYuRNEiEpXHe1ih9QX3", "AdDuyrTyjrDt935YnFu4VBCVDhHtY2Y6rcy7x2TFeiRi" ] res_data['public_key'] = 'NC8c8rYcAhyKVpx1PCV65CBmyq4YUbLysy3Rqrg8L8mz' ctx['index'] = pretty_json(res_data) # API index res = client.get('/api/v1/', environ_overrides={'HTTP_HOST': host}) ctx['api_index'] = pretty_json(json.loads(res.data.decode())) # tx create privkey = 'CfdqtD7sS7FgkMoGPXw55MVGGFwQLAoHYTcBhZDtF99Z' pubkey = '4K9sWUMFwTgaDGPfdynrbxWqWS6sWmKbZoTjxLtVUibD' asset = {'msg': 'Hello BigchainDB!'} tx = Transaction.create([pubkey], [([pubkey], 1)], asset=asset, metadata={'sequence': 0}) tx = tx.sign([privkey]) ctx['tx'] = pretty_json(tx.to_dict()) ctx['public_keys'] = tx.outputs[0].public_keys[0] ctx['txid'] = tx.id # tx transfer privkey_transfer = '3AeWpPdhEZzWLYfkfYHBfMFC2r1f8HEaGS9NtbbKssya' pubkey_transfer = '3yfQPHeWAa1MxTX9Zf9176QqcpcnWcanVZZbaHb8B3h9' cid = 0 input_ = Input(fulfillment=tx.outputs[cid].fulfillment, fulfills=TransactionLink(txid=tx.id, output=cid), owners_before=tx.outputs[cid].public_keys) tx_transfer = Transaction.transfer([input_], [([pubkey_transfer], 1)], asset_id=tx.id, metadata={'sequence': 1}) tx_transfer = tx_transfer.sign([privkey]) ctx['tx_transfer'] = pretty_json(tx_transfer.to_dict()) ctx['public_keys_transfer'] = tx_transfer.outputs[0].public_keys[0] ctx['tx_transfer_id'] = tx_transfer.id # privkey_transfer_last = 'sG3jWDtdTXUidBJK53ucSTrosktG616U3tQHBk81eQe' pubkey_transfer_last = '3Af3fhhjU6d9WecEM9Uw5hfom9kNEwE7YuDWdqAUssqm' cid = 0 input_ = Input(fulfillment=tx_transfer.outputs[cid].fulfillment, fulfills=TransactionLink(txid=tx_transfer.id, output=cid), owners_before=tx_transfer.outputs[cid].public_keys) tx_transfer_last = Transaction.transfer([input_], [([pubkey_transfer_last], 1)], asset_id=tx.id, metadata={'sequence': 2}) tx_transfer_last = tx_transfer_last.sign([privkey_transfer]) ctx['tx_transfer_last'] = pretty_json(tx_transfer_last.to_dict()) ctx['tx_transfer_last_id'] = tx_transfer_last.id ctx['public_keys_transfer_last'] = tx_transfer_last.outputs[0].public_keys[ 0] # block node_private = "5G2kE1zJAgTajkVSbPAQWo4c2izvtwqaNHYsaNpbbvxX" node_public = "DngBurxfeNVKZWCEcDnLj1eMPAS7focUZTE5FndFGuHT" signature = "53wxrEQDYk1dXzmvNSytbCfmNVnPqPkDQaTnAe8Jf43s6ssejPxezkCvUnGTnduNUmaLjhaan1iRLi3peu6s5DzA" block = Block(transactions=[tx], node_pubkey=node_public, voters=[node_public], signature=signature) ctx['block'] = pretty_json(block.to_dict()) ctx['blockid'] = block.id block_transfer = Block(transactions=[tx_transfer], node_pubkey=node_public, voters=[node_public], signature=signature) ctx['block_transfer'] = pretty_json(block.to_dict()) # vote DUMMY_SHA3 = '0123456789abcdef' * 4 b = Bigchain(public_key=node_public, private_key=node_private) vote = b.vote(block.id, DUMMY_SHA3, True) ctx['vote'] = pretty_json(vote) # block status block_list = [block_transfer.id, block.id] ctx['block_list'] = pretty_json(block_list) base_path = os.path.join(os.path.dirname(__file__), 'source/http-samples') if not os.path.exists(base_path): os.makedirs(base_path) for name, tpl in TPLS.items(): path = os.path.join(base_path, name + '.http') code = tpl % ctx with open(path, 'w') as handle: handle.write(code)
i = yield tx.to_dict() tx.asset = Asset(data={'n': i}, data_id='20170628150000', divisible=True) tx.sign([priv]) def write_tx(): for a in range(times): txs = transactions() tx_list.append(txs.send(None)) print("tx_list len", len(tx_list)) b.backend.write_transaction(tx_list.pop()) b = Bigchain() tx_list = [] times = 10000 num_clients = 30 start = time.time() print("start", start) for a in range(times): txs = transactions() tx_list.append(txs.send(None)) end = time.time() print("end", end) print("takes", end - start, "seconds") print("create tx", times / (end - start), "times/s") # for i in range(num_clients):