Skip to content

Commit

Permalink
Merge pull request #101 from weaveVM/dev
Browse files Browse the repository at this point in the history
merge: dev to main
  • Loading branch information
allnil authored Oct 28, 2024
2 parents 41304f6 + 0374165 commit f077b8a
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 58 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/primitives-traits/src/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub const ETHEREUM_BLOCK_GAS_LIMIT: LazyCell<u64> = LazyCell::new(|| 500_000_000
pub static MIN_PROTOCOL_BASE_FEE: LazyLock<AtomicU64> =
LazyLock::new(|| AtomicU64::new(500_000u64));

pub(crate) static WVM_FEE_MANAGER: LazyLock<Arc<WvmFeeManager>> = LazyLock::new(|| {
pub static WVM_FEE_MANAGER: LazyLock<Arc<WvmFeeManager>> = LazyLock::new(|| {
let fee = WvmFee::new(Some(Box::new(move |price| {
let original_price = price as f64 / 1_000_000_000f64;
let lowest_possible_gas_price_in_gwei =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use alloy_primitives::Bytes;
use arweave_upload::ArweaveRequest;
use eyre::eyre;
use rbrotli::to_brotli;
use reth::primitives::revm_primitives::{
Precompile, PrecompileError, PrecompileOutput, PrecompileResult,
};
use reth_revm::precompile::PrecompileErrors;
use std::str::FromStr;

pub const PC_ADDRESS: u64 = 0x17;
pub const ARWEAVE_PC_BASE: u64 = 3_450;
Expand Down Expand Up @@ -37,19 +37,26 @@ fn arweave_upload(input: &Bytes, gas_limit: u64) -> PrecompileResult {
)));
}

let res = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(
async {
ArweaveRequest::new()
.set_private_key(SOLANA_SILLY_PRIVATE_KEY.to_string())
.set_tag("Content-Type", "application/octet-stream")
.set_tag("WeaveVM:Precompile", "true")
.set_tag("WeaveVM:Encoding", "Brotli")
.set_tag("WeaveVM:Precompile-Address", PC_ADDRESS.to_string().as_str())
.set_data(input)
.send()
.await
},
);
let runtime = match tokio::runtime::Builder::new_current_thread().enable_all().build() {
Ok(r) => r,
Err(e) => {
return Err(PrecompileErrors::Error(PrecompileError::Other(
eyre!("Failed to build runtime to call arweave: {}", e).to_string(),
)));
}
};

let res = runtime.block_on(async {
ArweaveRequest::new()
.set_private_key(SOLANA_SILLY_PRIVATE_KEY.to_string())
.set_tag("Content-Type", "application/octet-stream")
.set_tag("WeaveVM:Precompile", "true")
.set_tag("WeaveVM:Encoding", "Brotli")
.set_tag("WeaveVM:Precompile-Address", PC_ADDRESS.to_string().as_str())
.set_data(input)
.send()
.await
});

let byte_resp = if let Ok(tx_id) = res { tx_id.into_bytes() } else { vec![] };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn arweave_read(input: &Bytes, gas_limit: u64) -> PrecompileResult {
)));
}

let id_str = unsafe { String::from_utf8(input.0.to_vec()) };
let id_str = String::from_utf8(input.0.to_vec());

let res = match id_str {
Ok(id) => {
Expand Down
22 changes: 14 additions & 8 deletions wvm-apps/wvm-exexed/crates/precompiles/src/inner/gbq_precompile.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::inner::{
arweave_read_precompile::ARWEAVE_PC_READ_BASE,
string_block::{from_sealed_block_senders, from_sealed_block_senders_value, Block},
string_block::from_sealed_block_senders_value,
wvm_block_precompile::{process_block_to_field, process_pc_response_from_str_bytes},
};
use alloy_primitives::Bytes;
use revm_primitives::{Precompile, PrecompileError, PrecompileErrors, PrecompileResult};
use serde_json::Value;
use wvm_static::WVM_BIGQUERY;
use wvm_static::PRECOMPILE_WVM_BIGQUERY_CLIENT;

pub const GBQ_READ_PC: Precompile = Precompile::Standard(gbq_read);

Expand All @@ -24,7 +24,7 @@ fn gbq_read(input: &Bytes, gas_limit: u64) -> PrecompileResult {
)));
}

let id_str = unsafe { String::from_utf8(input.0.to_vec()) };
let id_str = String::from_utf8(input.0.to_vec());

let res = match id_str {
Ok(val) => {
Expand All @@ -37,7 +37,7 @@ fn gbq_read(input: &Bytes, gas_limit: u64) -> PrecompileResult {
};

// It needs to be obtained OUTSIDE the thread
let wvm_bgc = (&*WVM_BIGQUERY).clone();
let wvm_bgc = (&*PRECOMPILE_WVM_BIGQUERY_CLIENT).clone();

let res_from_bgc = tokio::runtime::Builder::new_current_thread()
.enable_all()
Expand Down Expand Up @@ -79,10 +79,16 @@ mod tests {

#[test]
pub fn test_gbq_pc() {
std::env::set_var(
"CONFIG",
std::env::current_dir().unwrap().join("./../../../bq-config.json").to_str().unwrap(),
);
unsafe {
std::env::set_var(
"CONFIG",
std::env::current_dir()
.unwrap()
.join("./../../../bq-config.json")
.to_str()
.unwrap(),
);
}
let input = Bytes::from("253;hash".as_bytes());
let result = gbq_read(&input, 100_000);
assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ fn kyve_read(input: &Bytes, gas_limit: u64) -> PrecompileResult {
return Err(PrecompileErrors::Error(PrecompileError::OutOfGas));
}

let input_str = unsafe { String::from_utf8(input.0.to_vec()) }.unwrap();
let input_str = match String::from_utf8(input.0.to_vec()) {
Ok(s) => s,
Err(_) => {
return Err(PrecompileErrors::Error(PrecompileError::Other(
"Invalid input".to_string(),
)));
}
};

let (block_number, field) = {
let mut parts = input_str.split(";");
let block_number = parts.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn wvm_read_block_pc(input: &Bytes, gas_limit: u64) -> PrecompileResult {
)));
}

let block_id = unsafe { String::from_utf8(input.0.to_vec()) };
let block_id = String::from_utf8(input.0.to_vec());

match block_id {
Ok(input_data) => {
Expand Down
4 changes: 2 additions & 2 deletions wvm-apps/wvm-exexed/crates/reth-exexed/src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub const WVM_DEVNET_VERSION: &str = "v0.2.0";
pub const WVM_ALPHANET_VERSION: &str = "v0.1.1";
pub const WVM_DEVNET_VERSION: &str = "v0.3.0";
pub const WVM_ALPHANET_VERSION: &str = "v0.2.0";
49 changes: 36 additions & 13 deletions wvm-apps/wvm-exexed/crates/reth-exexed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ mod util;

use crate::{network_tag::get_network_tag, util::check_block_existence};
use arweave_upload::{ArweaveRequest, UploaderProvider};
use exex_wvm_bigquery::{init_bigquery_db, repository::StateRepository, BigQueryConfig};
use exex_wvm_bigquery::repository::StateRepository;
use exex_wvm_da::{DefaultWvmDataSettler, WvmDataSettler};
use futures::{Stream, StreamExt};
use futures::StreamExt;
use lambda::lambda::exex_lambda_processor;
use precompiles::node::WvmEthExecutorBuilder;
use reth::{api::FullNodeComponents, args::PruningArgs, builder::NodeBuilder};
Expand All @@ -20,7 +20,6 @@ use reth_primitives::constants::SLOT_DURATION;
use std::sync::Arc;
use tracing::{error, info};
use wvm_borsh::block::BorshSealedBlockWithSenders;
use wvm_static::WVM_BIGQUERY;

async fn exex_etl_processor<Node: FullNodeComponents>(
mut ctx: ExExContext<Node>,
Expand Down Expand Up @@ -64,7 +63,7 @@ async fn exex_etl_processor<Node: FullNodeComponents>(
target: "wvm::exex",
%err,
"Failed to send FinishedHeight event for block {}",
committed_chain.tip().number
committed_chain.tip().block.header.header().number
);
continue;
}
Expand All @@ -77,7 +76,7 @@ async fn exex_etl_processor<Node: FullNodeComponents>(
let brotli_borsh = match data_settler.process_block(&borsh_sealed_block) {
Ok(data) => data,
Err(err) => {
error!(target: "wvm::exex", %err, "Failed to do brotli encoding for block {}", sealed_block_with_senders.number);
error!(target: "wvm::exex", %err, "Failed to do brotli encoding for block {}", sealed_block_with_senders.block.header.header().number);
continue;
}
};
Expand All @@ -90,7 +89,10 @@ async fn exex_etl_processor<Node: FullNodeComponents>(
let res = ArweaveRequest::new()
.set_tag("Content-Type", "application/octet-stream")
.set_tag("WeaveVM:Encoding", "Borsh-Brotli")
.set_tag("Block-Number", sealed_block_with_senders.number.to_string().as_str())
.set_tag(
"Block-Number",
sealed_block_with_senders.block.header.header().number.to_string().as_str(),
)
.set_tag("Block-Hash", block_hash)
.set_tag("Client-Version", reth_primitives::constants::RETH_CLIENT_VERSION)
.set_tag("Network", get_network_tag().as_str())
Expand All @@ -102,22 +104,22 @@ async fn exex_etl_processor<Node: FullNodeComponents>(
let arweave_id = match res {
Ok(arweave_id) => arweave_id,
Err(err) => {
error!(target: "wvm::exex", %err, "Failed to construct arweave_id for block {}", sealed_block_with_senders.number);
error!(target: "wvm::exex", %err, "Failed to construct arweave_id for block {}", sealed_block_with_senders.block.header.header().number);
continue;
}
};

info!(target: "wvm::exex", "irys id: {}, for block: {}", arweave_id, sealed_block_with_senders.number);
info!(target: "wvm::exex", "irys id: {}, for block: {}", arweave_id, sealed_block_with_senders.block.header.header().number);

if let Err(err) = exex_wvm_bigquery::save_block(
&state_repository,
&sealed_block_with_senders,
committed_chain.tip().block.number,
committed_chain.tip().block.header.header().number,
arweave_id.clone(),
)
.await
{
error!(target: "wvm::exex", %err, "Failed to write to bigquery, block {}", sealed_block_with_senders.number);
error!(target: "wvm::exex", %err, "Failed to write to bigquery, block {}", sealed_block_with_senders.block.header.header().number);
continue;
};
}
Expand All @@ -129,6 +131,7 @@ async fn exex_etl_processor<Node: FullNodeComponents>(

/// Main loop of the exexed WVM node
fn main() -> eyre::Result<()> {
let _init_fee_manager = &*reth_primitives::constants::WVM_FEE_MANAGER;
reth::cli::Cli::parse_args().run(|builder, _| async move {
// Original config
let mut config = builder.config().clone();
Expand All @@ -151,9 +154,9 @@ fn main() -> eyre::Result<()> {

let run_exex = (std::env::var("RUN_EXEX").unwrap_or(String::from("false"))).to_lowercase();
if run_exex == "true" {
let big_query_client = (&*WVM_BIGQUERY).clone();
// init state repository
let state_repo = StateRepository::new(big_query_client);
let big_query_client = new_etl_exex_biguery_client().await;
let state_repo = StateRepository::new(Arc::new(big_query_client));

// init state processor
let state_processor = exex_etl::state_processor::StateProcessor::new();
// init irys provider
Expand Down Expand Up @@ -184,6 +187,26 @@ fn parse_prune_config(prune_conf: &str) -> u64 {
SLOT_DURATION.as_secs() * secs
}

use exex_wvm_bigquery::{BigQueryClient, BigQueryConfig};
async fn new_etl_exex_biguery_client() -> BigQueryClient {
let config_path: String =
std::env::var("CONFIG").unwrap_or_else(|_| "./bq-config.json".to_string());

info!(target: "wvm::exex","etl exex big_query config applied from: {}", config_path);

let config_file = std::fs::File::open(config_path).expect("bigquery config path exists");
let reader = std::io::BufReader::new(config_file);

let bq_config: BigQueryConfig =
serde_json::from_reader(reader).expect("bigquery config read from file");

let bgc = BigQueryClient::new(&bq_config).await.unwrap();

info!(target: "wvm::exex", "etl exex bigquery client initialized");

bgc
}

#[cfg(test)]
mod tests {
use crate::parse_prune_config;
Expand Down
20 changes: 5 additions & 15 deletions wvm-apps/wvm-exexed/crates/static/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
use exex_wvm_bigquery::{init_bigquery_db, BigQueryClient, BigQueryConfig};
use once_cell::sync::Lazy;
use std::{
num::NonZeroUsize,
sync::{Arc, LazyLock},
};
use exex_wvm_bigquery::{BigQueryClient, BigQueryConfig};
use std::sync::{Arc, LazyLock};
use tracing::info;

pub static WVM_BIGQUERY: LazyLock<Arc<BigQueryClient>> = LazyLock::new(|| {
pub static PRECOMPILE_WVM_BIGQUERY_CLIENT: LazyLock<Arc<BigQueryClient>> = LazyLock::new(|| {
tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(
async move {
let config_path: String =
std::env::var("CONFIG").unwrap_or_else(|_| "./bq-config.json".to_string());

info!(target: "wvm::exex","launch config applied from: {}", config_path);

println!("{}", config_path);
info!(target: "wvm::precompile","precompile big_query config applied from: {}", config_path);

let config_file =
std::fs::File::open(config_path).expect("bigquery config path exists");
Expand All @@ -24,12 +18,8 @@ pub static WVM_BIGQUERY: LazyLock<Arc<BigQueryClient>> = LazyLock::new(|| {
serde_json::from_reader(reader).expect("bigquery config read from file");

let bgc = BigQueryClient::new(&bq_config).await.unwrap();
//
// // init bigquery client
// let bigquery_client =
// init_bigquery_db(&bq_config).await.expect("bigquery client initialized");

info!(target: "wvm::exex", "bigquery client initialized");
info!(target: "wvm::precompile", "bigquery client initialized");

Arc::new(bgc)
},
Expand Down

0 comments on commit f077b8a

Please sign in to comment.