Bitcoin ABC 0.32.4
P2P Digital Currency
iguana.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 The Bitcoin 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 <clientversion.h>
6#include <common/args.h>
7#include <iguana_formatter.h>
9#include <memory>
10#include <policy/policy.h>
11#include <span.h>
12#include <streams.h>
13#include <tinyformat.h>
14#include <util/strencodings.h>
15#include <util/string.h>
16#include <util/translation.h>
17
18#include <iostream>
19
20const std::function<std::string(const char *)> G_TRANSLATION_FUN = nullptr;
21
22const int64_t DEFAULT_INPUT_INDEX = 0;
23const std::string DEFAULT_FORMAT = "human";
24
26 args.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY,
28 args.AddArg("-tx", "Raw tx hex to run the debugger for (required)",
31 args.AddArg(
32 "-inputindex",
33 strprintf("Input index to run (default: %d)", DEFAULT_INPUT_INDEX),
36 args.AddArg("-scriptpubkey",
37 "Hex of the scriptPubKey of the output being spent (required)",
40 args.AddArg("-value",
41 "Value (in sats) of the output being spent (required)",
44 args.AddArg("-format",
45 strprintf("Output format for the debug trace (Options: human, "
46 "csv. Default: %d)",
50}
51
52int main(int argc, char *argv[]) {
53 ArgsManager args;
54 SetupHelpOptions(args);
55 SetupIguanaArgs(args);
56
57 std::string error;
58 if (!args.ParseParameters(argc, argv, error)) {
59 std::cerr << "Error parsing command line arguments: " << error
60 << std::endl;
61 return -1;
62 }
63
64 if (args.GetBoolArg("-version")) {
65 std::cout << "Iguana " << FormatFullVersion() << std::endl;
66 return 0;
67 }
68
69 if (HelpRequested(args)) {
70 std::cout << "Usage: iguana [options]" << std::endl;
71 std::cout << args.GetHelpMessage();
72 return 0;
73 }
74
75 const std::string outputFormat = args.GetArg("-format", DEFAULT_FORMAT);
76 std::unique_ptr<IguanaFormatter> formatter;
77 if (outputFormat == "human") {
78 formatter.reset(new FormatterHumanReadable());
79 } else if (outputFormat == "csv") {
80 formatter.reset(new FormatterCsv());
81 } else {
82 std::cerr << "Unsupported output format " << outputFormat << std::endl;
83 return -1;
84 }
85
86 std::vector<std::string> missingArgs;
87 if (!args.IsArgSet("-tx")) {
88 missingArgs.push_back("-tx");
89 }
90 if (!args.IsArgSet("-scriptpubkey")) {
91 missingArgs.push_back("-scriptpubkey");
92 }
93 if (!args.IsArgSet("-value")) {
94 missingArgs.push_back("-value");
95 }
96 if (!missingArgs.empty()) {
97 std::cerr << "Missing required args " << Join(missingArgs, ", ") << ". "
98 << "Provide -h to see a description for each." << std::endl;
99 return -1;
100 }
101
102 std::string tx_hex = args.GetArg("-tx", "");
103 std::vector<uint8_t> tx_raw = ParseHex(tx_hex);
104 DataStream tx_stream(MakeByteSpan(tx_raw));
106 tx_stream >> tx;
107
108 const int64_t inputIndex =
109 args.GetIntArg("-inputindex", DEFAULT_INPUT_INDEX);
110
111 if (inputIndex < 0 || tx.vin.size() <= (size_t)inputIndex) {
112 std::cerr << "Transaction doesn't have input index " << inputIndex
113 << std::endl;
114 return -1;
115 }
116
117 const Amount value = args.GetIntArg("-value", 0) * Amount::satoshi();
118
119 std::string scriptPubKeyHex = args.GetArg("-scriptpubkey", "");
120 std::vector<uint8_t> scriptPubKeyRaw = ParseHex(scriptPubKeyHex);
121 CScript scriptPubKey(scriptPubKeyRaw.begin(), scriptPubKeyRaw.end());
122 CTxOut txout(value, scriptPubKey);
123
124 const uint32_t flags = STANDARD_SCRIPT_VERIFY_FLAGS;
125
126 ECCVerifyHandle ecc_handle;
127
128 IguanaInterpreter iguana(tx, inputIndex, txout, flags);
129 IguanaResult result = iguana.Run();
130
131 if (!formatter->Format(result)) {
132 return -1;
133 }
134
135 return 0;
136}
bool HelpRequested(const ArgsManager &args)
Definition: args.cpp:701
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: args.cpp:706
int flags
Definition: bitcoin-tx.cpp:542
@ ALLOW_ANY
disable validation
Definition: args.h:114
@ DISALLOW_NEGATION
unimplemented, draft implementation in #16545
Definition: args.h:124
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:211
std::string GetHelpMessage() const
Get the help string.
Definition: args.cpp:622
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:372
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:495
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:463
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:525
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:589
A mutable version of CTransaction.
Definition: transaction.h:274
std::vector< CTxIn > vin
Definition: transaction.h:276
An output of a transaction.
Definition: transaction.h:128
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:173
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:223
std::string FormatFullVersion()
int main(int argc, char *argv[])
Definition: iguana.cpp:52
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate string to current locale using Qt.
Definition: iguana.cpp:20
const int64_t DEFAULT_INPUT_INDEX
Definition: iguana.cpp:22
const std::string DEFAULT_FORMAT
Definition: iguana.cpp:23
void SetupIguanaArgs(ArgsManager &args)
Definition: iguana.cpp:25
def iguana(*args, expected_stderr="", expected_returncode=None)
Definition: test_iguana.py:33
static constexpr uint32_t STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
Definition: policy.h:91
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:302
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:63
Definition: amount.h:19
static constexpr Amount satoshi() noexcept
Definition: amount.h:33
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
template std::vector< std::byte > ParseHex(std::string_view)