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

Random Number Generation #2053

Merged
merged 21 commits into from
Sep 17, 2021
Merged

Random Number Generation #2053

merged 21 commits into from
Sep 17, 2021

Conversation

lrubiorod
Copy link
Contributor

@lrubiorod lrubiorod commented Sep 2, 2021

Close #2056

@lrubiorod lrubiorod force-pushed the random_numbers branch 3 times, most recently from 86ffe89 to 15b7bc5 Compare September 6, 2021 13:53
@lrubiorod lrubiorod changed the base branch from master to witnet-1.4 September 7, 2021 09:42
println!(
"expected_ta_tx.tally: {}",
hex::encode(&expected_ta_tx.tally)
);
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why?

Copy link
Contributor

Choose a reason for hiding this comment

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

I added it to help me write the tests, if the tests are done we don't need it.

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 had to modify several times the rng tests, so I think it could be useful in the future too

@lrubiorod lrubiorod marked this pull request as ready for review September 7, 2021 11:46
@lrubiorod lrubiorod force-pushed the random_numbers branch 2 times, most recently from 4659b63 to 58e5d0b Compare September 7, 2021 15:57
rad/src/lib.rs Outdated
Comment on lines 263 to 289
if is_rng {
Ok(RadonReport::from_result(
Ok(radon_types_vec[0].clone()),
context,
))
} else {
Copy link
Member

Choose a reason for hiding this comment

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

What is this doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unwrap() :P

Copy link
Contributor

Choose a reason for hiding this comment

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

In the rng case the request will usually have only one retrieval source. So this way we avoid the aggregation step, which is not needed. However I think that could apply to all kinds of requests, because you don't need aggregation if there is only one source. This would be a breaking change, but we can implement it with TAPI.

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 removed it and use the Mode instead, because the mode of a vector of one element is that element. To avoid strange cases

Comment on lines 253 to 255
if path.url.is_empty() || path.script.is_empty() {
is_rng = true;
break;
} else {
return Err(DataRequestError::InvalidRngRequest.into());
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't really follow the logic here hehehe.

  • I can see why url must be empty (won't be used).
  • I have doubts on whether script should be allowed or not.
  • By reading this code, I understand that as long as url is empty, script can be non-empty, but will it be used anyway?

Copy link
Member

@aesedepece aesedepece Sep 7, 2021

Choose a reason for hiding this comment

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

Some general but related comments:

  1. RADTypes only exist at the data source / retrieval level. We should avoid handling a data request differently only because one of the data sources is of a special kind.
  2. There won't exist such thing as RNG requests, only requests with RNG sources.
  3. RNG and HttpGet sources should coexist in the same data request as long as they have the same output type.

Copy link
Member

Choose a reason for hiding this comment

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

E.g. in the future we may have a data source type that performs general computation by running WASM bytecode in a sandbox.

And you may want to use that side by side with a classic HttpGet data source that triggers a similar computation on some cloud, or fetches similar outputs from some API.

My point is that you should perfectly be able to do so, and this part of the code should only care about routing the data source definitions to their "resolvers" (the resolver of HttpGet is our wrapper of the Surf library, the resolver of Rng is the RNG and the resolver of the hypothetical Wasm type would be a WASM virtual machine).

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree that is_rng is a hack and we should find a way to avoid it.

Comment on lines 262 to 265
if path.url.is_empty() {
return Err(DataRequestError::NoRetrievalSources.into());
}
Copy link
Member

Choose a reason for hiding this comment

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

Won't this cause an early return for any retrieval path / data source of RADType::Rng that has both url and script being empty?

Copy link
Contributor

Choose a reason for hiding this comment

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

In the rng case it does never reach this line because there is a break in the previous condition. But that limits rng requests to one retrieval source, so we need to change this logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it is a RADType::Rng, it will always return earlier in the previous branchs (break or DataRequestError::InvalidRngRequest)

Comment on lines 268 to 267
if is_rng && retrieval_paths.len() > 1 {
return Err(DataRequestError::InvalidRngRequest.into());
}
Copy link
Member

Choose a reason for hiding this comment

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

As per my general comment above, this goes against the three points.

@lrubiorod lrubiorod changed the title (wip) Random Number Generation Random Number Generation Sep 8, 2021
| RadonReducers::Mode
| RadonReducers::AverageMedian
| RadonReducers::HashConcatenate
| RadonReducers::Unwrap => {}
Copy link
Contributor

Choose a reason for hiding this comment

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

This function needs a context, the new operators can only be considered valid if the WIP is active.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is fixed in #2063, because it also affect to the median

@lrubiorod lrubiorod force-pushed the random_numbers branch 2 times, most recently from e8b3537 to fe14ad9 Compare September 9, 2021 16:05
#[repr(u8)]
pub enum RadonReducers {
// Implemented
Mode = 0x02,
AverageMean = 0x03,
AverageMedian = 0x05,
DeviationStandard = 0x07,
HashConcatenate = 0x0b,
Unwrap = 0x0c,
Copy link
Member

Choose a reason for hiding this comment

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

Interesting. Where did this come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We dont have a reducer to convert an array of 1 element in that element

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Deleted unwrap, using Mode instead of

rad/src/lib.rs Outdated
let random_bytes: [u8; 32] = rand::random();
let random_bytes = RadonTypes::from(RadonBytes::from(random_bytes.to_vec()));

Ok(RadonReport::from_result(Ok(random_bytes), context))
Copy link
Contributor

Choose a reason for hiding this comment

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

This ignores the retrieval script, according to the WIP the script should be executed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well actually I'm not sure if the retrieval script should be executed or not.

Copy link
Member

Choose a reason for hiding this comment

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

Let's discuss here:
witnet/WIPs#70 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What was the conclusion?

@tmpolaczyk
Copy link
Contributor

Using commit 0c4ffec and this data request:

{"jsonrpc":"2.0","method":"sendRequest","id":"1","params":{"dro":{"data_request":{"time_lock":0,"retrieve":[{"kind":"RNG","url":"","script":[128]}],"aggregate":{"filters":[],"reducer":3},"tally":{"filters":[],"reducer":3}},"witness_reward":1000,"witnesses":5,"commit_and_reveal_fee":10,"min_consensus_percentage":51,"collateral":1000000000},"fee":0}}

An updated node accepts that as a valid request, while a not-updated node does not, leading to a fork.

rad/src/lib.rs Outdated
/// Auxiliary function that returns the current active wips and the WIPs in voting process as actived
/// It is only used for testing
pub fn all_wips_active() -> ActiveWips {
let mut h = current_active_wips();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why h? 🤣

Copy link
Contributor

Choose a reason for hiding this comment

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

It stands for HashMap, because the first version of ActiveWips was just a HashMap.

@lrubiorod lrubiorod merged commit 1b3176c into witnet:master Sep 17, 2021
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.

Implement and test prototype of RNG functionality
4 participants