Skip to content

Provide EVM support for some linera features #3847

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

Merged
merged 6 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ lru = "0.12.3"
num-bigint = "0.4.3"
num-format = "0.4.4"
num-traits = "0.2.18"
num_enum = "0.7.3"
octocrab = "0.42.1"
oneshot = "0.1.6"
pathdiff = "0.2.1"
Expand Down
2 changes: 0 additions & 2 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions linera-base/src/crypto/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ impl TryFrom<&[u8]> for CryptoHash {
}
}

impl From<CryptoHash> for [u8; 32] {
fn from(crypto_hash: CryptoHash) -> Self {
crypto_hash.0 .0
}
}

impl From<[u64; 4]> for CryptoHash {
fn from(integers: [u64; 4]) -> Self {
CryptoHash(crate::crypto::u64_array_to_be_bytes(integers).into())
Expand Down
1 change: 0 additions & 1 deletion linera-execution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ linera-views.workspace = true
linera-views-derive.workspace = true
linera-witty = { workspace = true, features = ["log", "macros"] }
lru.workspace = true
num_enum.workspace = true
oneshot.workspace = true
prometheus = { workspace = true, optional = true }
proptest = { workspace = true, optional = true }
Expand Down
131 changes: 131 additions & 0 deletions linera-execution/solidity/Linera.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import "./LineraTypes.sol";

// This library provides Linera functionalities to EVM contracts
// It should not be modified.

// The Precompile keys below correspond to the BCS serialization of
// the `PrecompileTag` in `linera-execution/src/evm/revm.rs`.
// (0,0): chain_id
// (0,1): application_creator_chain_id
// (0,2): chain_ownership
// (0,3): read data blob
// (0,4): assert data blob exists
// (1,0): try_call_application
// (1,1): validation round
// (1,2): send_message
// (1,3): message_id
// (1,4): message_is_bouncing
// (2,0): try_query_application
Comment on lines +12 to +22
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the reasoning for choosing this particular encoding? each element of the tuple is uint8 so holds numbers up to 255. Wouldn't it be easier to simply keep the indexes monotonically increase?

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 has to match the bcs encoding of the type PrecompileTag.

Copy link
Contributor

Choose a reason for hiding this comment

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

So it also means that if we change the order of enum (variants) this would also have to change?

Why not manually assign values to it?

#[repr(u8)]
enum PrecompileTag {
    ChainId = 0x01,
    ApplicationCreatorChainId = 0x02
    ...
}

etc? It is automatically documented that we assign these indices for a reason - the diff is clearer as well.

strum or num_enum crates provide the macros to derive from/into impls.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This organization of the PrecompileTag is the one before this PR.

The problem is that some runtime operations are available in both Contract and Service, some are available only in Contract and some only in Service.

With the proposed reorganization, things become much clearer and the code is better organized:

#[derive(Debug, Serialize, Deserialize)]
enum PrecompileTag {
    Base(BasePrecompileTag),
    Contract(ContractPrecompileTag),
    Service(ServicePrecompileTag),
}

But since we do not want to have boilerplate code, this implies having 2 bytes instead of just 1.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. I still think it'd be good to have some better safety precautions here.

Maybe for the time being add a longer comment here that these indices have to match the serde encoding of Precompile tag. Also, maybe a comment on PrecompileTag with a warning that things should not be reorganised b/c they can break the integration.

library Linera {

function inner_chain_id(uint8 val) internal returns (LineraTypes.ChainId memory) {
address precompile = address(0x0b);
bytes memory input1 = new bytes(2);
input1[0] = bytes1(uint8(0));
input1[1] = bytes1(val);
(bool success, bytes memory output1) = precompile.call(input1);
require(success);
return LineraTypes.bcs_deserialize_ChainId(output1);
}

function chain_id() internal returns (LineraTypes.ChainId memory) {
return inner_chain_id(uint8(0));
}

function application_creator_chain_id() internal returns (LineraTypes.ChainId memory) {
return inner_chain_id(uint8(1));
}

function chain_ownership() internal returns (LineraTypes.ChainOwnership memory) {
address precompile = address(0x0b);
bytes memory input1 = new bytes(2);
input1[0] = bytes1(uint8(0));
input1[1] = bytes1(uint8(2));
(bool success, bytes memory output1) = precompile.call(input1);
require(success);
return LineraTypes.bcs_deserialize_ChainOwnership(output1);
}

function read_data_blob(bytes32 hash) internal returns (bytes memory) {
address precompile = address(0x0b);
bytes memory input1 = new bytes(2);
input1[0] = bytes1(uint8(0));
input1[1] = bytes1(uint8(3));
bytes memory input2 = abi.encodePacked(input1, hash);
(bool success, bytes memory output1) = precompile.call(input2);
require(success);
return output1;
}

function assert_data_blob_exists(bytes32 hash) internal {
address precompile = address(0x0b);
bytes memory input1 = new bytes(2);
input1[0] = bytes1(uint8(0));
input1[1] = bytes1(uint8(4));
bytes memory input2 = abi.encodePacked(input1, hash);
(bool success, bytes memory output1) = precompile.call(input2);
require(success);
assert(output1.length == 0);
}

function try_call_application(bytes32 universal_address, bytes memory operation) internal returns (bytes memory) {
address precompile = address(0x0b);
bytes memory input2 = abi.encodePacked(uint8(1), uint8(0), universal_address, operation);
(bool success, bytes memory output) = precompile.call(input2);
require(success);
return output;
}

function validation_round() internal returns (LineraTypes.opt_uint32 memory) {
address precompile = address(0x0b);
bytes memory input1 = new bytes(2);
input1[0] = bytes1(uint8(1));
input1[1] = bytes1(uint8(1));
(bool success, bytes memory output1) = precompile.call(input1);
require(success);
LineraTypes.opt_uint32 memory val = LineraTypes.bcs_deserialize_opt_uint32(output1);
return val;
}

function send_message(bytes32 input_chain_id, bytes memory message) internal {
address precompile = address(0x0b);
bytes memory input2 = abi.encodePacked(uint8(1), uint8(2), input_chain_id, message);
(bool success, bytes memory output) = precompile.call(input2);
require(success);
require(output.length == 0);
}

function message_id() internal returns (LineraTypes.opt_MessageId memory) {
address precompile = address(0x0b);
bytes memory input1 = new bytes(2);
input1[0] = bytes1(uint8(1));
input1[1] = bytes1(uint8(3));
(bool success, bytes memory output1) = precompile.call(input1);
require(success);
LineraTypes.opt_MessageId memory output2 = LineraTypes.bcs_deserialize_opt_MessageId(output1);
return output2;
}

function message_is_bouncing() internal returns (LineraTypes.MessageIsBouncing memory) {
address precompile = address(0x0b);
bytes memory input1 = new bytes(2);
input1[0] = bytes1(uint8(1));
input1[1] = bytes1(uint8(4));
(bool success, bytes memory output1) = precompile.call(input1);
require(success);
LineraTypes.MessageIsBouncing memory output2 = abi.decode(output1, (LineraTypes.MessageIsBouncing));
return output2;
}

function try_query_application(bytes32 universal_address, bytes memory argument) internal returns (bytes memory) {
address precompile = address(0x0b);
bytes memory input2 = abi.encodePacked(uint8(2), uint8(0), universal_address, argument);
(bool success, bytes memory output) = precompile.call(input2);
require(success);
return output;
}
}
Loading