-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Synchronize balances between Linera and the EVM. #4274
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
base: main
Are you sure you want to change the base?
Changes from all commits
9e56830
67d5d02
de05105
e023b0d
06324d9
6d92add
cdca92b
ae25c5e
7e09e71
File filter
Filter by extension
Conversations
Jump to
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.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,8 @@ use linera_witty::{WitLoad, WitStore, WitType}; | |||||
| use serde::{Deserialize, Serialize}; | ||||||
| use thiserror::Error; | ||||||
|
|
||||||
| use crate::data_types::Amount; | ||||||
|
|
||||||
| #[derive( | ||||||
| Clone, | ||||||
| Copy, | ||||||
|
|
@@ -63,7 +65,46 @@ pub enum EvmQuery { | |||||
| /// A read-only query. | ||||||
| Query(Vec<u8>), | ||||||
| /// A request to schedule an operation that can mutate the application state. | ||||||
| Mutation(Vec<u8>), | ||||||
| Operation(Vec<u8>), | ||||||
| /// A request to schedule operations that can mutate the application state. | ||||||
| Mutations(Vec<Vec<u8>>), | ||||||
| Operations(Vec<Vec<u8>>), | ||||||
| } | ||||||
|
|
||||||
| /// An EVM operation containing a value and argument data. | ||||||
| #[derive(Debug, Default, Serialize, Deserialize)] | ||||||
| pub struct EvmOperation { | ||||||
| /// The amount being transferred. | ||||||
| pub value: alloy_primitives::U256, | ||||||
| /// The encoded argument data. | ||||||
| pub argument: Vec<u8>, | ||||||
| } | ||||||
|
|
||||||
| impl EvmOperation { | ||||||
| /// An EVM transaction with a specified amount and function input. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(And maybe it should be "EVM operation" now?) |
||||||
| pub fn new(amount: Amount, argument: Vec<u8>) -> Self { | ||||||
| Self { | ||||||
| value: amount.into(), | ||||||
| argument, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Convert the input to a `Vec<u8>` if possible. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| pub fn to_bytes(&self) -> Result<Vec<u8>, bcs::Error> { | ||||||
| bcs::to_bytes(&self) | ||||||
| } | ||||||
|
|
||||||
| /// Creates an `EvmQuery` from the input. | ||||||
| pub fn to_evm_query(&self) -> Result<EvmQuery, bcs::Error> { | ||||||
| Ok(EvmQuery::Operation(self.to_bytes()?)) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// The instantiation argument to EVM smart contracts. | ||||||
| /// `value` is the amount being transferred. | ||||||
| #[derive(Default, Serialize, Deserialize)] | ||||||
| pub struct EvmInstantiation { | ||||||
| /// The initial value put in the instantiation of the contract. | ||||||
| pub value: alloy_primitives::U256, | ||||||
| /// The input to the `fn instantiate` of the EVM smart contract. | ||||||
| pub argument: Vec<u8>, | ||||||
| } | ||||||
|
Comment on lines
+102
to
110
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it important to have this as a separate type? Could instantiation simply be considered a specific kind of operation?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we can. |
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,15 @@ library Linera { | |
| return ChainId(entry.value.value); | ||
| } | ||
|
|
||
| function chainid_to(Linera.ChainId memory entry) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Below (and in Rust code everywhere) we use |
||
| internal | ||
| pure | ||
| returns (LineraTypes.ChainId memory) | ||
| { | ||
| LineraTypes.CryptoHash memory hash = LineraTypes.CryptoHash(entry.value); | ||
| return LineraTypes.ChainId(hash); | ||
| } | ||
|
|
||
| struct AccountOwner { | ||
| uint8 choice; | ||
| // choice=0 corresponds to Reserved | ||
|
|
@@ -54,6 +63,21 @@ library Linera { | |
| return LineraTypes.AccountOwner(owner.choice, owner.reserved, hash, owner.address20); | ||
| } | ||
|
|
||
| struct Account { | ||
| ChainId chain_id; | ||
| AccountOwner owner; | ||
| } | ||
|
|
||
| function account_to(Linera.Account memory account_i) | ||
| internal | ||
| pure | ||
| returns (LineraTypes.Account memory) | ||
| { | ||
| LineraTypes.ChainId memory chain_id2 = chainid_to(account_i.chain_id); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Why "2"?) |
||
| LineraTypes.AccountOwner memory owner2 = accountowner_to(account_i.owner); | ||
| return LineraTypes.Account(chain_id2, owner2); | ||
| } | ||
|
|
||
| struct AccountOwnerBalance { | ||
| AccountOwner account_owner; | ||
| uint256 balance; | ||
|
|
@@ -551,6 +575,19 @@ library Linera { | |
| return opt_uint32_from(output2); | ||
| } | ||
|
|
||
| function transfer(Linera.Account memory account, uint256 amount) internal { | ||
| address precompile = address(0x0b); | ||
| LineraTypes.Account memory account2 = account_to(account); | ||
| LineraTypes.Amount memory amount2 = LineraTypes.Amount(bytes32(amount)); | ||
| LineraTypes.ContractRuntimePrecompile_Transfer memory transfer_ = LineraTypes.ContractRuntimePrecompile_Transfer(account2, amount2); | ||
| LineraTypes.ContractRuntimePrecompile memory contract_ = LineraTypes.ContractRuntimePrecompile_case_transfer(transfer_); | ||
| LineraTypes.RuntimePrecompile memory input1 = LineraTypes.RuntimePrecompile_case_contract(contract_); | ||
| bytes memory input2 = LineraTypes.bcs_serialize_RuntimePrecompile(input1); | ||
| (bool success, bytes memory output) = precompile.call(input2); | ||
| require(success); | ||
| require(output.length == 0); | ||
| } | ||
|
|
||
| // ServiceRuntime functions. | ||
|
|
||
| function try_query_application(bytes32 universal_address, bytes memory argument) internal returns (bytes memory) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.