fc_rpc_core/types/
transaction.rs

1// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
2// This file is part of Frontier.
3//
4// Copyright (c) 2015-2020 Parity Technologies (UK) Ltd.
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use std::{sync::{Arc, Mutex}, collections::HashMap};
20use serde::{Serialize, Serializer};
21use serde::ser::SerializeStruct;
22use ethereum_types::{H160, H256, H512, U64, U256};
23use crate::types::Bytes;
24
25/// Transaction
26#[derive(Debug, Default, Clone, PartialEq, Serialize)]
27#[serde(rename_all = "camelCase")]
28pub struct Transaction {
29	/// Hash
30	pub hash: H256,
31	/// Nonce
32	pub nonce: U256,
33	/// Block hash
34	pub block_hash: Option<H256>,
35	/// Block number
36	pub block_number: Option<U256>,
37	/// Transaction Index
38	pub transaction_index: Option<U256>,
39	/// Sender
40	pub from: H160,
41	/// Recipient
42	pub to: Option<H160>,
43	/// Transfered value
44	pub value: U256,
45	/// Gas Price
46	pub gas_price: U256,
47	/// Gas
48	pub gas: U256,
49	/// Data
50	pub input: Bytes,
51	/// Creates contract
52	pub creates: Option<H160>,
53	/// Raw transaction data
54	pub raw: Bytes,
55	/// Public key of the signer.
56	pub public_key: Option<H512>,
57	/// The network id of the transaction, if any.
58	pub chain_id: Option<U64>,
59	/// The standardised V field of the signature (0 or 1).
60	pub standard_v: U256,
61	/// The standardised V field of the signature.
62	pub v: U256,
63	/// The R field of the signature.
64	pub r: U256,
65	/// The S field of the signature.
66	pub s: U256,
67}
68
69/// Local Transaction Status
70#[derive(Debug)]
71pub enum LocalTransactionStatus {
72	/// Transaction is pending
73	Pending,
74	/// Transaction is in future part of the queue
75	Future,
76	/// Transaction was mined.
77	Mined(Transaction),
78	/// Transaction was removed from the queue, but not mined.
79	Culled(Transaction),
80	/// Transaction was dropped because of limit.
81	Dropped(Transaction),
82	/// Transaction was replaced by transaction with higher gas price.
83	Replaced(Transaction, U256, H256),
84	/// Transaction never got into the queue.
85	Rejected(Transaction, String),
86	/// Transaction is invalid.
87	Invalid(Transaction),
88	/// Transaction was canceled.
89	Canceled(Transaction),
90}
91
92impl Serialize for LocalTransactionStatus {
93	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
94		where S: Serializer
95	{
96		use self::LocalTransactionStatus::*;
97
98		let elems = match *self {
99			Pending | Future => 1,
100			Mined(..) | Culled(..) | Dropped(..) | Invalid(..) | Canceled(..) => 2,
101			Rejected(..) => 3,
102			Replaced(..) => 4,
103		};
104
105		let status = "status";
106		let transaction = "transaction";
107
108		let mut struc = serializer.serialize_struct("LocalTransactionStatus", elems)?;
109		match *self {
110			Pending => struc.serialize_field(status, "pending")?,
111			Future => struc.serialize_field(status, "future")?,
112			Mined(ref tx) => {
113				struc.serialize_field(status, "mined")?;
114				struc.serialize_field(transaction, tx)?;
115			},
116			Culled(ref tx) => {
117				struc.serialize_field(status, "culled")?;
118				struc.serialize_field(transaction, tx)?;
119			},
120			Dropped(ref tx) => {
121				struc.serialize_field(status, "dropped")?;
122				struc.serialize_field(transaction, tx)?;
123			},
124			Canceled(ref tx) => {
125				struc.serialize_field(status, "canceled")?;
126				struc.serialize_field(transaction, tx)?;
127			},
128			Invalid(ref tx) => {
129				struc.serialize_field(status, "invalid")?;
130				struc.serialize_field(transaction, tx)?;
131			},
132			Rejected(ref tx, ref reason) => {
133				struc.serialize_field(status, "rejected")?;
134				struc.serialize_field(transaction, tx)?;
135				struc.serialize_field("error", reason)?;
136			},
137			Replaced(ref tx, ref gas_price, ref hash) => {
138				struc.serialize_field(status, "replaced")?;
139				struc.serialize_field(transaction, tx)?;
140				struc.serialize_field("hash", hash)?;
141				struc.serialize_field("gasPrice", gas_price)?;
142			},
143		}
144
145		struc.end()
146	}
147}
148
149/// Geth-compatible output for eth_signTransaction method
150#[derive(Debug, Default, Clone, PartialEq, Serialize)]
151pub struct RichRawTransaction {
152	/// Raw transaction RLP
153	pub raw: Bytes,
154	/// Transaction details
155	#[serde(rename = "tx")]
156	pub transaction: Transaction
157}
158
159pub struct PendingTransaction {
160	pub transaction: Transaction,
161	pub at_block: u64
162}
163
164impl PendingTransaction {
165	pub fn new(transaction: Transaction, at_block: u64) -> Self {
166		Self { transaction, at_block }
167	}
168}
169
170pub type PendingTransactions = Option<Arc<Mutex<HashMap<H256, PendingTransaction>>>>;