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

Commit 0216cd6

Browse files
edg-ljuanbonoxqft
authored andcommitted
Deserialize transactions too on the block info for the Rpc Reader SN (#961)
* initial commit * add get_class_hash_at * Added get_nonce_at * Added get_storage_at * Remove comments * Added get block info * Fixed get_contract_class() * WIP fixing desearlization * WIP Fix * Finished fixing get_contract_class() * Uncommented tests, new get_contract_class * Remove file * WIP Fixing tests * Finish fixing simple tests * Fixed transaction trace and block info * Fix import * Refactor, fixes, added test * Fixed warnings, removed tests * deserialize all transactions in the block info too * docs * refactor into standalone fn * get gas from block via RPC SN (#963) * get gas automatically from block * cleanup * fix wrong gas * unintended change --------- Co-authored-by: juanbono <juanbono94@gmail.com> Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy>
1 parent 54a10d3 commit 0216cd6

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ serde_json = { version = "1.0", features = [
4444
] }
4545
sha3 = "0.10.1"
4646
# TODO: Replace with sha3. We should look how to integrate it correctly to calculate sn_keccak
47-
keccak = "0.1.3"
47+
keccak = "0.1.3"
4848
starknet_api = { workspace = true }
4949
starknet-crypto = "0.5.1"
5050
thiserror = { workspace = true }

rpc_state_reader_sn_api/src/lib.rs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ pub struct RpcBlockInfo {
128128
pub block_timestamp: BlockTimestamp,
129129
/// The sequencer address of this block.
130130
pub sequencer_address: ContractAddress,
131+
/// The transactions of this block.
132+
pub transactions: Vec<Transaction>,
131133
}
132134

133135
#[derive(Deserialize)]
@@ -299,6 +301,29 @@ impl<'de> Deserialize<'de> for TransactionTrace {
299301
}
300302
}
301303

304+
/// Freestanding deserialize method to avoid a new type.
305+
fn deserialize_transaction_json(transaction: serde_json::Value) -> serde_json::Result<Transaction> {
306+
let tx_type: String = serde_json::from_value(transaction["type"].clone())?;
307+
let tx_version: String = serde_json::from_value(transaction["version"].clone())?;
308+
309+
match tx_type.as_str() {
310+
"INVOKE" => match tx_version.as_str() {
311+
"0x0" => Ok(Transaction::Invoke(InvokeTransaction::V0(
312+
serde_json::from_value(transaction)?,
313+
))),
314+
"0x1" => Ok(Transaction::Invoke(InvokeTransaction::V1(
315+
serde_json::from_value(transaction)?,
316+
))),
317+
x => Err(serde::de::Error::custom(format!(
318+
"unimplemented invoke version: {x}"
319+
))),
320+
},
321+
x => Err(serde::de::Error::custom(format!(
322+
"unimplemented transaction type deserialization: {x}"
323+
))),
324+
}
325+
}
326+
302327
impl RpcState {
303328
/// Requests the transaction trace to the Feeder Gateway API.
304329
/// It's useful for testing the transaction outputs like:
@@ -343,6 +368,25 @@ impl RpcState {
343368
}
344369
}
345370

371+
/// Gets the gas price of a given block.
372+
pub fn get_gas_price(&self, block_number: u64) -> serde_json::Result<u128> {
373+
let chain_name = self.get_chain_name();
374+
375+
let response = ureq::get(&format!(
376+
"https://{}.starknet.io/feeder_gateway/get_block",
377+
chain_name
378+
))
379+
.query("blockNumber", &block_number.to_string())
380+
.call()
381+
.unwrap();
382+
383+
let res: serde_json::Value = response.into_json().expect("should be json");
384+
385+
let gas_price_hex = res["gas_price"].as_str().unwrap();
386+
let gas_price = u128::from_str_radix(gas_price_hex.trim_start_matches("0x"), 16).unwrap();
387+
Ok(gas_price)
388+
}
389+
346390
pub fn get_chain_name(&self) -> ChainId {
347391
ChainId(match self.chain {
348392
RpcChain::MainNet => "alpha-mainnet".to_string(),
@@ -354,7 +398,7 @@ impl RpcState {
354398
pub fn get_block_info(&self) -> RpcBlockInfo {
355399
let get_block_info_params = ureq::json!({
356400
"jsonrpc": "2.0",
357-
"method": "starknet_getBlockWithTxHashes",
401+
"method": "starknet_getBlockWithTxs",
358402
"params": [self.block.to_value().unwrap()],
359403
"id": 1
360404
});
@@ -363,6 +407,13 @@ impl RpcState {
363407
let sequencer_address: StarkFelt =
364408
serde_json::from_value(block_info["result"]["sequencer_address"].clone()).unwrap();
365409

410+
let transactions: Vec<_> = block_info["result"]["transactions"]
411+
.as_array()
412+
.unwrap()
413+
.iter()
414+
.filter_map(|result| deserialize_transaction_json(result.clone()).ok())
415+
.collect();
416+
366417
RpcBlockInfo {
367418
block_number: BlockNumber(
368419
block_info["result"]["block_number"]
@@ -377,6 +428,7 @@ impl RpcState {
377428
.unwrap(),
378429
),
379430
sequencer_address: ContractAddress(sequencer_address.try_into().unwrap()),
431+
transactions,
380432
}
381433
}
382434

@@ -851,7 +903,6 @@ mod blockifier_transaction_tests {
851903
tx_hash: &str,
852904
network: RpcChain,
853905
block_number: BlockNumber,
854-
gas_price: u128,
855906
) -> (
856907
TransactionExecutionInfo,
857908
TransactionTrace,
@@ -861,13 +912,15 @@ mod blockifier_transaction_tests {
861912

862913
// Instantiate the RPC StateReader and the CachedState
863914
let rpc_reader = RpcStateReader(RpcState::new(network, block_number.into()));
915+
let gas_price = rpc_reader.0.get_gas_price(block_number.0).unwrap();
864916

865917
// Get values for block context before giving ownership of the reader
866918
let chain_id = rpc_reader.0.get_chain_name();
867919
let RpcBlockInfo {
868920
block_number,
869921
block_timestamp,
870922
sequencer_address,
923+
..
871924
} = rpc_reader.0.get_block_info();
872925

873926
// Get transaction before giving ownership of the reader
@@ -939,13 +992,20 @@ mod blockifier_transaction_tests {
939992
use super::*;
940993

941994
#[test]
942-
#[ignore = "working on fixes"]
995+
fn test_get_gas_price() {
996+
let block = BlockValue::Number(BlockNumber(169928));
997+
let rpc_state = RpcState::new(RpcChain::MainNet, block);
998+
999+
let price = rpc_state.get_gas_price(169928).unwrap();
1000+
assert_eq!(price, 22804578690);
1001+
}
1002+
1003+
#[test]
9431004
fn test_recent_tx() {
9441005
let (tx_info, trace, receipt) = execute_tx(
9451006
"0x05d200ef175ba15d676a68b36f7a7b72c17c17604eda4c1efc2ed5e4973e2c91",
9461007
RpcChain::MainNet,
9471008
BlockNumber(169928),
948-
17110275391107,
9491009
);
9501010

9511011
let TransactionExecutionInfo {

0 commit comments

Comments
 (0)