2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-03-20 17:29:39 +01:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-03-20 17:29:39 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-03-20 17:29:39 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-03-20 17:29:39 +01:00
|
|
|
|
2019-02-25 14:27:28 +01:00
|
|
|
use ethereum_types::{Bloom as H2048, H160, H256, U256, U64};
|
2021-02-08 14:55:03 +01:00
|
|
|
use types::receipt::{LocalizedReceipt, RichReceipt, TransactionOutcome, TypedReceipt};
|
2019-02-25 14:27:28 +01:00
|
|
|
use v1::types::Log;
|
2016-03-20 17:29:39 +01:00
|
|
|
|
2016-05-28 19:30:31 +02:00
|
|
|
/// Receipt
|
2016-03-20 17:29:39 +01:00
|
|
|
#[derive(Debug, Serialize)]
|
2018-10-29 16:49:04 +01:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2016-03-20 17:29:39 +01:00
|
|
|
pub struct Receipt {
|
2020-12-10 16:42:05 +01:00
|
|
|
/// Transaction Type
|
2021-02-17 16:52:51 +01:00
|
|
|
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
|
|
|
|
pub transaction_type: Option<U64>,
|
2016-05-28 19:30:31 +02:00
|
|
|
/// Transaction Hash
|
2016-06-18 12:09:29 +02:00
|
|
|
pub transaction_hash: Option<H256>,
|
2016-05-28 19:30:31 +02:00
|
|
|
/// Transaction index
|
2016-06-18 12:09:29 +02:00
|
|
|
pub transaction_index: Option<U256>,
|
2016-05-28 19:30:31 +02:00
|
|
|
/// Block hash
|
2016-06-18 12:09:29 +02:00
|
|
|
pub block_hash: Option<H256>,
|
2018-06-07 16:47:41 +02:00
|
|
|
/// Sender
|
|
|
|
pub from: Option<H160>,
|
|
|
|
/// Recipient
|
|
|
|
pub to: Option<H160>,
|
2016-05-28 19:30:31 +02:00
|
|
|
/// Block number
|
2016-06-18 12:09:29 +02:00
|
|
|
pub block_number: Option<U256>,
|
2016-05-28 19:30:31 +02:00
|
|
|
/// Cumulative gas used
|
2016-03-20 17:29:39 +01:00
|
|
|
pub cumulative_gas_used: U256,
|
2016-05-28 19:30:31 +02:00
|
|
|
/// Gas used
|
2016-08-17 19:25:02 +02:00
|
|
|
pub gas_used: Option<U256>,
|
2016-05-28 19:30:31 +02:00
|
|
|
/// Contract address
|
2016-07-06 11:23:29 +02:00
|
|
|
pub contract_address: Option<H160>,
|
2016-05-28 19:30:31 +02:00
|
|
|
/// Logs
|
2016-03-20 17:29:39 +01:00
|
|
|
pub logs: Vec<Log>,
|
2016-11-04 12:33:13 +01:00
|
|
|
/// State Root
|
2019-11-11 21:57:38 +01:00
|
|
|
// NOTE(niklasad1): EIP98 makes this optional field, if it's missing then skip serializing it
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none", rename = "root")]
|
2017-01-25 20:22:48 +01:00
|
|
|
pub state_root: Option<H256>,
|
2016-11-04 12:33:13 +01:00
|
|
|
/// Logs bloom
|
|
|
|
pub logs_bloom: H2048,
|
2017-09-21 10:11:53 +02:00
|
|
|
/// Status code
|
2019-11-11 21:57:38 +01:00
|
|
|
// NOTE(niklasad1): Unknown after EIP98 rules, if it's missing then skip serializing it
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none", rename = "status")]
|
2017-11-01 11:25:10 +01:00
|
|
|
pub status_code: Option<U64>,
|
2017-09-21 10:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Receipt {
|
|
|
|
fn outcome_to_state_root(outcome: TransactionOutcome) -> Option<H256> {
|
|
|
|
match outcome {
|
|
|
|
TransactionOutcome::Unknown | TransactionOutcome::StatusCode(_) => None,
|
2019-03-22 12:01:11 +01:00
|
|
|
TransactionOutcome::StateRoot(root) => Some(root),
|
2017-09-21 10:11:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 11:25:10 +01:00
|
|
|
fn outcome_to_status_code(outcome: &TransactionOutcome) -> Option<U64> {
|
2017-09-21 10:11:53 +02:00
|
|
|
match *outcome {
|
|
|
|
TransactionOutcome::Unknown | TransactionOutcome::StateRoot(_) => None,
|
2017-11-01 11:25:10 +01:00
|
|
|
TransactionOutcome::StatusCode(ref code) => Some((*code as u64).into()),
|
2017-09-21 10:11:53 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<LocalizedReceipt> for Receipt {
|
|
|
|
fn from(r: LocalizedReceipt) -> Self {
|
|
|
|
Receipt {
|
2018-06-07 16:47:41 +02:00
|
|
|
to: r.to.map(Into::into),
|
2019-03-22 12:01:11 +01:00
|
|
|
from: Some(r.from),
|
2021-02-17 16:52:51 +01:00
|
|
|
transaction_type: r.transaction_type.to_U64_option_id(),
|
2019-03-22 12:01:11 +01:00
|
|
|
transaction_hash: Some(r.transaction_hash),
|
2016-07-06 11:23:29 +02:00
|
|
|
transaction_index: Some(r.transaction_index.into()),
|
2019-03-22 12:01:11 +01:00
|
|
|
block_hash: Some(r.block_hash),
|
2016-07-06 11:23:29 +02:00
|
|
|
block_number: Some(r.block_number.into()),
|
2019-03-22 12:01:11 +01:00
|
|
|
cumulative_gas_used: r.cumulative_gas_used,
|
|
|
|
gas_used: Some(r.gas_used),
|
2016-08-17 19:25:02 +02:00
|
|
|
contract_address: r.contract_address.map(Into::into),
|
|
|
|
logs: r.logs.into_iter().map(Into::into).collect(),
|
2017-09-21 10:11:53 +02:00
|
|
|
status_code: Self::outcome_to_status_code(&r.outcome),
|
|
|
|
state_root: Self::outcome_to_state_root(r.outcome),
|
2019-03-22 12:01:11 +01:00
|
|
|
logs_bloom: r.log_bloom,
|
2016-08-17 19:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RichReceipt> for Receipt {
|
|
|
|
fn from(r: RichReceipt) -> Self {
|
|
|
|
Receipt {
|
2019-11-11 21:57:38 +01:00
|
|
|
from: Some(r.from),
|
|
|
|
to: r.to.map(Into::into),
|
2021-02-17 16:52:51 +01:00
|
|
|
transaction_type: r.transaction_type.to_U64_option_id(),
|
2019-03-22 12:01:11 +01:00
|
|
|
transaction_hash: Some(r.transaction_hash),
|
2016-08-17 19:25:02 +02:00
|
|
|
transaction_index: Some(r.transaction_index.into()),
|
|
|
|
block_hash: None,
|
|
|
|
block_number: None,
|
2019-03-22 12:01:11 +01:00
|
|
|
cumulative_gas_used: r.cumulative_gas_used,
|
|
|
|
gas_used: Some(r.gas_used),
|
2016-07-06 11:23:29 +02:00
|
|
|
contract_address: r.contract_address.map(Into::into),
|
|
|
|
logs: r.logs.into_iter().map(Into::into).collect(),
|
2017-09-21 10:11:53 +02:00
|
|
|
status_code: Self::outcome_to_status_code(&r.outcome),
|
|
|
|
state_root: Self::outcome_to_state_root(r.outcome),
|
2019-03-22 12:01:11 +01:00
|
|
|
logs_bloom: r.log_bloom,
|
2016-03-20 17:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:42:05 +01:00
|
|
|
impl From<TypedReceipt> for Receipt {
|
|
|
|
fn from(r: TypedReceipt) -> Self {
|
2021-02-17 16:52:51 +01:00
|
|
|
let transaction_type = r.tx_type().to_U64_option_id();
|
2020-12-10 16:42:05 +01:00
|
|
|
let r = r.receipt().clone();
|
2016-06-18 12:09:29 +02:00
|
|
|
Receipt {
|
2018-06-07 16:47:41 +02:00
|
|
|
from: None,
|
|
|
|
to: None,
|
2020-12-10 16:42:05 +01:00
|
|
|
transaction_type,
|
2016-06-18 12:09:29 +02:00
|
|
|
transaction_hash: None,
|
|
|
|
transaction_index: None,
|
|
|
|
block_hash: None,
|
|
|
|
block_number: None,
|
2019-03-22 12:01:11 +01:00
|
|
|
cumulative_gas_used: r.gas_used,
|
2016-08-17 19:25:02 +02:00
|
|
|
gas_used: None,
|
2016-06-18 12:09:29 +02:00
|
|
|
contract_address: None,
|
2016-07-06 11:23:29 +02:00
|
|
|
logs: r.logs.into_iter().map(Into::into).collect(),
|
2017-09-21 10:11:53 +02:00
|
|
|
status_code: Self::outcome_to_status_code(&r.outcome),
|
|
|
|
state_root: Self::outcome_to_state_root(r.outcome),
|
2019-03-22 12:01:11 +01:00
|
|
|
logs_bloom: r.log_bloom,
|
2016-06-18 12:09:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 11:47:50 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use serde_json;
|
2021-02-17 16:52:51 +01:00
|
|
|
use types::transaction::TypedTxId;
|
2016-11-04 12:33:13 +01:00
|
|
|
use v1::types::{Log, Receipt};
|
2016-03-21 11:47:50 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn receipt_serialization() {
|
2021-02-17 16:52:51 +01:00
|
|
|
let s = r#"{"type":"0x1","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x0","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","from":null,"to":null,"blockNumber":"0x4510c","cumulativeGasUsed":"0x20","gasUsed":"0x10","contractAddress":null,"logs":[{"address":"0x33990122638b9132ca29c723bdf037f1a891a70c","topics":["0xa6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc","0x4861736852656700000000000000000000000000000000000000000000000000"],"data":"0x","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x4510c","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x0","logIndex":"0x1","transactionLogIndex":null,"type":"mined","removed":false}],"root":"0x000000000000000000000000000000000000000000000000000000000000000a","logsBloom":"0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f","status":"0x1"}"#;
|
2016-03-21 11:47:50 +01:00
|
|
|
|
|
|
|
let receipt = Receipt {
|
2018-06-07 16:47:41 +02:00
|
|
|
from: None,
|
|
|
|
to: None,
|
2021-02-17 16:52:51 +01:00
|
|
|
transaction_type: TypedTxId::AccessList.to_U64_option_id(),
|
2016-11-04 12:33:13 +01:00
|
|
|
transaction_hash: Some(0.into()),
|
|
|
|
transaction_index: Some(0.into()),
|
|
|
|
block_hash: Some(
|
|
|
|
"ed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5"
|
|
|
|
.parse()
|
|
|
|
.unwrap(),
|
2020-08-05 06:08:03 +02:00
|
|
|
),
|
2016-11-04 12:33:13 +01:00
|
|
|
block_number: Some(0x4510c.into()),
|
|
|
|
cumulative_gas_used: 0x20.into(),
|
|
|
|
gas_used: Some(0x10.into()),
|
2016-03-21 11:47:50 +01:00
|
|
|
contract_address: None,
|
|
|
|
logs: vec![Log {
|
2016-11-04 12:33:13 +01:00
|
|
|
address: "33990122638b9132ca29c723bdf037f1a891a70c".parse().unwrap(),
|
2016-03-21 11:47:50 +01:00
|
|
|
topics: vec![
|
2016-11-04 12:33:13 +01:00
|
|
|
"a6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc"
|
|
|
|
.parse()
|
|
|
|
.unwrap(),
|
|
|
|
"4861736852656700000000000000000000000000000000000000000000000000"
|
|
|
|
.parse()
|
|
|
|
.unwrap(),
|
2016-03-21 11:47:50 +01:00
|
|
|
],
|
2016-07-06 11:23:29 +02:00
|
|
|
data: vec![].into(),
|
2016-11-04 12:33:13 +01:00
|
|
|
block_hash: Some(
|
|
|
|
"ed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5"
|
|
|
|
.parse()
|
|
|
|
.unwrap(),
|
2020-08-05 06:08:03 +02:00
|
|
|
),
|
2016-11-04 12:33:13 +01:00
|
|
|
block_number: Some(0x4510c.into()),
|
|
|
|
transaction_hash: Some(0.into()),
|
|
|
|
transaction_index: Some(0.into()),
|
2016-12-29 19:48:28 +01:00
|
|
|
transaction_log_index: None,
|
2016-11-04 12:33:13 +01:00
|
|
|
log_index: Some(1.into()),
|
|
|
|
log_type: "mined".into(),
|
2018-06-13 13:39:27 +02:00
|
|
|
removed: false,
|
2016-11-04 12:33:13 +01:00
|
|
|
}],
|
|
|
|
logs_bloom: 15.into(),
|
2017-01-25 20:22:48 +01:00
|
|
|
state_root: Some(10.into()),
|
2017-11-01 11:25:10 +01:00
|
|
|
status_code: Some(1u64.into()),
|
2016-03-21 11:47:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let serialized = serde_json::to_string(&receipt).unwrap();
|
|
|
|
assert_eq!(serialized, s);
|
|
|
|
}
|
|
|
|
}
|