Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ impl<'x> OpenBlock<'x> {
pub fn push_transaction<C: FindDoubleVoteHandler>(
&mut self,
tx: VerifiedTransaction,
h: Option<TxHash>,
client: &C,
parent_block_number: BlockNumber,
parent_block_timestamp: u64,
Expand All @@ -168,7 +167,7 @@ impl<'x> OpenBlock<'x> {
self.block.header.timestamp(),
) {
Ok(()) => {
self.block.transactions_set.insert(h.unwrap_or(hash));
self.block.transactions_set.insert(hash);
self.block.transactions.push(tx);
None
}
Expand All @@ -194,7 +193,7 @@ impl<'x> OpenBlock<'x> {
parent_block_timestamp: u64,
) -> Result<(), Error> {
for tx in transactions {
self.push_transaction(tx.clone(), None, client, parent_block_number, parent_block_timestamp)?;
self.push_transaction(tx.clone(), client, parent_block_number, parent_block_timestamp)?;
}
Ok(())
}
Expand All @@ -208,7 +207,7 @@ impl<'x> OpenBlock<'x> {
}

/// Turn this into a `ClosedBlock`.
fn close_impl(&mut self) -> Result<(), Error> {
pub fn close(mut self) -> Result<ClosedBlock, Error> {
if let Err(e) = self.engine.on_close_block(&mut self.block) {
warn!("Encountered error on closing the block: {}", e);
return Err(e)
Expand All @@ -223,13 +222,6 @@ impl<'x> OpenBlock<'x> {
let vset = vset_raw.create_compact_validator_set();
self.block.header.set_next_validator_set_hash(vset.hash());

Ok(())
}

/// Turn this into a `ClosedBlock`.
pub fn close(mut self) -> Result<ClosedBlock, Error> {
self.close_impl()?;

if self.block.header.transactions_root() == &BLAKE_NULL_RLP {
self.block.header.set_transactions_root(skewed_merkle_root(
BLAKE_NULL_RLP,
Expand Down
2 changes: 1 addition & 1 deletion core/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl TestBlockChainClient {
seqs: RwLock::new(HashMap::new()),
storage: RwLock::new(HashMap::new()),
queue_size: AtomicUsize::new(0),
miner: Arc::new(Miner::with_scheme(&scheme, db)),
miner: Arc::new(Miner::with_scheme_for_test(&scheme, db)),
scheme,
latest_block_timestamp: RwLock::new(10_000_000),
history: RwLock::new(None),
Expand Down
46 changes: 17 additions & 29 deletions core/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub struct Miner {

sealing_enabled: AtomicBool,

accounts: Option<Arc<AccountProvider>>,
accounts: Arc<AccountProvider>,
malicious_users: RwLock<HashSet<Address>>,
immune_users: RwLock<HashSet<Address>>,
}
Expand All @@ -107,20 +107,20 @@ impl Miner {
pub fn new(
options: MinerOptions,
scheme: &Scheme,
accounts: Option<Arc<AccountProvider>>,
accounts: Arc<AccountProvider>,
db: Arc<dyn KeyValueDB>,
) -> Arc<Self> {
Arc::new(Self::new_raw(options, scheme, accounts, db))
}

pub fn with_scheme(scheme: &Scheme, db: Arc<dyn KeyValueDB>) -> Self {
Self::new_raw(Default::default(), scheme, None, db)
pub fn with_scheme_for_test(scheme: &Scheme, db: Arc<dyn KeyValueDB>) -> Self {
Self::new_raw(Default::default(), scheme, AccountProvider::transient_provider(), db)
}

fn new_raw(
options: MinerOptions,
scheme: &Scheme,
accounts: Option<Arc<AccountProvider>>,
accounts: Arc<AccountProvider>,
db: Arc<dyn KeyValueDB>,
) -> Self {
let mem_limit = options.mem_pool_memory_limit.unwrap_or_else(usize::max_value);
Expand Down Expand Up @@ -183,15 +183,11 @@ impl Miner {
self.immune_users.write().insert(signer_address);
}

let origin = self
.accounts
.as_ref()
.and_then(|accounts| match accounts.has_public(&signer_public) {
Ok(true) => Some(TxOrigin::Local),
Ok(false) => None,
Err(_) => None,
})
.unwrap_or(default_origin);
let origin = if self.accounts.has_public(&signer_public).unwrap_or_default() {
TxOrigin::Local
} else {
default_origin
};

if self.malicious_users.read().contains(&signer_address) {
// FIXME: just to skip, think about another way.
Expand Down Expand Up @@ -324,8 +320,7 @@ impl Miner {
let hash = tx.hash();
let start = Instant::now();
// Check whether transaction type is allowed for sender
let result =
open_block.push_transaction(tx, None, chain, parent_header.number(), parent_header.timestamp());
let result = open_block.push_transaction(tx, chain, parent_header.number(), parent_header.timestamp());

match result {
// already have transaction - ignore
Expand Down Expand Up @@ -407,19 +402,12 @@ impl MinerService for Miner {
self.params.write().author = address;

if self.engine_type().need_signer_key() {
if let Some(ref ap) = self.accounts {
ctrace!(MINER, "Set author to {:?}", address);
// Sign test message
ap.get_unlocked_account(&address)?.sign(&Default::default())?;
self.engine.set_signer(ap.clone(), address);
Ok(())
} else {
cwarn!(MINER, "No account provider");
Err(AccountProviderError::NotFound)
}
} else {
Ok(())
ctrace!(MINER, "Set author to {:?}", address);
// Sign test message
self.accounts.get_unlocked_account(&address)?.sign(&Default::default())?;
self.engine.set_signer(Arc::clone(&self.accounts), address);
}
Ok(())
}

fn get_author_address(&self) -> Address {
Expand Down Expand Up @@ -714,7 +702,7 @@ pub mod test {
fn check_add_transactions_result_idx() {
let db = Arc::new(kvdb_memorydb::create(NUM_COLUMNS.unwrap()));
let scheme = Scheme::new_test();
let miner = Arc::new(Miner::with_scheme(&scheme, db.clone()));
let miner = Arc::new(Miner::with_scheme_for_test(&scheme, db.clone()));

let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db.clone());
let client = generate_test_client(db, Arc::clone(&miner), &scheme).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion foundry/run_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn new_miner(
ap: Arc<AccountProvider>,
db: Arc<dyn KeyValueDB>,
) -> Result<Arc<Miner>, String> {
let miner = Miner::new(config.miner_options()?, scheme, Some(ap), db);
let miner = Miner::new(config.miner_options()?, scheme, ap, db);

match miner.engine_type() {
EngineType::PBFT => match &config.mining.engine_signer {
Expand Down