-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
MathieuDutSik
merged 6 commits into
linera-io:main
from
MathieuDutSik:linera_feature_support
May 13, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ba54ab3
Add more functionalities to linera.sol
MathieuDutSik c258fc0
Reformatting.
MathieuDutSik f0ceae6
Change the code to switch to generated code.
MathieuDutSik 8205942
Switch to the deifferent scheme of the Linera.sol
MathieuDutSik 9d4651c
Reformatting.
MathieuDutSik 9752f5b
Precise the origin of the keys in Linera.sol
MathieuDutSik 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,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 | ||
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; | ||
} | ||
} |
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.
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?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.
It has to match the bcs encoding of the type
PrecompileTag
.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.
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?
etc? It is automatically documented that we assign these indices for a reason - the diff is clearer as well.
strum
ornum_enum
crates provide the macros to derivefrom
/into
impls.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.
This organization of the
PrecompileTag
is the one before this PR.The problem is that some runtime operations are available in both
Contract
andService
, some are available only inContract
and some only inService
.With the proposed reorganization, things become much clearer and the code is better organized:
But since we do not want to have boilerplate code, this implies having 2 bytes instead of just 1.
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.
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.