Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
1958: Problem: (fix crypto-com#1828)abci info endpoint returns uncommitted state r=tomtau a=linfeng-crypto

solution: get info from storage.


Co-authored-by: linfeng <linfeng@crypto.com>
  • Loading branch information
bors[bot] and linfeng-crypto committed Jul 21, 2020
2 parents 2ac81fa + 004c923 commit e0527f7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
5 changes: 4 additions & 1 deletion chain-abci/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use chain_core::init::config::NetworkParameters;
use chain_core::state::account::PunishmentKind;
use chain_core::state::tendermint::{BlockHeight, TendermintValidatorAddress, TendermintVotePower};
use chain_core::tx::TxAux;
use parity_scale_codec::Decode;

fn get_version() -> String {
format!(
Expand Down Expand Up @@ -68,7 +69,9 @@ impl<T: EnclaveProxy> abci::Application for ChainNodeApp<T> {
let mut resp = ResponseInfo::new();
resp.app_version = chain_core::APP_VERSION;
resp.version = get_version();
if let Some(app_state) = &self.last_state {
if let Some(raw) = chain_storage::get_last_app_state(&self.storage) {
let app_state =
ChainNodeState::decode(&mut raw.as_slice()).expect("decode chain node state");
resp.last_block_app_hash = app_state.last_apphash.to_vec();
resp.last_block_height = app_state.last_block_height.value().try_into().unwrap();
resp.data = serde_json::to_string(&app_state).expect("serialize app state to json");
Expand Down
47 changes: 39 additions & 8 deletions chain-abci/tests/abci_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,15 @@ fn staking_query_should_return_an_account() {
);
}

fn block_commit(app: &mut ChainNodeApp<MockClient>, tx: TxAux, block_height: i64) {
fn block_commit_with_check(app: &mut ChainNodeApp<MockClient>, tx: TxAux, block_height: i64) {
let r = RequestInfo::default();
let info_1 = app.info(&r);
let app_last_state_1 = app.last_state.clone().unwrap();
let app_last_block_height_1 = app_last_state_1.last_block_height.value();
let app_last_hash_1 = hex::encode(app_last_state_1.last_apphash);
assert_eq!(info_1.last_block_height as u64, app_last_block_height_1);
assert_eq!(hex::encode(&info_1.last_block_app_hash), app_last_hash_1);

let mut creq = RequestCheckTx::default();
creq.set_tx(tx.encode());
println!("checktx: {:?}", app.check_tx(&creq));
Expand All @@ -849,9 +857,31 @@ fn block_commit(app: &mut ChainNodeApp<MockClient>, tx: TxAux, block_height: i64
let mut breq = RequestEndBlock::default();
breq.set_height(block_height);
println!("endblock: {:?}", app.end_block(&breq));
let info_2 = app.info(&r);
let app_last_state_2 = app.last_state.clone().unwrap();
let app_last_block_height_2 = app_last_state_2.last_block_height.value();
let app_last_hash_2 = hex::encode(app_last_state_2.last_apphash);
// app hash didn't changed
assert_eq!(info_1.last_block_app_hash, info_2.last_block_app_hash);
// the uncommited last block height increased, but the one in the app info is not.
assert_eq!(info_2.last_block_height as u64 + 1, app_last_block_height_2);
assert_eq!(hex::encode(&info_2.last_block_app_hash), app_last_hash_2);
assert_eq!(
hex::encode(&info_2.last_block_app_hash),
hex::encode(&info_1.last_block_app_hash)
);
// commit block, app hash changed during commit
println!("commit: {:?}", app.commit(&RequestCommit::default()));
// after commit, block height increased, and app_hash changed
let info_3 = app.info(&r);
let app_last_state_3 = app.last_state.clone().unwrap();
let app_last_block_height_3 = app_last_state_3.last_block_height.value();
let app_last_hash_3 = hex::encode(app_last_state_3.last_apphash);
assert_eq!(hex::encode(&info_3.last_block_app_hash), app_last_hash_3);
// app hash changed
assert_ne!(info_2.last_block_app_hash, info_3.last_block_app_hash);
assert_eq!(info_3.last_block_height as u64, app_last_block_height_3);
}

pub fn get_account(
account_address: &RedeemAddress,
app: &ChainNodeApp<MockClient>,
Expand Down Expand Up @@ -918,7 +948,8 @@ fn all_valid_tx_types_should_commit() {
assert!(account.unbonded > Coin::zero());
assert_eq!(account.nonce, 0);
}
block_commit(&mut app, withdrawtx, 1);
block_commit_with_check(&mut app, withdrawtx, 1);

{
let account = get_account(&addr, &app).expect("acount not exist");
assert_eq!(account.unbonded, Coin::zero());
Expand Down Expand Up @@ -953,7 +984,7 @@ fn all_valid_tx_types_should_commit() {
let spent_utxos = get_tx_meta(&txid, &app);
assert!(!spent_utxos.any());
}
block_commit(&mut app, transfertx, 2);
block_commit_with_check(&mut app, transfertx, 2);
{
let spent_utxos0 = get_tx_meta(&txid, &app);
assert!(spent_utxos0[0] && !spent_utxos0[1]);
Expand Down Expand Up @@ -985,7 +1016,7 @@ fn all_valid_tx_types_should_commit() {
assert_eq!(account.bonded, Coin::zero());
assert_eq!(account.nonce, 1);
}
block_commit(&mut app, depositx, 3);
block_commit_with_check(&mut app, depositx, 3);
{
let spent_utxos0 = get_tx_meta(&txid, &app);
assert!(spent_utxos0[0] && spent_utxos0[1]);
Expand Down Expand Up @@ -1019,7 +1050,7 @@ fn all_valid_tx_types_should_commit() {
let account = get_account(&addr2, &app);
assert!(account.is_none());
}
block_commit(&mut app, depositx, 4);
block_commit_with_check(&mut app, depositx, 4);
{
let spent_utxos0 = get_tx_meta(txid, &app);
assert!(spent_utxos0[0] && spent_utxos0[1] && spent_utxos0[2]);
Expand Down Expand Up @@ -1052,7 +1083,7 @@ fn all_valid_tx_types_should_commit() {
);
assert_eq!(account.nonce, 1);
}
block_commit(&mut app, nodejointx, 5);
block_commit_with_check(&mut app, nodejointx, 5);
{
let account = get_account(&addr, &app).expect("account not exist");
assert!(account.node_meta.is_some());
Expand Down Expand Up @@ -1082,7 +1113,7 @@ fn all_valid_tx_types_should_commit() {
assert_eq!(account.nonce, 2);
account.bonded
};
block_commit(&mut app, unbondtx, 6);
block_commit_with_check(&mut app, unbondtx, 6);
{
let account = get_account(&addr, &app).expect("account not exist");
assert_eq!(account.unbonded, Coin::unit());
Expand Down
6 changes: 4 additions & 2 deletions integration-tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ function wait_port() {
}

function wait_service() {
# ra-sp-server
wait_port 8989 &&
if [ $BUILD_MODE == "sgx" ]; then
# ra-sp-server
wait_port 8989
fi
# tendermint rpc of first node
wait_port $TENDERMINT_RPC_PORT
}
Expand Down

0 comments on commit e0527f7

Please sign in to comment.