Skip to content

Latest commit

 

History

History
135 lines (102 loc) · 6.24 KB

consensus-context.md

File metadata and controls

135 lines (102 loc) · 6.24 KB

Consensus Context

Provides the system context for the consensus algorithm and deals with the actual content of blocks. Responsible primarily for the creation of new blocks (populated with transactions from TransactionPool) and for content validation of proposed blocks (verifying transaction results).

Currently a single instance per virtual chain per node.

Interacts with services

  • TransactionPool - Uses it to populate transactions in proposed blocks and validate ordering of others' proposed blocks.
  • VirtualMachine - Uses it to execute transactions and generate receipts and state diffs.
  • StateStorage - Queries the merkle root of the state for writing in proposed blocks and validating others' proposed ones.
  • Passive towards ConsensusAlgo and just provides services to it upon request.

 

Init (flow)

 

RequestOrderingCommittee (method)

Returns a sorted list of nodes (public keys) that participate in the approval committee for the ordering of a given block height. Called by consensus algo.

  • The current list of nodes (per block height) is managed by the configuration.
  • If the size of requested committee is larger than total nodes, select all nodes as the committee.
  • Order the nodes based on the weighted random sorting algorithm (reputation is taken into account here).

 

RequestValidationCommittee (method)

Returns a sorted list of nodes (public keys) that participate in the approval committee for the execution validation of a given block height. Called by consensus algo.

  • The current list of nodes (per block height) is managed by the configuration.
  • If the size of requested committee is larger than total nodes, select all nodes as the committee.
  • Order the nodes based on the weighted random sorting algorithm (reputation is taken into account here).

 

RequestNewTransactionsBlock (method)

Performed by consensus leader only, upon request from consensus algo to perform the ordering phase of the consensus during a live round.

Choose pending transactions

  • Get pending transactions by calling TransactionPool.GetTransactionsForOrdering.
    • If there are too little transactions to append (minimal block according to a configurable amount):
      • Wait configurable time (eg. 0.5 sec) and retry once.
      • If no transactions continue with an empty block (number of transactions is 0).

Build Transactions block

  • Current protocol version (0x1).
  • Current virtual chain.
  • Block height is given.
  • Hash pointer to the previous (latest) Transactions block is given.
  • 64b unix timestamp.
  • The merkle root hash of the transactions in the block.
  • Placeholder: metadata - holds reputation / algorithm data.
  • Hash of the block metadata.

 

RequestNewResultsBlock (method)

Performed by the leader only, upon request from consensus algo to perform the execution phase of the consensus during a live round.

Execute transactions

  • The Transactions block for this block height should be cached from previous call to RequestNewTransactionsBlock. -->
  • Execute the ordered transactions set by calling VirtualMachine.ProcessTransactionSet creating receipts and state diff.

Build Results block

  • Current protocol version (0x1).
  • Current virtual chain.
  • Block height is given.
  • Hash pointer to the previous (latest) Results block is given.
  • 64b unix timestamp.
  • The merkle root hash of the transaction receipts in the block.
  • The hash of the state diff in the block.
  • Hash pointer to the Transactions block of the same height.
  • Merkle root of the state prior to the block execution, retrieved by calling StateStorage.GetStateHash.
  • Transaction id bloom filter (see block format for structure).
  • Transaction timestamp bloom filter (see block format for structure).

 

ValidateTransactionsBlock (method)

Validates another node's proposed block. Performed upon request from consensus algo when receiving a proposal during a live consensus round.

Check Transactions block

  • Check protocol version.
  • Check virtual chain.
  • Check block height is the next of the previous block's height.
  • Check hash pointer indeed points to previous block.
  • Check timestamp is within configurable allowed jitter of system timestamp, and later than previous block.
  • Check transaction merkle root hash.
  • Check metadata hash.

Validate transaction choice

  • Call TransactionPool.ValidateTransactionsForOrdering to validate pre order checks, expiration and no duplication.

 

ValidateResultsBlock (method)

Validates another node's proposed block. Performed upon request from consensus algo when receiving a proposal during a live consensus round.

Check Results block

  • Check protocol version.
  • Check virtual chain.
  • Check block height is the next of the previous block's height.
  • Check hash pointer indeed points to previous block.
  • Check timestamp is within configurable allowed jitter of system timestamp, and later than previous block.
  • Check transaction receipts merkle root hash.
  • Check the hash of the state diff in the block.
  • Check hash pointer to the Transactions block of the same height.
  • Check merkle root of the state prior to the block execution, retrieved by calling StateStorage.GetStateHash.
  • Check transaction id bloom filter (see block format for structure).
  • Check transaction timestamp bloom filter (see block format for structure).

Validate transaction execution

  • Execute the ordered transactions set by calling VirtualMachine.ProcessTransactionSet creating receipts and state diff.
  • Compare the receipts merkle root hash to the one in the block.
  • Compare the state diff hash to the one in the block (supports only deterministic execution).