Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make process proposal stateless #656

Merged
merged 1 commit into from
Oct 20, 2022
Merged
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
77 changes: 43 additions & 34 deletions apps/src/lib/node/ledger/shims/abcipp_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use tower::Service;

use super::super::Shell;
use super::abcipp_shim_types::shim::request::{FinalizeBlock, ProcessedTx};
#[cfg(not(feature = "abcipp"))]
use super::abcipp_shim_types::shim::TxBytes;
use super::abcipp_shim_types::shim::{Error, Request, Response};
use crate::config;
#[cfg(not(feature = "abcipp"))]
Expand All @@ -30,7 +32,8 @@ pub struct AbcippShim {
service: Shell,
#[cfg(not(feature = "abcipp"))]
begin_block_request: Option<RequestBeginBlock>,
processed_txs: Vec<ProcessedTx>,
#[cfg(not(feature = "abcipp"))]
delivered_txs: Vec<TxBytes>,
shell_recv: std::sync::mpsc::Receiver<(
Req,
tokio::sync::oneshot::Sender<Result<Resp, BoxError>>,
Expand Down Expand Up @@ -63,7 +66,8 @@ impl AbcippShim {
),
#[cfg(not(feature = "abcipp"))]
begin_block_request: None,
processed_txs: vec![],
#[cfg(not(feature = "abcipp"))]
delivered_txs: vec![],
shell_recv,
},
AbciService { shell_send },
Expand All @@ -73,11 +77,8 @@ impl AbcippShim {
#[cfg(not(feature = "abcipp"))]
/// Get the hash of the txs in the block
pub fn get_hash(&self) -> Hash {
let bytes: Vec<u8> = self
.processed_txs
.iter()
.flat_map(|processed| processed.tx.clone())
.collect();
let bytes: Vec<u8> =
self.delivered_txs.iter().flat_map(Clone::clone).collect();
hash_tx(bytes.as_slice())
}

Expand All @@ -86,32 +87,28 @@ 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) => {
let response =
Ok(Resp::ProcessProposal((&resp).into()));
for (result, tx) in resp
.tx_results
.into_iter()
.zip(txs.into_iter())
{
self.processed_txs
.push(ProcessedTx { tx, result });
}
response
}
_ => 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).into()))
}
_ => unreachable!(),
}),
#[cfg(feature = "abcipp")]
Req::FinalizeBlock(block) => {
let mut txs = vec![];
std::mem::swap(&mut txs, &mut self.processed_txs);
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
.into_iter()
.zip(unprocessed_txs.into_iter())
{
txs.push(ProcessedTx { tx, result });
}
let mut finalize_req: FinalizeBlock = block.into();
finalize_req.txs = txs;
self.service
Expand All @@ -131,11 +128,23 @@ impl AbcippShim {
Ok(Resp::BeginBlock(Default::default()))
}
#[cfg(not(feature = "abcipp"))]
Req::DeliverTx(_) => Ok(Resp::DeliverTx(Default::default())),
Req::DeliverTx(tx) => {
self.delivered_txs.push(tx.tx);
Ok(Resp::DeliverTx(Default::default()))
}
#[cfg(not(feature = "abcipp"))]
Req::EndBlock(_) => {
let mut txs = vec![];
std::mem::swap(&mut txs, &mut self.processed_txs);
let processing_results =
self.service.process_txs(&self.delivered_txs);
let mut txs = Vec::with_capacity(self.delivered_txs.len());
let mut delivered = vec![];
std::mem::swap(&mut self.delivered_txs, &mut delivered);
for (result, tx) in processing_results
.into_iter()
.zip(delivered.into_iter())
{
txs.push(ProcessedTx { tx, result });
}
let mut end_block_request: FinalizeBlock =
self.begin_block_request.take().unwrap().into();
let hash = self.get_hash();
Expand Down