Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Add get_block_info to RPC StateReader #918

Merged
merged 6 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 81 additions & 79 deletions rpc_state_reader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,33 @@ impl RpcState {
RpcChain::TestNet2 => "alpha4-2".to_string(),
}
}

pub fn get_block_info(
&self,
starknet_os_config: starknet_in_rust::definitions::block_context::StarknetOsConfig,
) -> starknet_in_rust::state::BlockInfo {
let get_block_info_params = ureq::json!({
"jsonrpc": "2.0",
"method": "starknet_getBlockWithTxHashes",
"params": [self.block.to_value()],
"id": 1
});

let block_info: serde_json::Value = self.rpc_call(&get_block_info_params).unwrap();

starknet_in_rust::state::BlockInfo {
block_number: block_info["result"]["block_number"]
.to_string()
.parse::<u64>()
.unwrap(),
block_timestamp: block_info["result"]["timestamp"]
.to_string()
.parse::<u64>()
.unwrap(),
gas_price: *starknet_os_config.gas_price() as u64,
sequencer_address: starknet_os_config.fee_token_address().clone(),
}
}
}

impl StateReader for RpcState {
Expand Down Expand Up @@ -495,6 +522,52 @@ mod tests {
rpc_state.get_transaction(tx_hash);
}

#[test]
fn test_get_block_info() {
let rpc_state = RpcState::new(
RpcChain::MainNet,
BlockValue::Tag(serde_json::to_value("latest").unwrap()),
);

let gas_price_str = "13563643256";
let gas_price_u128 = gas_price_str.parse::<u128>().unwrap();

let fee_token_address = Address(felt_str!(
"049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
16
));

let get_block_info_params = ureq::json!({
"jsonrpc": "2.0",
"method": "starknet_getBlockWithTxHashes",
"params": [rpc_state.block.to_value()],
"id": 1
});
let network: StarknetChainId = rpc_state.chain.into();
let starknet_os_config =
starknet_in_rust::definitions::block_context::StarknetOsConfig::new(
network.to_felt(),
fee_token_address.clone(),
gas_price_u128,
);
let block_info: serde_json::Value = rpc_state.rpc_call(&get_block_info_params).unwrap();

let block_info = starknet_in_rust::state::BlockInfo {
block_number: block_info["result"]["block_number"]
.to_string()
.parse::<u64>()
.unwrap(),
block_timestamp: block_info["result"]["timestamp"]
.to_string()
.parse::<u64>()
.unwrap(),
gas_price: gas_price_u128 as u64,
sequencer_address: fee_token_address,
};

assert_eq!(rpc_state.get_block_info(starknet_os_config,), block_info);
}

/// Tested with the following query to the Feeder Gateway API:
/// https://alpha4-2.starknet.io/feeder_gateway/get_transaction_trace?transactionHash=0x019feb888a2d53ffddb7a1750264640afab8e9c23119e648b5259f1b5e7d51bc
#[test]
Expand Down Expand Up @@ -657,7 +730,7 @@ mod transaction_tests {
},
},
felt::felt_str,
state::{cached_state::CachedState, BlockInfo},
state::cached_state::CachedState,
};
use std::sync::Arc;

Expand All @@ -679,41 +752,20 @@ mod transaction_tests {
));
let mut state = CachedState::new(rpc_state.clone(), None, None);

// Retrieve the block context
let get_block_info_params = ureq::json!({
"jsonrpc": "2.0",
"method": "starknet_getBlockWithTxHashes",
"params": [rpc_state.block.to_value()],
"id": 1
});
let block_info: serde_json::Value = rpc_state.rpc_call(&get_block_info_params).unwrap();

// BlockContext with mainnet data.
// TODO look how to get this value from RPC call.
let gas_price_str = "13563643256";
let gas_price_u128 = gas_price_str.parse::<u128>().unwrap();
let gas_price_u64 = gas_price_str.parse::<u64>().unwrap();

let fee_token_address = Address(felt_str!(
"049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
16
));
let network: StarknetChainId = rpc_state.chain.into();
let starknet_os_config =
StarknetOsConfig::new(network.to_felt(), fee_token_address.clone(), gas_price_u128);
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price_u128);

let block_info = BlockInfo {
block_number: block_info["result"]["block_number"]
.to_string()
.parse::<u64>()
.unwrap(),
block_timestamp: block_info["result"]["timestamp"]
.to_string()
.parse::<u64>()
.unwrap(),
gas_price: gas_price_u64,
sequencer_address: fee_token_address,
};
let block_info = rpc_state.get_block_info(starknet_os_config.clone());

let block_context = BlockContext::new(
starknet_os_config,
Expand Down Expand Up @@ -754,41 +806,20 @@ mod transaction_tests {
));
let mut state = CachedState::new(rpc_state.clone(), None, None);

// Retrieve the block context
let get_block_info_params = ureq::json!({
"jsonrpc": "2.0",
"method": "starknet_getBlockWithTxHashes",
"params": [rpc_state.block.to_value()],
"id": 1
});
let block_info: serde_json::Value = rpc_state.rpc_call(&get_block_info_params).unwrap();

// BlockContext with mainnet data.
// TODO look how to get this value from RPC call.
let gas_price_str = "13572248835"; // from block 90_002
let gas_price_u128 = gas_price_str.parse::<u128>().unwrap();
let gas_price_u64 = gas_price_str.parse::<u64>().unwrap();

let fee_token_address = Address(felt_str!(
"049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
16
));
let network: StarknetChainId = rpc_state.chain.into();
let starknet_os_config =
StarknetOsConfig::new(network.to_felt(), fee_token_address.clone(), gas_price_u128);
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price_u128);

let block_info = BlockInfo {
block_number: block_info["result"]["block_number"]
.to_string()
.parse::<u64>()
.unwrap(),
block_timestamp: block_info["result"]["timestamp"]
.to_string()
.parse::<u64>()
.unwrap(),
gas_price: gas_price_u64,
sequencer_address: fee_token_address,
};
let block_info = rpc_state.get_block_info(starknet_os_config.clone());

let block_context = BlockContext::new(
starknet_os_config,
Expand Down Expand Up @@ -834,7 +865,6 @@ mod transaction_tests {
// TODO look how to get this value from RPC call.
let gas_price_str = "2917470325"; // from block 838683
let gas_price_u128 = gas_price_str.parse::<u128>().unwrap();
let gas_price_u64 = gas_price_str.parse::<u64>().unwrap();

let fee_token_address = Address(felt_str!(
"049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
Expand All @@ -844,15 +874,7 @@ mod transaction_tests {
let starknet_os_config =
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price_u128);

let block_info = BlockInfo {
block_number: 838684,
block_timestamp: 10,
gas_price: gas_price_u64,
sequencer_address: Address(felt_str!(
"01176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8",
16
)),
};
let block_info = rpc_state.get_block_info(starknet_os_config.clone());

let block_context = BlockContext::new(
starknet_os_config,
Expand Down Expand Up @@ -892,44 +914,24 @@ mod transaction_tests {
BlockValue::Number(serde_json::to_value(123001).unwrap()),
));

let get_block_info_params = ureq::json!({
"jsonrpc": "2.0",
"method": "starknet_getBlockWithTxHashes",
"params": [rpc_state.block.to_value()],
"id": 1
});
let block_info: serde_json::Value = rpc_state.rpc_call(&get_block_info_params).unwrap();

// BlockContext with mainnet data.
// TODO look how to get this value from RPC call.
let gas_price_str = "272679647"; // from block 123001
let gas_price_u128 = gas_price_str.parse::<u128>().unwrap();
let gas_price_u64 = gas_price_str.parse::<u64>().unwrap();

let fee_token_address = Address(felt_str!(
"49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
16
));

let block_info = BlockInfo {
block_number: block_info["result"]["block_number"]
.to_string()
.parse::<u64>()
.unwrap(),
block_timestamp: block_info["result"]["timestamp"]
.to_string()
.parse::<u64>()
.unwrap(),
gas_price: gas_price_u64,
sequencer_address: fee_token_address.clone(),
};

let mut state = CachedState::new(rpc_state.clone(), None, None);

let network: StarknetChainId = rpc_state.chain.into();
let starknet_os_config =
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price_u128);

let block_info = rpc_state.get_block_info(starknet_os_config.clone());

let block_context = BlockContext::new(
starknet_os_config,
DEFAULT_CONTRACT_STORAGE_COMMITMENT_TREE_HEIGHT,
Expand Down
1 change: 1 addition & 0 deletions src/definitions/block_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub struct StarknetOsConfig {
#[get = "pub"]
pub(crate) fee_token_address: Address,
/// Price of gas
#[get = "pub"]
pub(crate) gas_price: u128,
}

Expand Down