summaryrefslogtreecommitdiff
path: root/8e/fb17ecf6e1d2d296155b100c21e4887acc9038
blob: dddf39a7982f1cb6fd02030afd87844f0e21388f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
Return-Path: <rusty@gandalf.ozlabs.org>
Received: from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136])
 by lists.linuxfoundation.org (Postfix) with ESMTP id 0EBE2C0032
 for <bitcoin-dev@lists.linuxfoundation.org>;
 Tue, 24 Oct 2023 03:46:11 +0000 (UTC)
Received: from localhost (localhost [127.0.0.1])
 by smtp3.osuosl.org (Postfix) with ESMTP id EC3BD6F5BE
 for <bitcoin-dev@lists.linuxfoundation.org>;
 Tue, 24 Oct 2023 03:46:10 +0000 (UTC)
DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org EC3BD6F5BE
Authentication-Results: smtp3.osuosl.org;
 dkim=pass (2048-bit key) header.d=rustcorp.com.au header.i=@rustcorp.com.au
 header.a=rsa-sha256 header.s=202305 header.b=i1VgH7DS
X-Virus-Scanned: amavisd-new at osuosl.org
X-Spam-Flag: NO
X-Spam-Score: -1.753
X-Spam-Level: 
X-Spam-Status: No, score=-1.753 tagged_above=-999 required=5
 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
 DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
 SPF_HELO_PASS=-0.001, SPF_PASS=-0.001] autolearn=no autolearn_force=no
Received: from smtp3.osuosl.org ([127.0.0.1])
 by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024)
 with ESMTP id o8IZWP8UarXC
 for <bitcoin-dev@lists.linuxfoundation.org>;
 Tue, 24 Oct 2023 03:46:06 +0000 (UTC)
X-Greylist: delayed 91960 seconds by postgrey-1.37 at util1.osuosl.org;
 Tue, 24 Oct 2023 03:46:05 UTC
DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org D7D526F5B9
Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76])
 by smtp3.osuosl.org (Postfix) with ESMTPS id D7D526F5B9
 for <bitcoin-dev@lists.linuxfoundation.org>;
 Tue, 24 Oct 2023 03:46:05 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rustcorp.com.au;
 s=202305; t=1698119158;
 bh=ITMMx44TcLykG5QDC0FP/qD4Go+lyEdmoL0Bgbgxp2g=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=i1VgH7DSjGmUc22BD8vbWzPj3mxzOw27rfIfjZIcSWQficl2evIDXTSTRF5tCny3+
 PRodJLbtUeybfqUFcFpeNNqt/rfAaLQGb3S2sj1udoOGBpPYH6tvbjTRO0TAYqpGqG
 U/Sq2PQMf/CVKPgvqEDry1GAHIjl9b+xGq7Dl+WOqaQDcsSP6cUnCPiOOJ4V6SNEXk
 V3C58BtpvHKsmg+wiljTir8CmdCP2KIwbve1yQdRPpQMYFPRrK9whYZkGmT0Iz7CGh
 nTmJ1Bx8A/QDRiwVE29E/HYuvwd4ipV6gDHISueEdzEiRWlLQnbSEb1NjdFlDtRHWE
 ToDW/EyZcKF0A==
Received: by gandalf.ozlabs.org (Postfix, from userid 1011)
 id 4SDydf5wGQz4wdD; Tue, 24 Oct 2023 14:45:58 +1100 (AEDT)
From: Rusty Russell <rusty@rustcorp.com.au>
To: Andrew Poelstra <apoelstra@wpsoftware.net>
In-Reply-To: <ZTcbKM+XTCaJ2kIP@camus>
References: <CAEM=y+XDB7GGa5BTAWrQHqTqQHBE2VRyd7VWjEb+zCOMzRP+Lg@mail.gmail.com>
 <871qdmulvt.fsf@rustcorp.com.au> <ZTZ4H2y6+5pxRcs/@camus>
 <871qdku9pj.fsf@rustcorp.com.au> <ZTcbKM+XTCaJ2kIP@camus>
Date: Tue, 24 Oct 2023 14:15:49 +1030
Message-ID: <87r0lksmxe.fsf@rustcorp.com.au>
MIME-Version: 1.0
Content-Type: text/plain
Cc: Bitcoin Protocol Discussion <bitcoin-dev@lists.linuxfoundation.org>
Subject: Re: [bitcoin-dev] Proposed BIP for OP_CAT
X-BeenThere: bitcoin-dev@lists.linuxfoundation.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: Bitcoin Protocol Discussion <bitcoin-dev.lists.linuxfoundation.org>
List-Unsubscribe: <https://lists.linuxfoundation.org/mailman/options/bitcoin-dev>, 
 <mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=unsubscribe>
List-Archive: <http://lists.linuxfoundation.org/pipermail/bitcoin-dev/>
List-Post: <mailto:bitcoin-dev@lists.linuxfoundation.org>
List-Help: <mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=help>
List-Subscribe: <https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev>, 
 <mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=subscribe>
X-List-Received-Date: Tue, 24 Oct 2023 03:46:11 -0000

Andrew Poelstra <apoelstra@wpsoftware.net> writes:
> I had a similar thought. But my feeling is that replacing the stack
> interpreter data structure is still too invasive to justify the benefit.
>
> Also, one of my favorite things about this BIP is the tiny diff.

To be fair, this diff is even smaller than the OP_CAT diff :)

Though I had to strongly resist refactoring, that interpreter code
needs a good shake!  Using a class for the stack is worth doing anyway
(macros, really??).

diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp
index dcaf28c2472..2ee2034115f 100644
--- a/src/script/interpreter.cpp
+++ b/src/script/interpreter.cpp
@@ -403,6 +403,19 @@ static bool EvalChecksig(const valtype& sig, const valtype& pubkey, CScript::con
     assert(false);
 }
 
+// First 520 bytes is free, after than you consume an extra slot!
+static size_t effective_size(const std::vector<std::vector<unsigned char> >& stack)
+{
+    size_t esize = stack.size();
+
+    for (const auto& v : stack)
+    {
+        if (v.size() > MAX_SCRIPT_ELEMENT_SIZE) 
+            esize += (v.size() - 1) / MAX_SCRIPT_ELEMENT_SIZE;
+    }
+    return esize;
+}
+    
 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror)
 {
     static const CScriptNum bnZero(0);
@@ -1239,7 +1252,7 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
             }
 
             // Size limits
-            if (stack.size() + altstack.size() > MAX_STACK_SIZE)
+            if (effective_size(stack) + effective_size(altstack) > MAX_STACK_SIZE)
                 return set_error(serror, SCRIPT_ERR_STACK_SIZE);
         }
     }