Skip to content

Commit f43c4c2

Browse files
Seulgi Kimsgkim126
authored andcommitted
Modified the chain to include only successful transactions
1 parent 41e99a2 commit f43c4c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1276
-1684
lines changed

core/src/block.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ use ckey::Address;
2121
use cmerkle::skewed_merkle_root;
2222
use cstate::{FindActionHandler, StateDB, StateError, StateWithCache, TopLevelState};
2323
use ctypes::errors::HistoryError;
24-
use ctypes::invoice::Invoice;
2524
use ctypes::machine::{LiveBlock, Transactions};
2625
use ctypes::util::unexpected::Mismatch;
2726
use cvm::ChainTimeInfo;
2827
use primitives::{Bytes, H256};
2928
use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp};
3029

30+
use super::invoice::Invoice;
3131
use crate::consensus::CodeChainEngine;
3232
use crate::error::{BlockError, Error};
3333
use crate::header::{Header, Seal};
@@ -174,12 +174,26 @@ impl<'x> OpenBlock<'x> {
174174
return Err(HistoryError::TransactionAlreadyImported.into())
175175
}
176176

177-
let invoice = self.block.state.apply(&tx, &tx.hash(), &tx.signer_public(), client)?;
178-
179-
self.block.transactions_set.insert(h.unwrap_or_else(|| tx.hash()));
180-
self.block.transactions.push(tx);
181-
self.block.invoices.push(invoice);
182-
Ok(())
177+
let hash = tx.hash();
178+
let tracker = tx.tracker();
179+
let error = match self.block.state.apply(&tx, &hash, &tx.signer_public(), client) {
180+
Ok(()) => {
181+
self.block.transactions_set.insert(h.unwrap_or(hash));
182+
self.block.transactions.push(tx);
183+
None
184+
}
185+
Err(err) => Some(err),
186+
};
187+
self.block.invoices.push(Invoice {
188+
hash,
189+
tracker,
190+
error: error.clone().map(|err| err.to_string()),
191+
});
192+
193+
match error {
194+
None => Ok(()),
195+
Some(err) => Err(err.into()),
196+
}
183197
}
184198

185199
/// Push transactions onto the block.
@@ -204,7 +218,7 @@ impl<'x> OpenBlock<'x> {
204218
}
205219

206220
/// Turn this into a `ClosedBlock`.
207-
pub fn close(mut self, parent_transactions_root: H256, parent_results_root: H256) -> Result<ClosedBlock, Error> {
221+
pub fn close(mut self, parent_transactions_root: H256) -> Result<ClosedBlock, Error> {
208222
let unclosed_state = self.block.state.clone();
209223

210224
if let Err(e) = self.engine.on_close_block(&mut self.block) {
@@ -220,10 +234,6 @@ impl<'x> OpenBlock<'x> {
220234
self.block.transactions.iter().map(|e| e.rlp_bytes()),
221235
));
222236
self.block.header.set_state_root(state_root);
223-
self.block.header.set_results_root(skewed_merkle_root(
224-
parent_results_root,
225-
self.block.invoices.iter().map(|invoice| invoice.to_bool().rlp_bytes()),
226-
));
227237

228238
Ok(ClosedBlock {
229239
block: self.block,
@@ -232,11 +242,7 @@ impl<'x> OpenBlock<'x> {
232242
}
233243

234244
/// Turn this into a `LockedBlock`.
235-
pub fn close_and_lock(
236-
mut self,
237-
parent_transactions_root: H256,
238-
parent_results_root: H256,
239-
) -> Result<LockedBlock, Error> {
245+
pub fn close_and_lock(mut self, parent_transactions_root: H256) -> Result<LockedBlock, Error> {
240246
if let Err(e) = self.engine.on_close_block(&mut self.block) {
241247
warn!("Encountered error on closing the block: {}", e);
242248
return Err(e)
@@ -256,16 +262,6 @@ impl<'x> OpenBlock<'x> {
256262
self.block.header.transactions_root(),
257263
&skewed_merkle_root(parent_transactions_root, self.block.transactions.iter().map(Encodable::rlp_bytes),)
258264
);
259-
if self.block.header.results_root().is_zero() || self.block.header.results_root() == &BLAKE_NULL_RLP {
260-
self.block.header.set_results_root(skewed_merkle_root(
261-
parent_results_root,
262-
self.block.invoices.iter().map(|i| i.to_bool().rlp_bytes()),
263-
));
264-
}
265-
debug_assert_eq!(
266-
self.block.header.results_root(),
267-
&skewed_merkle_root(parent_results_root, self.block.invoices.iter().map(|i| i.to_bool().rlp_bytes()),)
268-
);
269265
self.block.header.set_state_root(state_root);
270266

271267
Ok(LockedBlock {
@@ -449,7 +445,7 @@ pub fn enact<C: ChainTimeInfo + FindActionHandler>(
449445
b.populate_from(header);
450446
b.push_transactions(transactions, client)?;
451447

452-
b.close_and_lock(*parent.transactions_root(), *parent.results_root())
448+
b.close_and_lock(*parent.transactions_root())
453449
}
454450

455451
#[cfg(test)]
@@ -466,8 +462,7 @@ mod tests {
466462
let db = scheme.ensure_genesis_state(get_temp_state_db()).unwrap();
467463
let b = OpenBlock::try_new(&*scheme.engine, db, &genesis_header, Address::default(), vec![], false).unwrap();
468464
let parent_transactions_root = *genesis_header.transactions_root();
469-
let parent_results_root = *genesis_header.results_root();
470-
let b = b.close_and_lock(parent_transactions_root, parent_results_root).unwrap();
465+
let b = b.close_and_lock(parent_transactions_root).unwrap();
471466
let _ = b.seal(&*scheme.engine, vec![]);
472467
}
473468
}

core/src/blockchain/blockchain.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use std::mem;
1818
use std::sync::Arc;
1919

20-
use ctypes::invoice::{BlockInvoices, Invoice};
2120
use ctypes::BlockNumber;
2221
use kvdb::{DBTransaction, KeyValueDB};
2322
use parking_lot::RwLock;
@@ -26,7 +25,7 @@ use rlp::RlpStream;
2625

2726
use super::block_info::BestBlockChanged;
2827
use super::body_db::{BodyDB, BodyProvider};
29-
use super::extras::{BlockDetails, EpochTransitions, TransactionAddress, TransactionAddresses, EPOCH_KEY_PREFIX};
28+
use super::extras::{BlockDetails, EpochTransitions, TransactionAddress, EPOCH_KEY_PREFIX};
3029
use super::headerchain::{HeaderChain, HeaderProvider};
3130
use super::invoice_db::{InvoiceDB, InvoiceProvider};
3231
use super::route::{tree_route, ImportRoute};
@@ -35,6 +34,7 @@ use crate::consensus::epoch::{PendingTransition as PendingEpochTransition, Trans
3534
use crate::consensus::CodeChainEngine;
3635
use crate::db::{self, Readable, Writable};
3736
use crate::encoded;
37+
use crate::invoice::Invoice;
3838
use crate::transaction::LocalizedTransaction;
3939
use crate::views::{BlockView, HeaderView};
4040

@@ -146,7 +146,9 @@ impl BlockChain {
146146
self.headerchain.insert_header(batch, &new_header, engine);
147147
self.body_db.insert_body(batch, &new_block);
148148
self.body_db.update_best_block(batch, &best_block_changed);
149-
self.invoice_db.insert_invoice(batch, &new_block_hash, invoices);
149+
for invoice in invoices {
150+
self.invoice_db.insert_invoice(batch, invoice.hash, invoice.tracker, invoice.error);
151+
}
150152

151153
if let Some(best_block_hash) = best_block_changed.new_best_hash() {
152154
let mut pending_best_block_hash = self.pending_best_block_hash.write();
@@ -563,8 +565,8 @@ impl BodyProvider for BlockChain {
563565
self.body_db.transaction_address(hash)
564566
}
565567

566-
fn transaction_addresses_by_tracker(&self, tracker: &H256) -> Option<TransactionAddresses> {
567-
self.body_db.transaction_addresses_by_tracker(tracker)
568+
fn transaction_address_by_tracker(&self, tracker: &H256) -> Option<TransactionAddress> {
569+
self.body_db.transaction_address_by_tracker(tracker)
568570
}
569571

570572
fn block_body(&self, hash: &H256) -> Option<encoded::Body> {
@@ -574,18 +576,16 @@ impl BodyProvider for BlockChain {
574576

575577
impl InvoiceProvider for BlockChain {
576578
/// Returns true if invoices for given hash is known
577-
fn is_known_invoice(&self, hash: &H256) -> bool {
578-
self.invoice_db.is_known_invoice(hash)
579+
fn is_known_error_hint(&self, hash: &H256) -> bool {
580+
self.invoice_db.is_known_error_hint(hash)
579581
}
580582

581-
/// Get invoices of block with given hash.
582-
fn block_invoices(&self, hash: &H256) -> Option<BlockInvoices> {
583-
self.invoice_db.block_invoices(hash)
583+
fn error_hints_by_tracker(&self, tracker: &H256) -> Vec<(H256, Option<String>)> {
584+
self.invoice_db.error_hints_by_tracker(tracker)
584585
}
585586

586-
/// Get transaction invoice.
587-
fn invoice(&self, address: &TransactionAddress) -> Option<Invoice> {
588-
self.invoice_db.invoice(address)
587+
fn error_hint(&self, hash: &H256) -> Option<String> {
588+
self.invoice_db.error_hint(hash)
589589
}
590590
}
591591

core/src/blockchain/body_db.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub trait BodyProvider {
283283
/// Get the address of parcel with given hash.
284284
fn transaction_address(&self, hash: &H256) -> Option<TransactionAddress>;
285285

286-
fn transaction_addresses_by_tracker(&self, tracker: &H256) -> Option<TransactionAddresses>;
286+
fn transaction_address_by_tracker(&self, tracker: &H256) -> Option<TransactionAddress>;
287287

288288
/// Get the block body (uncles and parcels).
289289
fn block_body(&self, hash: &H256) -> Option<encoded::Body>;
@@ -300,8 +300,9 @@ impl BodyProvider for BodyDB {
300300
Some(result)
301301
}
302302

303-
fn transaction_addresses_by_tracker(&self, tracker: &H256) -> Option<TransactionAddresses> {
304-
Some(self.db.read_with_cache(db::COL_EXTRA, &mut *self.transaction_address_cache.lock(), tracker)?)
303+
fn transaction_address_by_tracker(&self, tracker: &H256) -> Option<TransactionAddress> {
304+
let addresses = self.db.read_with_cache(db::COL_EXTRA, &mut *self.transaction_address_cache.lock(), tracker)?;
305+
addresses.into_iter().next()
305306
}
306307

307308
/// Get block body data

core/src/blockchain/extras.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use std::io::Write;
1818
use std::ops::{self, Add, AddAssign, Deref, Sub, SubAssign};
1919

20-
use ctypes::invoice::BlockInvoices;
2120
use ctypes::BlockNumber;
2221
use kvdb::PREFIX_LEN as DB_PREFIX_LEN;
2322
use primitives::{H256, H264, U256};
@@ -37,12 +36,10 @@ enum ExtrasIndex {
3736
ParcelAddress = 2,
3837
/// Transaction address index
3938
TransactionAddress = 3,
40-
/// Block invoices index
41-
BlockInvoices = 4,
4239
/// Epoch transition data index.
43-
EpochTransitions = 5,
40+
EpochTransitions = 4,
4441
/// Pending epoch transition data index.
45-
PendingEpochTransition = 6,
42+
PendingEpochTransition = 5,
4643
}
4744

4845
fn with_index(hash: &H256, i: ExtrasIndex) -> H264 {
@@ -93,14 +90,6 @@ impl Key<TransactionAddress> for H256 {
9390
}
9491
}
9592

96-
impl Key<BlockInvoices> for H256 {
97-
type Target = H264;
98-
99-
fn key(&self) -> H264 {
100-
with_index(self, ExtrasIndex::BlockInvoices)
101-
}
102-
}
103-
10493
impl Key<TransactionAddresses> for H256 {
10594
type Target = H264;
10695

0 commit comments

Comments
 (0)