Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
fuzz
hex.cpp
Go to the documentation of this file.
1
// Copyright (c) 2019-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <
core_io.h
>
6
#include <
primitives/block.h
>
7
#include <
primitives/transaction_identifier.h
>
8
#include <
pubkey.h
>
9
#include <rpc/util.h>
10
#include <
test/fuzz/fuzz.h
>
11
#include <
uint256.h
>
12
#include <
univalue.h
>
13
#include <
util/strencodings.h
>
14
15
#include <algorithm>
16
#include <cassert>
17
#include <cstdint>
18
#include <string>
19
#include <vector>
20
21
FUZZ_TARGET
(hex)
22
{
23
const
std::string random_hex_string(buffer.begin(), buffer.end());
24
const
std::vector<unsigned char> data =
ParseHex
(random_hex_string);
25
const
std::vector<std::byte> bytes{
ParseHex<std::byte>
(random_hex_string)};
26
assert
(std::ranges::equal(std::as_bytes(std::span{data}), bytes));
27
const
std::string hex_data =
HexStr
(data);
28
if
(
IsHex
(random_hex_string)) {
29
assert
(
ToLower
(random_hex_string) == hex_data);
30
}
31
if
(
uint256::FromHex
(random_hex_string)) {
32
assert
(random_hex_string.length() == 64);
33
assert
(
Txid::FromHex
(random_hex_string));
34
assert
(
Wtxid::FromHex
(random_hex_string));
35
assert
(
uint256::FromUserHex
(random_hex_string));
36
}
37
if
(
const
auto
result{
uint256::FromUserHex
(random_hex_string)}) {
38
const
auto
result_string{result->ToString()};
// ToString() returns a fixed-length string without "0x" prefix
39
assert
(result_string.length() == 64);
40
assert
(
IsHex
(result_string));
41
assert
(
TryParseHex
(result_string));
42
assert
(
Txid::FromHex
(result_string));
43
assert
(
Wtxid::FromHex
(result_string));
44
assert
(
uint256::FromHex
(result_string));
45
}
46
try
{
47
(void)
HexToPubKey
(random_hex_string);
48
}
catch
(
const
UniValue
&) {
49
}
50
CBlockHeader
block_header;
51
(void)
DecodeHexBlockHeader
(block_header, random_hex_string);
52
CBlock
block;
53
(void)
DecodeHexBlk
(block, random_hex_string);
54
}
block.h
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition
block.h:27
CBlock
Definition
block.h:74
UniValue
Definition
univalue.h:22
transaction_identifier< false >::FromHex
static std::optional< transaction_identifier > FromHex(std::string_view hex)
Definition
transaction_identifier.h:54
uint256::FromUserHex
static std::optional< uint256 > FromUserHex(std::string_view str)
Definition
uint256.h:198
uint256::FromHex
static std::optional< uint256 > FromHex(std::string_view str)
Definition
uint256.h:197
DecodeHexBlockHeader
bool DecodeHexBlockHeader(CBlockHeader &header, const std::string &hex_header)
Definition
core_io.cpp:237
DecodeHexBlk
bool DecodeHexBlk(CBlock &block, const std::string &strHexBlk)
Definition
core_io.cpp:250
core_io.h
fuzz.h
FUZZ_TARGET
#define FUZZ_TARGET(...)
Definition
fuzz.h:35
HexStr
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition
hex_base.cpp:30
pubkey.h
HexToPubKey
CPubKey HexToPubKey(const std::string &hex_in)
Definition
util.cpp:219
strencodings.h
ParseHex
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition
strencodings.h:69
transaction_identifier.h
uint256.h
univalue.h
IsHex
bool IsHex(std::string_view str)
Definition
strencodings.cpp:41
TryParseHex
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
Definition
strencodings.cpp:50
ToLower
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
Definition
strencodings.cpp:363
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.17.0