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 : uint8_t
13 {
16 Unknown,
17
20 Pending,
21
25
31 Invalid,
32 };
33
34 // Contains only the terminal values of TxStatus
35 enum class FinalTxStatus : std::underlying_type_t<TxStatus>
36 {
37 Committed =
38 static_cast<std::underlying_type_t<TxStatus>>(TxStatus::Committed),
39 Invalid = static_cast<std::underlying_type_t<TxStatus>>(TxStatus::Invalid),
40 };
41
42 constexpr char const* tx_status_to_str(TxStatus status)
43 {
44 switch (status)
45 {
47 {
48 return "Unknown";
49 }
51 {
52 return "Pending";
53 }
55 {
56 return "Committed";
57 }
59 {
60 return "Invalid";
61 }
62 default:
63 {
64 return "Unhandled value";
65 }
66 }
67 }
68
75
76 [[maybe_unused]] static TxStatus evaluate_tx_status(
77 View target_view,
78 SeqNo target_seqno,
79 View local_view,
80 View committed_view,
81 SeqNo committed_seqno)
82 {
83 const bool is_committed = committed_seqno >= target_seqno;
84 const bool views_match = local_view == target_view;
85 const bool view_known = local_view != VIEW_UNKNOWN;
86
87 if (is_committed && !view_known)
88 {
89 throw std::logic_error(fmt::format(
90 "Should know local view for seqnos up to {}, but have no view for {}",
91 committed_seqno,
92 target_seqno));
93 }
94
95 if (is_committed)
96 {
97 // The requested seqno has been committed, so we know for certain whether
98 // the requested tx id is committed or not
99 if (views_match)
100 {
101 return TxStatus::Committed;
102 }
103 return TxStatus::Invalid;
104 }
105
106 if (views_match)
107 {
108 // This node knows about the requested tx id, but it is not globally
109 // committed
110 return TxStatus::Pending;
111 }
112
113 if (committed_view > target_view)
114 {
115 // This node has seen the seqno in a different view, and committed
116 // further, so the requested tx id is impossible
117 return TxStatus::Invalid;
118 }
119
120 // Otherwise, we cannot state anything about this tx id. The most common
121 // reason is that the local_view is unknown (this transaction has never
122 // existed, or has not reached this node yet). It is also possible that
123 // this node believes locally that this tx id is impossible, but does not
124 // have a global commit to back this up - it will eventually receive
125 // either a global commit confirming this belief, or an election and
126 // global commit making this tx id invalid
127 return TxStatus::Unknown;
128 }
129}
#define DECLARE_JSON_ENUM(TYPE,...)
Definition json.h:864
Definition app_interface.h:13
FinalTxStatus
Definition tx_status.h:36
TxStatus
Definition tx_status.h:13
constexpr char const * tx_status_to_str(TxStatus status)
Definition tx_status.h:42
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