Bitcoin ABC 0.30.7
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)",
30 args.AddArg(
31 "-inputindex",
32 strprintf("Input index to run (default: %d)", DEFAULT_INPUT_INDEX),
34 args.AddArg("-scriptpubkey",
35 "Hex of the scriptPubKey of the output being spent (required)",
37 args.AddArg("-value",
38 "Value (in sats) of the output being spent (required)",
40 args.AddArg("-format",
41 strprintf("Output format for the debug trace (Options: human, "
42 "csv. Default: %d)",
45}
46
47int main(int argc, char *argv[]) {
48 ArgsManager args;
49 SetupHelpOptions(args);
50 SetupIguanaArgs(args);
51
52 std::string error;
53 if (!args.ParseParameters(argc, argv, error)) {
54 std::cerr << "Error parsing command line arguments: " << error
55 << std::endl;
56 return -1;
57 }
58
59 if (args.GetBoolArg("-version")) {
60 std::cout << "Iguana " << FormatFullVersion() << std::endl;
61 return 0;
62 }
63
64 if (HelpRequested(args)) {
65 std::cout << "Usage: iguana [options]" << std::endl;
66 std::cout << args.GetHelpMessage();
67 return 0;
68 }
69
70 const std::string outputFormat = args.GetArg("-format", DEFAULT_FORMAT);
71 std::unique_ptr<IguanaFormatter> formatter;
72 if (outputFormat == "human") {
73 formatter.reset(new FormatterHumanReadable());
74 } else if (outputFormat == "csv") {
75 formatter.reset(new FormatterCsv());
76 } else {
77 std::cerr << "Unsupported output format " << outputFormat << std::endl;
78 return -1;
79 }
80
81 std::vector<std::string> missingArgs;
82 if (!args.IsArgSet("-tx")) {
83 missingArgs.push_back("-tx");
84 }
85 if (!args.IsArgSet("-scriptpubkey")) {
86 missingArgs.push_back("-scriptpubkey");
87 }
88 if (!args.IsArgSet("-value")) {
89 missingArgs.push_back("-value");
90 }
91 if (!missingArgs.empty()) {
92 std::cerr << "Missing required args " << Join(missingArgs, ", ") << ". "
93 << "Provide -h to see a description for each." << std::endl;
94 return -1;
95 }
96
97 std::string tx_hex = args.GetArg("-tx", "");
98 std::vector<uint8_t> tx_raw = ParseHex(tx_hex);
99 CDataStream tx_stream(MakeByteSpan(tx_raw), 0, 0);
101 tx_stream >> tx;
102
103 const int64_t inputIndex =
104 args.GetIntArg("-inputindex", DEFAULT_INPUT_INDEX);
105
106 if (inputIndex < 0 || tx.vin.size() <= (size_t)inputIndex) {
107 std::cerr << "Transaction doesn't have input index " << inputIndex
108 << std::endl;
109 return -1;
110 }
111
112 const Amount value = args.GetIntArg("-value", 0) * Amount::satoshi();
113
114 std::string scriptPubKeyHex = args.GetArg("-scriptpubkey", "");
115 std::vector<uint8_t> scriptPubKeyRaw = ParseHex(scriptPubKeyHex);
116 CScript scriptPubKey(scriptPubKeyRaw.begin(), scriptPubKeyRaw.end());
117 CTxOut txout(value, scriptPubKey);
118
119 const uint32_t flags = STANDARD_SCRIPT_VERIFY_FLAGS;
120
121 ECCVerifyHandle ecc_handle;
122
123 IguanaInterpreter iguana(tx, inputIndex, txout, flags);
124 IguanaResult result = iguana.Run();
125
126 if (!formatter->Format(result)) {
127 return -1;
128 }
129
130 return 0;
131}
bool HelpRequested(const ArgsManager &args)
Definition: args.cpp:732
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: args.cpp:737
int flags
Definition: bitcoin-tx.cpp:541
@ ALLOW_ANY
Definition: args.h:103
@ ALLOW_INT
Definition: args.h:101
@ ALLOW_STRING
Definition: args.h:102
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:201
std::string GetHelpMessage() const
Get the help string.
Definition: args.cpp:653
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:381
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:526
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:494
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:556
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:620
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:177
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
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:223
std::string FormatFullVersion()
int main(int argc, char *argv[])
Definition: iguana.cpp:47
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
bool error(const char *fmt, const Args &...args)
Definition: logging.h:263
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:301
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)