src.block

Definitions of blocks, and the genesis block.

Functions

Classes

Block(prev_block_hash, time, nonce, height, ...) A block: a container for all the data associated with a block.
class src.block.Block(prev_block_hash, time, nonce, height, received_time, difficulty, transactions, merkle_root_hash=None)

A block: a container for all the data associated with a block.

To figure out whether the block is valid on top of a block chain, there are a few verify methods. Without calling these, you must assume the block was crafted maliciously.

Variables:
  • hash (bytes) – The hash value of this block.
  • prev_block_hash (bytes) – The hash of the previous block.
  • merkle_root_hash (bytes) – The hash of the merkle tree root of the transactions in this block.
  • time (datetime) – The time when this block was created.
  • nonce (int) – The nonce in this block that was required to achieve the proof of work.
  • height (int) – The height (accumulated difficulty) of this block.
  • received_time (datetime) – The time when we received this block.
  • difficulty (int) – The difficulty of this block.
  • transactions (List[Transaction]) – The list of transactions in this block.
classmethod create(blockchain, transactions, ts=None)

Create a new block for a certain blockchain, containing certain transactions.

finish_hash(hasher)

Finishes the hash in hasher with the nonce in this block. The proof of work can use this function to efficiently try different nonces. Other uses should use hash to get the complete hash in one step.

classmethod from_json_compatible(val)

Create a new block from its JSON-serializable representation.

get_partial_hash()

Computes a hash over the contents of this block, except for the nonce. The proof of work can use this partial hash to efficiently try different nonces. Other uses should use hash to get the complete hash.

to_json_compatible()

Returns a JSON-serializable representation of this object.

verify(chain)

Verifies that this block contains only valid data and can be applied on top of the block chain chain.

verify_difficulty()

Verifies that the hash value is correct and fulfills its difficulty promise.

verify_merkle()

Verify that the merkle root hash is correct for the transactions in this block.

verify_prev_block(chain)

Verifies that the previous block pointer points to the head of the given block chain and difficulty and height are correct.

verify_time(chain)

Verifies that blocks are not from far in the future, but a bit younger than the head of chain.

verify_transactions(chain)

Verifies that all transaction in this block are valid in the given block chain.