Skip to content

Commit

Permalink
[Relayer] Implement Relayer, support relayer Ethereum block to rooch …
Browse files Browse the repository at this point in the history
…and update RoochFramework timestamp (#790)

* [Relayer&Framework] Implement Relayer and timestamp, support relayer Ethereum block to rooch and update timestamp based on the block.
  • Loading branch information
jolestar authored Sep 13, 2023
1 parent 0c6ea74 commit ac8675a
Show file tree
Hide file tree
Showing 46 changed files with 1,595 additions and 105 deletions.
40 changes: 40 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"crates/rooch-genesis",
"crates/rooch-genesis-builder",
"crates/rooch-integration-test-runner",
"crates/rooch-relayer",
"crates/rooch-rpc-server",
"crates/rooch-rpc-client",
"crates/rooch-rpc-api",
Expand Down Expand Up @@ -82,6 +83,7 @@ rooch-framework-tests = { path = "crates/rooch-framework-tests" }
rooch-integration-test-runner = { path = "crates/rooch-integration-test-runner" }
rooch-genesis = { path = "crates/rooch-genesis" }
rooch-genesis-builder = { path = "crates/rooch-genesis-builder" }
rooch-relayer = { path = "crates/rooch-relayer" }
rooch-rpc-server = { path = "crates/rooch-rpc-server" }
rooch-rpc-client = { path = "crates/rooch-rpc-client" }
rooch-rpc-api = { path = "crates/rooch-rpc-api" }
Expand Down
21 changes: 13 additions & 8 deletions crates/rooch-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub static R_OPT_NET_HELP: &str = r#"Chain Network
Use rooch_generator command to generate a genesis config."#;

#[derive(Clone, Debug, Parser, Default, Serialize, Deserialize)]
#[clap(name = "rooch", about = "Rooch")]
pub struct RoochOpt {
#[serde(skip_serializing_if = "Option::is_none")]
#[clap(long = "data-dir", short = 'd', parse(from_os_str))]
Expand All @@ -66,18 +65,23 @@ pub struct RoochOpt {
/// If dev chainid, start the service with a temporary data store.
/// All data will be deleted when the service is stopped.
#[serde(skip_serializing_if = "Option::is_none")]
#[clap(long, short = 'n', help = R_OPT_NET_HELP)]
pub chain_id: Option<RoochChainID>,

// #[serde(skip_serializing_if = "Option::is_none")]
// #[clap(long = "genesis-config")]
// /// Init chain by a custom genesis config. if want to reuse builtin network config, just pass a builtin network name.
// /// This option only work for node init start.
// pub genesis_config: Option<String>,
pub store: Option<StoreConfig>,
#[clap(flatten)]
pub store: StoreConfig,

/// Optional custom port, which the rooch server should listen on.
/// The port on which the server should listen defaults to `50051`
#[serde(skip_serializing_if = "Option::is_none")]
#[clap(long, short = 'p')]
pub port: Option<u16>,

/// The Ethereum RPC URL to connect to for relay L1 block and transaction to L2.
/// If not set, the relayer service will not start.
#[serde(skip_serializing_if = "Option::is_none")]
#[clap(long)]
pub eth_rpc_url: Option<String>,
}

impl std::fmt::Display for RoochOpt {
Expand All @@ -95,8 +99,9 @@ impl RoochOpt {
RoochOpt {
base_data_dir: None,
chain_id: Some(RoochChainID::DEV),
store: None,
store: StoreConfig::default(),
port: None,
eth_rpc_url: None,
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions crates/rooch-config/src/store_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,24 @@ impl StoreConfig {
impl ConfigModule for StoreConfig {
fn merge_with_opt(&mut self, opt: &RoochOpt, base: Arc<BaseConfig>) -> Result<()> {
self.base = Some(base);
if opt.store.is_some() {
let store_config = opt.store.clone().unwrap();
if store_config.max_open_files.is_some() {
self.max_open_files = store_config.max_open_files;
}
if store_config.max_total_wal_size.is_some() {
self.max_total_wal_size = store_config.max_total_wal_size;
}
if store_config.cache_size.is_some() {
self.cache_size = store_config.cache_size;
}
if store_config.bytes_per_sync.is_some() {
self.bytes_per_sync = store_config.bytes_per_sync;
}
if store_config.wal_bytes_per_sync.is_some() {
self.wal_bytes_per_sync = store_config.wal_bytes_per_sync;
}

let store_config = opt.store.clone();
if store_config.max_open_files.is_some() {
self.max_open_files = store_config.max_open_files;
}
if store_config.max_total_wal_size.is_some() {
self.max_total_wal_size = store_config.max_total_wal_size;
}
if store_config.cache_size.is_some() {
self.cache_size = store_config.cache_size;
}
if store_config.bytes_per_sync.is_some() {
self.bytes_per_sync = store_config.bytes_per_sync;
}
if store_config.wal_bytes_per_sync.is_some() {
self.wal_bytes_per_sync = store_config.wal_bytes_per_sync;
}

Ok(())
}
}
Expand Down
9 changes: 7 additions & 2 deletions crates/rooch-executor/src/actor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use rooch_types::address::MultiChainAddress;
use rooch_types::framework::address_mapping::AddressMapping;
use rooch_types::framework::auth_validator::AuthValidatorCaller;
use rooch_types::framework::auth_validator::TxValidateResult;
use rooch_types::framework::genesis::GenesisContext;
use rooch_types::framework::transaction_validator::TransactionValidator;
use rooch_types::framework::{system_post_execute_functions, system_pre_execute_functions};
use rooch_types::transaction::AuthenticatorInfo;
Expand All @@ -57,8 +58,12 @@ type ValidateAuthenticatorResult =
Result<(TxValidateResult, Vec<FunctionCall>, Vec<FunctionCall>), VMStatus>;

impl ExecutorActor {
pub fn new(chain_id: u64, moveos_store: MoveOSStore, rooch_store: RoochStore) -> Result<Self> {
let genesis: RoochGenesis = rooch_genesis::RoochGenesis::build(chain_id)?;
pub fn new(
genesis_ctx: GenesisContext,
moveos_store: MoveOSStore,
rooch_store: RoochStore,
) -> Result<Self> {
let genesis: RoochGenesis = rooch_genesis::RoochGenesis::build(genesis_ctx)?;
let moveos = MoveOS::new(
moveos_store,
genesis.all_natives(),
Expand Down
3 changes: 2 additions & 1 deletion crates/rooch-framework-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ rust-version = { workspace = true }
[dependencies]
anyhow = { workspace = true }
better_any = { workspace = true }
ethers = { workspace = true }
fastcrypto = { workspace = true }
linked-hash-map = { workspace = true }
once_cell = { workspace = true }
serde = { workspace = true }
serde_bytes = { workspace = true }
serde_json = { workspace = true }
sha3 = { workspace = true }
smallvec = { workspace = true }
hex = { workspace = true }
ethers = { workspace = true }
bcs = { workspace = true }

move-binary-format = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-framework-tests/src/binding_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl RustBindingTest {
let moveos_store = MoveOSStore::mock_moveos_store()?;
let rooch_store = RoochStore::mock_rooch_store()?;
let executor =
ExecutorActor::new(RoochChainID::DEV.chain_id().id(), moveos_store, rooch_store)?;
ExecutorActor::new(RoochChainID::DEV.genesis_ctx(), moveos_store, rooch_store)?;
Ok(Self { executor })
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::binding_test;
use ethers::prelude::*;
use moveos_types::transaction::MoveAction;
use rooch_key::keystore::{AccountKeystore, InMemKeystore};
use rooch_types::address::RoochAddress;
use rooch_types::coin_type::CoinID;
use rooch_types::crypto::RoochKeyPair;
use rooch_types::framework::ethereum_light_client::BlockHeader;
use rooch_types::transaction::rooch::RoochTransactionData;

#[test]
fn test_submit_block() {
tracing_subscriber::fmt::init();
let mut binding_test = binding_test::RustBindingTest::new().unwrap();

let keystore = InMemKeystore::<RoochAddress, RoochKeyPair>::new_insecure_for_tests(1);
let sender = keystore.addresses()[0];
let sequence_number = 0;

let json = serde_json::json!(
{
"baseFeePerGas": "0x7",
"miner": "0x0000000000000000000000000000000000000001",
"number": "0x1b4",
"hash": "0x0e670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331",
"parentHash": "0x9646252be9520f6e71339a8df9c55e4d7619deeb018d2a3f2d21fc165dde5eb5",
"mixHash": "0x1010101010101010101010101010101010101010101010101010101010101010",
"nonce": "0x0000000000000000",
"sealFields": [
"0xe04d296d2460cfb8472af2c5fd05b5a214109c25688d3704aed5484f9a7792f2",
"0x0000000000000042"
],
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logsBloom": "0x0e670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d15273310e670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d15273310e670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d15273310e670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d15273310e670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d15273310e670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d15273310e670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d15273310e670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331",
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"stateRoot": "0xd5855eb08b3387c0af375e9cdb6acfc05eb8f519e419b874b6ff2ffda7ed1dff",
"difficulty": "0x27f07",
"totalDifficulty": "0x27f07",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"size": "0x27f07",
"gasLimit": "0x9f759",
"minGasPrice": "0x9f759",
"gasUsed": "0x9f759",
"timestamp": "0x54e34e8e",
"transactions": [],
"uncles": []
}
);

let ethereum_block: Block<()> = serde_json::from_value(json).unwrap();

let block_header = BlockHeader::try_from(&ethereum_block).unwrap();
let action = MoveAction::Function(rooch_types::framework::ethereum_light_client::EthereumLightClientModule::create_submit_new_block_call(&block_header));
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, CoinID::Rooch)
.unwrap();
binding_test.execute(tx).unwrap();

let timestamp_module =
binding_test.as_module_bundle::<rooch_types::framework::timestamp::TimestampModule>();

let now_microseconds = timestamp_module.now_microseconds().unwrap();
let duration = std::time::Duration::from_secs(block_header.timestamp.unchecked_as_u64());
println!(
"now_microseconds: {}, header_timestamp: {}",
now_microseconds, block_header.timestamp
);
assert_eq!(now_microseconds, duration.as_micros() as u64);
}
1 change: 1 addition & 0 deletions crates/rooch-framework-tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

mod empty_tests;
mod ethereum_light_client_test;
mod ethereum_validator_tests;
mod native_validator_tests;
mod transaction_validator_tests;
2 changes: 2 additions & 0 deletions crates/rooch-framework/doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ This is the reference documentation of the Rooch Framework.
- [`0x3::empty`](empty.md#0x3_empty)
- [`0x3::encoding`](encoding.md#0x3_encoding)
- [`0x3::ethereum_address`](ethereum_address.md#0x3_ethereum_address)
- [`0x3::ethereum_light_client`](ethereum_light_client.md#0x3_ethereum_light_client)
- [`0x3::ethereum_validator`](ethereum_validator.md#0x3_ethereum_validator)
- [`0x3::gas_coin`](gas_coin.md#0x3_gas_coin)
- [`0x3::genesis`](genesis.md#0x3_genesis)
- [`0x3::hash`](hash.md#0x3_hash)
- [`0x3::native_validator`](native_validator.md#0x3_native_validator)
- [`0x3::schnorr`](schnorr.md#0x3_schnorr)
- [`0x3::session_key`](session_key.md#0x3_session_key)
- [`0x3::timestamp`](timestamp.md#0x3_timestamp)
- [`0x3::transaction_fee`](transaction_fee.md#0x3_transaction_fee)
- [`0x3::transaction_validator`](transaction_validator.md#0x3_transaction_validator)

Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-framework/doc/ethereum_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@



<pre><code><b>struct</b> <a href="ethereum_address.md#0x3_ethereum_address_ETHAddress">ETHAddress</a> <b>has</b> drop, store
<pre><code><b>struct</b> <a href="ethereum_address.md#0x3_ethereum_address_ETHAddress">ETHAddress</a> <b>has</b> <b>copy</b>, drop, store
</code></pre>


Expand Down
Loading

0 comments on commit ac8675a

Please sign in to comment.