Ordering transactions deterministically is key to Tree of work. Transactions are ordered from the genesis transaction to the head transaction with the highest cumulative difficulty score; this is analogous to the LCR in bitcoin. The ordering works by following the dependency relationships created by the parent and uncle references. Specifically both parent and uncle transactions must be included in the ordering before the transaction that referenced them. The algorithm recursively orders parent transactions, then uncle transactions at every node in the tree.
def order_from(early_node, late_node, carry=None):
carry = [] if carry is None else carry
if early_node == late_node:
return [late_node]
if late_node.parent_hash == 0:
raise Exception('Root block encountered unexpectedly while ordering graph')
main_path = exclude_from(Graph.order_from(early_node, late_node.parent), carry)
aux_path = exclude_from(Graph.order_from(early_node, late_node.uncle), carry + main_path) if late_node.uncle is not None else []
return main_path + aux_path + [late_node]
