Skip to content

Commit

Permalink
- Add logging in stateless validation
Browse files Browse the repository at this point in the history
 - Add logging in stateful validation
  • Loading branch information
muratovv committed Aug 6, 2017
1 parent df4ad04 commit f71a20c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions irohad/validation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ target_link_libraries(stateful_validator
ed25519
rxcpp
model
logger
)

add_library(stateless_validator
impl/stateless_validator_impl.cpp
)
target_link_libraries(stateless_validator
model
logger
)

add_library(chain_validator
Expand Down
10 changes: 8 additions & 2 deletions irohad/validation/impl/stateful_validator_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
namespace iroha {
namespace validation {

StatefulValidatorImpl::StatefulValidatorImpl() {
log_ = logger::log("SFV");
}

model::Proposal StatefulValidatorImpl::validate(
const model::Proposal &proposal,
ametsuchi::TemporaryWsv &temporaryWsv) {
log_->info("transactions in proposal: {}", proposal.transactions.size());
auto checking_transaction = [&temporaryWsv](auto &tx, auto &executor,
auto &query) {
auto account = temporaryWsv.getAccount(tx.creator_account_id);
Expand All @@ -45,7 +50,7 @@ namespace iroha {
std::begin(tx.commands), std::end(tx.commands),
[&query, &account, &executor](auto &command) {
return command->validate(query, account.value()) &&
command->execute(query, executor);
command->execute(query, executor);
});

};
Expand All @@ -66,7 +71,8 @@ namespace iroha {
model::Proposal validated_proposal(
std::accumulate(txs.begin(), txs.end(), valid, filter));
validated_proposal.height = proposal.height;

log_->info("transactions in verified proposal: {}",
validated_proposal.transactions.size());
return validated_proposal;
}

Expand Down
8 changes: 8 additions & 0 deletions irohad/validation/impl/stateful_validator_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#define IROHA_STATEFUL_VALIDATIOR_IMPL_HPP

#include "validation/stateful_validator.hpp"

#include "logger/logger.hpp"

namespace iroha {
namespace validation {

Expand All @@ -26,6 +29,9 @@ namespace iroha {
*/
class StatefulValidatorImpl : public StatefulValidator {
public:

StatefulValidatorImpl();

/**
* Function perform stateful validation on proposal
* and return proposal with valid transactions
Expand All @@ -37,6 +43,8 @@ namespace iroha {
*/
model::Proposal validate(const model::Proposal& proposal,
ametsuchi::TemporaryWsv& temporaryWsv) override;
private:
logger::Logger log_;
};
} // namespace validation
} // namespace iroha
Expand Down
23 changes: 20 additions & 3 deletions irohad/validation/impl/stateless_validator_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,68 @@

#include "validation/impl/stateless_validator_impl.hpp"
#include <chrono>
#include <utility>

namespace iroha {
namespace validation {
StatelessValidatorImpl::StatelessValidatorImpl(
std::shared_ptr<model::ModelCryptoProvider> crypto_provider)
: crypto_provider_(crypto_provider) {}
: crypto_provider_(std::move(crypto_provider)) {
log_ = logger::log("SLV");
}

bool StatelessValidatorImpl::validate(
const model::Transaction& transaction) const {
// signatures are correct
if (!crypto_provider_->verify(transaction)) return false;
if (!crypto_provider_->verify(transaction)){
log_->warn("crypto verification broken");
return false;
}

// time between creation and validation of tx
uint64_t now = static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count());

if (now - transaction.created_ts > MAX_DELAY) {
log_->warn("timestamp broken: too old");
return false;
}

// tx is not sent from future
// todo make future gap for passing timestamp, like with old timestamps
if (now < transaction.created_ts) {
log_->warn("timestamp broken: send from future");
return false;
}
log_->info("transaction validated");
return true;
}

bool StatelessValidatorImpl::validate(std::shared_ptr<const model::Query> query) const {
// signatures are correct
if (!crypto_provider_->verify(query)) return false;
if (!crypto_provider_->verify(query)){
log_->warn("crypto verification broken");
return false;
}

// time between creation and validation of the query
uint64_t now = static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count());

if (now - query->created_ts > MAX_DELAY) {
log_->warn("timestamp broken: too old");
return false;
}

// query is not sent from future
// todo make future gap for passing timestamp, like with old timestamps
if (now < query->created_ts) {
log_->warn("timestamp broken: send from future");
return false;
}
log_->info("query validated");
return true;
}
}
Expand Down
4 changes: 4 additions & 0 deletions irohad/validation/impl/stateless_validator_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "model/model_crypto_provider.hpp"
#include "validation/stateless_validator.hpp"

#include "logger/logger.hpp"

namespace iroha {
namespace validation {

Expand All @@ -35,6 +37,8 @@ namespace iroha {
static constexpr uint64_t MAX_DELAY =
1000 * 3600 * 24; // max-delay between tx creation and validation
std::shared_ptr<model::ModelCryptoProvider> crypto_provider_;

logger::Logger log_;
};
}
}
Expand Down

0 comments on commit f71a20c

Please sign in to comment.