Skip to content

Commit

Permalink
fix tests in provider crate
Browse files Browse the repository at this point in the history
  • Loading branch information
error2215 committed Jul 2, 2024
1 parent d564f92 commit c396d5a
Show file tree
Hide file tree
Showing 17 changed files with 287 additions and 315 deletions.
8 changes: 4 additions & 4 deletions crates/consensus/src/receipt/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl<T> AnyReceiptEnvelope<T> {
}

/// Returns the cumulative gas used at this receipt.
pub const fn cumulative_gas_used(&self) -> u128 {
self.inner.receipt.cumulative_gas_used
pub const fn cumulative_energy_used(&self) -> u128 {
self.inner.receipt.cumulative_energy_used
}

/// Return the receipt logs.
Expand All @@ -83,8 +83,8 @@ impl<T> TxReceipt<T> for AnyReceiptEnvelope<T> {
}

/// Returns the cumulative gas used at this receipt.
fn cumulative_gas_used(&self) -> u128 {
self.inner.receipt.cumulative_gas_used
fn cumulative_energy_used(&self) -> u128 {
self.inner.receipt.cumulative_energy_used
}

/// Return the receipt logs.
Expand Down
8 changes: 4 additions & 4 deletions crates/consensus/src/receipt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait TxReceipt<T = Log> {
}

/// Returns the cumulative gas used in the block after this transaction was executed.
fn cumulative_gas_used(&self) -> u128;
fn cumulative_energy_used(&self) -> u128;

/// Returns the logs emitted by this transaction.
fn logs(&self) -> &[T];
Expand All @@ -43,7 +43,7 @@ mod tests {
let receipt =
ReceiptWithBloom {
receipt: Receipt {
cumulative_gas_used: 0x1u128,
cumulative_energy_used: 0x1u128,
logs: vec![Log {
address: cAddress!("00000000000000000000000000000000000000000011"),
data: LogData::new_unchecked(
Expand Down Expand Up @@ -73,7 +73,7 @@ mod tests {
let expected =
ReceiptWithBloom {
receipt: Receipt {
cumulative_gas_used: 0x1u128,
cumulative_energy_used: 0x1u128,
logs: vec![Log {
address: cAddress!("00000000000000000000000000000000000000000011"),
data: LogData::new_unchecked(
Expand All @@ -96,7 +96,7 @@ mod tests {
#[test]
fn gigantic_receipt() {
let receipt = Receipt {
cumulative_gas_used: 16747627,
cumulative_energy_used: 16747627,
status: true,
logs: vec![
Log {
Expand Down
19 changes: 10 additions & 9 deletions crates/consensus/src/receipt/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Receipt<T = Log> {
pub status: bool,
/// Gas used
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))]
pub cumulative_gas_used: u128,
pub cumulative_energy_used: u128,
/// Log send from contracts.
pub logs: Vec<T>,
}
Expand Down Expand Up @@ -57,8 +57,8 @@ where
self.bloom_slow()
}

fn cumulative_gas_used(&self) -> u128 {
self.cumulative_gas_used
fn cumulative_energy_used(&self) -> u128 {
self.cumulative_energy_used
}

fn logs(&self) -> &[T] {
Expand Down Expand Up @@ -104,8 +104,8 @@ impl<T> TxReceipt<T> for ReceiptWithBloom<T> {
Some(self.logs_bloom)
}

fn cumulative_gas_used(&self) -> u128 {
self.receipt.cumulative_gas_used
fn cumulative_energy_used(&self) -> u128 {
self.receipt.cumulative_energy_used
}

fn logs(&self) -> &[T] {
Expand Down Expand Up @@ -133,14 +133,14 @@ impl<T: Encodable> ReceiptWithBloom<T> {
fn encode_fields(&self, out: &mut dyn BufMut) {
self.receipt_rlp_header().encode(out);
self.receipt.status.encode(out);
self.receipt.cumulative_gas_used.encode(out);
self.receipt.cumulative_energy_used.encode(out);
self.logs_bloom.encode(out);
self.receipt.logs.encode(out);
}

fn payload_len(&self) -> usize {
self.receipt.status.length()
+ self.receipt.cumulative_gas_used.length()
+ self.receipt.cumulative_energy_used.length()
+ self.logs_bloom.length()
+ self.receipt.logs.length()
}
Expand Down Expand Up @@ -175,7 +175,8 @@ impl<T> ReceiptWithBloom<T> {
let bloom = Decodable::decode(b)?;
let logs = Decodable::decode(b)?;

let receipt = Receipt { status: success, cumulative_gas_used, logs };
let receipt =
Receipt { status: success, cumulative_energy_used: cumulative_gas_used, logs };

let this = Self { receipt, logs_bloom: bloom };
let consumed = started_len - b.len();
Expand All @@ -197,7 +198,7 @@ impl<T: Encodable> Encodable for ReceiptWithBloom<T> {

fn length(&self) -> usize {
let payload_length = self.receipt.status.length()
+ self.receipt.cumulative_gas_used.length()
+ self.receipt.cumulative_energy_used.length()
+ self.logs_bloom.length()
+ self.receipt.logs.length();
payload_length + length_of_length(payload_length)
Expand Down
14 changes: 7 additions & 7 deletions crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloy_json_abi::Function;
use alloy_network::{Ethereum, Network, ReceiptResponse, TransactionBuilder};
use alloy_primitives::{Address, Bytes, ChainId, IcanAddress, TxKind, U256};
use alloy_provider::{PendingTransactionBuilder, Provider};
use alloy_rpc_types::{state::StateOverride, AccessList, BlobTransactionSidecar, BlockId};
use alloy_rpc_types::{state::StateOverride, AccessList, BlockId};
use alloy_sol_types::SolCall;
use alloy_transport::Transport;
use std::{
Expand Down Expand Up @@ -313,11 +313,11 @@ impl<T: Transport + Clone, P: Provider<T, N>, D: CallDecoder, N: Network> CallBu
self
}

/// Sets the `sidecar` field in the transaction to the provided value.
pub fn sidecar(mut self, blob_sidecar: BlobTransactionSidecar) -> Self {
self.request.set_blob_sidecar(blob_sidecar);
self
}
// /// Sets the `sidecar` field in the transaction to the provided value.
// pub fn sidecar(mut self, blob_sidecar: BlobTransactionSidecar) -> Self {
// self.request.set_blob_sidecar(blob_sidecar);
// self
// }

/// Uses a Legacy transaction instead of an EIP-1559 one to execute the call
pub fn legacy(self) -> Self {
Expand Down Expand Up @@ -612,7 +612,7 @@ mod tests {
fn change_chain_id() {
let call_builder = build_call_builder().chain_id(1337);
assert_eq!(
call_builder.request.network_id.expect("chain_id should be set"),
call_builder.request.network_id,
1337,
"chain_id of request should be '1337'"
);
Expand Down
11 changes: 4 additions & 7 deletions crates/network/src/ethereum/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
// value and data may be None. If they are, they will be set to default.
// gas fields and nonce may be None, if they are, they will be populated
// with default values by the RPC server
self.from.is_some()
self.from.is_some() && self.network_id != 0
}

fn can_build(&self) -> bool {
// value and data may be none. If they are, they will be set to default
// values.

// chain_id and from may be none.
let common = self.energy.is_some() && self.nonce.is_some();
// from may be none.
let common = self.energy.is_some() && self.nonce.is_some() && self.network_id != 0;
let legacy = self.energy_price.is_some();
let eip2930 = legacy;

Expand All @@ -154,10 +154,7 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {

fn build_unsigned(self) -> BuildResult<TypedTransaction, Ethereum> {
if let Err(missing) = self.complete_legacy() {
return Err((
self,
TransactionBuilderError::InvalidTransactionRequest(missing),
));
return Err((self, TransactionBuilderError::InvalidTransactionRequest(missing)));
}
Ok(self.build_typed_tx().expect("checked by complete_legacy"))
}
Expand Down
2 changes: 2 additions & 0 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ alloy-transport-ws = { workspace = true, optional = true }
alloy-pubsub = { workspace = true, optional = true }
alloy-transport.workspace = true
alloy-primitives.workspace = true
alloy-consensus = { workspace = true, features = ["std"] }
alloy-signer.workspace = true

async-stream = "0.3"
async-trait.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/provider/src/ext/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ mod test {
.to(cAddress!("cb7175017a3fa2d4dc29489bfc01ec2e60b140e1c019"))
.value(U256::from(100));
tx.set_energy_price(energy_price);
tx.set_network_id(1);
// .max_fee_per_gas(energy_price + 1)
// .max_priority_fee_per_gas(energy_price + 1);
let pending = provider.send_transaction(tx).await.unwrap();
Expand Down
24 changes: 12 additions & 12 deletions crates/provider/src/ext/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub trait TxPoolApi<T, N = Ethereum>: Send + Sync {
/// Returns the content of the transaction pool filtered by a specific address.
///
/// See [here](https://gocore.ethereum.org/docs/rpc/ns-txpool#txpool_contentFrom) for more details
async fn txpool_content_from(&self, from: IcanAddress) -> TransportResult<TxpoolContentFrom>;
// async fn txpool_content_from(&self, from: IcanAddress) -> TransportResult<TxpoolContentFrom>;

/// Returns a textual summary of each transaction in the pool.
///
Expand Down Expand Up @@ -54,9 +54,9 @@ where
self.client().request("txpool_content", ()).await
}

async fn txpool_content_from(&self, from: IcanAddress) -> TransportResult<TxpoolContentFrom> {
self.client().request("txpool_contentFrom", (from,)).await
}
// async fn txpool_content_from(&self, from: IcanAddress) -> TransportResult<TxpoolContentFrom> {
// self.client().request("txpool_contentFrom", (from,)).await
// }

async fn txpool_inspect(&self) -> TransportResult<TxpoolInspect> {
self.client().request("txpool_inspect", ()).await
Expand All @@ -83,14 +83,14 @@ mod tests {
assert_eq!(content, TxpoolContent::default());
}

#[tokio::test]
async fn test_txpool_content_from() {
let temp_dir = tempfile::TempDir::with_prefix("gocore-test-").unwrap();
let gocore = Gocore::new().disable_discovery().data_dir(temp_dir.path()).spawn();
let provider = ProviderBuilder::new().on_http(gocore.endpoint_url());
let content = provider.txpool_content_from(IcanAddress::default()).await.unwrap();
assert_eq!(content, TxpoolContentFrom::default());
}
// #[tokio::test]
// async fn test_txpool_content_from() {
// let temp_dir = tempfile::TempDir::with_prefix("gocore-test-").unwrap();
// let gocore = Gocore::new().disable_discovery().data_dir(temp_dir.path()).spawn();
// let provider = ProviderBuilder::new().on_http(gocore.endpoint_url());
// let content = provider.txpool_content_from(IcanAddress::default()).await.unwrap();
// assert_eq!(content, TxpoolContentFrom::default());
// }

#[tokio::test]
async fn test_txpool_inspect() {
Expand Down
Loading

0 comments on commit c396d5a

Please sign in to comment.