Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: align with cairo 0.8.1 #231

Merged
merged 2 commits into from
Apr 8, 2022
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
4 changes: 4 additions & 0 deletions crates/pathfinder/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ mod tests {
let txn0_hash = StarknetTransactionHash(StarkHash::from_be_slice(b"txn 0").unwrap());
let txn0 = Transaction {
calldata: None,
class_hash: None,
constructor_calldata: None,
contract_address: contract0_addr,
contract_address_salt: None,
Expand All @@ -441,6 +442,7 @@ mod tests {
r#type: Type::Deploy,
};
let receipt0 = Receipt {
actual_fee: None,
events: vec![],
execution_resources: ExecutionResources {
builtin_instance_counter: BuiltinInstanceCounter::Empty(
Expand Down Expand Up @@ -2175,6 +2177,7 @@ mod tests {
) -> [(transaction::Transaction, transaction::Receipt); NUM_TRANSACTIONS] {
let transactions = (0..NUM_TRANSACTIONS).map(|i| transaction::Transaction {
calldata: None,
class_hash: None,
constructor_calldata: None,
contract_address: ContractAddress(
StarkHash::from_hex_str(&"2".repeat(i + 3)).unwrap(),
Expand All @@ -2190,6 +2193,7 @@ mod tests {
max_fee: None,
});
let receipts = (0..NUM_TRANSACTIONS).map(|i| transaction::Receipt {
actual_fee: None,
events: vec![transaction::Event {
from_address: ContractAddress(
StarkHash::from_hex_str(&"2".repeat(i + 3)).unwrap(),
Expand Down
10 changes: 8 additions & 2 deletions crates/pathfinder/src/sequencer/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ pub struct TransactionStatus {
pub mod transaction {
use crate::{
core::{
CallParam, ConstructorParam, ContractAddress, ContractAddressSalt, EntryPoint,
EthereumAddress, EventData, EventKey, Fee, L1ToL2MessageNonce,
CallParam, ConstructorParam, ContractAddress, ContractAddressSalt, ContractHash,
EntryPoint, EthereumAddress, EventData, EventKey, Fee, L1ToL2MessageNonce,
L1ToL2MessagePayloadElem, L2ToL1MessagePayloadElem, StarknetTransactionHash,
StarknetTransactionIndex, TransactionSignatureElem,
},
Expand Down Expand Up @@ -196,6 +196,9 @@ pub mod transaction {
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Receipt {
#[serde_as(as = "Option<FeeAsHexStr>")]
#[serde(default)]
pub actual_fee: Option<Fee>,
pub events: Vec<Event>,
pub execution_resources: ExecutionResources,
pub l1_to_l2_consumed_message: Option<L1ToL2Message>,
Expand Down Expand Up @@ -233,6 +236,9 @@ pub mod transaction {
#[serde_as(as = "Option<Vec<CallParamAsDecimalStr>>")]
#[serde(default)]
pub calldata: Option<Vec<CallParam>>,
/// None for Invoke, Some() for Deploy
#[serde(default)]
pub class_hash: Option<ContractHash>,
#[serde_as(as = "Option<Vec<ConstructorParamAsDecimalStr>>")]
#[serde(default)]
pub constructor_calldata: Option<Vec<ConstructorParam>>,
Expand Down
186 changes: 179 additions & 7 deletions crates/pathfinder/src/storage/schema/revision_0005.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,180 @@
use crate::storage::schema::PostMigrationAction;
use anyhow::Context;
use rusqlite::{named_params, Transaction};
use tracing::info;

use crate::{sequencer::reply::transaction, storage::schema::PostMigrationAction};
// This is a copy of data structures and their serialization specification as of
// revision 4. We have to keep these intact so that future changes to these types
// do not break database upgrades.
mod transaction {
use crate::{
core::{
CallParam, ConstructorParam, ContractAddress, ContractAddressSalt, EntryPoint,
EthereumAddress, EventData, EventKey, L1ToL2MessageNonce, L1ToL2MessagePayloadElem,
L2ToL1MessagePayloadElem, StarknetTransactionHash, StarknetTransactionIndex,
TransactionSignatureElem,
},
rpc::serde::{
CallParamAsDecimalStr, ConstructorParamAsDecimalStr, EthereumAddressAsHexStr,
EventDataAsDecimalStr, EventKeyAsDecimalStr, L1ToL2MessagePayloadElemAsDecimalStr,
L2ToL1MessagePayloadElemAsDecimalStr, TransactionSignatureElemAsDecimalStr,
},
};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

/// Represents deserialized L2 transaction entry point values.
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub enum EntryPointType {
#[serde(rename = "EXTERNAL")]
External,
#[serde(rename = "L1_HANDLER")]
L1Handler,
}

/// Represents execution resources for L2 transaction.
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ExecutionResources {
pub builtin_instance_counter: execution_resources::BuiltinInstanceCounter,
pub n_steps: u64,
pub n_memory_holes: u64,
}

/// Types used when deserializing L2 execution resources related data.
pub mod execution_resources {
use serde::{Deserialize, Serialize};

/// Sometimes `builtin_instance_counter` JSON object is returned empty.
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
#[serde(deny_unknown_fields)]
pub enum BuiltinInstanceCounter {
Normal(NormalBuiltinInstanceCounter),
Empty(EmptyBuiltinInstanceCounter),
}

#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct NormalBuiltinInstanceCounter {
bitwise_builtin: u64,
ecdsa_builtin: u64,
ec_op_builtin: u64,
output_builtin: u64,
pedersen_builtin: u64,
range_check_builtin: u64,
}

#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct EmptyBuiltinInstanceCounter {}
}

/// Represents deserialized L1 to L2 message.
#[serde_as]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct L1ToL2Message {
#[serde_as(as = "EthereumAddressAsHexStr")]
pub from_address: EthereumAddress,
#[serde_as(as = "Vec<L1ToL2MessagePayloadElemAsDecimalStr>")]
pub payload: Vec<L1ToL2MessagePayloadElem>,
pub selector: EntryPoint,
pub to_address: ContractAddress,
#[serde(default)]
pub nonce: Option<L1ToL2MessageNonce>,
}

/// Represents deserialized L2 to L1 message.
#[serde_as]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct L2ToL1Message {
pub from_address: ContractAddress,
#[serde_as(as = "Vec<L2ToL1MessagePayloadElemAsDecimalStr>")]
pub payload: Vec<L2ToL1MessagePayloadElem>,
#[serde_as(as = "EthereumAddressAsHexStr")]
pub to_address: EthereumAddress,
}

/// Represents deserialized L2 transaction receipt data.
#[serde_as]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Receipt {
pub events: Vec<Event>,
pub execution_resources: ExecutionResources,
pub l1_to_l2_consumed_message: Option<L1ToL2Message>,
pub l2_to_l1_messages: Vec<L2ToL1Message>,
pub transaction_hash: StarknetTransactionHash,
pub transaction_index: StarknetTransactionIndex,
}

/// Represents deserialized L2 transaction event data.
#[serde_as]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Event {
#[serde_as(as = "Vec<EventDataAsDecimalStr>")]
pub data: Vec<EventData>,
pub from_address: ContractAddress,
#[serde_as(as = "Vec<EventKeyAsDecimalStr>")]
pub keys: Vec<EventKey>,
}

/// Represents deserialized object containing L2 contract address and transaction type.
#[serde_as]
#[derive(Copy, Clone, Debug, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Source {
pub contract_address: ContractAddress,
pub r#type: Type,
}

/// Represents deserialized L2 transaction data.
#[serde_as]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Transaction {
#[serde_as(as = "Option<Vec<CallParamAsDecimalStr>>")]
#[serde(default)]
pub calldata: Option<Vec<CallParam>>,
#[serde_as(as = "Option<Vec<ConstructorParamAsDecimalStr>>")]
#[serde(default)]
pub constructor_calldata: Option<Vec<ConstructorParam>>,
pub contract_address: ContractAddress,
#[serde(default)]
pub contract_address_salt: Option<ContractAddressSalt>,
#[serde(default)]
pub entry_point_type: Option<EntryPointType>,
#[serde(default)]
pub entry_point_selector: Option<EntryPoint>,
#[serde_as(as = "Option<Vec<TransactionSignatureElemAsDecimalStr>>")]
#[serde(default)]
pub signature: Option<Vec<TransactionSignatureElem>>,
pub transaction_hash: StarknetTransactionHash,
pub r#type: Type,
}

/// Describes L2 transaction types.
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub enum Type {
#[serde(rename = "DEPLOY")]
Deploy,
#[serde(rename = "INVOKE_FUNCTION")]
InvokeFunction,
}

/// Describes L2 transaction failure details.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Failure {
pub code: String,
pub error_message: String,
pub tx_id: u64,
}
}

/// This schema migration moves the Starknet transactions and transaction receipts into
/// their own table. These tables are indexed by the origin Starknet block hash.
Expand Down Expand Up @@ -116,14 +288,15 @@ mod tests {

use crate::{
core::{ContractAddress, StarknetTransactionHash, StarknetTransactionIndex},
sequencer::reply::transaction::{
self,
execution_resources::{BuiltinInstanceCounter, EmptyBuiltinInstanceCounter},
ExecutionResources,
},
storage::schema,
};

use super::transaction::{
self,
execution_resources::{BuiltinInstanceCounter, EmptyBuiltinInstanceCounter},
ExecutionResources,
};

#[test]
fn empty() {
let mut conn = Connection::open_in_memory().unwrap();
Expand Down Expand Up @@ -159,7 +332,6 @@ mod tests {
contract_address_salt: None,
entry_point_type: None,
entry_point_selector: None,
max_fee: None,
signature: None,
transaction_hash: StarknetTransactionHash(
StarkHash::from_hex_str(&"fe".repeat(i as usize + 3)).unwrap(),
Expand Down
15 changes: 7 additions & 8 deletions crates/pathfinder/src/storage/schema/revision_0007.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::storage::{schema::PostMigrationAction, state::StarknetEventsTable};
use anyhow::Context;
use rusqlite::{named_params, Transaction};

use crate::storage::schema::PostMigrationAction;
use crate::storage::state::StarknetEventsTable;

// This is a copy of data structures and their serialization specification as of
// revision 6. We have to keep these intact so that future changes to these types
// do not break database upgrades.
Expand Down Expand Up @@ -336,14 +334,15 @@ mod tests {
core::{
ContractAddress, EventData, EventKey, StarknetTransactionHash, StarknetTransactionIndex,
},
sequencer::reply::transaction::{
self as starknet_transaction,
execution_resources::{BuiltinInstanceCounter, EmptyBuiltinInstanceCounter},
ExecutionResources,
},
storage::schema,
};

use super::transaction::{
self as starknet_transaction,
execution_resources::{BuiltinInstanceCounter, EmptyBuiltinInstanceCounter},
ExecutionResources,
};

use super::*;

#[test]
Expand Down
3 changes: 3 additions & 0 deletions crates/pathfinder/src/storage/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,7 @@ mod tests {
) -> [(transaction::Transaction, transaction::Receipt); NUM_TRANSACTIONS] {
let transactions = (0..NUM_TRANSACTIONS).map(|i| transaction::Transaction {
calldata: None,
class_hash: None,
constructor_calldata: None,
contract_address: ContractAddress(
StarkHash::from_hex_str(&"2".repeat(i + 3)).unwrap(),
Expand All @@ -1611,6 +1612,7 @@ mod tests {
max_fee: None,
});
let receipts = (0..NUM_TRANSACTIONS).map(|i| transaction::Receipt {
actual_fee: None,
events: vec![transaction::Event {
from_address: ContractAddress(
StarkHash::from_hex_str(&"2".repeat(i + 3)).unwrap(),
Expand Down Expand Up @@ -1850,6 +1852,7 @@ mod tests {
StarknetTransactionHash(StarkHash::from_be_slice(b"transaction 0 hash").unwrap());
let transaction0 = Transaction {
calldata: None,
class_hash: None,
constructor_calldata: None,
contract_address: contract0_address,
contract_address_salt: None,
Expand Down