Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tranduy1dol committed Aug 5, 2024
1 parent 95eb31c commit 4b91434
Show file tree
Hide file tree
Showing 8 changed files with 12,156 additions and 3,390 deletions.
11,241 changes: 7,913 additions & 3,328 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"crates/orchestrator",
"crates/da-clients/da-client-interface",
"crates/da-clients/ethereum",
"crates/da-clients/aptos",
"crates/prover-services/prover-client-interface",
"crates/prover-services/gps-fact-checker",
"crates/prover-services/sharp-service",
Expand Down Expand Up @@ -78,6 +79,7 @@ madara-prover-rpc-client = { git = "https://github.com/Moonsong-Labs/madara-prov
# Project
da-client-interface = { path = "crates/da-clients/da-client-interface" }
ethereum-da-client = { path = "crates/da-clients/ethereum" }
aptos-data-client = { path = "crates/da-clients/aptos" }

settlement-client-interface = { path = "crates/settlement-clients/settlement-client-interface" }
ethereum-settlement-client = { path = "crates/settlement-clients/ethereum" }
Expand All @@ -88,3 +90,9 @@ prover-client-interface = { path = "crates/prover-services/prover-client-interfa
gps-fact-checker = { path = "crates/prover-services/gps-fact-checker" }
sharp-service = { path = "crates/prover-services/sharp-service" }
orchestrator = { path = "crates/orchestrator" }

# Aptos
aptos-sdk = { git = "https://github.com/sota-zk-labs/aptos-core" }
[patch.crates-io]
merlin = { git = "https://github.com/aptos-labs/merlin" }
x25519-dalek = { git = "https://github.com/Tranduy1dol/x25519-dalek", branch = "zeroize_v1.7" }
4 changes: 3 additions & 1 deletion crates/da-clients/aptos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ async-trait = { workspace = true }
c-kzg = { workspace = true }
color-eyre = { workspace = true }
da-client-interface = { workspace = true }
dotenv = { workspace = true }
dotenvy = { workspace = true }
hex = "0.4.3"
serde = { workspace = true }
tokio = "1.38.0"
url = { workspace = true }
utils = { workspace = true }
53 changes: 36 additions & 17 deletions crates/da-clients/aptos/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
use aptos_sdk::crypto::_once_cell::sync::Lazy;
use crate::AptosDaClient;
use aptos_sdk::crypto::ed25519::Ed25519PrivateKey;
use aptos_sdk::crypto::ValidCryptoMaterialStringExt;
use aptos_sdk::rest_client::Client;
use aptos_sdk::types::chain_id::ChainId;
use aptos_sdk::types::{AccountKey, LocalAccount};
use async_trait::async_trait;
use c_kzg::KzgSettings;
use da_client_interface::DaConfig;
use dotenvy::dotenv;
use std::path::Path;
use std::str::FromStr;
use url::Url;
use utils::env_utils::get_env_var_or_panic;

#[derive(Clone, Debug)]
pub struct AptosDaConfig {
pub node_url: String,
pub private_key: String,
pub account_address: String,
pub module_address: String,
pub chain_id: String,
}

impl DaConfig for AptosDaConfig {
#[async_trait]
impl DaConfig<AptosDaClient> for AptosDaConfig {
fn new_from_env() -> Self {
static NODE_URL: Lazy<Url> = Lazy::new(|| {
Url::from_str(
std::env::var("APTOS_NODE_URL")
.as_ref()
.map(|s| s.as_str())
.unwrap_or("https://fullnode.devnet.aptoslabs.com"),
)
.unwrap()
});
dotenv().expect("Failed to load .env file");
let node_url = get_env_var_or_panic("APTOS_NODE_URL");
let private_key = get_env_var_or_panic("APTOS_PRIVATE_KEY");
let account_address = get_env_var_or_panic("APTOS_ACCOUNT_ADDRESS");
let module_address = get_env_var_or_panic("APTOS_MODULE_ADDRESS");
let chain_id = get_env_var_or_panic("CHAIN_ID");

Self {
node_url: NODE_URL.to_string(),
private_key: get_env_var_or_panic("PRIVATE_KEY"),
account_address: get_env_var_or_panic("ADDRESS"),
}
Self { chain_id, node_url, private_key, account_address, module_address }
}

async fn build_client(&self) -> AptosDaClient {
let client = Client::new(self.node_url.parse().unwrap());
let private_key =
Ed25519PrivateKey::from_encoded_string(&self.private_key).expect("Failed to parse private key");
let account_key = AccountKey::from(private_key);
let account_address = self.account_address.parse().expect("Invalid account address");
let account = LocalAccount::new(account_address, account_key, 500);
let module_address = self.module_address.parse().expect("Invalid module address");
let chain_id = ChainId::from_str(&self.chain_id).expect("Invalid chain id");
let trusted_setup = KzgSettings::load_trusted_setup_file(Path::new("./trusted_setup.txt"))
.expect("Failed to load trusted setup");

AptosDaClient { client, account, module_address, chain_id, trusted_setup }
}
}
11 changes: 0 additions & 11 deletions crates/da-clients/aptos/src/conversion.rs

This file was deleted.

65 changes: 32 additions & 33 deletions crates/da-clients/aptos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use aptos_sdk::crypto::HashValue;
use aptos_sdk::move_types::account_address::AccountAddress;
use aptos_sdk::move_types::identifier::Identifier;
use aptos_sdk::move_types::language_storage::ModuleId;
use aptos_sdk::move_types::value::{MoveValue, serialize_values};
use aptos_sdk::move_types::value::{serialize_values, MoveValue};
use aptos_sdk::rest_client::Client;
use aptos_sdk::transaction_builder::TransactionBuilder;
use aptos_sdk::types::chain_id::ChainId;
use aptos_sdk::types::LocalAccount;
use aptos_sdk::types::transaction::{EntryFunction, TransactionPayload};
use aptos_sdk::types::LocalAccount;
use async_trait::async_trait;
use c_kzg::{Blob, BYTES_PER_BLOB, KzgCommitment, KzgProof, KzgSettings};
use c_kzg::{Blob, KzgCommitment, KzgProof, KzgSettings, BYTES_PER_BLOB};

use da_client_interface::{DaClient, DaVerificationStatus};

Expand All @@ -27,55 +27,51 @@ pub struct AptosDaClient {
pub account: LocalAccount,
pub module_address: AccountAddress,
pub chain_id: ChainId,
pub trusted_setup: KzgSettings
pub trusted_setup: KzgSettings,
}

#[async_trait]
impl DaClient for AptosDaClient {
async fn publish_state_diff(&self, state_diff: Vec<Vec<u8>>, _to: &[u8; 32]) -> color_eyre::Result<String> {
let (blobs, commitments, proofs) = prepare_blob(&state_diff, &self.trusted_setup).await?;

let sequence_number = self.client.get_account(self.account.address()).await.unwrap().into_inner().sequence_number;
let sequence_number =
self.client.get_account(self.account.address()).await.unwrap().into_inner().sequence_number;
self.account.set_sequence_number(sequence_number);
let sequencer_number = self.account.increment_sequence_number();

let payload = TransactionPayload::EntryFunction(
EntryFunction::new(
ModuleId::new(
self.account.address(),
Identifier::new("starknet_validity").unwrap(),
),
Identifier::new("blob_submission").unwrap(),
vec![],
serialize_values(
vec![
&MoveValue::vector_u8(blobs[0].last_chunk::<32>().unwrap().to_vec()),
&MoveValue::vector_u8(commitments[0].last_chunk::<32>().unwrap().to_vec()),
&MoveValue::vector_u8(proofs[0].last_chunk::<32>().unwrap().to_vec()),
]
),
)
);
let payload = TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(self.account.address(), Identifier::new("starknet_validity").unwrap()),
Identifier::new("blob_submission").unwrap(),
vec![],
serialize_values(vec![
&MoveValue::vector_u8(blobs[0].last_chunk::<32>().unwrap().to_vec()),
&MoveValue::vector_u8(commitments[0].last_chunk::<32>().unwrap().to_vec()),
&MoveValue::vector_u8(proofs[0].last_chunk::<32>().unwrap().to_vec()),
]),
));

// Build transaction.
let txn = TransactionBuilder::new(
payload,
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() + 60,
self.chain_id,
)
.sender(self.account.address())
.sequence_number(sequencer_number)
.max_gas_amount(10000)
.gas_unit_price(100)
.build();
.sender(self.account.address())
.sequence_number(sequencer_number)
.max_gas_amount(10000)
.gas_unit_price(100)
.build();

// Sign transaction.
let signed_txn = self.account.sign_transaction(txn);

// Submit transaction.
let tx = self.client
let tx = self
.client
.submit_and_wait(&signed_txn)
.await.expect("Failed to submit update state transaction")
.await
.expect("Failed to submit update state transaction")
.into_inner();

Ok(tx.transaction_info().unwrap().hash.to_string())
Expand Down Expand Up @@ -132,17 +128,20 @@ async fn prepare_blob(

#[cfg(test)]
mod test {
use super::*;
use crate::config::AptosDaConfig;
use aptos_sdk::move_types::u256;
use da_client_interface::DaConfig;
use crate::config::AptosDaConfig;
use super::*;

#[tokio::test]
async fn test_submit_blob() {
let da_config = AptosDaConfig::new_from_env();
let da_client = AptosDaConfig::build_client(&da_config).await;

let data = vec![hex::decode(include_str!("../test_utils/hex_block_630872.txt")).unwrap()];
da_client.publish_state_diff(data, &u256::U256::from(0u128).to_le_bytes()).await.expect("Failed to submit blob!");
da_client
.publish_state_diff(data, &u256::U256::from(0u128).to_le_bytes())
.await
.expect("Failed to submit blob!");
}
}
}
1 change: 1 addition & 0 deletions crates/da-clients/aptos/test_utils/hex_block_630872.txt

Large diffs are not rendered by default.

Loading

0 comments on commit 4b91434

Please sign in to comment.