CCF
Loading...
Searching...
No Matches
transactions.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
6#include "ccf/json_handler.h"
7#include "ccf/tx_id.h"
8#include "ccf/tx_status.h"
10
11namespace ccf::gov::endpoints
12{
14 {
15 auto get_transaction_status = [&](auto& ctx, ApiVersion api_version) {
16 switch (api_version)
17 {
19 case ApiVersion::v1:
20 default:
21 {
22 // Extract transaction ID from path parameter
23 std::string tx_id_str;
24 std::string error;
26 ctx.rpc_ctx->get_request_path_params(),
27 "transactionId",
28 tx_id_str,
29 error))
30 {
31 detail::set_gov_error(
32 ctx.rpc_ctx,
33 HTTP_STATUS_BAD_REQUEST,
34 ccf::errors::InvalidResourceName,
35 std::move(error));
36 return;
37 }
38
39 // Parse transaction ID from string
40 const auto tx_id = ccf::TxID::from_str(tx_id_str);
41 if (!tx_id.has_value())
42 {
43 detail::set_gov_error(
44 ctx.rpc_ctx,
45 HTTP_STATUS_BAD_REQUEST,
46 ccf::errors::InvalidQueryParameterValue,
47 fmt::format(
48 "The value '{}' passed as parameter 'transactionId' could not "
49 "be converted to a valid Transaction ID.",
50 tx_id_str));
51 return;
52 }
53
54 // Lookup status
56 const auto result =
57 registry.get_status_for_txid_v1(tx_id->view, tx_id->seqno, status);
58 if (result != ccf::ApiResult::OK)
59 {
60 detail::set_gov_error(
61 ctx.rpc_ctx,
62 HTTP_STATUS_INTERNAL_SERVER_ERROR,
63 ccf::errors::InternalError,
64 fmt::format(
65 "get_status_for_txid_v1 returned error: {}",
66 ccf::api_result_to_str(result)));
67 return;
68 }
69
70 // Build response
71 auto body = nlohmann::json::object();
72
73 body["status"] = status;
74 body["transactionId"] = tx_id->to_str();
75
76 ctx.rpc_ctx->set_response_json(body, HTTP_STATUS_OK);
77 return;
78 }
79 }
80 };
81 registry
83 "/service/transactions/{transactionId}",
84 HTTP_GET,
85 api_version_adapter(get_transaction_status),
86 no_auth_required)
88 .install();
89
90 auto get_commit = [&](auto& ctx, ApiVersion api_version) {
91 switch (api_version)
92 {
94 case ApiVersion::v1:
95 default:
96 {
97 // Lookup committed
98 ccf::View view = 0;
99 ccf::SeqNo seqno = 0;
100 auto result = registry.get_last_committed_txid_v1(view, seqno);
101 if (result != ccf::ApiResult::OK)
102 {
103 detail::set_gov_error(
104 ctx.rpc_ctx,
105 HTTP_STATUS_INTERNAL_SERVER_ERROR,
106 ccf::errors::InternalError,
107 fmt::format(
108 "get_last_committed_txid_v1 returned error: {}",
109 ccf::api_result_to_str(result)));
110 return;
111 }
112
113 // Lookup status
115 result = registry.get_status_for_txid_v1(view, seqno, status);
116 if (result != ccf::ApiResult::OK)
117 {
118 detail::set_gov_error(
119 ctx.rpc_ctx,
120 HTTP_STATUS_INTERNAL_SERVER_ERROR,
121 ccf::errors::InternalError,
122 fmt::format(
123 "get_status_for_txid_v1 returned error: {}",
124 ccf::api_result_to_str(result)));
125 return;
126 }
127
128 // Build response
129 auto body = nlohmann::json::object();
130
131 body["status"] = status;
132 body["transactionId"] = ccf::TxID{view, seqno};
133
134 ctx.rpc_ctx->set_response_json(body, HTTP_STATUS_OK);
135 return;
136 }
137 }
138 };
139 registry
141 "/service/transactions/commit",
142 HTTP_GET,
143 api_version_adapter(get_commit),
144 no_auth_required)
145 .set_openapi_hidden(true)
146 .install();
147 }
148}
Definition base_endpoint_registry.h:121
ApiResult get_status_for_txid_v1(ccf::View view, ccf::SeqNo seqno, ccf::TxStatus &tx_status)
Definition base_endpoint_registry.cpp:65
ApiResult get_last_committed_txid_v1(ccf::View &view, ccf::SeqNo &seqno)
Definition base_endpoint_registry.cpp:93
virtual Endpoint make_command_endpoint(const std::string &method, RESTVerb verb, const CommandEndpointFunction &f, const AuthnPolicies &ap)
Definition endpoint_registry.cpp:254
bool get_path_param(const ccf::PathParams &params, const std::string &param_name, T &value, std::string &error)
Definition endpoint_registry.h:64
Definition api_version.h:11
ApiVersion
Definition api_version.h:13
auto api_version_adapter(Fn &&f, ApiVersion min_accepted=ApiVersion::MIN)
Definition api_version.h:101
void init_transactions_handlers(ccf::BaseEndpointRegistry &registry)
Definition transactions.h:13
constexpr char const * api_result_to_str(ApiResult result)
Definition base_endpoint_registry.h:35
TxStatus
Definition tx_status.h:13
view
Definition signatures.h:54
seqno
Definition signatures.h:54
uint64_t View
Definition tx_id.h:23
@ error
Definition tls_session.h:23
uint64_t SeqNo
Definition tx_id.h:36
Definition tx_id.h:44
static std::optional< TxID > from_str(const std::string_view &sv)
Definition tx_id.h:53
Endpoint & set_openapi_hidden(bool hidden)
Definition endpoint.cpp:10
void install()
Definition endpoint.cpp:135