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

Save and load all preimages on the database #495

Merged
merged 2 commits into from Jan 31, 2020

Conversation

linked0
Copy link
Contributor

@linked0 linked0 commented Jan 23, 2020

A validator must periodically reveal the preimages.
For that requirement, the preimages should be stored to database for a cycle.

This implementation is the first definition of done of the issue #492

@linked0 linked0 force-pushed the save-preimage branch 2 times, most recently from 5841a1f to 7c89d29 Compare January 23, 2020 07:46
@codecov
Copy link

codecov bot commented Jan 23, 2020

Codecov Report

Merging #495 into v0.x.x will decrease coverage by 1.11%.
The diff coverage is 86.11%.

Impacted file tree graph

@@            Coverage Diff             @@
##           v0.x.x     #495      +/-   ##
==========================================
- Coverage   88.82%   87.71%   -1.12%     
==========================================
  Files          58       57       -1     
  Lines        4143     4151       +8     
==========================================
- Hits         3680     3641      -39     
- Misses        463      510      +47
Flag Coverage Δ
#integration ?
#unittests 87.71% <86.11%> (-0.05%) ⬇️
Impacted Files Coverage Δ
source/agora/common/Serializer.d 98.66% <100%> (+0.05%) ⬆️
source/agora/common/Deserializer.d 91.66% <100%> (+0.52%) ⬆️
source/agora/consensus/EnrollmentManager.d 86.26% <82.14%> (-0.92%) ⬇️
source/agora/common/Task.d 0% <0%> (-100%) ⬇️
source/agora/common/Config.d 72.72% <0%> (-17.49%) ⬇️
source/agora/node/Node.d 85.93% <0%> (-10.99%) ⬇️
source/agora/network/NetworkManager.d 70.21% <0%> (-4.97%) ⬇️
source/agora/common/crypto/Key.d 84.25% <0%> (-4.63%) ⬇️
source/agora/consensus/protocol/Nominator.d 77.92% <0%> (-1.3%) ⬇️
source/agora/node/BlockStorage.d 67.87% <0%> (-0.73%) ⬇️
... and 1 more

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 f9fa2e1...e494213. Read the comment docs.

@linked0
Copy link
Contributor Author

linked0 commented Jan 23, 2020

Ready for review

public struct PreimageStore
{
/// Preimages of hashes of random value
public Hash[] data;
Copy link
Contributor

Choose a reason for hiding this comment

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

We could avoid writing all this code if we added serialization support to Hash[] in Serializer.d and Deserializer.d.

@Geod24 can you think of any drawbacks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After adding serialization support to Hash[] in this PR, I will request a review again.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@AndrejMitrovic : Actually for the binary protocol, I wrote a way to serialize ranges... Guess it's time to PR it.

@linked0
Copy link
Contributor Author

linked0 commented Jan 29, 2020

Ready for review.

results = this.db.execute("SELECT val FROM node_enroll_data " ~
"WHERE key = ?", "preimages");

foreach (row; results)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think maybe this would work too:

if (!results.empty)
    this.preimages = results.oneValue!(ubyte[]).deserializeFull!(Hash[]);

Could be worth trying.

foreach (i; 0 .. this.data.cycle_length-1)
this.data.random_seed = hashFull(this.data.random_seed);
this.preimages ~= hashFull(this.preimages[i]);
this.data.random_seed = this.preimages[this.data.cycle_length-1];
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just this.preimages[$ - 1];? Is it not the same?

@@ -453,6 +506,38 @@ public class EnrollmentManager
}
}

/// test for serialization and deserialization of PreimageStore
Copy link
Contributor

Choose a reason for hiding this comment

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

Slightly outdated comment, as PreimageStore doesn't exist anymore.

Copy link
Contributor

@AndrejMitrovic AndrejMitrovic left a comment

Choose a reason for hiding this comment

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

small issues

@linked0
Copy link
Contributor Author

linked0 commented Jan 29, 2020

Ready for review. Thanks.

@@ -453,6 +502,38 @@ public class EnrollmentManager
}
}

/// test for serialization and deserialization of a Hash array
unittest
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to just test serialization? It's not testing EnrollmentManager.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, It's not. I will make the unittest with EnrollmentManager.

@linked0 linked0 force-pushed the save-preimage branch 2 times, most recently from 6d55c73 to 97e99c4 Compare January 30, 2020 02:39
@linked0
Copy link
Contributor Author

linked0 commented Jan 30, 2020

Ready for review.

@@ -587,4 +672,13 @@ unittest
man.getUnregisteredEnrollments(enrolls);
assert(enrolls.length == 3);
assert(enrolls.isStrictlyMonotonic!("a.utxo_key < b.utxo_key"));

// test serialization/deserializetion for pre-images
auto preimages_1 = man.getPreimages();
Copy link
Contributor

Choose a reason for hiding this comment

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

You can access the field directly, because private allows access from the same module.

So just auto preimages_1 = man.preimages;.

auto preimages_1 = man.getPreimages();
auto preimages_2 = man.loadPreimages();
int true_count;
foreach (i; 0 .. 1008)
Copy link
Contributor

Choose a reason for hiding this comment

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

These few lines can be replaced with assert(preimages_1[] == preimages_2[]);. This will check the length too.

foreach (i; 0 .. 1008)
if (preimages_1[i] == preimages_2[i])
true_count++;
assert(true_count == 1008);
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a missing check that the preimages are actually preimages. You can use this:

import std.algorithm;
assert(equal!((a, b) => a.hashFull() == b)(preimages_1[0 .. $ - 1], preimages_1[1 .. $]));

@linked0
Copy link
Contributor Author

linked0 commented Jan 30, 2020

Ready for review after applying the suggestions of @AndrejMitrovic .

Copy link
Contributor

@AndrejMitrovic AndrejMitrovic left a comment

Choose a reason for hiding this comment

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

LGTM.

A validator must periodically reveal the pre-images.
For that requirement, the pre-images should be stored to database for a cycle.
@linked0
Copy link
Contributor Author

linked0 commented Jan 31, 2020

Ping @Geod24

@Geod24 Geod24 merged commit 3f4bd69 into bosagora:v0.x.x Jan 31, 2020
@linked0 linked0 deleted the save-preimage branch March 29, 2020 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants