github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/mempool/execution_tree.go (about) 1 package mempool 2 3 import ( 4 "github.com/onflow/flow-go/model/flow" 5 ) 6 7 // ExecutionTree represents a concurrency-safe memory pool for execution 8 // Receipts. Its is aware of the tree structure formed by execution results. 9 // All execution receipts for the _same result_ form an equivalence class and 10 // are represented by _one_ vertex in the execution tree. The mempool utilizes 11 // knowledge about the height of the block the result is for. Hence, the 12 // Mempool can only store and process Receipts whose block is known. 13 // 14 // Implementations are concurrency safe. 15 type ExecutionTree interface { 16 17 // AddResult adds an Execution Result to the Execution Tree (without any receipts), in 18 // case the result is not already stored in the tree. 19 // This is useful for crash recovery: 20 // After recovering from a crash, the mempools are wiped and the sealed results will not 21 // be stored in the Execution Tree anymore. Adding the result to the tree allows to create 22 // a vertex in the tree without attaching any Execution Receipts to it. 23 AddResult(result *flow.ExecutionResult, block *flow.Header) error 24 25 // AddReceipt adds the given execution receipt to the memory pool. Requires height 26 // of the block the receipt is for. We enforce data consistency on an API 27 // level by using the block header as input. 28 AddReceipt(receipt *flow.ExecutionReceipt, block *flow.Header) (bool, error) 29 30 // HasReceipt returns true if the given receipt is already present in the mempool. 31 HasReceipt(receipt *flow.ExecutionReceipt) bool 32 33 // ReachableReceipts returns a slice of ExecutionReceipt, whose result 34 // is computationally reachable from resultID. Context: 35 // * Conceptually, the Execution results form a tree, which we refer to as 36 // Execution Tree. A branch in the execution can be due to a fork in the main 37 // chain. Furthermore, the execution branches if ENs disagree about the result 38 // for the same block. 39 // * As the ID of an execution result contains the BlockID, which the result 40 // for, all Execution Results with the same ID necessarily are for the same 41 // block. All Execution Receipts committing to the same result from an 42 // equivalence class and can be represented as one vertex in the Execution 43 // Tree. 44 // * An execution result r1 points (field ExecutionResult.ParentResultID) to 45 // its parent result r0 , whose end state was used as the starting state 46 // to compute r1. Formally, we have an edge r0 -> r1 in the Execution Tree, 47 // if a result r1 is stored in the mempool, whose ParentResultID points to 48 // r0. 49 // ReachableReceipts implements a tree search on the Execution Tree starting 50 // from the provided resultID. Execution Receipts are traversed in a 51 // parent-first manner, meaning that a the parent result is traversed 52 // _before_ any of its derived results. The algorithm only traverses to 53 // results, for which there exists a sequence of interim result in the 54 // mempool without any gaps. 55 // 56 // Two filters are supplied: 57 // * blockFilter: the tree search will only travers to results for 58 // blocks which pass the filter. Often higher-level logic is only 59 // interested in results for blocks in a specific fork. Such can be 60 // implemented by a suitable blockFilter. 61 // * receiptFilter: for a reachable result (subject to the restrictions 62 // imposed by blockFilter, all known receipts are returned. 63 // While _all_ Receipts for the parent result are guaranteed to be 64 // listed before the receipts for the derived results, there is no 65 // specific ordering for the receipts committing to the same result 66 // (random order). If only a subset of receipts for a result is desired 67 // (e.g. for de-duplication with parent blocks), receiptFilter should 68 // be used. 69 // Note the important difference between the two filters: 70 // * The blockFilter suppresses traversal to derived results. 71 // * The receiptFilter does _not_ suppresses traversal to derived results. 72 // Only individual receipts are dropped. 73 // 74 // Error returns: 75 // * UnknownExecutionResultError (sentinel) if resultID is unknown 76 // * all other error are unexpected and potential indicators of corrupted internal state 77 ReachableReceipts(resultID flow.Identifier, blockFilter BlockFilter, receiptFilter ReceiptFilter) ([]*flow.ExecutionReceipt, error) 78 79 // Size returns the number of receipts stored in the mempool 80 Size() uint 81 82 // PruneUpToHeight prunes all results for all blocks with height up to but 83 // NOT INCLUDING `newLowestHeight`. Errors if newLowestHeight is smaller than 84 // the previous value (as we cannot recover previously pruned results). 85 PruneUpToHeight(newLowestHeight uint64) error 86 87 // LowestHeight returns the lowest height, where results are still 88 // stored in the mempool. 89 LowestHeight() uint64 90 } 91 92 // BlockFilter is used for controlling the ExecutionTree's Execution Tree search. 93 // The search only traverses to results for blocks which pass the filter. 94 // If an the block for an execution result does not pass the filter, the entire 95 // sub-tree of derived results is not traversed. 96 type BlockFilter func(header *flow.Header) bool 97 98 // ReceiptFilter is used to drop specific receipts from. It does NOT 99 // affect the ExecutionTree's Execution Tree search. 100 type ReceiptFilter func(receipt *flow.ExecutionReceipt) bool