The Bigchain class¶
The Bigchain class is the top-level Python API for BigchainDB. If you want to create and initialize a BigchainDB database, you create a Bigchain instance (object). Then you can use its various methods to create transactions, write transactions (to the object/database), read transactions, etc.
-
class
bigchaindb.
Bigchain
(public_key=None, private_key=None, keyring=[], connection=None, backlog_reassign_delay=None)[source]¶ Bigchain API
Create, read, sign, write transactions to the database
-
__init__
(public_key=None, private_key=None, keyring=[], connection=None, backlog_reassign_delay=None)[source]¶ Initialize the Bigchain instance
A Bigchain instance has several configuration parameters (e.g. host). If a parameter value is passed as an argument to the Bigchain __init__ method, then that is the value it will have. Otherwise, the parameter value will come from an environment variable. If that environment variable isn’t set, then the value will come from the local configuration file. And if that variable isn’t in the local configuration file, then the parameter will have its default value (defined in bigchaindb.__init__).
Parameters: - public_key (str) – the base58 encoded public key for the ED25519 curve.
- private_key (str) – the base58 encoded private key for the ED25519 curve.
- keyring (list[str]) – list of base58 encoded public keys of the federation nodes.
- connection (
Connection
) – A connection to the database.
-
BLOCK_INVALID
= 'invalid'¶ return if a block has been voted invalid
-
BLOCK_VALID
= 'valid'¶ return if a block is valid, or tx is in valid block
-
TX_VALID
= 'valid'¶ return if a block is valid, or tx is in valid block
-
BLOCK_UNDECIDED
= 'undecided'¶ return if block is undecided, or tx is in undecided block
-
TX_UNDECIDED
= 'undecided'¶ return if block is undecided, or tx is in undecided block
-
TX_IN_BACKLOG
= 'backlog'¶ return if transaction is in backlog
-
federation
¶ Set of federation member public keys
-
write_transaction
(signed_transaction)[source]¶ Write the transaction to bigchain.
When first writing a transaction to the bigchain the transaction will be kept in a backlog until it has been validated by the nodes of the federation.
Parameters: signed_transaction (Transaction) – transaction with the signature included. Returns: database response Return type: dict
-
reassign_transaction
(transaction)[source]¶ Assign a transaction to a new node
Parameters: transaction (dict) – assigned transaction Returns: database response or None if no reassignment is possible Return type: dict
-
delete_transaction
(*transaction_id)[source]¶ Delete a transaction from the backlog.
Parameters: *transaction_id (str) – the transaction(s) to delete Returns: The database response.
-
get_stale_transactions
()[source]¶ Get a cursor of stale transactions.
Transactions are considered stale if they have been assigned a node, but are still in the backlog after some amount of time specified in the configuration
-
validate_transaction
(transaction)[source]¶ Validate a transaction.
Parameters: transaction (Transaction) – transaction to validate. Returns: The transaction if the transaction is valid else it raises an exception describing the reason why the transaction is invalid.
-
is_new_transaction
(txid, exclude_block_id=None)[source]¶ Return True if the transaction does not exist in any VALID or UNDECIDED block. Return False otherwise.
Parameters:
-
get_block
(block_id, include_status=False)[source]¶ Get the block with the specified block_id (and optionally its status)
Returns the block corresponding to block_id or None if no match is found.
Parameters:
-
get_transaction
(txid, include_status=False)[source]¶ Get the transaction with the specified txid (and optionally its status)
This query begins by looking in the bigchain table for all blocks containing a transaction with the specified txid. If one of those blocks is valid, it returns the matching transaction from that block. Else if some of those blocks are undecided, it returns a matching transaction from one of them. If the transaction was found in invalid blocks only, or in no blocks, then this query looks for a matching transaction in the backlog table, and if it finds one there, it returns that.
Parameters: Returns: A
Transaction
instance if the transaction was found in a valid block, an undecided block, or the backlog table, otherwiseNone
. Ifinclude_status
isTrue
, also returns the transaction’s status if the transaction was found.
-
get_status
(txid)[source]¶ Retrieve the status of a transaction with txid from bigchain.
Parameters: txid (str) – transaction id of the transaction to query Returns: transaction status (‘valid’, ‘undecided’, or ‘backlog’). If no transaction with that txid was found it returns None Return type: (string)
-
get_blocks_status_containing_tx
(txid)[source]¶ Retrieve block ids and statuses related to a transaction
Transactions may occur in multiple blocks, but no more than one valid block.
Parameters: txid (str) – transaction id of the transaction to query Returns: A dict of blocks containing the transaction, e.g. {block_id_1: ‘valid’, block_id_2: ‘invalid’ ...}, or None
-
get_asset_by_id
(asset_id)[source]¶ Returns the asset associated with an asset_id.
Parameters: asset_id (str) – The asset id. Returns: dict if the asset exists else None.
-
get_spent
(txid, output)[source]¶ Check if a txid was already used as an input.
A transaction can be used as an input for another transaction. Bigchain needs to make sure that a given (txid, output) is only used once.
This method will check if the (txid, output) has already been spent in a transaction that is in either the VALID, UNDECIDED or BACKLOG state.
Parameters: - txid (str) – The id of the transaction
- output (num) – the index of the output in the respective transaction
Returns: The transaction (Transaction) that used the (txid, output) as an input else None
Raises: CriticalDoubleSpend
– If the given (txid, output) was spent in- more than one valid transaction.
-
get_outputs
(owner)[source]¶ - Retrieve a list of links to transaction outputs for a given public
- key.
Parameters: owner (str) – base58 encoded public key. Returns: list of txid
s andoutput
s pointing to another transaction’s conditionReturn type: list
of TransactionLink
-
is_tx_strictly_in_invalid_block
(txid)[source]¶ Checks whether the transaction with the given
txid
strictly belongs to an invalid block.Parameters: txid (str) – Transaction id. Returns: True
if the transaction strictly belongs to a block that is invalid.False
otherwise.Return type: bool Note
Since a transaction may be in multiple blocks, with different statuses, the term “strictly” is used to emphasize that if a transaction is said to be in an invalid block, it means that it is not in any other block that is either valid or undecided.
-
get_owned_ids
(owner)[source]¶ Retrieve a list of
txid
s that can be used as inputs.Parameters: owner (str) – base58 encoded public key. Returns: list of txid
s andoutput
s pointing to another transaction’s conditionReturn type: list
of TransactionLink
-
get_outputs_filtered
(owner, include_spent=True)[source]¶ Get a list of output links filtered on some criteria
-
get_transactions_filtered
(asset_id, operation=None)[source]¶ Get a list of transactions filtered on some criteria
-
create_block
(validated_transactions)[source]¶ Creates a block given a list of validated_transactions.
Note that this method does not validate the transactions. Transactions should be validated before calling create_block.
Parameters: validated_transactions (list(Transaction)) – list of validated transactions. Returns: created block. Return type: Block
-
validate_block
(block)[source]¶ Validate a block.
Parameters: block (Block) – block to validate. Returns: The block if the block is valid else it raises and exception describing the reason why the block is invalid.
-
has_previous_vote
(block_id)[source]¶ Check for previous votes from this node
Parameters: block_id (str) – the id of the block to check Returns: True
if this block already has a valid vote from this node,False
otherwise.Return type: bool
-
write_block
(block)[source]¶ Write a block to bigchain.
Parameters: block (Block) – block to write to bigchain.
-
create_genesis_block
()[source]¶ Create the genesis block
Block created when bigchain is first initialized. This method is not atomic, there might be concurrency problems if multiple instances try to write the genesis block when the BigchainDB Federation is started, but it’s a highly unlikely scenario.
-
vote
(block_id, previous_block_id, decision, invalid_reason=None)[source]¶ Create a signed vote for a block given the
previous_block_id
and thedecision
(valid/invalid).Parameters:
-