-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(katana): start block timer only if there txs (#2950)
Only start the block interval timer when we have executed txs. This means we no longer produce empty blocks after every fixed interval.
- Loading branch information
Showing
5 changed files
with
138 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use arbitrary::{Arbitrary, Unstructured}; | ||
use futures::pin_mut; | ||
use katana_chain_spec::DEV; | ||
use katana_executor::implementation::noop::NoopExecutorFactory; | ||
use katana_primitives::transaction::{ExecutableTx, InvokeTx}; | ||
use katana_primitives::Felt; | ||
use katana_provider::providers::db::DbProvider; | ||
use tokio::time; | ||
|
||
use super::*; | ||
use crate::backend::gas_oracle::GasOracle; | ||
use crate::backend::storage::Blockchain; | ||
|
||
fn test_backend() -> Arc<Backend<NoopExecutorFactory>> { | ||
let chain_spec = Arc::new(DEV.clone()); | ||
let provider = DbProvider::new_ephemeral(); | ||
let executor_factory = NoopExecutorFactory::new(); | ||
let blockchain = Blockchain::new_with_chain(provider, chain_spec.as_ref()).unwrap(); | ||
let gas_oracle = GasOracle::fixed(Default::default(), Default::default()); | ||
Arc::new(Backend::new(chain_spec, blockchain, gas_oracle, executor_factory)) | ||
} | ||
|
||
#[tokio::test] | ||
async fn interval_initial_state() { | ||
let backend = test_backend(); | ||
let producer = IntervalBlockProducer::new(backend, Some(1000)); | ||
|
||
assert!(producer.timer.is_none()); | ||
assert!(producer.queued.is_empty()); | ||
assert!(producer.ongoing_mining.is_none()); | ||
assert!(producer.ongoing_execution.is_none()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn interval_force_mine_without_transactions() { | ||
let backend = test_backend(); | ||
|
||
let mut producer = IntervalBlockProducer::new(backend.clone(), None); | ||
producer.force_mine(); | ||
|
||
let latest_num = backend.blockchain.provider().latest_number().unwrap(); | ||
assert_eq!(latest_num, 1); | ||
} | ||
|
||
#[tokio::test] | ||
async fn interval_mine_after_timer() { | ||
let backend = test_backend(); | ||
let mut producer = IntervalBlockProducer::new(backend.clone(), Some(1000)); | ||
// Initial state | ||
assert!(producer.timer.is_none()); | ||
|
||
producer.queued.push_back(vec![dummy_transaction()]); | ||
|
||
let stream = producer; | ||
pin_mut!(stream); | ||
|
||
// Process the transaction, the timer should be automatically started | ||
let _ = stream.next().await; | ||
assert!(stream.timer.is_some()); | ||
|
||
// Advance time to trigger mining | ||
time::sleep(Duration::from_secs(1)).await; | ||
let result = stream.next().await.expect("should mine block").unwrap(); | ||
|
||
assert_eq!(result.block_number, 1); | ||
assert_eq!(backend.blockchain.provider().latest_number().unwrap(), 1); | ||
|
||
// Final state | ||
assert!(stream.timer.is_none()); | ||
} | ||
|
||
// Helper functions to create test transactions | ||
fn dummy_transaction() -> ExecutableTxWithHash { | ||
fn tx() -> ExecutableTx { | ||
let data = (0..InvokeTx::size_hint(0).0).map(|_| rand::random::<u8>()).collect::<Vec<u8>>(); | ||
let mut unstructured = Unstructured::new(&data); | ||
ExecutableTx::Invoke(InvokeTx::arbitrary(&mut unstructured).unwrap()) | ||
} | ||
|
||
ExecutableTxWithHash { hash: Felt::ONE, transaction: tx() } | ||
} |