Skip to content

Commit ba544cd

Browse files
committed
Use ceilinged_add, style.
1 parent e953fa5 commit ba544cd

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

include/bitcoin/system/chain/witness.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,11 @@ class BC_API witness
131131
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
132132
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
133133
static size_t serialized_size(const chunk_cptrs& stack) NOEXCEPT;
134-
static inline size_t element_size(size_t total,
135-
const chunk_cptr& element) NOEXCEPT
134+
static inline size_t element_size(const chunk_cptr& element) NOEXCEPT
136135
{
137136
// Each witness is prefixed with number of elements (bip144).
138137
const auto size = element->size();
139-
return total + variable_size(size) + size;
138+
return ceilinged_add(variable_size(size), size);
140139
};
141140
BC_POP_WARNING()
142141
BC_POP_WARNING()

src/chain/witness.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void witness::assign_data(reader& source, bool prefix) NOEXCEPT
183183
break;
184184

185185
stack_.emplace_back(POINTER(data_chunk, allocator, bytes));
186-
size_ = element_size(size_, stack_.back());
186+
size_ = ceilinged_add(size_, element_size(stack_.back()));
187187
}
188188
}
189189
else
@@ -197,7 +197,7 @@ void witness::assign_data(reader& source, bool prefix) NOEXCEPT
197197
break;
198198

199199
stack_.emplace_back(POINTER(data_chunk, allocator, bytes));
200-
size_ = element_size(size_, stack_.back());
200+
size_ = ceilinged_add(size_, element_size(stack_.back()));
201201
}
202202
}
203203

@@ -300,15 +300,19 @@ const chunk_cptrs& witness::stack() const NOEXCEPT
300300
// static/private
301301
size_t witness::serialized_size(const chunk_cptrs& stack) NOEXCEPT
302302
{
303-
return std::accumulate(stack.begin(), stack.end(), zero, element_size);
303+
return std::accumulate(stack.begin(), stack.end(), zero,
304+
[](size_t total, const chunk_cptr& element) NOEXCEPT
305+
{
306+
return ceilinged_add(total, element_size(element));
307+
});
304308
}
305309

306310
size_t witness::serialized_size(bool prefix) const NOEXCEPT
307311
{
308312
// Witness prefix is an element count, not byte length (unlike script).
309313
// An empty stack is not a valid witnessed tx (no inputs) but a consistent
310314
// serialization is used independently by database so zero stack allowed.
311-
return prefix ? ceilinged_add(size_, variable_size(stack_.size())) : size_;
315+
return prefix ? ceilinged_add(variable_size(stack_.size()), size_) : size_;
312316
}
313317

314318
// Utilities.

0 commit comments

Comments
 (0)