Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
script_segwit_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2012-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 <script/script.h>
7
8#include <boost/test/unit_test.hpp>
9
11
13{
14 uint256 dummy;
16 p2wsh << OP_0 << ToByteVector(dummy);
17 BOOST_CHECK(p2wsh.IsPayToWitnessScriptHash());
18
19 std::vector<unsigned char> bytes = {OP_0, 32};
20 bytes.insert(bytes.end(), 32, 0);
21 BOOST_CHECK(CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
22}
23
31
39
41{
42 uint256 dummy;
44 notp2wsh << OP_0 << OP_NOP << ToByteVector(dummy);
45 BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());
46}
47
53
55{
56 // A script is not P2WSH if OP_PUSHDATA is used to push the hash.
57 std::vector<unsigned char> bytes = {OP_0, OP_PUSHDATA1, 32};
58 bytes.insert(bytes.end(), 32, 0);
59 BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
60
61 bytes = {OP_0, OP_PUSHDATA2, 32, 0};
62 bytes.insert(bytes.end(), 32, 0);
63 BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
64
65 bytes = {OP_0, OP_PUSHDATA4, 32, 0, 0, 0};
66 bytes.insert(bytes.end(), 32, 0);
67 BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());
68}
69
70namespace {
71
72bool IsExpectedWitnessProgram(const CScript& script, const int expectedVersion, const std::vector<unsigned char>& expectedProgram)
73{
74 int actualVersion;
75 std::vector<unsigned char> actualProgram;
76 if (!script.IsWitnessProgram(actualVersion, actualProgram)) {
77 return false;
78 }
81 return true;
82}
83
85{
86 int dummyVersion;
87 std::vector<unsigned char> dummyProgram;
88 return !script.IsWitnessProgram(dummyVersion, dummyProgram);
89}
90
91} // anonymous namespace
92
94{
95 // Witness programs have a minimum data push of 2 bytes.
96 std::vector<unsigned char> program = {42, 18};
98 wit << OP_0 << program;
100
101 wit.clear();
102 // Witness programs have a maximum data push of 40 bytes.
103 program.resize(40);
104 wit << OP_16 << program;
106
107 program.resize(32);
108 std::vector<unsigned char> bytes = {OP_5, static_cast<unsigned char>(program.size())};
109 bytes.insert(bytes.end(), program.begin(), program.end());
110 BOOST_CHECK(IsExpectedWitnessProgram(CScript(bytes.begin(), bytes.end()), 5, program));
111}
112
120
122{
123 std::vector<unsigned char> program(1);
125 nowit << OP_0 << program;
127
128 nowit.clear();
129 program.resize(41);
130 nowit << OP_0 << program;
132}
133
135{
136 std::vector<unsigned char> program(10);
138 nowit << OP_0 << OP_NOP << program;
140}
141
147
149{
150 // A script is no witness program if OP_PUSHDATA is used to push the hash.
151 std::vector<unsigned char> bytes = {OP_0, OP_PUSHDATA1, 32};
152 bytes.insert(bytes.end(), 32, 0);
153 BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));
154
155 bytes = {OP_0, OP_PUSHDATA2, 32, 0};
156 bytes.insert(bytes.end(), 32, 0);
157 BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));
158
159 bytes = {OP_0, OP_PUSHDATA4, 32, 0, 0, 0};
160 bytes.insert(bytes.end(), 32, 0);
161 BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));
162}
163
Serialized script, used inside transaction inputs and outputs.
Definition script.h:405
bool IsPayToWitnessScriptHash() const
Definition script.cpp:233
160-bit opaque blob.
Definition uint256.h:183
256-bit opaque blob.
Definition uint256.h:195
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:17
#define BOOST_CHECK(expr)
Definition object.cpp:16
@ OP_PUSHDATA4
Definition script.h:80
@ OP_1NEGATE
Definition script.h:81
@ OP_16
Definition script.h:99
@ OP_NOP
Definition script.h:102
@ OP_1
Definition script.h:83
@ OP_PUSHDATA1
Definition script.h:78
@ OP_PUSHDATA2
Definition script.h:79
@ OP_0
Definition script.h:76
@ OP_5
Definition script.h:88
std::vector< unsigned char > ToByteVector(const T &in)
Definition script.h:67
BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Valid)
Basic testing setup.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73