Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for hashing SCPStatement & SCPEnvelope #546

Merged
merged 2 commits into from
Apr 3, 2020

Conversation

AndrejMitrovic
Copy link
Contributor

@AndrejMitrovic AndrejMitrovic commented Feb 17, 2020

This will be needed for signing SCP statements.

Edit: Now it's also necessary to fix the issue in #716

I'm not sure where to place this hashing support, other than into the Nominator module because this is the only place it will be used.

I cannot move it to scpd.types.Stellar_SCP because this module should not import hashing routines from agora.*, and I'm not sure agora.common.Hash is a good place for it either.

Also side-note: maybe there should be a generic way of testing hashing behavior for any struct types. Something like a "fuzzer" type helper function that manipulates each field and verifies that the hash changed.

@codecov
Copy link

codecov bot commented Feb 18, 2020

Codecov Report

Merging #546 into v0.x.x will increase coverage by 0.1%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           v0.x.x     #546     +/-   ##
=========================================
+ Coverage   91.14%   91.25%   +0.1%     
=========================================
  Files          63       63             
  Lines        4809     4892     +83     
=========================================
+ Hits         4383     4464     +81     
- Misses        426      428      +2
Flag Coverage Δ
#integration 52.68% <0%> (-1.17%) ⬇️
#unittests 90.01% <100%> (+0.11%) ⬆️
Impacted Files Coverage Δ
source/agora/consensus/protocol/Nominator.d 91.39% <100%> (+6.93%) ⬆️
source/agora/node/BlockStorage.d 69.25% <0%> (-1.12%) ⬇️
source/agora/common/Hash.d 98.33% <0%> (+1.66%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7a99b4d...1d20c65. Read the comment docs.

@AndrejMitrovic
Copy link
Contributor Author

Disallowing sources: ubuntu-toolchain-r-test
To add unlisted APT sources, follow instructions in https://docs.travis-ci.com/user/installing-dependencies#Installing-Packages-with-the-APT-Addon
13.81s$ travis_apt_get_update
Installing APT Packages
0.40s$ sudo -E apt-get -yq --no-install-suggests --no-install-recommends $(travis_apt_get_options) install libsodium-dev g++-9 libsqlite3-dev
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package g++-9
E: Couldn't find any package by regex 'g++-9'
apt-get.diagnostics
apt-get install failed
The command "sudo -E apt-get -yq --no-install-suggests --no-install-recommends $(travis_apt_get_options) install libsodium-dev g++-9 libsqlite3-dev" failed and exited with 100 during .

Is Travis broken all of a sudden?

static assert(SCPNomination.sizeof == 80);

/// instance
const SCPStatement st;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't that do a copy tho ?


***************************************************************************/

public void computeHash (const ref SCPStatement._pledges_t._prepare_t prep,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like those could be static ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same for the following ones

@AndrejMitrovic
Copy link
Contributor Author

Fixed the comments. And also added hashing for SCPEnvelope, which just embeds the SCPStatement and the signature.

@AndrejMitrovic AndrejMitrovic changed the title Add support for hashing SCPStatements Add support for hashing SCPStatement & SCPEnvelope Apr 3, 2020
@AndrejMitrovic AndrejMitrovic force-pushed the hash-statements branch 3 times, most recently from 5b287cc to f4588b4 Compare April 3, 2020 07:57
@AndrejMitrovic
Copy link
Contributor Author

Hmm on ldc I am getting different hashes compared to DMD. lol

@AndrejMitrovic
Copy link
Contributor Author

I found a weird bug with lambdas.

This code is OK:

// OK
auto sign_data = pair.secret.sign(msg[]);

() @trusted {
    auto sig = sign_data[];
    env.signature = sig.toVec();
    writefln("\n\nbef: %s", env.signature);
}();

writefln("\n\naft: %s", env.signature);

Here the signature will be the same inside the lambda and outside of it. Output:

bef: [ 93, 231, 92, 250, 9, 186, 244, 162, 189, 114, 110, 68, 95, 241, 177, 218, 40, 166, 154, 157, 8, 1, 223, 5, 78, 116, 107, 134, 111, 93, 240, 85, 182, 29, 241, 59, 88, 47, 230, 118, 206, 240, 76, 117, 70, 192, 24, 156, 38, 225, 143, 125, 152, 217, 191, 189, 111, 165, 217, 8, 212, 240, 55, 9 ]


aft: [ 93, 231, 92, 250, 9, 186, 244, 162, 189, 114, 110, 68, 95, 241, 177, 218, 40, 166, 154, 157, 8, 1, 223, 5, 78, 116, 107, 134, 111, 93, 240, 85, 182, 29, 241, 59, 88, 47, 230, 118, 206, 240, 76, 117, 70, 192, 24, 156, 38, 225, 143, 125, 152, 217, 191, 189, 111, 165, 217, 8, 212, 240, 55, 9 ]

But if I move the sign data inside the lambda:

() @trusted {
    // FAIL
    auto sign_data = pair.secret.sign(msg[]);
    auto sig = sign_data[];
    env.signature = sig.toVec();
    writefln("\n\nbef: %s", env.signature);
}();

writefln("\n\naft: %s", env.signature);

Then the signatures are different:

bef: [ 93, 231, 92, 250, 9, 186, 244, 162, 189, 114, 110, 68, 95, 241, 177, 218, 40, 166, 154, 157, 8, 1, 223 , 5, 78, 116, 107, 134, 111, 93, 240, 85, 182, 29, 241, 59, 88, 47, 230, 118, 206, 240, 76, 117, 70, 192, 24, 156, 38, 225, 143, 125, 152, 217, 191, 189, 111, 165, 217, 8, 212, 240, 55, 9 ]


aft: [ 12, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 176, 171, 55, 192, 255, 127 , 0, 0, 224, 186, 88, 74, 49, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 165, 217, 8, 212, 240, 55, 9 ]

I've tried GC.disable() but that didn't do the trick. It almost seems like maybe the memory is allocated on the stack? Hmm..

@AndrejMitrovic
Copy link
Contributor Author

Fixed. Should go green soon.

Copy link
Collaborator

@Geod24 Geod24 left a comment

Choose a reason for hiding this comment

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

Just 3 nits, otherwise LGTM. Feel free to fix and merge at will.

private const SCPStatement* st;

/// Ctor
public this (const SCPStatement* st) @nogc nothrow @trusted
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
public this (const SCPStatement* st) @nogc nothrow @trusted
public this (const SCPStatement* st) @nogc nothrow @safe pure

No ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not really know ctors can be pure. I'm also surprised a function can be safe when assigning pointers. Oh well.

hashPart(this.st.slotIndex, dg);
hashPart(this.st.pledges.type_, dg);

switch (this.st.pledges.type_)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
switch (this.st.pledges.type_)
final switch (this.st.pledges.type_)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was gonna say that final switch sucks because of https://issues.dlang.org/show_bug.cgi?id=11051

But then I saw that the issue had a PR.. dlang/dmd#6095

lol

private const SCPEnvelope* env;

/// Ctor
public this (const SCPEnvelope* env) @nogc nothrow @trusted
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
public this (const SCPEnvelope* env) @nogc nothrow @trusted
public this (const SCPEnvelope* env) @nogc nothrow @safe pure

@AndrejMitrovic
Copy link
Contributor Author

Fixed again.

@Geod24
Copy link
Collaborator

Geod24 commented Apr 3, 2020

Drey 0 - Travis 1

@Geod24 Geod24 merged commit 94f8ee8 into bosagora:v0.x.x Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-enhancement An improvement of existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants