From e0442782c8c04bdc48c2743e70f46e41c507e5f4 Mon Sep 17 00:00:00 2001 From: Rob N Date: Tue, 19 Nov 2024 12:42:55 -1000 Subject: [PATCH] fix(script): account for data pushing opcodes in `is_standard_op_return` While I believe the original commit used 80 bytes for the entire script as the limit, Bitcoin Core as of [this commit](https://github.com/bitcoin/bitcoin/blame/7a172c76d2361fc3cdf6345590e26c79a7821672/src/policy/policy.h) will relay OP_RETURN outputs as long as the data itself is not above 80 bytes, meaning a script of maximum size 83 bytes should be standard. --- bitcoin/src/blockdata/script/borrowed.rs | 4 ++-- bitcoin/src/blockdata/script/tests.rs | 6 ++++-- bitcoin/src/policy.rs | 3 +++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/bitcoin/src/blockdata/script/borrowed.rs b/bitcoin/src/blockdata/script/borrowed.rs index e1fe91b78..a922d7313 100644 --- a/bitcoin/src/blockdata/script/borrowed.rs +++ b/bitcoin/src/blockdata/script/borrowed.rs @@ -12,7 +12,7 @@ use super::{ use crate::consensus::Encodable; use crate::opcodes::all::*; use crate::opcodes::{self, Opcode}; -use crate::policy::DUST_RELAY_TX_FEE; +use crate::policy::{DUST_RELAY_TX_FEE, MAX_OP_RETURN_RELAY}; use crate::prelude::{sink, DisplayHex, String, ToString}; use crate::taproot::{LeafVersion, TapLeafHash, TapLeafHashExt as _}; use crate::FeeRate; @@ -217,7 +217,7 @@ crate::internal_macros::define_extension_trait! { /// What this function considers to be standard may change without warning pending Bitcoin Core /// changes. #[inline] - fn is_standard_op_return(&self) -> bool { self.is_op_return() && self.len() <= 80 } + fn is_standard_op_return(&self) -> bool { self.is_op_return() && self.len() <= MAX_OP_RETURN_RELAY } /// Checks whether a script is trivially known to have no satisfying input. /// diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs index 5a3409af6..7010bb19b 100644 --- a/bitcoin/src/blockdata/script/tests.rs +++ b/bitcoin/src/blockdata/script/tests.rs @@ -416,8 +416,10 @@ fn standard_op_return() { assert!(ScriptBuf::from_hex("6a48656c6c6f2c2074686973206973206d7920666972737420636f6e747269627574696f6e20746f207275737420626974636f696e2e20506c6561736520617070726f7665206d79205052206672656e") .unwrap() .is_standard_op_return()); - - assert!(!ScriptBuf::from_hex("6a48656c6c6f2c2074686973206973206d7920666972737420636f6e747269627574696f6e20746f207275737420626974636f696e2e20506c6561736520617070726f7665206d79205052206672656e21") + assert!(ScriptBuf::from_hex("6a48656c6c6f2c2074686973206973206d7920666972737420636f6e747269627574696f6e20746f207275737420626974636f696e2e20506c6561736520617070726f7665206d79205052206672656e21") + .unwrap() + .is_standard_op_return()); + assert!(!ScriptBuf::from_hex("6a48656c6c6f2c2074686973206973206d7920666972737420636f6e747269627574696f6e20746f207275737420626974636f696e2e20506c6561736520617070726f7665206d79205052206672656e21524f42") .unwrap() .is_standard_op_return()); } diff --git a/bitcoin/src/policy.rs b/bitcoin/src/policy.rs index 35c2dda8b..89eb08261 100644 --- a/bitcoin/src/policy.rs +++ b/bitcoin/src/policy.rs @@ -43,6 +43,9 @@ pub const DEFAULT_MIN_RELAY_TX_FEE: u32 = 1_000; /// mempools. pub const DEFAULT_MEMPOOL_EXPIRY: u32 = 336; +// 80 bytes of data, +1 for OP_RETURN, +2 for the pushdata opcodes. +pub(crate) const MAX_OP_RETURN_RELAY: usize = 83; + /// The virtual transaction size, as computed by default by bitcoind node. pub fn get_virtual_tx_size(weight: i64, n_sigops: i64) -> i64 { (cmp::max(weight, n_sigops * DEFAULT_BYTES_PER_SIGOP as i64) + WITNESS_SCALE_FACTOR as i64 - 1)