Skip to content

Commit

Permalink
Merge pull request #129 from weaveVM/pc-fixes-mainnet
Browse files Browse the repository at this point in the history
fix: Precompile unwraps, streaming resp
  • Loading branch information
andreespirela authored Jan 13, 2025
2 parents fc2850c + 0c8a7e5 commit 5bb3c02
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ fn arweave_read(input: &Bytes, gas_limit: u64) -> PrecompileResult {
mod arweave_read_pc_tests {
use crate::inner::arweave_read_precompile::{arweave_read, parse_gateway_content};
use alloy_primitives::Bytes;
use borsh::BorshDeserialize;
use reth::primitives::revm_primitives::PrecompileOutput;
use std::time::Instant;
use borsh::BorshDeserialize;
use wvm_borsh::block::BorshSealedBlockWithSenders;

#[test]
Expand Down
12 changes: 7 additions & 5 deletions wvm-apps/wvm-exexed/crates/precompiles/src/inner/graphql_util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::inner::REQ_TIMEOUT;
use eyre::Error;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::sync::OnceLock;
use crate::inner::REQ_TIMEOUT;

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Response {
Expand Down Expand Up @@ -112,10 +112,12 @@ pub fn build_transaction_query(
}

pub fn send_graphql(gateway: &str, query: &str) -> Result<Response, Error> {
let res = ureq::post(format!("{}/{}", gateway, "graphql").as_str()).timeout((&*REQ_TIMEOUT).clone()).send_json(ureq::json!({
"variables": {},
"query": query
}));
let res = ureq::post(format!("{}/{}", gateway, "graphql").as_str())
.timeout((&*REQ_TIMEOUT).clone())
.send_json(ureq::json!({
"variables": {},
"query": query
}));

res.unwrap().into_json::<Response>().map_err(|e| Error::new(e))
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::inner::REQ_TIMEOUT;
use alloy_primitives::Bytes;
use revm_primitives::{
Precompile, PrecompileError, PrecompileErrors, PrecompileOutput, PrecompileResult,
};
use wvm_static::internal_block;
use crate::inner::REQ_TIMEOUT;

pub const KYVE_PC_BASE: u64 = 10_000;
pub const KYVE_API_URL: &str = "https://data.services.kyve.network";
Expand Down Expand Up @@ -52,8 +52,11 @@ fn kyve_read(input: &Bytes, gas_limit: u64) -> PrecompileResult {
}

let blk_number = block_number.unwrap();
let usize_blk_number = blk_number.to_string().parse::<usize>().map_err(|_| {
PrecompileErrors::Error(PrecompileError::Other("Invalid Block Number".to_string()))
})?;

if !(blk_number.to_string().parse::<usize>().unwrap() >= 19426589) {
if !(usize_blk_number >= 19426589) {
return Err(PrecompileErrors::Error(PrecompileError::Other(
"Can only read from block 19426589".to_string(),
)));
Expand All @@ -65,7 +68,7 @@ fn kyve_read(input: &Bytes, gas_limit: u64) -> PrecompileResult {
format!("{}/ethereum/beacon/blob_sidecars?block_height={}", KYVE_API_URL, blk_number)
.as_str(),
)
.timeout((&*REQ_TIMEOUT).clone())
.timeout((&*REQ_TIMEOUT).clone())
.call();

match req {
Expand Down
11 changes: 9 additions & 2 deletions wvm-apps/wvm-exexed/crates/precompiles/src/inner/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::cell::LazyCell;
use std::time::Duration;
use crate::inner::{
arweave_precompile::ARWEAVE_UPLOAD_PC, arweave_read_precompile::ARWEAVE_READ_PC,
gbq_precompile::GBQ_READ_PC, kyve_precompile::KYVE_READ_PC, test_precompile::HELLO_WORLD_PC,
wvm_block_precompile::WVM_BLOCK_PC,
};
use reth::revm::precompile::{u64_to_address, PrecompileWithAddress};
use std::cell::LazyCell;
use std::time::Duration;

pub mod arweave_precompile;
mod arweave_read_precompile;
Expand Down Expand Up @@ -34,6 +34,13 @@ pub const REQ_TIMEOUT: LazyCell<Duration> = LazyCell::new(|| {
Duration::from_millis(duration_seconds)
});

pub const REQ_SIZE: LazyCell<u64> = LazyCell::new(|| {
std::env::var("REQ_SIZE")
.ok()
.and_then(|val| val.parse::<u64>().ok())
.unwrap_or((1024 * 1024) * 4)
});

pub fn wvm_precompiles() -> impl Iterator<Item = PrecompileWithAddress> {
// ORDER OF THINGS MATTER
// ORDER OF THINGS MATTER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,8 @@ impl From<BorshSealedBlockWithSenders> for Block {
fn from(value: BorshSealedBlockWithSenders) -> Self {
let sealed_block = value.0;
match sealed_block {
WvmSealedBlockWithSenders::V1(data) => {
from_sealed_block_senders(data.into())
}
WvmSealedBlockWithSenders::V1(data) => from_sealed_block_senders(data.into()),
}

}
}

Expand Down
8 changes: 5 additions & 3 deletions wvm-apps/wvm-exexed/crates/precompiles/src/inner/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::inner::{REQ_SIZE, REQ_TIMEOUT};
use alloy_primitives::Bytes;
use revm_primitives::{PrecompileError, PrecompileErrors, PrecompileOutput};
use std::io::Read;
use wvm_static::internal_block;
use crate::inner::REQ_TIMEOUT;

pub const DEFAULT_ARWEAVE_TX_ENDPOINT: &str = "https://arweave.net/";

Expand All @@ -28,10 +28,12 @@ pub fn download_tx(
// ))),
// }
// }).unwrap()
let download_tx = ureq::get(format!("{}/{}", clean_gateway, tx_id.as_str()).as_str()).timeout((&*REQ_TIMEOUT).clone()).call();
let download_tx = ureq::get(format!("{}/{}", clean_gateway, tx_id.as_str()).as_str())
.timeout((&*REQ_TIMEOUT).clone())
.call();
match download_tx {
Ok(tx) => Ok(PrecompileOutput::new(gas_used, {
let mut reader = tx.into_reader();
let mut reader = tx.into_reader().take((&*REQ_SIZE).clone());
let mut buffer = vec![];
let _ = reader.read_to_end(&mut buffer).map_err(|_| {
PrecompileError::Other("Arweave Transaction was not found".to_string())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@ fn wvm_read_block_pc(input: &Bytes, gas_limit: u64) -> PrecompileResult {
"Borsh-Brotli" => {
let unbrotli = from_brotli(bytes);
let unborsh =
borsh::from_slice::<BorshSealedBlockWithSenders>(&unbrotli)
.unwrap();
borsh::from_slice::<BorshSealedBlockWithSenders>(&unbrotli);

let unborsh = unborsh.map_err(|_| {
PrecompileError::Other(
"Block could not be deserialized".to_string(),
)
})?;

let str_block = Block::from(unborsh);
let data = process_block_to_field(field, str_block);
Expand Down

0 comments on commit 5bb3c02

Please sign in to comment.