github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/src/journal/chain_commit_state.rs (about) 1 /* 2 * Copyright 2018 Intel Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * ------------------------------------------------------------------------------ 16 */ 17 18 use std::collections::HashSet; 19 20 use cpython; 21 use cpython::ObjectProtocol; 22 23 use batch::Batch; 24 25 pub struct TransactionCommitCache { 26 committed: HashSet<String>, 27 28 blockstore: cpython::PyObject, 29 } 30 31 impl TransactionCommitCache { 32 pub fn new(blockstore: cpython::PyObject) -> Self { 33 TransactionCommitCache { 34 committed: HashSet::new(), 35 blockstore, 36 } 37 } 38 39 pub fn add(&mut self, transaction_id: String) { 40 self.committed.insert(transaction_id); 41 } 42 43 pub fn add_batch(&mut self, batch: &Batch) { 44 batch 45 .transactions 46 .iter() 47 .for_each(|txn| self.add(txn.header_signature.clone())); 48 } 49 50 pub fn remove(&mut self, transaction_id: &str) { 51 self.committed.remove(transaction_id); 52 } 53 54 pub fn remove_batch(&mut self, batch: &Batch) { 55 batch 56 .transactions 57 .iter() 58 .for_each(|txn| self.remove(txn.header_signature.as_str())); 59 } 60 61 pub fn contains(&self, transaction_id: &str) -> bool { 62 self.committed.contains(transaction_id) || self.blockstore_has_txn(transaction_id) 63 } 64 65 fn blockstore_has_txn(&self, transaction_id: &str) -> bool { 66 let gil = cpython::Python::acquire_gil(); 67 let py = gil.python(); 68 self.blockstore 69 .call_method(py, "has_transaction", (transaction_id,), None) 70 .unwrap() 71 .extract::<bool>(py) 72 .unwrap() 73 } 74 }