Skip to content

Commit

Permalink
abci: crude logic and tests for finalize_block
Browse files Browse the repository at this point in the history
  • Loading branch information
mzabaluev committed May 9, 2023
1 parent c08249f commit f169a93
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
2 changes: 1 addition & 1 deletion abci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ description = """
[[bin]]
name = "kvstore-rs"
path = "src/application/kvstore/main.rs"
required-features = [ "binary", "kvstore-app" ]
required-features = [ "binary", "client", "kvstore-app" ]

[features]
default = ["flex-error/std", "flex-error/eyre_tracer"]
Expand Down
52 changes: 45 additions & 7 deletions abci/src/application/kvstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::{

use bytes::BytesMut;
use tendermint_proto::v0_38::abci::{
RequestCheckTx, RequestInfo, RequestQuery, ResponseCheckTx, ResponseCommit, ResponseInfo,
ResponseQuery,
Event, EventAttribute, RequestCheckTx, RequestFinalizeBlock, RequestInfo, RequestQuery,
ResponseCheckTx, ResponseCommit, ResponseFinalizeBlock, ResponseInfo, ResponseQuery,
};
use tracing::{debug, info};

Expand All @@ -20,9 +20,10 @@ use crate::{codec::MAX_VARINT_LENGTH, Application, Error};
/// store - the [`KeyValueStoreDriver`].
///
/// ## Example
/// ```rust
///
/// ```
/// use tendermint_abci::{KeyValueStoreApp, ServerBuilder, ClientBuilder};
/// use tendermint_proto::abci::{RequestEcho, RequestDeliverTx, RequestQuery};
/// use tendermint_proto::abci::{RequestEcho, RequestFinalizeBlock, RequestQuery};
///
/// // Create our key/value store application
/// let (app, driver) = KeyValueStoreApp::new();
Expand All @@ -46,8 +47,9 @@ use crate::{codec::MAX_VARINT_LENGTH, Application, Error};
///
/// // Deliver a transaction and then commit the transaction
/// client
/// .deliver_tx(RequestDeliverTx {
/// tx: "test-key=test-value".into(),
/// .finalize_block(RequestFinalizeBlock {
/// txs: vec!["test-key=test-value".into()],
/// ..Default::default()
/// })
/// .unwrap();
/// client.commit().unwrap();
Expand Down Expand Up @@ -188,7 +190,43 @@ impl Application for KeyValueStoreApp {
}
}

// FIXME: override some logic in finalize_block?
fn finalize_block(&self, request: RequestFinalizeBlock) -> ResponseFinalizeBlock {
let mut events = Vec::new();
for tx in request.txs {
let tx = std::str::from_utf8(&tx).unwrap();
let tx_parts = tx.split('=').collect::<Vec<&str>>();
let (key, value) = if tx_parts.len() == 2 {
(tx_parts[0], tx_parts[1])
} else {
(tx, tx)
};
let _ = self.set(key, value).unwrap();
events.push(Event {
r#type: "app".to_string(),
attributes: vec![
EventAttribute {
key: "key".to_owned(),
value: key.to_owned(),
index: true,
},
EventAttribute {
key: "index_key".to_owned(),
value: "index is working".to_owned(),
index: true,
},
EventAttribute {
key: "noindex_key".to_owned(),
value: "index is working".to_owned(),
index: false,
},
],
});
}
ResponseFinalizeBlock {
events,
..Default::default()
}
}
}

/// Manages key/value store state.
Expand Down
14 changes: 7 additions & 7 deletions abci/tests/kvstore_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod kvstore_app_integration {
use std::thread;

use tendermint_abci::{ClientBuilder, KeyValueStoreApp, ServerBuilder};
use tendermint_proto::v0_38::abci::{RequestEcho, RequestQuery};
use tendermint_proto::v0_38::abci::{RequestEcho, RequestFinalizeBlock, RequestQuery};

#[test]
fn happy_path() {
Expand All @@ -23,12 +23,12 @@ mod kvstore_app_integration {
.unwrap();
assert_eq!(res.message, "Hello ABCI!");

// FIXME: adapt to 0.38
// client
// .deliver_tx(RequestDeliverTx {
// tx: "test-key=test-value".into(),
// })
// .unwrap();
client
.finalize_block(RequestFinalizeBlock {
txs: vec!["test-key=test-value".into()],
..Default::default()
})
.unwrap();
client.commit().unwrap();

let res = client
Expand Down

0 comments on commit f169a93

Please sign in to comment.