This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Contract Calls #47
Merged
Merged
Contract Calls #47
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
247064e
contract calls sans correct gas charging or error handling
rachel-bousfield 3c97931
Merge branch 'stylus' into stylus-contract-calls
rachel-bousfield fe3fcf0
gas pricing & spec compliance
rachel-bousfield ccc433c
return data and status code consistency
rachel-bousfield 9007dcd
refactor stylus failure cases
rachel-bousfield 91dfe89
cleanup
rachel-bousfield 8ff54a7
address review comments
rachel-bousfield b980605
lazy return data copying
rachel-bousfield 4270d8a
remove debug printlns
rachel-bousfield be6de5d
update language comment
rachel-bousfield File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2023, Offchain Labs, Inc. | ||
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE | ||
|
||
use super::util::{Bytes20, Bytes32}; | ||
|
||
#[derive(Copy, Clone)] | ||
#[repr(C)] | ||
struct RustVec { | ||
ptr: *mut u8, | ||
len: usize, | ||
cap: usize, | ||
} | ||
|
||
impl Default for RustVec { | ||
fn default() -> Self { | ||
Self { | ||
ptr: std::ptr::null_mut(), | ||
len: 0, | ||
cap: 0, | ||
} | ||
} | ||
} | ||
|
||
#[link(wasm_import_module = "forward")] | ||
extern "C" { | ||
fn call_contract( | ||
contract: *const u8, | ||
calldata: *const u8, | ||
calldata_len: usize, | ||
value: *const u8, | ||
gas: u64, | ||
return_data_len: *mut usize, | ||
) -> u8; | ||
|
||
fn read_return_data(dest: *mut u8); | ||
} | ||
|
||
pub fn call(contract: Bytes20, calldata: &[u8], value: Option<Bytes32>, gas: Option<u64>) -> Result<Vec<u8>, Vec<u8>> { | ||
let mut outs_len = 0; | ||
let value = value.unwrap_or_default(); | ||
let gas = gas.unwrap_or(u64::MAX); // will be clamped by 63/64 rule | ||
let status = unsafe { | ||
call_contract( | ||
contract.ptr(), | ||
calldata.as_ptr(), | ||
calldata.len(), | ||
value.ptr(), | ||
gas, | ||
&mut outs_len as *mut _, | ||
) | ||
}; | ||
let outs = unsafe { | ||
let mut outs = Vec::with_capacity(outs_len); | ||
read_return_data(outs.as_mut_ptr()); | ||
outs.set_len(outs_len); | ||
outs | ||
}; | ||
match status { | ||
0 => Ok(outs), | ||
_ => Err(outs), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
pub use util::{Bytes20, Bytes32}; | ||
|
||
pub mod contract; | ||
pub mod debug; | ||
mod util; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be useful to add documentation to this function, specifically regarding what the error result contains. In the case the unsafe call to read_return_data fails, what will be included in outs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea, I've added some documentation :)
If
read_return_data
fails, then execution will halt before resuming client execution. So from the user's perspective, this isn't something they need to worry about unless they edit the crate to callread_return_data
directly. I've still added a comment on thatextern
just in case though