Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Ametsuchi: handle insertion failure #2213

Merged
merged 3 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 15 additions & 7 deletions irohad/ametsuchi/impl/storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,14 @@ namespace iroha {
void StorageImpl::prepareBlock(std::unique_ptr<TemporaryWsv> wsv) {
auto &wsv_impl = static_cast<TemporaryWsvImpl &>(*wsv);
if (not prepared_blocks_enabled_) {
log_->warn("prepared block are not enabled");
log_->warn("prepared blocks are not enabled");
return;
}
if (not block_is_prepared) {
if (block_is_prepared) {
log_->warn(
"Refusing to add new prepared state, because there already is one. "
"Multiple prepared states are not yet supported.");
} else {
soci::session &sql = *wsv_impl.sql_;
try {
sql << "PREPARE TRANSACTION '" + prepared_block_name_ + "';";
Expand Down Expand Up @@ -585,12 +589,16 @@ namespace iroha {
auto json_result = converter_->serialize(*block);
return json_result.match(
[this, &block](const expected::Value<std::string> &v) {
block_store_->add(block->height(), stringToBytes(v.value));
notifier_.get_subscriber().on_next(block);
return true;
if (block_store_->add(block->height(), stringToBytes(v.value))) {
notifier_.get_subscriber().on_next(block);
return true;
} else {
log_->error("Block insertion failed: {}", *block);
return false;
}
},
[this](const expected::Error<std::string> &e) {
log_->error(e.error);
[this, &block](const expected::Error<std::string> &e) {
log_->error("Block serialization failed: {}: {}", *block, e.error);
return false;
});
}
Expand Down
10 changes: 5 additions & 5 deletions test/module/irohad/ametsuchi/ametsuchi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ TEST_F(AmetsuchiTest, TestingWsvAfterCommitBlock) {
.signAndAddSignature(key)
.finish();

auto expected_block = createBlock({add_ast_tx}, 1, genesis_block->hash());
auto expected_block = createBlock({add_ast_tx}, 2, genesis_block->hash());

static auto wrapper =
make_test_subscriber<CallExact>(storage->on_commit(), 1);
Expand Down Expand Up @@ -614,9 +614,9 @@ TEST_F(PreparedBlockTest, PrepareBlockNoStateChanged) {
* @then state of the ledger is changed
*/
TEST_F(PreparedBlockTest, CommitPreparedStateChanged) {
shared_model::interface::Amount added_amount{"5.00"};
auto other_tx = createAddAsset("5.00");

auto block = createBlock({*initial_tx});
auto block = createBlock({other_tx}, 2);

auto result = temp_wsv->apply(*initial_tx);
ASSERT_FALSE(framework::expected::err(result));
Expand All @@ -641,7 +641,7 @@ TEST_F(PreparedBlockTest, PrepareBlockCommitDifferentBlock) {
// tx which actually gets commited
auto other_tx = createAddAsset("10.00");

auto block = createBlock({other_tx});
auto block = createBlock({other_tx}, 2);

auto result = temp_wsv->apply(*initial_tx);
ASSERT_TRUE(framework::expected::val(result));
Expand All @@ -665,7 +665,7 @@ TEST_F(PreparedBlockTest, CommitPreparedFailsAfterCommit) {
// tx which actually gets commited
auto other_tx = createAddAsset("10.00");

auto block = createBlock({other_tx});
auto block = createBlock({other_tx}, 2);

auto result = temp_wsv->apply(*initial_tx);
ASSERT_FALSE(framework::expected::err(result));
Expand Down