OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
validation.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef OPENCBDC_TX_SRC_TRANSACTION_VALIDATION_H_
7#define OPENCBDC_TX_SRC_TRANSACTION_VALIDATION_H_
8
9#include "transaction.hpp"
10
11#include <cassert>
12#include <memory>
13#include <optional>
14#include <secp256k1.h>
15#include <secp256k1_schnorrsig.h>
16#include <set>
17#include <variant>
18
21 enum class witness_program_type : uint8_t {
22 p2pk = 0x0
23 };
24
25 static constexpr auto p2pk_witness_prog_len
26 = sizeof(witness_program_type) + sizeof(pubkey_t);
27 static constexpr auto p2pk_witness_len = p2pk_witness_prog_len + sig_len;
28
30 enum class input_error_code : uint8_t {
34 };
35
37 enum class output_error_code : uint8_t {
39 };
40
45 struct input_error {
46 auto operator==(const input_error& rhs) const -> bool;
47
50
52 std::optional<output_error_code> m_data_err;
53
55 uint64_t m_idx{};
56 };
57
72
75 enum class tx_error_code : uint8_t {
76 no_inputs,
83 };
84
87 auto operator==(const witness_error& rhs) const -> bool;
88
91
93 uint64_t m_idx{};
94 };
95
97 struct output_error {
98 auto operator==(const output_error& rhs) const -> bool;
99
102
104 uint64_t m_idx{};
105 };
106
113 using tx_error = std::
114 variant<input_error, output_error, witness_error, tx_error_code>;
115
122 auto check_tx(const transaction::full_tx& tx) -> std::optional<tx_error>;
124 -> std::optional<tx_error>;
125 auto check_input_structure(const transaction::input& inp) -> std::optional<
126 std::pair<input_error_code, std::optional<output_error_code>>>;
128 -> std::optional<tx_error>;
129 // TODO: check input assumptions with flags for whether preconditions have
130 // already been checked.
131 auto check_witness(const transaction::full_tx& tx, size_t idx)
132 -> std::optional<witness_error_code>;
133 auto check_p2pk_witness(const transaction::full_tx& tx, size_t idx)
134 -> std::optional<witness_error_code>;
135 auto check_p2pk_witness_len(const transaction::full_tx& tx, size_t idx)
136 -> std::optional<witness_error_code>;
138 size_t idx)
139 -> std::optional<witness_error_code>;
141 size_t idx)
142 -> std::optional<witness_error_code>;
144 -> std::optional<tx_error>;
146 -> std::optional<tx_error>;
148 -> std::optional<tx_error>;
150 -> std::optional<tx_error>;
152 -> std::optional<output_error_code>;
153 auto get_p2pk_witness_commitment(const pubkey_t& payee) -> hash_t;
154 auto to_string(const tx_error& err) -> std::string;
155
164 const transaction::compact_tx& tx,
165 const std::unordered_set<pubkey_t, hashing::null>& pubkeys,
166 size_t threshold) -> bool;
167}
168
169#endif // OPENCBDC_TX_SRC_TRANSACTION_VALIDATION_H_
auto check_input_structure(const cbdc::transaction::input &inp) -> std::optional< std::pair< input_error_code, std::optional< output_error_code > > >
auto check_p2pk_witness_commitment(const cbdc::transaction::full_tx &tx, size_t idx) -> std::optional< witness_error_code >
auto check_input_set(const cbdc::transaction::full_tx &tx) -> std::optional< tx_error >
auto get_p2pk_witness_commitment(const pubkey_t &payee) -> hash_t
std:: variant< input_error, output_error, witness_error, tx_error_code > tx_error
An error that may occur when sentinels or clients statically validate a transaction.
auto to_string(cbdc::transaction::validation::tx_error_code err) -> std::string
auto check_tx(const cbdc::transaction::full_tx &tx) -> std::optional< tx_error >
Runs static validation checks on the given transaction.
auto check_attestations(const transaction::compact_tx &tx, const std::unordered_set< pubkey_t, hashing::null > &pubkeys, size_t threshold) -> bool
Validates the sentinel attestations attached to a compact transaction.
auto check_output_count(const cbdc::transaction::full_tx &tx) -> std::optional< tx_error >
auto check_witness_count(const cbdc::transaction::full_tx &tx) -> std::optional< tx_error >
tx_error_code
Types of errors that may occur when a sentinel statically validates a transaction.
@ missing_witness
The number of witnesses and inputs do not match.
@ value_overflow
The total value of inputs/outputs overflows a 64-bit integer.
@ asymmetric_values
The total values of inputs and outputs do not match.
auto check_p2pk_witness(const cbdc::transaction::full_tx &tx, size_t idx) -> std::optional< witness_error_code >
auto check_output_value(const cbdc::transaction::output &out) -> std::optional< output_error_code >
auto check_input_count(const cbdc::transaction::full_tx &tx) -> std::optional< tx_error >
witness_error_code
Types of errors that may occur when sentinels validate witness commitments.
@ invalid_signature
The witness's signature is invalid.
@ malformed
The witness's format appears invalid.
@ unknown_witness_program_type
The validation system does not recognize the provided witness_program_type.
@ invalid_public_key
The witness's public key is invalid.
@ program_mismatch
The witness's specified program doesn't match its commitment.
@ missing_witness_program_type
The witness did not provide a witness_program_type.
witness_program_type
Specifies how validators should interpret the witness program.
output_error_code
A transaction input validation error.
auto check_tx_structure(const cbdc::transaction::full_tx &tx) -> std::optional< tx_error >
auto check_p2pk_witness_signature(const cbdc::transaction::full_tx &tx, size_t idx) -> std::optional< witness_error_code >
auto check_p2pk_witness_len(const cbdc::transaction::full_tx &tx, size_t idx) -> std::optional< witness_error_code >
auto check_witness(const cbdc::transaction::full_tx &tx, size_t idx) -> std::optional< witness_error_code >
input_error_code
Types of input validation errors.
@ duplicate
More than one transaction input contains the same output.
@ data_error
A transaction input includes invalid output data.
auto check_in_out_set(const cbdc::transaction::full_tx &tx) -> std::optional< tx_error >
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
std::array< unsigned char, pubkey_len > pubkey_t
A public key of a public/private keypair.
Definition keys.hpp:25
A condensed, hash-only transaction representation.
A complete transaction.
An input for a new transaction.
An output of a transaction.
An error that may occur when sentinels validate inputs.
auto operator==(const input_error &rhs) const -> bool
input_error_code m_code
The type of input error.
uint64_t m_idx
The index of the input in the transaction.
std::optional< output_error_code > m_data_err
Additional error information.
An error that may occur when sentinels validate transaction outputs.
uint64_t m_idx
The index of the output in the transaction.
auto operator==(const output_error &rhs) const -> bool
output_error_code m_code
The type of output error.
An error that may occur when sentinels validate witness commitments.
auto operator==(const witness_error &rhs) const -> bool
uint64_t m_idx
The index of the witness in the transaction.
witness_error_code m_code
The type of witness error.