Skip to content

Commit

Permalink
Modify the signature to return bytes instead of the Status/Value conv…
Browse files Browse the repository at this point in the history
…ention
  • Loading branch information
MoeMahhouk committed Mar 19, 2024
1 parent fc0937a commit 08343d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 47 deletions.
5 changes: 2 additions & 3 deletions examples/Andromeda.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ contract Andromeda {
require(success);
}

function volatileGet(bytes32 key) public view returns (bool status, bytes32 val) {
function volatileGet(bytes32 key) public view returns (bytes memory val) {
(bool success, bytes memory value) = VOLATILEGET_ADDR.staticcall(abi.encodePacked((key)));
require(success);
// decode value into a boolean status and a bytes32 value
(status, val) = abi.decode(value, (bool, bytes32));
return abi.decode(value, (bytes));
}

function attestSgx(bytes memory userdata) public view returns (bytes memory) {
Expand Down
53 changes: 19 additions & 34 deletions examples/andromeda_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn simulate() -> eyre::Result<()> {
"function localRandom() returns (bytes32)",
"function attestSgx(bytes) returns (bytes)",
"function volatileSet(bytes32,bytes32)",
"function volatileGet(bytes32) returns (bool, bytes32)",
"function volatileGet(bytes32) returns (bytes)",
"function sha512(bytes) returns (bytes)",
"struct HttpRequest { string url; string method; string[] headers; bytes body; bool withFlashbotsSignature; }",
"function doHTTPRequest(HttpRequest memory request) returns (bytes memory)",
Expand Down Expand Up @@ -84,7 +84,7 @@ fn simulate() -> eyre::Result<()> {
//////////////////////////
// Suave.volatileSet/Get
//////////////////////////
let mykey = "deadbeefdeadbeefdeadbeefdeadbeef".as_bytes().to_vec();
let mykey: Vec<u8> = "deadbeefdeadbeefdeadbeefdeadbeef".as_bytes().to_vec();
let myval = "cafebabecafebabecafebabecafebabe".as_bytes().to_vec();
{
let calldata = abi.encode(
Expand All @@ -100,7 +100,7 @@ fn simulate() -> eyre::Result<()> {
let _result = evm.transact()?;
//dbg!(result);
}
// Existing key test
// Existing key test
{
let calldata = abi.encode("volatileGet", (Token::FixedBytes(mykey),))?;
evm.context.env.tx = TxEnv {
Expand All @@ -110,53 +110,38 @@ fn simulate() -> eyre::Result<()> {
..Default::default()
};
let result = evm.transact()?;
let decoded = ethabi::decode(
&[ethabi::ParamType::Bool, ethabi::ParamType::FixedBytes(32)],
result.result.output().unwrap(),
)?;
let status = match &decoded[0] {
Token::Bool(b) => b,
_ => todo!(),
};
assert_eq!(status, &true);
let val = match &decoded[1] {
Token::FixedBytes(b) => b,
let decoded = ethabi::decode(&[ethabi::ParamType::Bytes], result.result.output().unwrap())?;
let val = match &decoded[0] {
Token::Bytes(b) => b,
_ => todo!(),
};
assert_eq!(val.to_vec(), myval);

assert_eq!(val.to_vec(), myval.to_vec());
dbg!(std::str::from_utf8(val).unwrap());

}
// Non-existing key test
{
let nonExistingKey = "beefdeadbeefdeadbeefdeadbeefdead".as_bytes().to_vec();
let calldata = abi.encode("volatileGet", (Token::FixedBytes(nonExistingKey),))?;
// Non Existing key test
{
let notmykey: Vec<u8> = "deadbeefdeadbeefdeadbeefdeadbeff".as_bytes().to_vec();
let calldata = abi.encode("volatileGet", (Token::FixedBytes(notmykey),))?;
evm.context.env.tx = TxEnv {
caller: ADDR_A,
transact_to: revm::primitives::TransactTo::Call(ADDR_B),
data: revm::primitives::Bytes::from(calldata.0),
..Default::default()
};
let result = evm.transact()?;
let decoded = ethabi::decode(
&[ethabi::ParamType::Bool, ethabi::ParamType::FixedBytes(32)],
result.result.output().unwrap(),
)?;
let status = match &decoded[0] {
Token::Bool(b) => b,
_ => todo!(),
};
assert_eq!(status, &false);
let val = match &decoded[1] {
Token::FixedBytes(b) => b,
let decoded = ethabi::decode(&[ethabi::ParamType::Bytes], result.result.output().unwrap())?;
let val = match &decoded[0] {
Token::Bytes(b) => b,
_ => todo!(),
};
assert_eq!(val.to_vec(), vec![0u8; 32]);
//dbg!(std::str::from_utf8(val).unwrap());
// empty vector
let emptyvec: Vec<u8> = Vec::new();
assert_eq!(val.to_vec(), emptyvec.to_vec());
dbg!(std::str::from_utf8(val).unwrap());
}



//////////////////////////
// Suave.sha512
//////////////////////////
Expand Down
15 changes: 5 additions & 10 deletions src/precompiles/sgxattest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,14 @@ fn sgxattest_volatile_get(input: &[u8], gas_limit: u64, env: &Env) -> Precompile
let mut key: [u8; 52] = [0; 52];
key[0..20].copy_from_slice(&domain_sep.0 .0);
key[20..52].copy_from_slice(&input[0..32]);
let mut success = true;
let mut value = [0; 32];
let mut value = ethers::abi::encode(&[Token::Bytes(vec![])]);
if let Some(val) = vol.get(&key) {
value = val.clone();
value = ethers::abi::encode(&[Token::Bytes(val.to_vec())]);
return Ok((gas_used, value.to_vec()));
} else {
success = false;
// return empty value bytes if key does not exist
return Ok((gas_used, value.to_vec()));
}
let result = &ethers::abi::encode(&[
Token::Bool(success),
Token::FixedBytes(value.to_vec()),
]);

return Ok((gas_used, result.to_vec()));
}
}

Expand Down

0 comments on commit 08343d5

Please sign in to comment.