Skip to content

Commit

Permalink
Update starknet-rs with rpc v3 support
Browse files Browse the repository at this point in the history
commit-id:2444f382
  • Loading branch information
tarrencev committed May 11, 2023
1 parent 8899cfb commit 559242c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 81 deletions.
23 changes: 15 additions & 8 deletions katana-core/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Sequencer for KatanaSequencer {
from_block: BlockId,
to_block: BlockId,
address: Option<StarkFelt>,
keys: Option<Vec<StarkFelt>>,
keys: Option<Vec<Vec<StarkFelt>>>,
_continuation_token: Option<String>,
_chunk_size: u64,
) -> Result<Vec<EmittedEvent>, blockifier::state::errors::StateError> {
Expand Down Expand Up @@ -274,11 +274,18 @@ impl Sequencer for KatanaSequencer {
// Check the keys condition
match &keys {
Some(keys) => {
if let Some(event_key) = event.content.keys.first() {
keys.contains(&event_key.0)
} else {
false
}
// "Per key (by position), designate the possible values to be matched
// for events to be returned. Empty array designates 'any' value"
let keys_to_check =
std::cmp::min(keys.len(), event.content.keys.len());

event
.content
.keys
.iter()
.zip(keys.iter())
.take(keys_to_check)
.all(|(key, filter)| filter.contains(&key.0))
}
None => true,
}
Expand All @@ -301,7 +308,7 @@ impl Sequencer for KatanaSequencer {
&self,
block_id: BlockId,
) -> Result<StateUpdate, blockifier::state::errors::StateError> {
let block_number = self.block_number_from_block_id(block_id.clone()).ok_or(
let block_number = self.block_number_from_block_id(block_id).ok_or(
blockifier::state::errors::StateError::StateReadError(format!(
"block id {block_id:?} not found",
)),
Expand Down Expand Up @@ -365,7 +372,7 @@ pub trait Sequencer {
from_block: BlockId,
to_block: BlockId,
address: Option<StarkFelt>,
keys: Option<Vec<StarkFelt>>,
keys: Option<Vec<Vec<StarkFelt>>>,
continuation_token: Option<String>,
chunk_size: u64,
) -> Result<Vec<EmittedEvent>, blockifier::state::errors::StateError>;
Expand Down
20 changes: 11 additions & 9 deletions katana-core/src/starknet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use blockifier::{
};
use starknet::{
core::types::{FieldElement, TransactionStatus},
providers::jsonrpc::models::StateUpdate,
providers::jsonrpc::models::{PendingStateUpdate, StateUpdate},
};
use starknet_api::{
block::{BlockHash, BlockNumber, BlockTimestamp, GasPrice},
Expand Down Expand Up @@ -180,15 +180,17 @@ impl StarknetWrapper {
StateUpdate {
block_hash: block_hash.0.into(),
new_root: new_block.header().state_root.0.into(),
old_root: if new_block.block_number() == BlockNumber(0) {
FieldElement::ZERO
} else {
self.blocks
.latest()
.map(|last_block| last_block.header().state_root.0.into())
.unwrap()
pending_state_update: PendingStateUpdate {
old_root: if new_block.block_number() == BlockNumber(0) {
FieldElement::ZERO
} else {
self.blocks
.latest()
.map(|last_block| last_block.header().state_root.0.into())
.unwrap()
},
state_diff: convert_state_diff_to_rpc_state_diff(state_diff.clone()),
},
state_diff: convert_state_diff_to_rpc_state_diff(state_diff.clone()),
},
);

Expand Down
12 changes: 9 additions & 3 deletions katana-core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use blockifier::{
use starknet::{
core::types::{contract::legacy::LegacyContractClass, FieldElement},
providers::jsonrpc::models::{
ContractStorageDiffItem, DeployedContractItem, NonceUpdate, StateDiff, StorageEntry,
ContractStorageDiffItem, DeclaredClassItem, DeployedContractItem, NonceUpdate, StateDiff,
StorageEntry,
},
};
use starknet_api::{
Expand Down Expand Up @@ -193,11 +194,15 @@ pub fn convert_state_diff_to_rpc_state_diff(state_diff: CommitmentStateDiff) ->
.collect(),
})
.collect(),
deprecated_declared_classes: vec![],
// TODO: This will change with RPC spec v3.0.0. Also, are we supposed to return the class hash or the compiled class hash?
declared_contract_hashes: state_diff
declared_classes: state_diff
.class_hash_to_compiled_class_hash
.iter()
.map(|class_hash| class_hash.0 .0.into())
.map(|(class_hash, compiled_class_hash)| DeclaredClassItem {
class_hash: class_hash.0.into(),
compiled_class_hash: compiled_class_hash.0.into(),
})
.collect(),
deployed_contracts: state_diff
.address_to_class_hash
Expand All @@ -207,6 +212,7 @@ pub fn convert_state_diff_to_rpc_state_diff(state_diff: CommitmentStateDiff) ->
class_hash: class_hash.0.into(),
})
.collect(),
replaced_classes: vec![],
nonces: state_diff
.address_to_nonce
.iter()
Expand Down
16 changes: 9 additions & 7 deletions katana-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<S: Sequencer + Send + Sync + 'static> KatanaApiServer for KatanaRpc<S> {
.sequencer
.read()
.await
.block(block_id.clone())
.block(block_id)
.ok_or(Error::from(KatanaApiError::BlockNotFound))?;

block
Expand Down Expand Up @@ -161,7 +161,7 @@ impl<S: Sequencer + Send + Sync + 'static> KatanaApiServer for KatanaRpc<S> {
.sequencer
.read()
.await
.block(block_id.clone())
.block(block_id)
.ok_or(Error::from(KatanaApiError::BlockNotFound))?;

let sequencer_address = FieldElement::from_hex_be(SEQUENCER_ADDRESS).unwrap();
Expand Down Expand Up @@ -206,7 +206,7 @@ impl<S: Sequencer + Send + Sync + 'static> KatanaApiServer for KatanaRpc<S> {
.sequencer
.read()
.await
.block(block_id.clone())
.block(block_id)
.ok_or(Error::from(KatanaApiError::BlockNotFound))?;

let transaction = block
Expand All @@ -223,7 +223,7 @@ impl<S: Sequencer + Send + Sync + 'static> KatanaApiServer for KatanaRpc<S> {
.sequencer
.read()
.await
.block(block_id.clone())
.block(block_id)
.ok_or(Error::from(KatanaApiError::BlockNotFound))?;

let sequencer_address = FieldElement::from_hex_be(SEQUENCER_ADDRESS).unwrap();
Expand Down Expand Up @@ -316,9 +316,11 @@ impl<S: Sequencer + Send + Sync + 'static> KatanaApiServer for KatanaRpc<S> {
from_block,
to_block,
filter.address.map(|fe| field_element_to_starkfelt(&fe)),
filter
.keys
.map(|keys| keys.iter().map(field_element_to_starkfelt).collect()),
filter.keys.map(|keys| {
keys.iter()
.map(|key| key.iter().map(|key| (*key).into()).collect())
.collect()
}),
continuation_token,
chunk_size,
)
Expand Down
1 change: 0 additions & 1 deletion katana-rpc/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod contract;
pub mod state_diff;
pub mod transaction;
47 changes: 0 additions & 47 deletions katana-rpc/src/utils/state_diff.rs

This file was deleted.

14 changes: 8 additions & 6 deletions katana-rpc/src/utils/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,14 @@ fn convert_declare_to_rpc_tx(transaction: InnerDeclareTransaction) -> Result<Dec
})
}
InnerDeclareTransaction::V2(tx) => DeclareTransaction::V2(DeclareTransactionV2 {
nonce: tx.nonce.0.into(),
max_fee: FieldElement::from_str(&tx.max_fee.0.to_string())?,
class_hash: tx.class_hash.0.into(),
transaction_hash: tx.transaction_hash.0.into(),
sender_address: (*tx.sender_address.0.key()).into(),
signature: convert_stark_felt_array_to_field_element_array(&tx.signature.0)?,
declare_txn_v1: DeclareTransactionV1 {
nonce: tx.nonce.0.into(),
max_fee: FieldElement::from_str(&tx.max_fee.0.to_string())?,
class_hash: tx.class_hash.0.into(),
transaction_hash: tx.transaction_hash.0.into(),
sender_address: (*tx.sender_address.0.key()).into(),
signature: convert_stark_felt_array_to_field_element_array(&tx.signature.0)?,
},
compiled_class_hash: tx.compiled_class_hash.0.into(),
}),
})
Expand Down

0 comments on commit 559242c

Please sign in to comment.