Skip to content

Commit

Permalink
EVM: Introduce DevSigner (Sovereign-Labs#837)
Browse files Browse the repository at this point in the history
* Add signer

* Add signers in sov-ethereum

* Eth signers in the rollup

* read tx signers

* implement dev signer

* update bench

* make lint

* cleanup
  • Loading branch information
bkolad authored Sep 12, 2023
1 parent 80230c8 commit afe2291
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 94 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

17 changes: 9 additions & 8 deletions examples/demo-prover/Cargo.lock

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

45 changes: 36 additions & 9 deletions examples/demo-prover/methods/guest/Cargo.lock

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

6 changes: 4 additions & 2 deletions examples/demo-rollup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ tracing = { workspace = true }
hex = { workspace = true }
bytes = { workspace = true }
jmt = { workspace = true }
secp256k1 = { workspace = true, optional = true }
futures = "0.3"

# Crates which only this package depends on
tokio = { workspace = true }
reth-primitives = { workspace = true, optional = true }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

demo-stf = { path = "../demo-stf", features = ["native"] }
Expand All @@ -43,6 +44,7 @@ const-rollup-config = { path = "../const-rollup-config" }
sov-cli = { path = "../../module-system/sov-cli" }
clap = { workspace = true }


[dev-dependencies]
sov-evm = { path = "../../module-system/module-implementations/sov-evm", features = ["smart_contracts"] }
sov-bank = { path = "../../module-system/module-implementations/sov-bank", features = ["native"] }
Expand All @@ -69,7 +71,7 @@ sov-demo-rollup = { path = ".", features = ["experimental"] }

[features]
default = []
experimental = ["sov-ethereum/experimental"]
experimental = ["sov-ethereum/experimental", "reth-primitives", "secp256k1"]

[[bench]]
name = "rollup_bench"
Expand Down
2 changes: 2 additions & 0 deletions examples/demo-rollup/benches/rollup_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ fn rollup_bench(_bench: &mut Criterion) {
sequencer_private_key.default_address(),
sequencer_da_address.as_ref().to_vec(),
&sequencer_private_key,
#[cfg(feature = "experimental")]
Default::default(),
);

demo.init_chain(demo_genesis_config);
Expand Down
2 changes: 2 additions & 0 deletions examples/demo-rollup/benches/rollup_coarse_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ async fn main() -> Result<(), anyhow::Error> {
sequencer_private_key.default_address(),
sequencer_da_address.as_ref().to_vec(),
&sequencer_private_key,
#[cfg(feature = "experimental")]
Default::default(),
);

demo.init_chain(demo_genesis_config);
Expand Down
28 changes: 19 additions & 9 deletions examples/demo-rollup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ mod rollup;
use const_rollup_config::ROLLUP_NAMESPACE_RAW;
use demo_stf::genesis_config::create_demo_genesis_config;
use demo_stf::runtime::GenesisConfig;
#[cfg(feature = "experimental")]
pub use rollup::read_tx_signer_priv_key;
pub use rollup::{
new_rollup_with_celestia_da, new_rollup_with_mock_da, new_rollup_with_mock_da_from_config,
Rollup,
Expand Down Expand Up @@ -40,20 +38,32 @@ pub fn initialize_ledger(path: impl AsRef<std::path::Path>) -> LedgerDB {
/// ```
pub fn get_genesis_config<Da: DaSpec>(
sequencer_da_address: <<Da as DaSpec>::BlobTransaction as BlobReaderTrait>::Address,
#[cfg(feature = "experimental")] eth_accounts: Vec<reth_primitives::Address>,
) -> GenesisConfig<DefaultContext, Da> {
let data = std::fs::read_to_string("../test-data/keys/token_deployer_private_key.json")
.expect("Unable to read file to string");
let key_and_address: PrivateKeyAndAddress<DefaultContext> = serde_json::from_str(&data)
.unwrap_or_else(|_| panic!("Unable to convert data {} to PrivateKeyAndAddress", &data));
let token_deployer_data =
std::fs::read_to_string("../test-data/keys/token_deployer_private_key.json")
.expect("Unable to read file to string");

let token_deployer: PrivateKeyAndAddress<DefaultContext> =
serde_json::from_str(&token_deployer_data).unwrap_or_else(|_| {
panic!(
"Unable to convert data {} to PrivateKeyAndAddress",
&token_deployer_data
)
});

assert!(
key_and_address.is_matching_to_default(),
token_deployer.is_matching_to_default(),
"Inconsistent key data"
);

// TODO: #840
create_demo_genesis_config(
100000000,
key_and_address.address,
token_deployer.address,
sequencer_da_address.as_ref().to_vec(),
&key_and_address.private_key,
&token_deployer.private_key,
#[cfg(feature = "experimental")]
eth_accounts,
)
}
36 changes: 31 additions & 5 deletions examples/demo-rollup/src/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use const_rollup_config::SEQUENCER_DA_ADDRESS;
use demo_stf::app::DefaultPrivateKey;
use demo_stf::app::{App, DefaultContext};
use demo_stf::runtime::{get_rpc_methods, GenesisConfig};
#[cfg(feature = "experimental")]
use secp256k1::SecretKey;
use sov_celestia_adapter::verifier::address::CelestiaAddress;
use sov_celestia_adapter::verifier::RollupParams;
use sov_celestia_adapter::CelestiaService;
Expand Down Expand Up @@ -72,7 +74,14 @@ pub async fn new_rollup_with_celestia_da(

let app = App::new(rollup_config.storage);
let sequencer_da_address = CelestiaAddress::from_str(SEQUENCER_DA_ADDRESS)?;
let genesis_config = get_genesis_config(sequencer_da_address);

#[cfg(feature = "experimental")]
let eth_signer = read_eth_tx_signers();
let genesis_config = get_genesis_config(
sequencer_da_address,
#[cfg(feature = "experimental")]
eth_signer.signers(),
);

Ok(Rollup {
app,
Expand All @@ -83,7 +92,8 @@ pub async fn new_rollup_with_celestia_da(
#[cfg(feature = "experimental")]
eth_rpc_config: EthRpcConfig {
min_blob_size: Some(1),
tx_signer_priv_key: read_tx_signer_priv_key()?,
sov_tx_signer_priv_key: read_sov_tx_signer_priv_key()?,
eth_signer,
},
})
}
Expand All @@ -108,8 +118,14 @@ pub fn new_rollup_with_mock_da_from_config(
let sequencer_da_address = MockAddress::from([0u8; 32]);
let da_service = MockDaService::new(sequencer_da_address);

#[cfg(feature = "experimental")]
let eth_signer = read_eth_tx_signers();
let app = App::new(rollup_config.storage);
let genesis_config = get_genesis_config(sequencer_da_address);
let genesis_config = get_genesis_config(
sequencer_da_address,
#[cfg(feature = "experimental")]
eth_signer.signers(),
);

Ok(Rollup {
app,
Expand All @@ -120,15 +136,16 @@ pub fn new_rollup_with_mock_da_from_config(
#[cfg(feature = "experimental")]
eth_rpc_config: EthRpcConfig {
min_blob_size: Some(1),
tx_signer_priv_key: read_tx_signer_priv_key()?,
sov_tx_signer_priv_key: read_sov_tx_signer_priv_key()?,
eth_signer,
},
})
}

#[cfg(feature = "experimental")]
/// Ethereum RPC wraps EVM transaction in a rollup transaction.
/// This function reads the private key of the rollup transaction signer.
pub fn read_tx_signer_priv_key() -> Result<DefaultPrivateKey, anyhow::Error> {
fn read_sov_tx_signer_priv_key() -> Result<DefaultPrivateKey, anyhow::Error> {
let data = std::fs::read_to_string(TX_SIGNER_PRIV_KEY_PATH).context("Unable to read file")?;

let key_and_address: PrivateKeyAndAddress<DefaultContext> = serde_json::from_str(&data)
Expand All @@ -137,6 +154,15 @@ pub fn read_tx_signer_priv_key() -> Result<DefaultPrivateKey, anyhow::Error> {
Ok(key_and_address.private_key)
}

// TODO: #840
#[cfg(feature = "experimental")]
fn read_eth_tx_signers() -> sov_ethereum::DevSigner {
sov_ethereum::DevSigner::new(vec![SecretKey::from_str(
"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
)
.unwrap()])
}

impl<Vm: Zkvm, Da: DaService<Error = anyhow::Error> + Clone> Rollup<Vm, Da> {
/// Runs the rollup.
pub async fn run(self) -> Result<(), anyhow::Error> {
Expand Down
1 change: 1 addition & 0 deletions examples/demo-rollup/tests/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ async fn send_tx_test_to_eth(rpc_address: SocketAddr) -> Result<(), Box<dyn std:
.with_chain_id(chain_id);

let contract = SimpleStorageContract::default();

let from_addr = Address::from_str("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266").unwrap();

let test_client = TestClient::new(chain_id, key, from_addr, contract, rpc_address).await;
Expand Down
47 changes: 27 additions & 20 deletions examples/demo-stf/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn create_demo_genesis_config<C: Context, Da: DaSpec>(
sequencer_address: C::Address,
sequencer_da_address: Vec<u8>,
value_setter_admin_private_key: &DefaultPrivateKey,
#[cfg(feature = "experimental")] evm_genesis_addresses: Vec<reth_primitives::Address>,
) -> GenesisConfig<C, Da> {
let token_config: sov_bank::TokenConfig<C> = sov_bank::TokenConfig {
token_name: DEMO_TOKEN_NAME.to_owned(),
Expand Down Expand Up @@ -53,13 +54,6 @@ pub fn create_demo_genesis_config<C: Context, Da: DaSpec>(
admin: value_setter_admin_private_key.pub_key().to_address(),
};

#[cfg(feature = "experimental")]
let genesis_evm_address = reth_primitives::Address::from_slice(
hex::decode("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
.unwrap()
.as_slice(),
);

let chain_state_config = ChainStateConfig {
// TODO: Put actual value
initial_slot_height: 0,
Expand All @@ -73,22 +67,33 @@ pub fn create_demo_genesis_config<C: Context, Da: DaSpec>(
value_setter_config,
sov_accounts::AccountConfig { pub_keys: vec![] },
#[cfg(feature = "experimental")]
EvmConfig {
data: vec![AccountData {
address: genesis_evm_address,
balance: AccountData::balance(u64::MAX),
code_hash: AccountData::empty_code(),
code: vec![],
nonce: 0,
}],
chain_id: 1,
limit_contract_code_size: None,
spec: vec![(0, SpecId::LATEST)].into_iter().collect(),
..Default::default()
},
get_evm_config(evm_genesis_addresses),
)
}

// TODO: #840
#[cfg(feature = "experimental")]
fn get_evm_config(genesis_addresses: Vec<reth_primitives::Address>) -> EvmConfig {
let data = genesis_addresses
.into_iter()
.map(|address| AccountData {
address,
balance: AccountData::balance(u64::MAX),
code_hash: AccountData::empty_code(),
code: vec![],
nonce: 0,
})
.collect();

EvmConfig {
data,
chain_id: 1,
limit_contract_code_size: None,
spec: vec![(0, SpecId::LATEST)].into_iter().collect(),
..Default::default()
}
}

pub fn create_demo_config<Da: DaSpec>(
initial_sequencer_balance: u64,
value_setter_admin_private_key: &DefaultPrivateKey,
Expand All @@ -98,5 +103,7 @@ pub fn create_demo_config<Da: DaSpec>(
generate_address::<DefaultContext>(DEMO_SEQ_PUB_KEY_STR),
DEMO_SEQUENCER_DA_ADDRESS.to_vec(),
value_setter_admin_private_key,
#[cfg(feature = "experimental")]
Vec::default(),
)
}
Loading

0 comments on commit afe2291

Please sign in to comment.