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 return data to VM #19548

Merged
merged 1 commit into from
Sep 10, 2021
Merged

Conversation

seanyoung
Copy link
Contributor

@seanyoung seanyoung commented Sep 1, 2021

Implementation for #19318

  • Add test for returning simple data
  • Add test for returning data via CPI
  • Needs to be feature gated
  • Returning data via CPI needs to be costed (why?)

Signed-off-by: Sean Young [email protected]

@seanyoung seanyoung marked this pull request as draft September 1, 2021 10:27
@codecov
Copy link

codecov bot commented Sep 1, 2021

Codecov Report

Merging #19548 (132dc2e) into master (eade497) will decrease coverage by 0.0%.
The diff coverage is 27.1%.

❗ Current head 132dc2e differs from pull request most recent head e9c7ff4. Consider uploading reports for the commit e9c7ff4 to get more accurate results

@@            Coverage Diff            @@
##           master   #19548     +/-   ##
=========================================
- Coverage    82.6%    82.6%   -0.1%     
=========================================
  Files         471      471             
  Lines      132557   132658    +101     
=========================================
+ Hits       109574   109611     +37     
- Misses      22983    23047     +64     

@seanyoung
Copy link
Contributor Author

I've created a PR for Solang to use this new system, and it works very well. The result is much nicer. 🎉

@seanyoung seanyoung force-pushed the return-data-impl branch 3 times, most recently from dc92466 to d54e41d Compare September 2, 2021 16:23
@seanyoung seanyoung marked this pull request as ready for review September 2, 2021 16:24
@seanyoung seanyoung requested review from jackcmay and mvines September 2, 2021 16:24
@seanyoung
Copy link
Contributor Author

Compared the proposal, there are two changes:

  • return data is always cleared on entry (so we don't "return" data that the caller set)
  • the syscalls charge for their arguments, according to the length of data. Passing return data is not charged in itself, as the calls already charge.

@seanyoung seanyoung force-pushed the return-data-impl branch 4 times, most recently from e6c99ca to 516cdaa Compare September 8, 2021 17:08
@@ -309,6 +313,19 @@ pub mod stable_log {
ic_logger_msg!(logger, "Program log: {}", message);
}

/// Log return data as from the program itself.
Copy link
Member

Choose a reason for hiding this comment

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

Just to be complete please also document here that the Program return data: will not be present if no return data was provided (as opposed to an empty <program-generated-in-base64> value)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So with the program_id, the return_data becomes an Option<>. Now it is present, even if it is empty.

/// The general form is:
///
/// ```notrust
/// "Program return data: <program-generated-in-base64>"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// "Program return data: <program-generated-in-base64>"
/// "Program return data: <program-generated-data-in-base64>"

If we end up adding the program id to the return data (cc: #19318 (comment)) then I think it'll be good to include the program id that generated the return data in this log as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely. Now it included the program_id of the setter.

mvines
mvines previously approved these changes Sep 8, 2021
Copy link
Member

@mvines mvines left a comment

Choose a reason for hiding this comment

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

lgtm! Would want @jackcmay's careful 👁️ on this too

@jackcmay
Copy link
Contributor

jackcmay commented Sep 8, 2021

* the syscalls charge  for their arguments, according to the length of data. Passing return data is not charged in itself, as the calls already charge.

We want to avoid a program calling into the runtime and consuming cycles that are not accounted for. For example, a program could call sol_get_return_data in a loop, and the program would incur a small cost of calling from BPF but a lot more unaccounted cost doing work in the syscall runtime. That work in the runtime needs to be accounted for

sdk/program/src/program.rs Outdated Show resolved Hide resolved
// Return the actual length, rather the length returned
*result = Ok(return_data.len() as u64);
} else {
question_mark!(
Copy link
Contributor

Choose a reason for hiding this comment

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

Charge base cost always, then charge byte cost if there are bytes to get

sdk/program/src/pubkey.rs Outdated Show resolved Hide resolved
sdk/src/compute_budget.rs Outdated Show resolved Hide resolved
@seanyoung seanyoung force-pushed the return-data-impl branch 3 times, most recently from 0b02fde to b046897 Compare September 9, 2021 19:03
if size == 0 {
None
} else {
let size = std::cmp::min(size as usize, MAX_RETURN_DATA);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use a use for std::cmp

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 agree having the full namespace there is a bit much. Since this is in a target == bpf block, we can't have a use std::cmp::min; at the top without `#[allow(unused)]'. So I've put the use in the same block.

jackcmay
jackcmay previously approved these changes Sep 9, 2021
Copy link
Contributor

@jackcmay jackcmay left a comment

Choose a reason for hiding this comment

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

Looks great besides one last nit ;-)

/// The general form is:
///
/// ```notrust
/// "Program return data: <program-generated-data-in-base64>"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// "Program return data: <program-generated-data-in-base64>"
/// "Program return data: <program-id> <program-generated-data-in-base64>"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching that, fixed.

@mergify mergify bot dismissed jackcmay’s stale review September 10, 2021 08:17

Pull request has been modified.

@seanyoung seanyoung force-pushed the return-data-impl branch 2 times, most recently from 134b62e to dfd1b56 Compare September 10, 2021 09:50
This consists of:
 - syscalls
 - passing return data from invoked to invoker
 - printing to stable log
 - rust and C SDK changes
@seanyoung seanyoung merged commit 0985852 into solana-labs:master Sep 10, 2021
@seanyoung seanyoung deleted the return-data-impl branch September 10, 2021 13:25
seanyoung added a commit to seanyoung/solang that referenced this pull request Sep 10, 2021
seanyoung added a commit to seanyoung/solang that referenced this pull request Sep 14, 2021
seanyoung added a commit to seanyoung/solang that referenced this pull request Sep 14, 2021
seanyoung added a commit to seanyoung/solang that referenced this pull request Sep 18, 2021
seanyoung added a commit to seanyoung/solang that referenced this pull request Sep 18, 2021
seanyoung added a commit to seanyoung/solang that referenced this pull request Sep 18, 2021
seanyoung added a commit to seanyoung/solang that referenced this pull request Sep 18, 2021
seanyoung added a commit to hyperledger-solang/solang that referenced this pull request Sep 18, 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.

3 participants