Skip to content

Commit

Permalink
Merge pull request #2087 from eqlabs/krisztian/fix-rpc-receipt-serial…
Browse files Browse the repository at this point in the history
…ization

fix(rpc/dto): fix JSON-RPC receipt response serialization
  • Loading branch information
kkovaacs authored Jun 19, 2024
2 parents f6baf79 + b0d10ec commit 48e5a51
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ More expansive patch notes and explanations may be found in the specific [pathfi
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- `starknet_getTransactionReceipt` responses are not compliant with the JSON-RPC specification:
- L1 handler receipts throw an internal error
- `execution_status` and `revert_reason` properties are missing

## [0.13.0] - 2024-06-18

### Fixed
Expand Down
46 changes: 45 additions & 1 deletion crates/rpc/src/dto/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ impl From<&pathfinder_common::receipt::ExecutionStatus> for TxnExecutionStatus {
}
}

struct TxnExecutionStatusWithRevertReason<'a>(pub &'a pathfinder_common::receipt::ExecutionStatus);

#[derive(Copy, Clone)]
pub enum TxnFinalityStatus {
AcceptedOnL2,
Expand Down Expand Up @@ -95,6 +97,26 @@ impl SerializeForVersion for TxnExecutionStatus {
}
}

impl SerializeForVersion for TxnExecutionStatusWithRevertReason<'_> {
fn serialize(&self, serializer: Serializer) -> Result<serialize::Ok, serialize::Error> {
use pathfinder_common::receipt::ExecutionStatus;

let mut serializer = serializer.serialize_struct()?;

match self.0 {
ExecutionStatus::Succeeded => {
serializer.serialize_field("execution_status", &TxnExecutionStatus::Succeeded)?;
}
ExecutionStatus::Reverted { reason } => {
serializer.serialize_field("execution_status", &TxnExecutionStatus::Reverted)?;
serializer.serialize_field("revert_reason", reason)?;
}
}

serializer.end()
}
}

impl SerializeForVersion for TxnFinalityStatus {
fn serialize(&self, serializer: Serializer) -> Result<serialize::Ok, serialize::Error> {
match self {
Expand Down Expand Up @@ -139,7 +161,7 @@ impl SerializeForVersion for TxnReceipt<'_> {
TransactionKind::Deploy => serializer.serialize(&DeployTxnReceipt(self)),
TransactionKind::DeployAccount => serializer.serialize(&DeployAccountTxnReceipt(self)),
TransactionKind::Invoke => serializer.serialize(&InvokeTxnReceipt(self)),
TransactionKind::L1Handler => serializer.serialize(&DeclareTxnReceipt(self)),
TransactionKind::L1Handler => serializer.serialize(&L1HandlerTxnReceipt(self)),
}
}
}
Expand Down Expand Up @@ -260,6 +282,7 @@ impl SerializeForVersion for L1HandlerTxnReceipt<'_> {
serializer.end()
}
}

impl SerializeForVersion for CommonReceiptProperties<'_> {
fn serialize(&self, serializer: Serializer) -> Result<serialize::Ok, serialize::Error> {
let mut serializer = serializer.serialize_struct()?;
Expand Down Expand Up @@ -291,6 +314,9 @@ impl SerializeForVersion for CommonReceiptProperties<'_> {
"execution_resources",
&ExecutionResources(&self.0.receipt.execution_resources),
)?;
serializer.flatten(&TxnExecutionStatusWithRevertReason(
&self.0.receipt.execution_status,
))?;

serializer.end()
}
Expand Down Expand Up @@ -430,4 +456,22 @@ mod tests {
let encoded = input.serialize(Serializer::default()).unwrap();
assert_eq!(encoded, expected);
}

#[test]
fn txn_execution_status_with_revert_reason() {
let input = TxnExecutionStatusWithRevertReason(
&pathfinder_common::receipt::ExecutionStatus::Succeeded,
);
let expected = json!({"execution_status": "SUCCEEDED"});
let encoded = input.serialize(Serializer::default()).unwrap();
assert_eq!(encoded, expected);

let reverted_status = pathfinder_common::receipt::ExecutionStatus::Reverted {
reason: "reason".to_owned(),
};
let input = TxnExecutionStatusWithRevertReason(&reverted_status);
let expected = json!({"execution_status": "REVERTED", "revert_reason": "reason"});
let encoded = input.serialize(Serializer::default()).unwrap();
assert_eq!(encoded, expected);
}
}

0 comments on commit 48e5a51

Please sign in to comment.