Skip to content

Commit 71eb040

Browse files
committed
common: implement op_return test.
Since we included the spec for it, this is a good time to implement it. I also asked chatgpt to write some unit tests. I had to mangle them a bit, but it probably saved me a few minutes. Signed-off-by: Rusty Russell <[email protected]>
1 parent 733efcf commit 71eb040

8 files changed

+232
-8
lines changed

common/shutdown_scriptpubkey.c

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
#include "config.h"
22
#include <bitcoin/script.h>
33
#include <common/shutdown_scriptpubkey.h>
4+
#include <wire/wire.h>
5+
6+
/* BOLT #2:
7+
* 4. if (and only if) `option_simple_close` is negotiated:
8+
* * `OP_RETURN` followed by one of:
9+
* * `6` to `75` inclusive followed by exactly that many bytes
10+
* * `76` followed by `76` to `80` followed by exactly that many bytes
11+
*/
12+
static bool is_valid_op_return(const u8 *scriptpubkey, size_t scriptpubkey_len)
13+
{
14+
u8 v;
15+
16+
if (fromwire_u8(&scriptpubkey, &scriptpubkey_len) != OP_RETURN)
17+
return false;
18+
19+
v = fromwire_u8(&scriptpubkey, &scriptpubkey_len);
20+
if (v >= 6 && v <= 75)
21+
return scriptpubkey_len == v;
22+
if (v == 76) {
23+
v = fromwire_u8(&scriptpubkey, &scriptpubkey_len);
24+
if (v >= 76 && v <= 80)
25+
return scriptpubkey_len == v;
26+
}
27+
return false;
28+
}
429

530
/* BOLT #2:
631
* 3. if (and only if) `option_shutdown_anysegwit` is negotiated:
@@ -48,7 +73,8 @@ static bool is_valid_witnessprog(const u8 *scriptpubkey, size_t scriptpubkey_len
4873

4974
bool valid_shutdown_scriptpubkey(const u8 *scriptpubkey,
5075
bool anysegwit,
51-
bool allow_oldstyle)
76+
bool allow_oldstyle,
77+
bool option_simple_close)
5278
{
5379
const size_t script_len = tal_bytelen(scriptpubkey);
5480
if (allow_oldstyle) {
@@ -59,5 +85,6 @@ bool valid_shutdown_scriptpubkey(const u8 *scriptpubkey,
5985

6086
return is_p2wpkh(scriptpubkey, script_len, NULL)
6187
|| is_p2wsh(scriptpubkey, script_len, NULL)
62-
|| (anysegwit && is_valid_witnessprog(scriptpubkey, script_len));
88+
|| (anysegwit && is_valid_witnessprog(scriptpubkey, script_len))
89+
|| (option_simple_close && is_valid_op_return(scriptpubkey, script_len));
6390
}

common/shutdown_scriptpubkey.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* never will send such a thing!) if they're not using anchors. */
2626
bool valid_shutdown_scriptpubkey(const u8 *scriptpubkey,
2727
bool anysegwit,
28-
bool allow_oldstyle);
28+
bool allow_oldstyle,
29+
bool option_simple_close);
2930

3031
#endif /* LIGHTNING_COMMON_SHUTDOWN_SCRIPTPUBKEY_H */

common/test/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,6 @@ common/test/run-trace: \
125125
common/test/run-htable: \
126126
common/pseudorand.o
127127

128+
common/test/run-shutdown_scriptpubkey: wire/towire.o wire/fromwire.o
129+
128130
check-units: $(COMMON_TEST_PROGRAMS:%=unittest/%)
+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
#include "config.h"
2+
#include "../shutdown_scriptpubkey.c"
3+
#include <assert.h>
4+
#include <ccan/array_size/array_size.h>
5+
#include <ccan/err/err.h>
6+
#include <common/amount.h>
7+
#include <common/setup.h>
8+
#include <common/utils.h>
9+
#include <stdio.h>
10+
#include <wire/wire.h>
11+
12+
/* AUTOGENERATED MOCKS START */
13+
/* Generated stub for amount_asset_is_main */
14+
bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)
15+
{ fprintf(stderr, "amount_asset_is_main called!\n"); abort(); }
16+
/* Generated stub for amount_asset_to_sat */
17+
struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED)
18+
{ fprintf(stderr, "amount_asset_to_sat called!\n"); abort(); }
19+
/* Generated stub for amount_feerate */
20+
bool amount_feerate(u32 *feerate UNNEEDED, struct amount_sat fee UNNEEDED, size_t weight UNNEEDED)
21+
{ fprintf(stderr, "amount_feerate called!\n"); abort(); }
22+
/* Generated stub for amount_sat */
23+
struct amount_sat amount_sat(u64 satoshis UNNEEDED)
24+
{ fprintf(stderr, "amount_sat called!\n"); abort(); }
25+
/* Generated stub for amount_sat_add */
26+
bool amount_sat_add(struct amount_sat *val UNNEEDED,
27+
struct amount_sat a UNNEEDED,
28+
struct amount_sat b UNNEEDED)
29+
{ fprintf(stderr, "amount_sat_add called!\n"); abort(); }
30+
/* Generated stub for amount_sat_eq */
31+
bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
32+
{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); }
33+
/* Generated stub for amount_sat_greater_eq */
34+
bool amount_sat_greater_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
35+
{ fprintf(stderr, "amount_sat_greater_eq called!\n"); abort(); }
36+
/* Generated stub for amount_sat_sub */
37+
bool amount_sat_sub(struct amount_sat *val UNNEEDED,
38+
struct amount_sat a UNNEEDED,
39+
struct amount_sat b UNNEEDED)
40+
{ fprintf(stderr, "amount_sat_sub called!\n"); abort(); }
41+
/* Generated stub for amount_sat_to_asset */
42+
struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u8 *asset UNNEEDED)
43+
{ fprintf(stderr, "amount_sat_to_asset called!\n"); abort(); }
44+
/* Generated stub for amount_tx_fee */
45+
struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED)
46+
{ fprintf(stderr, "amount_tx_fee called!\n"); abort(); }
47+
/* AUTOGENERATED MOCKS END */
48+
49+
/* Thanks ChatGPT! */
50+
static const u8 *construct_script(const tal_t *ctx,
51+
const u8 *data, size_t data_len)
52+
{
53+
u8 *script = tal_arr(ctx, u8, 0);
54+
towire_u8(&script, OP_RETURN);
55+
if (data_len >= 6 && data_len <= 75) {
56+
towire_u8(&script, data_len);
57+
towire(&script, data, data_len);
58+
} else if (data_len >= 76 && data_len <= 80) {
59+
towire_u8(&script, 76);
60+
towire_u8(&script, data_len);
61+
towire(&script, data, data_len);
62+
} else {
63+
return tal_free(script); // Invalid case
64+
}
65+
return script;
66+
}
67+
68+
static void test_valid_op_returns(void)
69+
{
70+
u8 data[80];
71+
const u8 *script;
72+
73+
for (size_t i = 6; i <= 80; i++) {
74+
memset(data, i, sizeof(data));
75+
script = construct_script(tmpctx, data, i);
76+
assert(is_valid_op_return(script, tal_bytelen(script)));
77+
}
78+
}
79+
80+
static void test_invalid_op_return_too_short(void)
81+
{
82+
u8 data[80];
83+
const u8 *script;
84+
85+
for (size_t i = 0; i < 6; i++) {
86+
memset(data, i, sizeof(data));
87+
script = construct_script(tmpctx, data, i);
88+
assert(!is_valid_op_return(script, tal_bytelen(script)));
89+
}
90+
}
91+
92+
static void test_invalid_op_return_too_long(void)
93+
{
94+
u8 data[100];
95+
const u8 *script;
96+
97+
for (size_t i = 81; i < sizeof(data); i++) {
98+
memset(data, i, sizeof(data));
99+
script = construct_script(tmpctx, data, i);
100+
assert(!is_valid_op_return(script, tal_bytelen(script)));
101+
}
102+
}
103+
104+
// Test case: Invalid OP_RETURN with incorrect push length (e.g., 77 bytes using wrong prefix)
105+
static void test_invalid_op_return_wrong_push_length(void)
106+
{
107+
u8 script[90] = {0x6a, 0x76, 0x77}; // Invalid push of 77 bytes
108+
assert(!is_valid_op_return(script, 3 + 77));
109+
}
110+
111+
// Test case: Invalid OP_RETURN with incorrect OP_RETURN opcode
112+
static void test_invalid_op_return_wrong_opcode(void)
113+
{
114+
u8 script[10] = {0x00, 0x06, 1, 2, 3, 4, 5, 6}; // 0x00 instead of 0x6a
115+
assert(!is_valid_op_return(script, 8));
116+
}
117+
118+
// Test case: Invalid OP_RETURN with no data
119+
static void test_invalid_op_return_empty(void)
120+
{
121+
u8 script[1] = {0x6a}; // Only OP_RETURN, no data
122+
assert(!is_valid_op_return(script, 1));
123+
}
124+
125+
static const u8 *construct_witness_script(const tal_t *ctx, u8 version,
126+
const u8 *data, size_t data_len)
127+
{
128+
u8 *script = tal_arr(ctx, u8, 0);
129+
130+
if (version < OP_1 || version > OP_16 || data_len < 2 || data_len > 40)
131+
return tal_free(script); // Invalid case
132+
133+
towire_u8(&script, version); // OP_1 to OP_16
134+
towire_u8(&script, data_len); // Push length
135+
towire(&script, data, data_len); // Data
136+
137+
return script;
138+
}
139+
140+
static void test_valid_witnessprogs(void)
141+
{
142+
u8 data[40];
143+
const u8 *script;
144+
145+
for (u8 version = OP_1; version <= OP_16; version++) {
146+
for (size_t i = 2; i <= 40; i++) {
147+
memset(data, i, sizeof(data));
148+
script = construct_witness_script(tmpctx, version, data, i);
149+
assert(is_valid_witnessprog(script, tal_bytelen(script)));
150+
}
151+
}
152+
}
153+
154+
static void test_invalid_witnessprogs(void)
155+
{
156+
u8 data[41];
157+
const u8 *script;
158+
159+
// Test: Invalid versions (0 and >16)
160+
memset(data, 0xAA, sizeof(data));
161+
script = construct_witness_script(tmpctx, OP_0, data, 20);
162+
assert(!is_valid_witnessprog(script, tal_bytelen(script)));
163+
164+
script = construct_witness_script(tmpctx, OP_16 + 1, data, 20);
165+
assert(!is_valid_witnessprog(script, tal_bytelen(script)));
166+
167+
// Test: Invalid data lengths (1 byte and >40 bytes)
168+
script = construct_witness_script(tmpctx, OP_1, data, 1);
169+
assert(!is_valid_witnessprog(script, tal_bytelen(script)));
170+
171+
script = construct_witness_script(tmpctx, OP_2, data, 41);
172+
assert(!is_valid_witnessprog(script, tal_bytelen(script)));
173+
174+
// Test: Completely empty script (invalid)
175+
script = tal_arr(tmpctx, u8, 0);
176+
assert(!is_valid_witnessprog(script, tal_bytelen(script)));
177+
}
178+
179+
// Test runner
180+
int main(int argc, char *argv[])
181+
{
182+
common_setup(argv[0]);
183+
184+
test_valid_op_returns();
185+
test_invalid_op_return_too_short();
186+
test_invalid_op_return_too_long();
187+
test_invalid_op_return_wrong_push_length();
188+
test_invalid_op_return_wrong_opcode();
189+
test_invalid_op_return_empty();
190+
test_valid_witnessprogs();
191+
test_invalid_witnessprogs();
192+
common_shutdown();
193+
return 0;
194+
}

lightningd/channel_control.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ static void peer_got_shutdown(struct channel *channel, const u8 *msg)
12271227
* - if the `scriptpubkey` is not in one of the above forms:
12281228
* - SHOULD send a `warning`.
12291229
*/
1230-
if (!valid_shutdown_scriptpubkey(scriptpubkey, anysegwit, !anchors)) {
1230+
if (!valid_shutdown_scriptpubkey(scriptpubkey, anysegwit, !anchors, false)) {
12311231
u8 *warning = towire_warningfmt(NULL,
12321232
&channel->cid,
12331233
"Bad shutdown scriptpubkey %s",

lightningd/closing_control.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,10 @@ static struct command_result *json_close(struct command *cmd,
759759
/* In theory, this could happen if the peer had anysegwit when channel was
760760
* established, and doesn't now. Doesn't happen, and if it did we could
761761
* provide a new address manually. */
762-
if (!valid_shutdown_scriptpubkey(close_to_script, anysegwit, false)) {
762+
if (!valid_shutdown_scriptpubkey(close_to_script, anysegwit, false, false)) {
763763
/* Explicit check for future segwits. */
764764
if (!anysegwit &&
765-
valid_shutdown_scriptpubkey(close_to_script, true, false)) {
765+
valid_shutdown_scriptpubkey(close_to_script, true, false, false)) {
766766
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
767767
"Peer does not allow v1+ shutdown addresses");
768768
}

lightningd/dual_open_control.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ static void handle_peer_wants_to_close(struct subd *dualopend,
15881588
* - if the `scriptpubkey` is not in one of the above forms:
15891589
* - SHOULD send a `warning`
15901590
*/
1591-
if (!valid_shutdown_scriptpubkey(scriptpubkey, anysegwit, !anchors)) {
1591+
if (!valid_shutdown_scriptpubkey(scriptpubkey, anysegwit, !anchors, false)) {
15921592
u8 *warning = towire_warningfmt(NULL,
15931593
&channel->cid,
15941594
"Bad shutdown scriptpubkey %s",

openingd/common.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ char *validate_remote_upfront_shutdown(const tal_t *ctx,
229229
*state_script = tal_steal(ctx, shutdown_scriptpubkey);
230230

231231
if (shutdown_scriptpubkey
232-
&& !valid_shutdown_scriptpubkey(shutdown_scriptpubkey, anysegwit, !anchors))
232+
&& !valid_shutdown_scriptpubkey(shutdown_scriptpubkey, anysegwit, !anchors, false))
233233
return tal_fmt(tmpctx,
234234
"Unacceptable upfront_shutdown_script %s",
235235
tal_hex(tmpctx, shutdown_scriptpubkey));

0 commit comments

Comments
 (0)