Skip to content

Commit

Permalink
support the origin opcode (#103)
Browse files Browse the repository at this point in the history
Signed-off-by: Cyrill Leutwiler <[email protected]>
  • Loading branch information
xermicus authored Oct 29, 2024
1 parent a4043ec commit 5b3b90d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
54 changes: 54 additions & 0 deletions crates/integration/contracts/Transaction.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/* runner.json
{
"differential": true,
"actions": [
{
"Upload": {
"code": {
"Solidity": {
"contract": "TransactionOrigin"
}
}
}
},
{
"Instantiate": {
"code": {
"Solidity": {
"contract": "TransactionTester"
}
}
}
},
{
"Call": {
"dest": {
"Instantiated": 0
},
"data": "f8a8fd6d"
}
}
]
}
*/

contract TransactionTester {
constructor() payable {
assert(tx.origin == new TransactionOrigin().test());
}

function test() public payable returns (address ret) {
ret = tx.origin;
}
}

contract TransactionOrigin {
function test() public payable returns (address ret) {
assert(msg.sender != tx.origin);

ret = tx.origin;
}
}
1 change: 1 addition & 0 deletions crates/integration/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ test_spec!(call, "Caller", "Call.sol");
test_spec!(transfer, "Transfer", "Transfer.sol");
test_spec!(return_data_oob, "ReturnDataOob", "ReturnDataOob.sol");
test_spec!(immutables, "Immutables", "Immutables.sol");
test_spec!(transaction, "Transaction", "Transaction.sol");

fn instantiate(path: &str, contract: &str) -> Vec<SpecsAction> {
vec![Instantiate {
Expand Down
11 changes: 9 additions & 2 deletions crates/llvm-context/src/polkavm/evm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ where

/// Translates the `tx.origin` instruction.
pub fn origin<'ctx, D>(
_context: &mut Context<'ctx, D>,
context: &mut Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
todo!()
let address_type = context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS);
let address_pointer = context.build_alloca_at_entry(address_type, "origin_address");
context.build_store(address_pointer, address_type.const_zero())?;
context.build_runtime_call(
revive_runtime_api::polkavm_imports::ORIGIN,
&[address_pointer.to_int(context).into()],
);
context.build_load_address(address_pointer)
}

/// Translates the `chain_id` instruction.
Expand Down
2 changes: 2 additions & 0 deletions crates/runtime-api/src/polkavm_imports.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ POLKAVM_IMPORT(uint32_t, instantiate, uint32_t)

POLKAVM_IMPORT(void, now, uint32_t)

POLKAVM_IMPORT(void, origin, uint32_t)

POLKAVM_IMPORT(void, seal_return, uint32_t, uint32_t, uint32_t)

POLKAVM_IMPORT(uint32_t, set_storage, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)
Expand Down
5 changes: 4 additions & 1 deletion crates/runtime-api/src/polkavm_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub static INSTANTIATE: &str = "instantiate";

pub static NOW: &str = "now";

pub static ORIGIN: &str = "origin";

pub static RETURN: &str = "seal_return";

pub static SET_STORAGE: &str = "set_storage";
Expand All @@ -60,7 +62,7 @@ pub static VALUE_TRANSFERRED: &str = "value_transferred";

/// All imported runtime API symbols.
/// Useful for configuring common attributes and linkage.
pub static IMPORTS: [&str; 24] = [
pub static IMPORTS: [&str; 25] = [
SBRK,
MEMORY_SIZE,
ADDRESS,
Expand All @@ -79,6 +81,7 @@ pub static IMPORTS: [&str; 24] = [
INPUT,
INSTANTIATE,
NOW,
ORIGIN,
RETURN,
RETURNDATACOPY,
RETURNDATASIZE,
Expand Down

0 comments on commit 5b3b90d

Please sign in to comment.