diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2018-07-18 17:52:43 -0700 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2018-07-19 16:16:37 -0700 |
commit | 84547fa6d408bdda1685f6d5972232bb19d97a7d (patch) | |
tree | 2ff4d9ba84086f1e1d9fab2206893d6b5cfe25f1 /src/script | |
parent | 4a3e8c5aa6a5d8dda15a76d644b2a9f0f40cdec7 (diff) |
Avoid creating a temporary vector for size-prefixed elements
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/sign.h | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/src/script/sign.h b/src/script/sign.h index e3a6196b28..250b637035 100644 --- a/src/script/sign.h +++ b/src/script/sign.h @@ -116,26 +116,24 @@ static constexpr uint8_t PSBT_OUT_BIP32_DERIVATION = 0x02; // as a 0 length key which indicates that this is the separator. The separator has no value. static constexpr uint8_t PSBT_SEPARATOR = 0x00; -// Takes a stream and multiple arguments and serializes them into a vector and then into the stream +// Takes a stream and multiple arguments and serializes them as if first serialized into a vector and then into the stream // The resulting output into the stream has the total serialized length of all of the objects followed by all objects concatenated with each other. template<typename Stream, typename... X> void SerializeToVector(Stream& s, const X&... args) { - std::vector<unsigned char> ret; - CVectorWriter ss(SER_NETWORK, PROTOCOL_VERSION, ret, 0); - SerializeMany(ss, args...); - s << ret; + WriteCompactSize(s, GetSerializeSizeMany(s, args...)); + SerializeMany(s, args...); } // Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments template<typename Stream, typename... X> void UnserializeFromVector(Stream& s, X&... args) { - std::vector<unsigned char> data; - s >> data; - CDataStream ss(data, SER_NETWORK, PROTOCOL_VERSION); - UnserializeMany(ss, args...); - if (!ss.eof()) { + size_t expected_size = ReadCompactSize(s); + size_t remaining_before = s.size(); + UnserializeMany(s, args...); + size_t remaining_after = s.size(); + if (remaining_after + expected_size != remaining_before) { throw std::ios_base::failure("Size of value was not the stated size"); } } |