Skip to content

Commit

Permalink
Change store::block::successor to return an std::optional to signal i…
Browse files Browse the repository at this point in the history
…f there is a successor block.
  • Loading branch information
clemahieu committed Mar 13, 2024
1 parent 73384d8 commit b897c69
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
7 changes: 1 addition & 6 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1253,12 +1253,7 @@ std::optional<nano::block_hash> nano::ledger::successor (store::transaction cons
{
if (!root_a.previous ().is_zero ())
{
auto result = store.block.successor (transaction_a, root_a.previous ());
if (result.is_zero ())
{
return std::nullopt;
}
return result;
return store.block.successor (transaction_a, root_a.previous ());
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion nano/store/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <nano/store/iterator.hpp>

#include <functional>
#include <optional>

namespace nano
{
Expand All @@ -28,7 +29,7 @@ class block
public:
virtual void put (store::write_transaction const &, nano::block_hash const &, nano::block const &) = 0;
virtual void raw_put (store::write_transaction const &, std::vector<uint8_t> const &, nano::block_hash const &) = 0;
virtual nano::block_hash successor (store::transaction const &, nano::block_hash const &) const = 0;
virtual std::optional<nano::block_hash> successor (store::transaction const &, nano::block_hash const &) const = 0;
virtual void successor_clear (store::write_transaction const &, nano::block_hash const &) = 0;
virtual std::shared_ptr<nano::block> get (store::transaction const &, nano::block_hash const &) const = 0;
virtual std::shared_ptr<nano::block> random (store::transaction const &) = 0;
Expand Down
6 changes: 5 additions & 1 deletion nano/store/lmdb/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void nano::store::lmdb::block::raw_put (store::write_transaction const & transac
store.release_assert_success (status);
}

nano::block_hash nano::store::lmdb::block::successor (store::transaction const & transaction_a, nano::block_hash const & hash_a) const
std::optional<nano::block_hash> nano::store::lmdb::block::successor (store::transaction const & transaction_a, nano::block_hash const & hash_a) const
{
nano::store::lmdb::db_val value;
block_raw_get (transaction_a, hash_a, value);
Expand All @@ -67,6 +67,10 @@ nano::block_hash nano::store::lmdb::block::successor (store::transaction const &
{
result.clear ();
}
if (result.is_zero ())
{
return std::nullopt;
}
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion nano/store/lmdb/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class block : public nano::store::block
explicit block (nano::store::lmdb::component & store_a);
void put (store::write_transaction const & transaction_a, nano::block_hash const & hash_a, nano::block const & block_a) override;
void raw_put (store::write_transaction const & transaction_a, std::vector<uint8_t> const & data, nano::block_hash const & hash_a) override;
nano::block_hash successor (store::transaction const & transaction_a, nano::block_hash const & hash_a) const override;
std::optional<nano::block_hash> successor (store::transaction const & transaction_a, nano::block_hash const & hash_a) const override;
void successor_clear (store::write_transaction const & transaction_a, nano::block_hash const & hash_a) override;
std::shared_ptr<nano::block> get (store::transaction const & transaction_a, nano::block_hash const & hash_a) const override;
std::shared_ptr<nano::block> random (store::transaction const & transaction_a) override;
Expand Down
6 changes: 5 additions & 1 deletion nano/store/rocksdb/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void nano::store::rocksdb::block::raw_put (store::write_transaction const & tran
store.release_assert_success (status);
}

nano::block_hash nano::store::rocksdb::block::successor (store::transaction const & transaction_a, nano::block_hash const & hash_a) const
std::optional<nano::block_hash> nano::store::rocksdb::block::successor (store::transaction const & transaction_a, nano::block_hash const & hash_a) const
{
nano::store::rocksdb::db_val value;
block_raw_get (transaction_a, hash_a, value);
Expand All @@ -67,6 +67,10 @@ nano::block_hash nano::store::rocksdb::block::successor (store::transaction cons
{
result.clear ();
}
if (result.is_zero ())
{
return std::nullopt;
}
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion nano/store/rocksdb/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class block : public nano::store::block
explicit block (nano::store::rocksdb::component & store_a);
void put (store::write_transaction const & transaction_a, nano::block_hash const & hash_a, nano::block const & block_a) override;
void raw_put (store::write_transaction const & transaction_a, std::vector<uint8_t> const & data, nano::block_hash const & hash_a) override;
nano::block_hash successor (store::transaction const & transaction_a, nano::block_hash const & hash_a) const override;
std::optional<nano::block_hash> successor (store::transaction const & transaction_a, nano::block_hash const & hash_a) const override;
void successor_clear (store::write_transaction const & transaction_a, nano::block_hash const & hash_a) override;
std::shared_ptr<nano::block> get (store::transaction const & transaction_a, nano::block_hash const & hash_a) const override;
std::shared_ptr<nano::block> random (store::transaction const & transaction_a) override;
Expand Down

0 comments on commit b897c69

Please sign in to comment.