CCF
Loading...
Searching...
No Matches
default_on_commit.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
7#include "ccf/receipt.h"
8#include "ccf/rpc_context.h"
9
10namespace ccf::samples
11{
12 // A simple ConsensusCommittedEndpointFunction that returns the original
13 // response once committed, or an error if the transaction was invalidated.
15 {
16 if (info.status == ccf::FinalTxStatus::Invalid)
17 {
18 info.rpc_ctx->set_error(
19 HTTP_STATUS_INTERNAL_SERVER_ERROR,
20 ccf::errors::TransactionInvalid,
21 fmt::format(
22 "While waiting for TxID {} to commit, it was invalidated",
23 info.tx_id.to_str()));
24 }
25
26 // Else leave the original response untouched, and return it now
27 }
28
29 // Returns a ConsensusCommittedEndpointFunction that builds a COSE receipt
30 // for the committed transaction and returns it as the response body.
33 {
34 return [&context](ccf::endpoints::CommittedTxInfo& info) {
35 if (info.status == ccf::FinalTxStatus::Invalid)
36 {
37 info.rpc_ctx->set_error(
38 HTTP_STATUS_INTERNAL_SERVER_ERROR,
39 ccf::errors::TransactionInvalid,
40 fmt::format(
41 "While waiting for TxID {} to commit, it was invalidated",
42 info.tx_id.to_str()));
43 return;
44 }
45
46 auto receipt =
48 if (receipt == nullptr)
49 {
50 return;
51 }
52
53 auto cose_receipt = ccf::describe_cose_receipt_v1(*receipt);
54 if (!cose_receipt.has_value())
55 {
56 info.rpc_ctx->set_error(
57 HTTP_STATUS_INTERNAL_SERVER_ERROR,
58 ccf::errors::InternalError,
59 "No COSE receipt produced for this transaction");
60 return;
61 }
62
63 info.rpc_ctx->set_response_status(HTTP_STATUS_OK);
64 info.rpc_ctx->set_response_header(
65 ccf::http::headers::CONTENT_TYPE,
66 ccf::http::headervalues::contenttype::COSE);
67 info.rpc_ctx->set_response_body(cose_receipt.value());
68 };
69 }
70}
std::function< void(CommittedTxInfo &info)> ConsensusCommittedEndpointFunction
Definition endpoint_context.h:82
ccf::TxReceiptImplPtr build_receipt_for_committed_tx(ccf::AbstractNodeContext &context, CommittedTxInfo &info)
Definition endpoint_registry.cpp:208
Definition default_on_commit.h:11
void default_respond_on_commit(ccf::endpoints::CommittedTxInfo &info)
Definition default_on_commit.h:14
ccf::endpoints::ConsensusCommittedEndpointFunction make_respond_with_receipt_on_commit(ccf::AbstractNodeContext &context)
Definition default_on_commit.h:32
std::optional< SerialisedCoseReceipt > describe_cose_receipt_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:274
Definition node_context.h:12
std::string to_str() const
Definition tx_id.h:48
Definition endpoint_context.h:72
std::shared_ptr< ccf::RpcContext > rpc_ctx
Definition endpoint_context.h:73
ccf::FinalTxStatus status
Definition endpoint_context.h:75
ccf::TxID tx_id
Definition endpoint_context.h:74