Skip to content

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Nov 4, 2021

This changes the serialize code (.read() and .write() functions) to take a Span instead of a pointer and size. This is a breaking change for the serialize interface, so at no additional cost we can also switch to std::byte (instead of using char).

The benefits of using Span:

  • Less verbose and less fragile code when passing an already existing Span(-like) object to or from serialization

The benefits of using std::byte:

  • std::byte can't accidentally be mistaken for an integer

The goal here is to only change serialize to use spans of std::byte. If needed, AsBytes, MakeUCharSpan, ... can be used (temporarily) to pass spans of the right type.

Other changes that are included here:

  • #22167 (refactor: Remove char serialize by MarcoFalke)
  • #21906 (Preserve const in cast on CTransactionSignatureSerializer by promag)

@sipa
Copy link
Member

sipa commented Nov 4, 2021

Oh, std::byte is already in C++17!

@maflcko
Copy link
Member Author

maflcko commented Nov 4, 2021

There seems to be a bug on msvc, because it doesn't eat the code I prepared. There is also no error. Any ideas @sipsorcery ?

@maflcko maflcko changed the title Use spans of std::byte in serialize refactor: Use spans of std::byte in serialize Nov 4, 2021
@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 5, 2021

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #24143 (WIP: net/p2p:rename command*/Command/* to message*/Message* by RandyMcMillan)
  • #23829 (refactor: use braced init for integer literals instead of c style casts by PastaPastaPasta)
  • #23810 (refactor: destroy all C-style casts; use modern C++ casts, enforce via -Wold-style-cast by PastaPastaPasta)
  • #19690 (util: improve FindByte() performance by LarryRuane)
  • #16981 (Improve runtime performance of --reindex by LarryRuane)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@sipsorcery
Copy link
Contributor

There seems to be a bug on msvc, because it doesn't eat the code I prepared. There is also no error. Any ideas @sipsorcery ?

Seems to be a const change for a bitcoinconsensus_verify_script_with_amount parameter.

Changing this line to the below fixes it (I added the const cast):

auto op0Result = bitcoinconsensus_verify_script_with_amount(pubKeyScript.data(), pubKeyScript.size(), amount, (const unsigned char*)stream.data(), stream.size(), 0, bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL, &err);

But perhaps a better thing to do would be to remove the build_msvc/testconsensus directory completely. It's a test app that, in hindsight, would have been better put into a unit test somewhere. I suspect no one ever uses it. I haven't looked at it in the 4 years since I first wrote it... I'll submit a PR to remove it.

sipsorcery added a commit to sipsorcery/bitcoin that referenced this pull request Nov 6, 2021
The testconsensus project is not integral to the Bitcoin Core code. It was originally added as a quick and dirty demo of how to do a consensus check with the msvc build. There are better examples.

PR bitcoin#23438 made a change that caused a compiler error with the buildmsvc/testconsensus code. Rather than leave it hanging around to incur potential bitrot, or furhter PR build failures, it should be removed.
@maflcko maflcko marked this pull request as draft November 7, 2021 09:43
fanquake added a commit that referenced this pull request Nov 8, 2021
bb1c840 Remove the build_msvc/testconsensus project (Aaron Clauson)

Pull request description:

  The testconsensus project is not integral to the Bitcoin Core code. It was originally added as a quick and dirty demo of how to do a consensus check with the msvc build. There are better examples.

  PR #23438 made a change that caused a compiler error with the buildmsvc/testconsensus code. Rather than leave it hanging around to incur potential bitrot, or further PR build failures, it should be removed.

ACKs for top commit:
  hebasto:
    ACK bb1c840, tested on Windows 10 Pro (20H2).

Tree-SHA512: d81b99eb09171b66c179961b15f0b2e2e97e5ee7f011f18667e890c90e3d169593ad9aedd05a8616e962212952048827b7159d3c2a2ecb7ac378136b80bf6b23
Copy link
Member

@sipa sipa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK face063

@maflcko
Copy link
Member Author

maflcko commented Dec 6, 2021

Thanks, force pushed to replace data-end with begin-end.

@sipa
Copy link
Member

sipa commented Dec 21, 2021

re-utACK fade794

@fanquake fanquake requested a review from laanwj December 23, 2021 06:58
MarcoFalke added 3 commits January 2, 2022 11:13
@maflcko
Copy link
Member Author

maflcko commented Jan 2, 2022

Rebased due to silent conflict. Trivial to re-review via git range-diff bitcoin-core/master fade794217 fa5d2e678c.

Copy link
Contributor

@PastaPastaPasta PastaPastaPasta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

I have reviewed the code, and didn't find any issues.

I am very happen to see std::byte being used, as well as spans :) All good refactoring as far as I can tell

@fanquake fanquake requested a review from sipa January 10, 2022 02:57
Copy link
Member

@sipa sipa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-utACK fa5d2e6

@PastaPastaPasta
Copy link
Contributor

IMO, this PR could be merged. It's been 17 days w/o any review, and two acks. And it's been in high-priority for review for 7 days. I'm not sure what exactly the procedure is for bitcoin-core, but I think this relatively simple refactoring PR can be merged at this point.

@maflcko
Copy link
Member Author

maflcko commented Jan 27, 2022

it's been in high-priority for review for 7 days. I'm not sure what exactly the procedure is for bitcoin-core, but I think this relatively simple refactoring PR can be merged at this point.

There is no strict procedure in this project, but if a pull request hasn't been merged, it can either mean that all maintainers that looked at it preferred more review, or it can mean that it went unnoticed from maintainers.
High-priority doesn't have any meaning for the project. It is a way for individual contributors to share their favourite pet (their most important pull among all of their pulls) with others.
While it isn't incredibly hard to review this, it also isn't trivial. Integral types in C++ aren't type safe. While this changes some stuff to span<byte>, which is type safe, there are conversions at the "boundaries", which might cause issues even if the code looks right and compiles and passes all tests.

@laanwj
Copy link
Member

laanwj commented Jan 27, 2022

Concept and code review ACK fa5d2e6
Removing the bare char serializer makes a lot of sense, it's always been kind of a wildcard type resulting on annoying and potentially dangerous platform differences.

@@ -421,7 +422,7 @@ class CDataStream
}

for (size_type i = 0, j = 0; i != size(); i++) {
vch[i] ^= key[j++];
vch[i] ^= std::byte{key[j++]};
Copy link
Member

@laanwj laanwj Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense to make ObfuscateKey a std::byte throughout, avoiding this cast. Though no need to do so in this PR. Could also pass in a span here then instead of const std::vector<unsigned char>&.

@laanwj laanwj merged commit 196b459 into bitcoin:master Jan 27, 2022
@maflcko maflcko deleted the 2105-uin8t branch January 27, 2022 18:42
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jan 28, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jan 28, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 12, 2022
@bitcoin bitcoin locked and limited conversation to collaborators Jan 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants