Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

feat(network): add conversion from protobuf transaction outputs to sn api #2055

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
127 changes: 127 additions & 0 deletions crates/papyrus_protobuf/src/converters/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ impl TryFrom<protobuf::Receipt> for TransactionOutput {
}
}

impl From<TransactionOutput> for protobuf::Receipt {
fn from(value: TransactionOutput) -> Self {
match value {
TransactionOutput::Invoke(invoke) => {
protobuf::Receipt { r#type: Some(protobuf::receipt::Type::Invoke(invoke.into())) }
}
TransactionOutput::L1Handler(l1_handler) => protobuf::Receipt {
r#type: Some(protobuf::receipt::Type::L1Handler(l1_handler.into())),
},
TransactionOutput::Declare(declare) => {
protobuf::Receipt { r#type: Some(protobuf::receipt::Type::Declare(declare.into())) }
}
TransactionOutput::Deploy(deploy) => protobuf::Receipt {
r#type: Some(protobuf::receipt::Type::DeprecatedDeploy(deploy.into())),
},
TransactionOutput::DeployAccount(deploy_account) => protobuf::Receipt {
r#type: Some(protobuf::receipt::Type::DeployAccount(deploy_account.into())),
},
}
}
}

// The output will have an empty events vec
impl TryFrom<protobuf::receipt::DeployAccount> for DeployAccountTransactionOutput {
type Error = ProtobufConversionError;
Expand Down Expand Up @@ -82,6 +104,24 @@ impl TryFrom<protobuf::receipt::DeployAccount> for DeployAccountTransactionOutpu
}
}

impl From<DeployAccountTransactionOutput> for protobuf::receipt::DeployAccount {
/// The returned price_unit isn't correct.
/// It can be fixed by calling set_price_unit_based_on_transaction
fn from(value: DeployAccountTransactionOutput) -> Self {
let common = create_proto_receipt_common_from_txn_output_fields(
value.actual_fee,
value.messages_sent,
value.execution_resources,
value.execution_status,
);

protobuf::receipt::DeployAccount {
common: Some(common),
contract_address: Some(StarkFelt::from(value.contract_address).into()),
}
}
}

// The output will have an empty events vec
impl TryFrom<protobuf::receipt::Deploy> for DeployTransactionOutput {
type Error = ProtobufConversionError;
Expand Down Expand Up @@ -114,6 +154,24 @@ impl TryFrom<protobuf::receipt::Deploy> for DeployTransactionOutput {
}
}

impl From<DeployTransactionOutput> for protobuf::receipt::Deploy {
/// The returned price_unit isn't correct.
/// It can be fixed by calling set_price_unit_based_on_transaction
fn from(value: DeployTransactionOutput) -> Self {
let common = create_proto_receipt_common_from_txn_output_fields(
value.actual_fee,
value.messages_sent,
value.execution_resources,
value.execution_status,
);

protobuf::receipt::Deploy {
common: Some(common),
contract_address: Some(StarkFelt::from(value.contract_address).into()),
}
}
}

// The output will have an empty events vec
impl TryFrom<protobuf::receipt::Declare> for DeclareTransactionOutput {
type Error = ProtobufConversionError;
Expand All @@ -127,6 +185,21 @@ impl TryFrom<protobuf::receipt::Declare> for DeclareTransactionOutput {
}
}

impl From<DeclareTransactionOutput> for protobuf::receipt::Declare {
/// The returned price_unit isn't correct.
/// It can be fixed by calling set_price_unit_based_on_transaction
fn from(value: DeclareTransactionOutput) -> Self {
let common = create_proto_receipt_common_from_txn_output_fields(
value.actual_fee,
value.messages_sent,
value.execution_resources,
value.execution_status,
);

protobuf::receipt::Declare { common: Some(common) }
}
}

// The output will have an empty events vec
impl TryFrom<protobuf::receipt::Invoke> for InvokeTransactionOutput {
type Error = ProtobufConversionError;
Expand All @@ -140,6 +213,21 @@ impl TryFrom<protobuf::receipt::Invoke> for InvokeTransactionOutput {
}
}

impl From<InvokeTransactionOutput> for protobuf::receipt::Invoke {
/// The returned price_unit isn't correct.
/// It can be fixed by calling set_price_unit_based_on_transaction
fn from(value: InvokeTransactionOutput) -> Self {
let common = create_proto_receipt_common_from_txn_output_fields(
value.actual_fee,
value.messages_sent,
value.execution_resources,
value.execution_status,
);

protobuf::receipt::Invoke { common: Some(common) }
}
}

// The output will have an empty events vec
impl TryFrom<protobuf::receipt::L1Handler> for L1HandlerTransactionOutput {
type Error = ProtobufConversionError;
Expand All @@ -153,6 +241,21 @@ impl TryFrom<protobuf::receipt::L1Handler> for L1HandlerTransactionOutput {
}
}

impl From<L1HandlerTransactionOutput> for protobuf::receipt::L1Handler {
/// The returned price_unit isn't correct.
/// It can be fixed by calling set_price_unit_based_on_transaction
fn from(value: L1HandlerTransactionOutput) -> Self {
let common = create_proto_receipt_common_from_txn_output_fields(
value.actual_fee,
value.messages_sent,
value.execution_resources,
value.execution_status,
);

protobuf::receipt::L1Handler { common: Some(common), msg_hash: None }
}
}

type ProtobufBuiltinCounter = protobuf::receipt::execution_resources::BuiltinCounter;

impl TryFrom<ProtobufBuiltinCounter> for HashMap<Builtin, u64> {
Expand Down Expand Up @@ -350,3 +453,27 @@ fn parse_common_receipt_fields(
)?)?;
Ok((actual_fee, messages_sent, execution_status, execution_resources))
}

fn create_proto_receipt_common_from_txn_output_fields(
actual_fee: Fee,
messages_sent: Vec<MessageToL1>,
execution_resources: ExecutionResources,
execution_status: TransactionExecutionStatus,
) -> protobuf::receipt::Common {
let actual_fee = StarkFelt::from(actual_fee).into();
let messages_sent = messages_sent.into_iter().map(protobuf::MessageToL1::from).collect();
let execution_resources = execution_resources.into();
let revert_reason =
if let TransactionExecutionStatus::Reverted(reverted_status) = execution_status {
Some(reverted_status.revert_reason)
} else {
None
};
protobuf::receipt::Common {
actual_fee: Some(actual_fee),
price_unit: 0,
messages_sent,
execution_resources: Some(execution_resources),
revert_reason,
}
}
34 changes: 34 additions & 0 deletions crates/papyrus_protobuf/src/converters/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,3 +1101,37 @@ impl From<Transaction> for protobuf::transaction::Txn {
}
}
}

#[allow(dead_code)]
pub fn set_price_unit_based_on_transaction(
receipt: &mut protobuf::Receipt,
transaction: &protobuf::Transaction,
) {
let price_unit = match &transaction.txn {
Some(protobuf::transaction::Txn::DeclareV1(_)) => protobuf::PriceUnit::Wei,
Some(protobuf::transaction::Txn::DeclareV2(_)) => protobuf::PriceUnit::Wei,
Some(protobuf::transaction::Txn::DeclareV3(_)) => protobuf::PriceUnit::Fri,
Some(protobuf::transaction::Txn::Deploy(_)) => protobuf::PriceUnit::Wei,
Some(protobuf::transaction::Txn::DeployAccountV1(_)) => protobuf::PriceUnit::Wei,
Some(protobuf::transaction::Txn::DeployAccountV3(_)) => protobuf::PriceUnit::Fri,
Some(protobuf::transaction::Txn::InvokeV1(_)) => protobuf::PriceUnit::Wei,
Some(protobuf::transaction::Txn::InvokeV3(_)) => protobuf::PriceUnit::Fri,
Some(protobuf::transaction::Txn::L1Handler(_)) => protobuf::PriceUnit::Wei,
_ => return,
};
let Some(ref mut receipt_type) = receipt.r#type else {
return;
};

let common = match receipt_type {
protobuf::receipt::Type::Invoke(invoke) => invoke.common.as_mut(),
protobuf::receipt::Type::L1Handler(l1_handler) => l1_handler.common.as_mut(),
protobuf::receipt::Type::Declare(declare) => declare.common.as_mut(),
protobuf::receipt::Type::DeprecatedDeploy(deploy) => deploy.common.as_mut(),
protobuf::receipt::Type::DeployAccount(deploy_account) => deploy_account.common.as_mut(),
};

if let Some(common) = common {
common.price_unit = price_unit.into();
}
}
Loading