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