Skip to content

Commit

Permalink
ledger: make prepare_proposal and process_proposal stateless
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Aug 4, 2022
1 parent 67eee8c commit 43aa815
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 82 deletions.
3 changes: 3 additions & 0 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ where
&mut self,
req: shim::request::FinalizeBlock,
) -> Result<shim::response::FinalizeBlock> {
// reset gas meter before we start
self.gas_meter.reset();

let mut response = shim::response::FinalizeBlock::default();
// begin the next block and check if a new epoch began
let (height, new_epoch) =
Expand Down
10 changes: 3 additions & 7 deletions apps/src/lib/node/ledger/shell/prepare_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ where
/// the proposal is rejected (unless we can simply overwrite
/// them in the next block).
pub fn prepare_proposal(
&mut self,
&self,
req: RequestPrepareProposal,
) -> response::PrepareProposal {
// We can safely reset meter, because if the block is rejected,
// we'll reset again on the next proposal, until the
// proposal is accepted
self.gas_meter.reset();
let txs = if let ShellMode::Validator { .. } = self.mode {
// TODO: This should not be hardcoded
let privkey = <EllipticCurve as PairingEngine>::G2Affine::prime_subgroup_generator();
Expand Down Expand Up @@ -127,7 +123,7 @@ mod test_prepare_proposal {
/// proposed block.
#[test]
fn test_prepare_proposal_rejects_non_wrapper_tx() {
let (mut shell, _) = TestShell::new();
let (shell, _) = TestShell::new();
let tx = Tx::new(
"wasm_code".as_bytes().to_owned(),
Some("transaction_data".as_bytes().to_owned()),
Expand All @@ -148,7 +144,7 @@ mod test_prepare_proposal {
/// we simply exclude it from the proposal
#[test]
fn test_error_in_processing_tx() {
let (mut shell, _) = TestShell::new();
let (shell, _) = TestShell::new();
let keypair = gen_keypair();
let tx = Tx::new(
"wasm_code".as_bytes().to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion apps/src/lib/node/ledger/shell/process_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
/// included txs violates the order decided upon in the previous
/// block.
pub fn process_proposal(
&mut self,
&self,
req: RequestProcessProposal,
) -> ResponseProcessProposal {
let tx_results = self.process_txs(&req.txs);
Expand Down
105 changes: 31 additions & 74 deletions apps/src/lib/node/ledger/shims/abcipp_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ use crate::config;
#[derive(Debug)]
pub struct AbcippShim {
service: Shell,
/// This is `Some` only when `ProcessProposal` request is received before
/// `FinalizeBlock`, which is optional for non-validator nodes.
processed_txs: Option<Vec<ProcessedTx>>,
shell_recv: std::sync::mpsc::Receiver<(
Req,
tokio::sync::oneshot::Sender<Result<Resp, BoxError>>,
Expand Down Expand Up @@ -55,7 +52,6 @@ impl AbcippShim {
vp_wasm_compilation_cache,
tx_wasm_compilation_cache,
),
processed_txs: None,
shell_recv,
},
AbciService { shell_send },
Expand All @@ -67,65 +63,30 @@ impl AbcippShim {
pub fn run(mut self) {
while let Ok((req, resp_sender)) = self.shell_recv.recv() {
let resp = match req {
Req::ProcessProposal(proposal) => {
let txs = proposal.txs.clone();
self.service
.call(Request::ProcessProposal(proposal))
.map_err(Error::from)
.and_then(|res| match res {
Response::ProcessProposal(resp) => {
self.processed_txs =
Some(Vec::with_capacity(txs.len()));
let processed_txs =
self.processed_txs.as_mut().unwrap();
for (result, tx) in resp
.tx_results
.iter()
.map(TxResult::from)
.zip(txs.into_iter())
{
processed_txs
.push(ProcessedTx { tx, result });
}
Ok(Resp::ProcessProposal(resp))
}
_ => unreachable!(),
})
}
Req::ProcessProposal(proposal) => self
.service
.call(Request::ProcessProposal(proposal))
.map_err(Error::from)
.and_then(|res| match res {
Response::ProcessProposal(resp) => {
Ok(Resp::ProcessProposal(resp))
}
_ => unreachable!(),
}),
Req::FinalizeBlock(block) => {
let (txs, processing_results) =
match self.processed_txs.take() {
Some(processed_txs) => {
// When there are processed_txs from
// `ProcessProposal`, we don't need to add
// processing
// results again
(processed_txs, None)
}
None => {
// When there are no processed txs, it means
// that `ProcessProposal` request has not been
// received and so we need to process
// transactions first in the same way as
// `ProcessProposal`.
let unprocessed_txs = block.txs.clone();
let processing_results =
self.service.process_txs(&block.txs);
let mut txs =
Vec::with_capacity(unprocessed_txs.len());
for (result, tx) in processing_results
.iter()
.map(TxResult::from)
.zip(unprocessed_txs.into_iter())
{
txs.push(ProcessedTx { tx, result });
}
// Then we also have to add the processing
// result events to the `FinalizeBlock`
// tx_results
(txs, Some(processing_results))
}
};
// Process transactions first in the same way as
// `ProcessProposal`.
let unprocessed_txs = block.txs.clone();
let processing_results =
self.service.process_txs(&block.txs);
let mut txs = Vec::with_capacity(unprocessed_txs.len());
for (result, tx) in processing_results
.iter()
.map(TxResult::from)
.zip(unprocessed_txs.into_iter())
{
txs.push(ProcessedTx { tx, result });
}

let mut finalize_req: FinalizeBlock = block.into();
finalize_req.txs = txs;
Expand All @@ -138,19 +99,15 @@ impl AbcippShim {
let mut resp: ResponseFinalizeBlock =
resp.into();

// Add processing results, if any
if let Some(processing_results) =
processing_results
// Add processing results
for (tx_result, processing_result) in resp
.tx_results
.iter_mut()
.zip(processing_results)
{
for (tx_result, processing_result) in resp
.tx_results
.iter_mut()
.zip(processing_results)
{
tx_result
.events
.extend(processing_result.events);
}
tx_result
.events
.extend(processing_result.events);
}

Ok(Resp::FinalizeBlock(resp))
Expand Down

0 comments on commit 43aa815

Please sign in to comment.