bhive.blockchain module

class bhive.blockchain.Blockchain(hive_instance=None, mode='irreversible', max_block_wait_repetition=None, data_refresh_time_seconds=900)

Bases: object

This class allows to access the blockchain and read data from it

Parameters
  • hive_instance (Hive) – Hive instance

  • mode (str) – (default) Irreversible block (irreversible) or actual head block (head)

  • max_block_wait_repetition (int) – maximum wait repetition for next block where each repetition is block_interval long (default is 3)

This class let’s you deal with blockchain related data and methods. Read blockchain related data:

Read current block and blockchain info

print(chain.get_current_block())
print(chain.hive.info())

Monitor for new blocks. When stop is not set, monitoring will never stop.

blocks = []
current_num = chain.get_current_block_num()
for block in chain.blocks(start=current_num - 99, stop=current_num):
    blocks.append(block)
len(blocks)
100

or each operation individually:

ops = []
current_num = chain.get_current_block_num()
for operation in chain.ops(start=current_num - 99, stop=current_num):
    ops.append(operation)
awaitTxConfirmation(transaction, limit=10)

Returns the transaction as seen by the blockchain after being included into a block

Parameters
  • transaction (dict) – transaction to wait for

  • limit (int) – (optional) number of blocks to wait for the transaction (default: 10)

Note

If you want instant confirmation, you need to instantiate class:bhive.blockchain.Blockchain with mode="head", otherwise, the call will wait until confirmed in an irreversible block.

Note

This method returns once the blockchain has included a transaction with the same signature. Even though the signature is not usually used to identify a transaction, it still cannot be forfeited and is derived from the transaction contented and thus identifies a transaction uniquely.

block_time(block_num)

Returns a datetime of the block with the given block number.

Parameters

block_num (int) – Block number

block_timestamp(block_num)

Returns the timestamp of the block with the given block number as integer.

Parameters

block_num (int) – Block number

blocks(start=None, stop=None, max_batch_size=None, threading=False, thread_num=8, only_ops=False, only_virtual_ops=False)

Yields blocks starting from start.

Parameters
  • start (int) – Starting block

  • stop (int) – Stop at this block

  • max_batch_size (int) – only for appbase nodes. When not None, batch calls of are used. Cannot be combined with threading

  • threading (bool) – Enables threading. Cannot be combined with batch calls

  • thread_num (int) – Defines the number of threads, when threading is set.

  • only_ops (bool) – Only yield operations (default: False). Cannot be combined with only_virtual_ops=True.

  • only_virtual_ops (bool) – Only yield virtual operations (default: False)

Note

If you want instant confirmation, you need to instantiate class:bhive.blockchain.Blockchain with mode="head", otherwise, the call will wait until confirmed in an irreversible block.

find_change_recovery_account_requests(accounts)

Find pending change_recovery_account requests for one or more specific accounts.

Parameters

accounts (str/list) – account name or list of account names to find change_recovery_account requests for.

Returns

list of change_recovery_account requests for the given account(s).

Return type

list

>>> from bhive.blockchain import Blockchain
>>> blockchain = Blockchain()
>>> ret = blockchain.find_change_recovery_account_requests('bott')
find_rc_accounts(name)

Returns the RC parameters of one or more accounts.

Parameters

name (str) – account name to search rc params for (can also be a list of accounts)

Returns

RC params

Return type

list

>>> from bhive.blockchain import Blockchain
>>> blockchain = Blockchain()
>>> ret = blockchain.find_rc_accounts(["test"])
>>> len(ret) == 1
True
get_account_count()

Returns the number of accounts

get_account_reputations(start='', stop='', steps=1000.0, limit=-1, **kwargs)

Yields account reputation between start and stop.

Parameters
  • start (str) – Start at this account name

  • stop (str) – Stop at this account name

  • steps (int) – Obtain steps ret with a single call from RPC

get_all_accounts(start='', stop='', steps=1000.0, limit=-1, **kwargs)

Yields account names between start and stop.

Parameters
  • start (str) – Start at this account name

  • stop (str) – Stop at this account name

  • steps (int) – Obtain steps ret with a single call from RPC

get_current_block(only_ops=False, only_virtual_ops=False)

This call returns the current block

Parameters
  • only_ops (bool) – Returns block with operations only, when set to True (default: False)

  • only_virtual_ops (bool) – Includes only virtual operations (default: False)

Note

The block number returned depends on the mode used when instantiating from this class.

get_current_block_num()

This call returns the current block number

Note

The block number returned depends on the mode used when instantiating from this class.

get_estimated_block_num(date, estimateForwards=False, accurate=True)

This call estimates the block number based on a given date

Parameters

date (datetime) – block time for which a block number is estimated

Note

The block number returned depends on the mode used when instantiating from this class.

>>> from bhive.blockchain import Blockchain
>>> from datetime import datetime
>>> blockchain = Blockchain()
>>> block_num = blockchain.get_estimated_block_num(datetime(2019, 6, 18, 5 ,8, 27))
>>> block_num == 33898184
True
get_similar_account_names(name, limit=5)

Returns limit similar accounts with name as list

Parameters
  • name (str) – account name to search similars for

  • limit (int) – limits the number of accounts, which will be returned

Returns

Similar account names as list

Return type

list

>>> from bhive.blockchain import Blockchain
>>> blockchain = Blockchain()
>>> ret = blockchain.get_similar_account_names("test", limit=5)
>>> len(ret) == 5
True
get_transaction(transaction_id)

Returns a transaction from the blockchain

Parameters

transaction_id (str) – transaction_id

get_transaction_hex(transaction)

Returns a hexdump of the serialized binary form of a transaction.

Parameters

transaction (dict) – transaction

static hash_op(event)

This method generates a hash of blockchain operation.

is_irreversible_mode()
list_change_recovery_account_requests(start='', limit=1000, order='by_account')

List pending change_recovery_account requests.

Parameters
  • start (str/list) – Start the listing from this entry. Leave empty to start from the beginning. If order is set to by_account, start has to be an account name. If order is set to by_effective_date, start has to be a list of [effective_on, account_to_recover], e.g. start=[‘2018-12-18T01:46:24’, ‘bott’].

  • limit (int) – maximum number of results to return (default and maximum: 1000).

  • order (str) – valid values are “by_account” (default) or “by_effective_date”.

Returns

list of change_recovery_account requests.

Return type

list

>>> from bhive.blockchain import Blockchain
>>> blockchain = Blockchain()
>>> ret = blockchain.list_change_recovery_account_requests(limit=1)
ops(start=None, stop=None, only_virtual_ops=False, **kwargs)

Blockchain.ops() is deprecated. Please use Blockchain.stream() instead.

ops_statistics(start, stop=None, add_to_ops_stat=None, with_virtual_ops=True, verbose=False)

Generates statistics for all operations (including virtual operations) starting from start.

Parameters
  • start (int) – Starting block

  • stop (int) – Stop at this block, if set to None, the current_block_num is taken

  • add_to_ops_stat (dict) – if set, the result is added to add_to_ops_stat

  • verbose (bool) – if True, the current block number and timestamp is printed

This call returns a dict with all possible operations and their occurrence.

stream(opNames=[], raw_ops=False, *args, **kwargs)

Yield specific operations (e.g. comments) only

Parameters
  • opNames (array) – List of operations to filter for

  • raw_ops (bool) – When set to True, it returns the unmodified operations (default: False)

  • start (int) – Start at this block

  • stop (int) – Stop at this block

  • max_batch_size (int) – only for appbase nodes. When not None, batch calls of are used. Cannot be combined with threading

  • threading (bool) – Enables threading. Cannot be combined with batch calls

  • thread_num (int) – Defines the number of threads, when threading is set.

  • only_ops (bool) – Only yield operations (default: False) Cannot be combined with only_virtual_ops=True

  • only_virtual_ops (bool) – Only yield virtual operations (default: False)

The dict output is formated such that type carries the operation type. Timestamp and block_num are taken from the block the operation was stored in and the other keys depend on the actual operation.

Note

If you want instant confirmation, you need to instantiate class:bhive.blockchain.Blockchain with mode="head", otherwise, the call will wait until confirmed in an irreversible block.

output when raw_ops=False is set:

{
    'type': 'transfer',
    'from': 'johngreenfield',
    'to': 'thundercurator',
    'amount': '0.080 HBD',
    'memo': 'https://hive.blog/lofi/@johngreenfield/lofi-joji-yeah-right',
    '_id': '6d4c5f2d4d8ef1918acaee4a8dce34f9da384786',
    'timestamp': datetime.datetime(2018, 5, 9, 11, 23, 6, tzinfo=<UTC>),
    'block_num': 22277588, 'trx_num': 35, 'trx_id': 'cf11b2ac8493c71063ec121b2e8517ab1e0e6bea'
}

output when raw_ops=True is set:

{
    'block_num': 22277588,
    'op':
        [
            'transfer',
                {
                    'from': 'johngreenfield', 'to': 'thundercurator',
                    'amount': '0.080 HBD',
                    'memo': 'https://hive.blog/lofi/@johngreenfield/lofi-joji-yeah-right'
                }
        ],
        'timestamp': datetime.datetime(2018, 5, 9, 11, 23, 6, tzinfo=<UTC>)
}
wait_for_and_get_block(block_number, blocks_waiting_for=None, only_ops=False, only_virtual_ops=False, block_number_check_cnt=-1, last_current_block_num=None)

Get the desired block from the chain, if the current head block is smaller (for both head and irreversible) then we wait, but a maxmimum of blocks_waiting_for * max_block_wait_repetition time before failure.

Parameters
  • block_number (int) – desired block number

  • blocks_waiting_for (int) – difference between block_number and current head and defines how many blocks we are willing to wait, positive int (default: None)

  • only_ops (bool) – Returns blocks with operations only, when set to True (default: False)

  • only_virtual_ops (bool) – Includes only virtual operations (default: False)

  • block_number_check_cnt (int) – limit the number of retries when greater than -1

  • last_current_block_num (int) – can be used to reduce the number of get_current_block_num() api calls

class bhive.blockchain.Pool(thread_count, batch_mode=True, exception_handler=<function default_handler>)

Bases: object

Pool of threads consuming tasks from a queue

abort(block=False)

Tell each worker that its done working

alive()

Returns True if any threads are currently running

done()

Returns True if not tasks are left to be completed

enqueue(func, *args, **kargs)

Add a task to the queue

idle()

Returns True if all threads are waiting for work

join()

Wait for completion of all the tasks in the queue

results(sleep_time=0)

Get the set of results that have been processed, repeatedly call until done

run(block=False)

Start the threads, or restart them if you’ve aborted

class bhive.blockchain.Worker(name, queue, results, abort, idle, exception_handler)

Bases: threading.Thread

Thread executing tasks from a given tasks queue

run()

Thread work loop calling the function with the params

bhive.blockchain.default_handler(name, exception, *args, **kwargs)