CCF
Loading...
Searching...
No Matches
tx_status.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/ds/json.h"
6#include "ccf/tx_id.h"
7
8namespace ccf
9{
12 enum class TxStatus
13 {
16 Unknown,
17
20 Pending,
21
25
31 Invalid,
32 };
33
34 constexpr char const* tx_status_to_str(TxStatus status)
35 {
36 switch (status)
37 {
39 {
40 return "Unknown";
41 }
43 {
44 return "Pending";
45 }
47 {
48 return "Committed";
49 }
51 {
52 return "Invalid";
53 }
54 default:
55 {
56 return "Unhandled value";
57 }
58 }
59 }
60
67
68 [[maybe_unused]] static TxStatus evaluate_tx_status(
69 View target_view,
70 SeqNo target_seqno,
71 View local_view,
72 View committed_view,
73 SeqNo committed_seqno)
74 {
75 const bool is_committed = committed_seqno >= target_seqno;
76 const bool views_match = local_view == target_view;
77 const bool view_known = local_view != VIEW_UNKNOWN;
78
79 if (is_committed && !view_known)
80 {
81 throw std::logic_error(fmt::format(
82 "Should know local view for seqnos up to {}, but have no view for {}",
83 committed_seqno,
84 target_seqno));
85 }
86
87 if (is_committed)
88 {
89 // The requested seqno has been committed, so we know for certain whether
90 // the requested tx id is committed or not
91 if (views_match)
92 {
94 }
95 else
96 {
97 return TxStatus::Invalid;
98 }
99 }
100 else if (views_match)
101 {
102 // This node knows about the requested tx id, but it is not globally
103 // committed
104 return TxStatus::Pending;
105 }
106 else if (committed_view > target_view)
107 {
108 // This node has seen the seqno in a different view, and committed
109 // further, so the requested tx id is impossible
110 return TxStatus::Invalid;
111 }
112 else
113 {
114 // Otherwise, we cannot state anything about this tx id. The most common
115 // reason is that the local_view is unknown (this transaction has never
116 // existed, or has not reached this node yet). It is also possible that
117 // this node believes locally that this tx id is impossible, but does not
118 // have a global commit to back this up - it will eventually receive
119 // either a global commit confirming this belief, or an election and
120 // global commit making this tx id invalid
121 return TxStatus::Unknown;
122 }
123 }
124}
#define DECLARE_JSON_ENUM(TYPE,...)
Definition json.h:837
Definition app_interface.h:14
TxStatus
Definition tx_status.h:13
constexpr char const * tx_status_to_str(TxStatus status)
Definition tx_status.h:34
constexpr View VIEW_UNKNOWN
Definition tx_id.h:26
uint64_t View
Definition tx_id.h:23
uint64_t SeqNo
Definition tx_id.h:36