├── .github └── workflows │ └── main.yml ├── .gitignore ├── Cargo.toml ├── LICENSE └── src └── lib.rs /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | name: Continuous integration 4 | 5 | jobs: 6 | ci: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | matrix: 10 | os: [ubuntu-latest, windows-latest, macOS-latest] 11 | 12 | steps: 13 | - uses: actions/checkout@v2 14 | 15 | - uses: actions-rs/toolchain@v1 16 | with: 17 | profile: minimal 18 | toolchain: stable 19 | override: true 20 | components: rustfmt, clippy 21 | 22 | - uses: actions-rs/cargo@v1 23 | with: 24 | command: fmt 25 | args: --all -- --check --config merge_imports=true 26 | 27 | - uses: actions-rs/cargo@v1 28 | with: 29 | command: install 30 | args: cargo-hack 31 | 32 | - uses: actions-rs/cargo@v1 33 | with: 34 | command: hack 35 | args: check --all --ignore-private --each-feature --no-dev-deps 36 | 37 | - uses: actions-rs/cargo@v1 38 | with: 39 | command: check 40 | args: --all --all-targets --all-features 41 | 42 | - uses: actions-rs/cargo@v1 43 | with: 44 | command: test 45 | 46 | - uses: actions-rs/cargo@v1 47 | with: 48 | command: clippy 49 | args: -- -D warnings 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ethereum-txpool" 3 | version = "0.1.0" 4 | authors = ["Artem Vorotnikov "] 5 | edition = "2018" 6 | license = "Apache-2.0" 7 | 8 | [dependencies] 9 | anyhow = "1" 10 | ethereum = { version = "0.7", default-features = false } 11 | ethereum-types = "0.11" 12 | thiserror = "1" 13 | rlp = "0.5" 14 | secp256k1 = { version = "0.20", features = ["global-context", "recovery"] } 15 | sha3 = "0.9" 16 | tracing = "0.1" 17 | 18 | [dev-dependencies] 19 | hex = "0.4" 20 | hex-literal = "0.3" 21 | maplit = "1" 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Transaction pool for Ethereum. 2 | 3 | use anyhow::{anyhow, bail}; 4 | use ethereum::Transaction; 5 | use ethereum_types::{Address, H256, U256}; 6 | use rlp::{Encodable, RlpStream}; 7 | use secp256k1::{ 8 | recovery::{RecoverableSignature, RecoveryId}, 9 | Message, PublicKey, SECP256K1, 10 | }; 11 | use sha3::{Digest, Keccak256}; 12 | use std::{ 13 | collections::{ 14 | hash_map::Entry::{Occupied, Vacant}, 15 | BTreeMap, HashMap, VecDeque, 16 | }, 17 | convert::{TryFrom, TryInto}, 18 | sync::Arc, 19 | }; 20 | use thiserror::Error; 21 | use tracing::*; 22 | 23 | struct RichTransaction { 24 | inner: Transaction, 25 | sender: Address, 26 | hash: H256, 27 | // temporary crutch 28 | nonce: u64, 29 | } 30 | 31 | impl RichTransaction { 32 | fn cost(&self) -> U256 { 33 | self.inner.gas_limit * self.inner.gas_price + self.inner.value 34 | } 35 | } 36 | 37 | fn pk2addr(pk: &PublicKey) -> Address { 38 | Address::from_slice(&Keccak256::digest(&pk.serialize_uncompressed()[1..])[12..]) 39 | } 40 | 41 | impl TryFrom for RichTransaction { 42 | type Error = anyhow::Error; 43 | 44 | fn try_from(tx: Transaction) -> Result { 45 | let h = { 46 | let mut stream = RlpStream::new(); 47 | tx.rlp_append(&mut stream); 48 | Keccak256::digest(&stream.out()) 49 | }; 50 | let hash = H256::from_slice(h.as_slice()); 51 | 52 | let mut sig = [0_u8; 64]; 53 | sig[..32].copy_from_slice(tx.signature.r().as_bytes()); 54 | sig[32..].copy_from_slice(tx.signature.s().as_bytes()); 55 | let rec = RecoveryId::from_i32(tx.signature.standard_v() as i32).unwrap(); 56 | 57 | let public = &SECP256K1.recover( 58 | &Message::from_slice( 59 | ethereum::TransactionMessage::from(tx.clone()) 60 | .hash() 61 | .as_bytes(), 62 | )?, 63 | &RecoverableSignature::from_compact(&sig, rec)?, 64 | )?; 65 | 66 | Ok(Self { 67 | sender: pk2addr(public), 68 | hash, 69 | nonce: tx 70 | .nonce 71 | .try_into() 72 | .map_err(|e| anyhow!("invalid nonce: {}", e))?, 73 | inner: tx, 74 | }) 75 | } 76 | } 77 | 78 | #[derive(Clone, Copy, Debug)] 79 | pub struct BlockHeader { 80 | pub hash: H256, 81 | pub parent: H256, 82 | } 83 | 84 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] 85 | pub struct AccountInfo { 86 | pub balance: U256, 87 | pub nonce: u64, 88 | } 89 | 90 | /// Account diff 91 | #[derive(Clone, Copy, Debug)] 92 | pub enum AccountDiff { 93 | Changed(AccountInfo), 94 | Deleted, 95 | } 96 | 97 | #[derive(Debug, Error)] 98 | pub enum ImportError { 99 | #[error("need state for account: {0}")] 100 | NoState(Address), 101 | #[error("invalid transaction: {0}")] 102 | InvalidTransaction(anyhow::Error), 103 | #[error("nonce gap")] 104 | NonceGap, 105 | #[error("stale transaction")] 106 | StaleTransaction, 107 | #[error("invalid sender: {0}")] 108 | InvalidSender(anyhow::Error), 109 | #[error("fee too low")] 110 | FeeTooLow, 111 | #[error("not enough balance to pay for gas")] 112 | InsufficientBalance, 113 | #[error("current block has not been set")] 114 | NoCurrentBlock, 115 | #[error("other: {0}")] 116 | Other(anyhow::Error), 117 | } 118 | 119 | #[derive(Clone, Copy, Debug)] 120 | pub struct Status { 121 | pub transactions: usize, 122 | pub senders: usize, 123 | } 124 | 125 | struct AccountPool { 126 | info: AccountInfo, 127 | txs: VecDeque>, 128 | } 129 | 130 | impl AccountPool { 131 | fn prune_insufficient_balance( 132 | &mut self, 133 | cache: Option<(usize, U256)>, 134 | ) -> VecDeque> { 135 | let (offset, mut cumulative_balance) = cache.unwrap_or((0, self.info.balance)); 136 | 137 | for (i, tx) in self.txs.iter().enumerate().skip(offset as usize) { 138 | if let Some(balance) = cumulative_balance.checked_sub(tx.cost()) { 139 | cumulative_balance = balance; 140 | } else { 141 | return self.txs.split_off(i); 142 | } 143 | } 144 | 145 | VecDeque::new() 146 | } 147 | } 148 | 149 | /// Transaction pool that is able to import Ethereum transactions, check their validity and provide traversal over all of them. 150 | /// 151 | /// Unlike most pool implementations, [Pool] does not support and guards against nonce gaps. 152 | /// As a consequence, we can sort transactions by sender and store in double ended queue, where `tx_nonce = account_nonce + queue_pos`. 153 | /// This makes traversal efficient. 154 | /// 155 | /// [Pool] **must** be used together with a client: validity checks require knowledge about account nonce and balance. 156 | #[derive(Default)] 157 | pub struct Pool { 158 | block: Option, 159 | by_hash: HashMap>, 160 | by_sender: HashMap, 161 | } 162 | 163 | impl Pool { 164 | /// Create a new pool instance. 165 | pub fn new() -> Self { 166 | Default::default() 167 | } 168 | 169 | /// Get status of this pool. 170 | pub fn status(&self) -> Status { 171 | Status { 172 | transactions: self.by_hash.len(), 173 | senders: self.by_sender.len(), 174 | } 175 | } 176 | 177 | /// Get transaction by its hash. 178 | pub fn get(&self, hash: H256) -> Option<&Transaction> { 179 | self.by_hash.get(&hash).map(|tx| &tx.inner) 180 | } 181 | 182 | fn import_one_rich(&mut self, tx: Arc) -> Result { 183 | self.block.ok_or(ImportError::NoCurrentBlock)?; 184 | 185 | match self.by_hash.entry(tx.hash) { 186 | Occupied(_) => { 187 | // Tx already there. 188 | Ok(false) 189 | } 190 | Vacant(tx_by_hash_entry) => { 191 | // This is a new transaction. 192 | let account_pool = self 193 | .by_sender 194 | .get_mut(&tx.sender) 195 | .ok_or(ImportError::NoState(tx.sender))?; 196 | 197 | if let Some(offset) = tx.nonce.checked_sub(account_pool.info.nonce) { 198 | // This transaction's nonce is account nonce or greater. 199 | if offset <= account_pool.txs.len() as u64 { 200 | // This transaction is between existing txs in the pool, or right the next one. 201 | 202 | // Compute balance after executing all txs before it. 203 | let cumulative_balance = account_pool 204 | .txs 205 | .iter() 206 | .take(offset as usize) 207 | .fold(account_pool.info.balance, |balance, tx| balance - tx.cost()); 208 | 209 | if cumulative_balance.checked_sub(tx.cost()).is_none() { 210 | return Err(ImportError::InsufficientBalance); 211 | } 212 | 213 | // If this is a replacement transaction, pick between this and old. 214 | if let Some(pooled_tx) = account_pool.txs.get_mut(offset as usize) { 215 | if pooled_tx.inner.gas_price >= tx.inner.gas_price { 216 | return Err(ImportError::FeeTooLow); 217 | } 218 | 219 | tx_by_hash_entry.insert(tx.clone()); 220 | assert!(self.by_hash.remove(&pooled_tx.hash).is_some()); 221 | *pooled_tx = tx; 222 | } else { 223 | // Not a replacement transaction. 224 | assert_eq!(tx.nonce, account_pool.txs.len() as u64); 225 | 226 | tx_by_hash_entry.insert(tx.clone()); 227 | account_pool.txs.push_back(tx); 228 | } 229 | 230 | // Compute the balance after executing remaining transactions. Select for removal those for which we do not have enough balance. 231 | for tx in account_pool 232 | .prune_insufficient_balance(Some((offset as usize, cumulative_balance))) 233 | { 234 | assert!(self.by_hash.remove(&tx.hash).is_some()); 235 | } 236 | 237 | Ok(true) 238 | } else { 239 | Err(ImportError::NonceGap) 240 | } 241 | } else { 242 | // Nonce lower than account, meaning it's stale. 243 | Err(ImportError::StaleTransaction) 244 | } 245 | } 246 | } 247 | } 248 | 249 | /// Import one transaction. 250 | pub fn import_one(&mut self, tx: Transaction) -> Result { 251 | let tx = Arc::new(RichTransaction::try_from(tx).map_err(ImportError::InvalidTransaction)?); 252 | 253 | self.import_one_rich(tx) 254 | } 255 | 256 | /// Import several transactions. 257 | pub fn import_many( 258 | &mut self, 259 | txs: impl Iterator, 260 | ) -> Vec> { 261 | let mut out = vec![]; 262 | 263 | let txs = txs.enumerate().fold( 264 | HashMap::>::new(), 265 | |mut txs, (idx, tx)| { 266 | out.push(Ok(false)); 267 | match RichTransaction::try_from(tx) { 268 | Err(e) => { 269 | out[idx] = Err(ImportError::InvalidTransaction(e)); 270 | txs 271 | } 272 | Ok(tx) => { 273 | match txs.entry(tx.sender).or_default().entry(tx.nonce) { 274 | std::collections::btree_map::Entry::Vacant(entry) => { 275 | entry.insert((idx, tx)); 276 | } 277 | std::collections::btree_map::Entry::Occupied(mut entry) => { 278 | let (old_idx, old_tx) = entry.get(); 279 | 280 | if tx.inner.gas_price > old_tx.inner.gas_price { 281 | out[*old_idx] = Err(ImportError::FeeTooLow); 282 | *entry.get_mut() = (idx, tx); 283 | } else { 284 | out[idx] = Err(ImportError::FeeTooLow); 285 | } 286 | } 287 | }; 288 | txs 289 | } 290 | } 291 | }, 292 | ); 293 | 294 | for (_sender, account_txs) in txs { 295 | let mut chain_error = false; 296 | let mut no_state = None; 297 | for (_nonce, (idx, tx)) in account_txs { 298 | out[idx] = { 299 | if chain_error { 300 | Err({ 301 | if let Some(address) = no_state { 302 | ImportError::NoState(address) 303 | } else { 304 | ImportError::NonceGap 305 | } 306 | }) 307 | } else { 308 | let res = self.import_one_rich(Arc::new(tx)); 309 | 310 | if let Err(e) = &res { 311 | // If we drop one account tx, then the remaining are invalid because of nonce gap. 312 | chain_error = true; 313 | 314 | if let ImportError::NoState(address) = e { 315 | no_state = Some(*address); 316 | } 317 | } 318 | 319 | res 320 | } 321 | }; 322 | } 323 | } 324 | 325 | out 326 | } 327 | 328 | /// Get current block this pool is at. 329 | pub fn current_block(&self) -> Option { 330 | self.block 331 | } 332 | 333 | /// Erase all transactions from the pool. 334 | pub fn erase(&mut self) { 335 | self.by_hash.clear(); 336 | self.by_sender.clear(); 337 | } 338 | 339 | /// Reset transaction pool to this block. 340 | pub fn reset(&mut self, block: Option) { 341 | self.erase(); 342 | self.block = block; 343 | } 344 | 345 | /// Add state of the account to this pool. 346 | pub fn add_account_state(&mut self, address: Address, info: AccountInfo) -> bool { 347 | if let Vacant(entry) = self.by_sender.entry(address) { 348 | entry.insert(AccountPool { 349 | info, 350 | txs: Default::default(), 351 | }); 352 | 353 | return true; 354 | } 355 | 356 | false 357 | } 358 | 359 | /// Drop account from this pool. 360 | pub fn drop_account(&mut self, address: Address) -> bool { 361 | if let Some(pool) = self.by_sender.remove(&address) { 362 | for tx in pool.txs { 363 | assert!(self.by_hash.remove(&tx.hash).is_some()); 364 | } 365 | return true; 366 | } 367 | 368 | false 369 | } 370 | 371 | /// Get account state in this pool. 372 | pub fn account_state(&self, address: Address) -> Option { 373 | self.by_sender.get(&address).map(|pool| pool.info) 374 | } 375 | 376 | fn apply_block_inner( 377 | &mut self, 378 | block: BlockHeader, 379 | account_diffs: &HashMap, 380 | ) -> anyhow::Result<()> { 381 | let current_block = if let Some(b) = self.block { 382 | b 383 | } else { 384 | return Ok(()); 385 | }; 386 | 387 | if block.parent != current_block.hash { 388 | bail!( 389 | "block gap detected: attempted to apply {} which is not child of {}", 390 | block.parent, 391 | current_block.hash 392 | ); 393 | } 394 | 395 | for (address, account_diff) in account_diffs { 396 | // Only do something if we actually have pool for this sender. 397 | if let Occupied(mut address_entry) = self.by_sender.entry(*address) { 398 | match account_diff { 399 | AccountDiff::Changed(new_info) => { 400 | let pool = address_entry.get_mut(); 401 | let nonce_diff = new_info 402 | .nonce 403 | .checked_sub(pool.info.nonce) 404 | .ok_or_else(|| anyhow!("nonce can only move forward!"))?; 405 | 406 | for _ in 0..nonce_diff { 407 | if let Some(tx) = pool.txs.pop_front() { 408 | assert!(self.by_hash.remove(&tx.hash).is_some()); 409 | } 410 | } 411 | 412 | // update info 413 | pool.info = *new_info; 414 | // More expensive tx could have squeezed in - we have to recheck our balance. 415 | for tx in pool.prune_insufficient_balance(None) { 416 | assert!(self.by_hash.remove(&tx.hash).is_some()); 417 | } 418 | 419 | if pool.txs.is_empty() { 420 | address_entry.remove(); 421 | } 422 | } 423 | AccountDiff::Deleted => { 424 | for tx in address_entry.remove().txs { 425 | assert!(self.by_hash.remove(&tx.hash).is_some()); 426 | } 427 | } 428 | } 429 | } 430 | } 431 | 432 | Ok(()) 433 | } 434 | 435 | /// Apply state updates from new confirmed block. 436 | /// This should be called by the client when it confirms new block. 437 | pub fn apply_block( 438 | &mut self, 439 | block: BlockHeader, 440 | account_diffs: &HashMap, 441 | ) { 442 | if let Err(e) = self.apply_block_inner(block, account_diffs) { 443 | warn!( 444 | "Failed to apply block {}: {}. Resetting transaction pool.", 445 | block.hash, e 446 | ); 447 | 448 | self.erase(); 449 | } 450 | self.block = Some(block); 451 | } 452 | 453 | fn revert_block_inner( 454 | &mut self, 455 | block: BlockHeader, 456 | _reverted_txs: Vec, 457 | ) -> anyhow::Result<()> { 458 | let current_block = if let Some(b) = self.block { 459 | b 460 | } else { 461 | return Ok(()); 462 | }; 463 | 464 | if current_block.parent != block.hash { 465 | bail!( 466 | "block gap detected: attempted to revert to {}, expected {}", 467 | block.hash, 468 | current_block.parent 469 | ); 470 | } 471 | 472 | Err(anyhow!("not implemented")) 473 | } 474 | 475 | /// Revert state updates from block that has become non-canonical, restore its transactions to the pool. 476 | /// This should be called by the client when it encounters a reorg. 477 | pub fn revert_block(&mut self, block: BlockHeader, reverted_txs: Vec) { 478 | if let Err(e) = self.revert_block_inner(block, reverted_txs) { 479 | warn!( 480 | "Failed to revert block {}: {}. Resetting transaction pool.", 481 | block.hash, e 482 | ); 483 | 484 | self.erase(); 485 | } 486 | self.block = Some(block); 487 | } 488 | 489 | /// Produce an iterator over all pending transactions in random order. 490 | pub fn pending_transactions(&self) -> impl Iterator { 491 | self.by_hash.values().map(|tx| &tx.inner) 492 | } 493 | 494 | /// Produce an iterator for pending transactions from `sender`, sorted by nonce in ascending order. 495 | pub fn pending_transactions_for_sender( 496 | &self, 497 | sender: Address, 498 | ) -> Option> { 499 | self.by_sender 500 | .get(&sender) 501 | .map(|pool| pool.txs.iter().map(|tx| &tx.inner)) 502 | } 503 | } 504 | 505 | #[cfg(test)] 506 | mod tests { 507 | use super::*; 508 | use ethereum::{TransactionAction, TransactionMessage, TransactionSignature}; 509 | use hex_literal::hex; 510 | use maplit::hashmap; 511 | use secp256k1::SecretKey; 512 | use std::iter::repeat_with; 513 | 514 | const CHAIN_ID: u64 = 1; 515 | 516 | const B_0: BlockHeader = BlockHeader { 517 | parent: H256(hex!( 518 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 519 | )), 520 | hash: H256(hex!( 521 | "0000000000000000000000000000000000000000000000000000000000000000" 522 | )), 523 | }; 524 | 525 | const B_1: BlockHeader = BlockHeader { 526 | parent: H256(hex!( 527 | "0000000000000000000000000000000000000000000000000000000000000000" 528 | )), 529 | hash: H256(hex!( 530 | "0000000000000000000000000000000000000000000000000000000000000001" 531 | )), 532 | }; 533 | 534 | fn cost(tx: &TransactionMessage) -> U256 { 535 | tx.gas_limit * tx.gas_price + tx.value 536 | } 537 | 538 | fn sign_tx(secret_key: &SecretKey, transaction_message: TransactionMessage) -> Transaction { 539 | let (rec, sig) = SECP256K1 540 | .sign_recoverable( 541 | &Message::from_slice(transaction_message.hash().as_bytes()).unwrap(), 542 | &secret_key, 543 | ) 544 | .serialize_compact(); 545 | 546 | let mut v = rec.to_i32() as u64; 547 | 548 | v += if let Some(n) = transaction_message.chain_id { 549 | 35 + n * 2 550 | } else { 551 | 27 552 | }; 553 | 554 | Transaction { 555 | nonce: transaction_message.nonce, 556 | gas_price: transaction_message.gas_price, 557 | gas_limit: transaction_message.gas_limit, 558 | action: transaction_message.action, 559 | value: transaction_message.value, 560 | input: transaction_message.input, 561 | signature: TransactionSignature::new( 562 | v, 563 | H256::from_slice(&sig[..32]), 564 | H256::from_slice(&sig[32..]), 565 | ) 566 | .unwrap(), 567 | } 568 | } 569 | 570 | fn sk2addr(sk: &SecretKey) -> Address { 571 | pk2addr(&PublicKey::from_secret_key(SECP256K1, &sk)) 572 | } 573 | 574 | fn fixture() -> (Pool, Vec) { 575 | let s = repeat_with(|| SecretKey::new(&mut secp256k1::rand::thread_rng())) 576 | .take(3) 577 | .collect::>(); 578 | 579 | let txs = vec![ 580 | TransactionMessage { 581 | nonce: 0.into(), 582 | gas_limit: 100000.into(), 583 | gas_price: 100000.into(), 584 | action: TransactionAction::Create, 585 | value: 0.into(), 586 | input: vec![], 587 | chain_id: Some(CHAIN_ID), 588 | }, 589 | TransactionMessage { 590 | nonce: 1.into(), 591 | gas_limit: 50000.into(), 592 | gas_price: 50000.into(), 593 | action: TransactionAction::Create, 594 | value: 0.into(), 595 | input: vec![], 596 | chain_id: Some(CHAIN_ID), 597 | }, 598 | TransactionMessage { 599 | nonce: 2.into(), 600 | gas_limit: 2000000.into(), 601 | gas_price: 10000.into(), 602 | action: TransactionAction::Create, 603 | value: 0.into(), 604 | input: vec![], 605 | chain_id: Some(CHAIN_ID), 606 | }, 607 | ]; 608 | 609 | let total_cost = txs.iter().fold(U256::zero(), |sum, tx| sum + cost(tx)); 610 | let base_account_info = AccountInfo { 611 | nonce: 0, 612 | balance: total_cost, 613 | }; 614 | 615 | let base_state = hashmap! { 616 | sk2addr(&s[0]) => base_account_info, 617 | sk2addr(&s[1]) => base_account_info, 618 | sk2addr(&s[2]) => base_account_info, 619 | }; 620 | 621 | let mut pool = Pool::new(); 622 | pool.reset(Some(B_0)); 623 | 624 | for (&address, &state) in &base_state { 625 | pool.add_account_state(address, state); 626 | } 627 | 628 | for sk in &s { 629 | for tx in &txs { 630 | let tx = sign_tx(sk, tx.clone()); 631 | let rich_tx = RichTransaction::try_from(tx.clone()).unwrap(); 632 | assert_eq!(rich_tx.sender, sk2addr(sk)); 633 | println!("signing and importing {}/{}", sk, rich_tx.hash); 634 | assert!(pool.import_one(tx).unwrap()); 635 | } 636 | } 637 | 638 | (pool, s) 639 | } 640 | 641 | #[test] 642 | fn parse_transaction() { 643 | let raw = hex!("f86e821d82850525dbd38082520894ce96e38eeff1c972f8f2851a7275b79b13b9a0498805e148839955f4008025a0ab1d3780cf338d1f86f42181ed13cd6c7fee7911a942c7583d36c9c83f5ec419a04984933928fac4b3242b9184aed633cc848f6a11d42af743f262ccf6592b8f71"); 644 | 645 | let res = RichTransaction::try_from(rlp::decode::(&raw).unwrap()).unwrap(); 646 | 647 | assert_eq!( 648 | &hex::encode(res.hash.0), 649 | "560ab39fe63d8fbbf688f0ebb6cfcacee4ce2ae8d85b096aa04c22a7c4c438af" 650 | ); 651 | 652 | assert_eq!( 653 | &hex::encode(res.sender.as_bytes()), 654 | "3123c4396f1306678f382c186aee2dccce44f72c" 655 | ); 656 | } 657 | 658 | #[test] 659 | fn basic() { 660 | fixture(); 661 | } 662 | 663 | #[test] 664 | fn import_multiple() { 665 | let (pool, sk) = fixture(); 666 | 667 | let mut new_pool = Pool::new(); 668 | new_pool.reset(pool.current_block()); 669 | for sk in &sk { 670 | let address = sk2addr(sk); 671 | assert!(new_pool.add_account_state(address, pool.account_state(address).unwrap())) 672 | } 673 | 674 | for res in new_pool.import_many(pool.pending_transactions().cloned()) { 675 | assert!(res.unwrap()) 676 | } 677 | } 678 | 679 | #[test] 680 | fn replace_transaction() { 681 | let (mut pool, sk) = fixture(); 682 | 683 | let sk = &sk[0]; 684 | 685 | let mut new_pool_txs = pool 686 | .pending_transactions_for_sender(sk2addr(sk)) 687 | .unwrap() 688 | .cloned() 689 | .collect::>(); 690 | 691 | // No balance for this one 692 | new_pool_txs.pop().unwrap(); 693 | 694 | let replaced_tx = new_pool_txs.pop().unwrap(); 695 | 696 | let replacement = sign_tx( 697 | sk, 698 | TransactionMessage { 699 | nonce: replaced_tx.nonce, 700 | gas_price: replaced_tx.gas_price + 1, 701 | gas_limit: replaced_tx.gas_limit, 702 | action: replaced_tx.action, 703 | value: replaced_tx.value, 704 | input: replaced_tx.input, 705 | chain_id: Some(CHAIN_ID), 706 | }, 707 | ); 708 | 709 | new_pool_txs.push(replacement.clone()); 710 | 711 | assert!(pool.import_one(replacement).unwrap()); 712 | 713 | assert_eq!( 714 | new_pool_txs, 715 | pool.pending_transactions_for_sender(sk2addr(sk)) 716 | .unwrap() 717 | .cloned() 718 | .collect::>() 719 | ); 720 | } 721 | 722 | #[test] 723 | fn apply_block() { 724 | let (mut pool, sk) = fixture(); 725 | 726 | let mut account_infos = HashMap::new(); 727 | for sk in &sk { 728 | let mut pool_txs = pool 729 | .pending_transactions_for_sender(sk2addr(sk)) 730 | .unwrap() 731 | .cloned() 732 | .collect::>(); 733 | 734 | let last_tx: Transaction = pool_txs.pop().unwrap(); 735 | 736 | let new_info = AccountInfo { 737 | nonce: last_tx.nonce.as_u64().checked_sub(1).unwrap_or_default(), 738 | balance: cost(&last_tx.into()), 739 | }; 740 | account_infos.insert(sk2addr(sk), new_info); 741 | } 742 | 743 | pool.apply_block( 744 | B_1, 745 | &account_infos 746 | .iter() 747 | .map(|(&addr, &state)| (addr, AccountDiff::Changed(state))) 748 | .collect(), 749 | ); 750 | 751 | for (address, new_info) in account_infos { 752 | assert_eq!(pool.account_state(address).unwrap(), new_info); 753 | } 754 | } 755 | } 756 | --------------------------------------------------------------------------------