Skip to content

Commit

Permalink
Compile time db indices validation (#702)
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan authored Aug 31, 2021
1 parent 49d39fc commit 1a03b0c
Show file tree
Hide file tree
Showing 27 changed files with 156 additions and 211 deletions.
12 changes: 6 additions & 6 deletions src/flushablestorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ class CStorageIteratorWrapper {
std::unique_ptr<CStorageKVIterator> it;

void UpdateValidity() {
valid = it->Valid() && BytesToDbType(it->Key(), key) && key.first == By::prefix;
valid = it->Valid() && BytesToDbType(it->Key(), key) && key.first == By::prefix();
}

struct Resolver {
Expand Down Expand Up @@ -413,7 +413,7 @@ class CStorageIteratorWrapper {
UpdateValidity();
}
void Seek(const KeyType& newKey) {
key = std::make_pair(By::prefix, newKey);
key = std::make_pair(By::prefix(), newKey);
it->Seek(DbTypeToBytes(key));
UpdateValidity();
}
Expand Down Expand Up @@ -441,7 +441,7 @@ class CStorageView {
}
template<typename By, typename KeyType>
bool ExistsBy(const KeyType& key) const {
return Exists(std::make_pair(By::prefix, key));
return Exists(std::make_pair(By::prefix(), key));
}

template<typename KeyType, typename ValueType>
Expand All @@ -452,7 +452,7 @@ class CStorageView {
}
template<typename By, typename KeyType, typename ValueType>
bool WriteBy(const KeyType& key, const ValueType& value) {
return Write(std::make_pair(By::prefix, key), value);
return Write(std::make_pair(By::prefix(), key), value);
}

template<typename KeyType>
Expand All @@ -462,7 +462,7 @@ class CStorageView {
}
template<typename By, typename KeyType>
bool EraseBy(const KeyType& key) {
return Erase(std::make_pair(By::prefix, key));
return Erase(std::make_pair(By::prefix(), key));
}

template<typename KeyType, typename ValueType>
Expand All @@ -473,7 +473,7 @@ class CStorageView {
}
template<typename By, typename KeyType, typename ValueType>
bool ReadBy(const KeyType& key, ValueType& value) const {
return Read(std::make_pair(By::prefix, key), value);
return Read(std::make_pair(By::prefix(), key), value);
}
// second type of 'ReadBy' (may be 'GetBy'?)
template<typename By, typename ResultType, typename KeyType>
Expand Down
4 changes: 0 additions & 4 deletions src/masternodes/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

#include <masternodes/accounts.h>

/// @attention make sure that it does not overlap with those in masternodes.cpp/tokens.cpp/undos.cpp/accounts.cpp !!!
const unsigned char CAccountsView::ByBalanceKey::prefix = 'a';
const unsigned char CAccountsView::ByHeightKey::prefix = 'b';

void CAccountsView::ForEachBalance(std::function<bool(CScript const &, CTokenAmount const &)> callback, BalanceKey const & start)
{
ForEach<ByBalanceKey, BalanceKey, CAmount>([&callback] (BalanceKey const & key, CAmount val) {
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/accounts.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class CAccountsView : public virtual CStorageView
Res UpdateBalancesHeight(CScript const & owner, uint32_t height);

// tags
struct ByBalanceKey { static const unsigned char prefix; };
struct ByHeightKey { static const unsigned char prefix; };
struct ByBalanceKey { static constexpr uint8_t prefix() { return 'a'; } };
struct ByHeightKey { static constexpr uint8_t prefix() { return 'b'; } };

private:
Res SetBalance(CScript const & owner, CTokenAmount amount);
Expand Down
3 changes: 0 additions & 3 deletions src/masternodes/accountshistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#include <masternodes/masternodes.h>
#include <key_io.h>

/// @Note it's in own database
const unsigned char CAccountsHistoryView::ByAccountHistoryKey::prefix = 'h';

void CAccountsHistoryView::ForEachAccountHistory(std::function<bool(AccountHistoryKey const &, CLazySerialize<AccountHistoryValue>)> callback, AccountHistoryKey const & start)
{
ForEach<ByAccountHistoryKey, AccountHistoryKey, AccountHistoryValue>(callback, start);
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/accountshistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CAccountsHistoryView : public virtual CStorageView
void ForEachAccountHistory(std::function<bool(AccountHistoryKey const &, CLazySerialize<AccountHistoryValue>)> callback, AccountHistoryKey const & start = {});

// tags
struct ByAccountHistoryKey { static const unsigned char prefix; };
struct ByAccountHistoryKey { static constexpr uint8_t prefix() { return 'h'; } };
};

class CAccountHistoryStorage : public CAccountsHistoryView
Expand Down
2 changes: 0 additions & 2 deletions src/masternodes/gv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <masternodes/govvariables/lp_daily_dfi_reward.h>
#include <masternodes/govvariables/lp_splits.h>

const unsigned char CGovView::ByName::prefix = 'g';

Res CGovView::SetVariable(GovVariable const & var)
{
return WriteBy<ByName>(var.GetName(), var) ? Res::Ok() : Res::Err("can't write to DB");
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/gv.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CGovView : public virtual CStorageView
Res SetVariable(GovVariable const & var);
std::shared_ptr<GovVariable> GetVariable(std::string const &govKey) const;

struct ByName { static const unsigned char prefix; };
struct ByName { static constexpr uint8_t prefix() { return 'g'; } };
};


Expand Down
26 changes: 0 additions & 26 deletions src/masternodes/icxorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,6 @@
#include <rpc/util.h> /// AmountFromValue
#include <core_io.h> /// ValueFromAmount

/// @attention make sure that it does not overlap with other views !!!
const unsigned char CICXOrderView::ICXOrderCreationTx ::prefix = '1';
const unsigned char CICXOrderView::ICXMakeOfferCreationTx ::prefix = '2';
const unsigned char CICXOrderView::ICXSubmitDFCHTLCCreationTx ::prefix = '3';
const unsigned char CICXOrderView::ICXSubmitEXTHTLCCreationTx ::prefix = '4';
const unsigned char CICXOrderView::ICXClaimDFCHTLCCreationTx ::prefix = '5';
const unsigned char CICXOrderView::ICXCloseOrderCreationTx ::prefix = '6';
const unsigned char CICXOrderView::ICXCloseOfferCreationTx ::prefix = '7';

const unsigned char CICXOrderView::ICXOrderOpenKey ::prefix = 0x01;
const unsigned char CICXOrderView::ICXOrderCloseKey ::prefix = 0x02;
const unsigned char CICXOrderView::ICXMakeOfferOpenKey ::prefix = 0x03;
const unsigned char CICXOrderView::ICXMakeOfferCloseKey ::prefix = 0x04;
const unsigned char CICXOrderView::ICXSubmitDFCHTLCOpenKey ::prefix = 0x05;
const unsigned char CICXOrderView::ICXSubmitDFCHTLCCloseKey ::prefix = 0x06;
const unsigned char CICXOrderView::ICXSubmitEXTHTLCOpenKey ::prefix = 0x07;
const unsigned char CICXOrderView::ICXSubmitEXTHTLCCloseKey ::prefix = 0x08;
const unsigned char CICXOrderView::ICXClaimDFCHTLCKey ::prefix = 0x09;

const unsigned char CICXOrderView::ICXOrderStatus ::prefix = 0x0A;
const unsigned char CICXOrderView::ICXOfferStatus ::prefix = 0x0B;
const unsigned char CICXOrderView::ICXSubmitDFCHTLCStatus ::prefix = 0x0C;
const unsigned char CICXOrderView::ICXSubmitEXTHTLCStatus ::prefix = 0x0D;

const unsigned char CICXOrderView::ICXVariables ::prefix = 0x0F;

const uint32_t CICXOrder::DEFAULT_EXPIRY = 2880;
const uint8_t CICXOrder::TYPE_INTERNAL = 1;
const uint8_t CICXOrder::TYPE_EXTERNAL = 2;
Expand Down
48 changes: 24 additions & 24 deletions src/masternodes/icxorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,30 +449,30 @@ class CICXOrderView : public virtual CStorageView {
Res ICXSetTakerFeePerBTC(CAmount amount);
CAmount ICXGetTakerFeePerBTC();

struct ICXOrderCreationTx { static const unsigned char prefix; };
struct ICXMakeOfferCreationTx { static const unsigned char prefix; };
struct ICXSubmitDFCHTLCCreationTx { static const unsigned char prefix; };
struct ICXSubmitEXTHTLCCreationTx { static const unsigned char prefix; };
struct ICXClaimDFCHTLCCreationTx { static const unsigned char prefix; };
struct ICXCloseOrderCreationTx { static const unsigned char prefix; };
struct ICXCloseOfferCreationTx { static const unsigned char prefix; };

struct ICXOrderOpenKey { static const unsigned char prefix; };
struct ICXOrderCloseKey { static const unsigned char prefix; };
struct ICXMakeOfferOpenKey { static const unsigned char prefix; };
struct ICXMakeOfferCloseKey { static const unsigned char prefix; };
struct ICXSubmitDFCHTLCOpenKey { static const unsigned char prefix; };
struct ICXSubmitDFCHTLCCloseKey { static const unsigned char prefix; };
struct ICXSubmitEXTHTLCOpenKey { static const unsigned char prefix; };
struct ICXSubmitEXTHTLCCloseKey { static const unsigned char prefix; };
struct ICXClaimDFCHTLCKey { static const unsigned char prefix; };

struct ICXOrderStatus { static const unsigned char prefix; };
struct ICXOfferStatus { static const unsigned char prefix; };
struct ICXSubmitDFCHTLCStatus { static const unsigned char prefix; };
struct ICXSubmitEXTHTLCStatus { static const unsigned char prefix; };

struct ICXVariables { static const unsigned char prefix; };
struct ICXOrderCreationTx { static constexpr uint8_t prefix() { return '1'; } };
struct ICXMakeOfferCreationTx { static constexpr uint8_t prefix() { return '2'; } };
struct ICXSubmitDFCHTLCCreationTx { static constexpr uint8_t prefix() { return '3'; } };
struct ICXSubmitEXTHTLCCreationTx { static constexpr uint8_t prefix() { return '4'; } };
struct ICXClaimDFCHTLCCreationTx { static constexpr uint8_t prefix() { return '5'; } };
struct ICXCloseOrderCreationTx { static constexpr uint8_t prefix() { return '6'; } };
struct ICXCloseOfferCreationTx { static constexpr uint8_t prefix() { return '7'; } };

struct ICXOrderOpenKey { static constexpr uint8_t prefix() { return 0x01; } };
struct ICXOrderCloseKey { static constexpr uint8_t prefix() { return 0x02; } };
struct ICXMakeOfferOpenKey { static constexpr uint8_t prefix() { return 0x03; } };
struct ICXMakeOfferCloseKey { static constexpr uint8_t prefix() { return 0x04; } };
struct ICXSubmitDFCHTLCOpenKey { static constexpr uint8_t prefix() { return 0x05; } };
struct ICXSubmitDFCHTLCCloseKey { static constexpr uint8_t prefix() { return 0x06; } };
struct ICXSubmitEXTHTLCOpenKey { static constexpr uint8_t prefix() { return 0x07; } };
struct ICXSubmitEXTHTLCCloseKey { static constexpr uint8_t prefix() { return 0x08; } };
struct ICXClaimDFCHTLCKey { static constexpr uint8_t prefix() { return 0x09; } };

struct ICXOrderStatus { static constexpr uint8_t prefix() { return 0x0A; } };
struct ICXOfferStatus { static constexpr uint8_t prefix() { return 0x0B; } };
struct ICXSubmitDFCHTLCStatus { static constexpr uint8_t prefix() { return 0x0C; } };
struct ICXSubmitEXTHTLCStatus { static constexpr uint8_t prefix() { return 0x0D; } };

struct ICXVariables { static constexpr uint8_t prefix() { return 0x0F; } };
};

#endif // DEFI_MASTERNODES_ICXORDER_H
4 changes: 0 additions & 4 deletions src/masternodes/incentivefunding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

#include <masternodes/incentivefunding.h>

/// @attention make sure that it does not overlap with those in masternodes.cpp/tokens.cpp/undos.cpp/accounts.cpp !!!
const unsigned char CCommunityBalancesView::ById::prefix = 'F';


CAmount CCommunityBalancesView::GetCommunityBalance(CommunityAccountType account) const
{
CAmount val;
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/incentivefunding.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CCommunityBalancesView : public virtual CStorageView
Res SubCommunityBalance(CommunityAccountType account, CAmount amount);

// tags
struct ById { static const unsigned char prefix; };
struct ById { static constexpr uint8_t prefix() { return 'F'; } };
};

#endif //DEFI_MASTERNODES_INCENTIVEFUNDING_H
20 changes: 4 additions & 16 deletions src/masternodes/loan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@
#include <chainparams.h>
#include <masternodes/loan.h>

const unsigned char CLoanView::LoanSetCollateralTokenCreationTx ::prefix = 0x10;
const unsigned char CLoanView::LoanSetCollateralTokenKey ::prefix = 0x11;
const unsigned char CLoanView::LoanSchemeKey ::prefix = 0x12;
const unsigned char CLoanView::DefaultLoanSchemeKey ::prefix = 0x13;
const unsigned char CLoanView::DelayedLoanSchemeKey ::prefix = 0x14;
const unsigned char CLoanView::DestroyLoanSchemeKey ::prefix = 0x15;
const unsigned char CLoanView::LoanSetLoanTokenCreationTx ::prefix = 0x16;
const unsigned char CLoanView::LoanSetLoanTokenKey ::prefix = 0x17;
const unsigned char CLoanView::LoanInterestedRate ::prefix = 0x18;
const unsigned char CLoanView::LoanTokenAmount ::prefix = 0x19;
const unsigned char CLoanView::LoanLiquidationPenalty ::prefix = 0x20;

std::unique_ptr<CLoanView::CLoanSetCollateralTokenImpl> CLoanView::GetLoanSetCollateralToken(uint256 const & txid) const
{
auto collToken = ReadBy<LoanSetCollateralTokenCreationTx,CLoanSetCollateralTokenImpl>(txid);
Expand Down Expand Up @@ -137,15 +125,15 @@ void CLoanView::ForEachDelayedDestroyScheme(std::function<bool (const std::strin

Res CLoanView::StoreDefaultLoanScheme(const std::string& loanSchemeID)
{
Write(DefaultLoanSchemeKey::prefix, loanSchemeID);
Write(DefaultLoanSchemeKey::prefix(), loanSchemeID);

return Res::Ok();
}

boost::optional<std::string> CLoanView::GetDefaultLoanScheme()
{
std::string loanSchemeID;
if (Read(DefaultLoanSchemeKey::prefix, loanSchemeID)) {
if (Read(DefaultLoanSchemeKey::prefix(), loanSchemeID)) {
return loanSchemeID;
}

Expand Down Expand Up @@ -306,14 +294,14 @@ void CLoanView::ForEachLoanToken(std::function<bool(const CVaultId&, const CBala

Res CLoanView::SetLoanLiquidationPenalty(CAmount penalty)
{
Write(LoanLiquidationPenalty::prefix, penalty);
Write(LoanLiquidationPenalty::prefix(), penalty);
return Res::Ok();
}

CAmount CLoanView::GetLoanLiquidationPenalty()
{
CAmount penalty;
if (Read(LoanLiquidationPenalty::prefix, penalty)) {
if (Read(LoanLiquidationPenalty::prefix(), penalty)) {
return penalty;
}
return 5 * COIN / 100;
Expand Down
22 changes: 11 additions & 11 deletions src/masternodes/loan.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,17 @@ class CLoanView : public virtual CStorageView {
Res SetLoanLiquidationPenalty(CAmount penalty);
CAmount GetLoanLiquidationPenalty();

struct LoanSetCollateralTokenCreationTx { static const unsigned char prefix; };
struct LoanSetCollateralTokenKey { static const unsigned char prefix; };
struct LoanSetLoanTokenCreationTx { static const unsigned char prefix; };
struct LoanSetLoanTokenKey { static const unsigned char prefix; };
struct LoanSchemeKey { static const unsigned char prefix; };
struct DefaultLoanSchemeKey { static const unsigned char prefix; };
struct DelayedLoanSchemeKey { static const unsigned char prefix; };
struct DestroyLoanSchemeKey { static const unsigned char prefix; };
struct LoanInterestedRate { static const unsigned char prefix; };
struct LoanTokenAmount { static const unsigned char prefix; };
struct LoanLiquidationPenalty { static const unsigned char prefix; };
struct LoanSetCollateralTokenCreationTx { static constexpr uint8_t prefix() { return 0x10; } };
struct LoanSetCollateralTokenKey { static constexpr uint8_t prefix() { return 0x11; } };
struct LoanSetLoanTokenCreationTx { static constexpr uint8_t prefix() { return 0x12; } };
struct LoanSetLoanTokenKey { static constexpr uint8_t prefix() { return 0x13; } };
struct LoanSchemeKey { static constexpr uint8_t prefix() { return 0x14; } };
struct DefaultLoanSchemeKey { static constexpr uint8_t prefix() { return 0x15; } };
struct DelayedLoanSchemeKey { static constexpr uint8_t prefix() { return 0x16; } };
struct DestroyLoanSchemeKey { static constexpr uint8_t prefix() { return 0x17; } };
struct LoanInterestedRate { static constexpr uint8_t prefix() { return 0x18; } };
struct LoanTokenAmount { static constexpr uint8_t prefix() { return 0x19; } };
struct LoanLiquidationPenalty { static constexpr uint8_t prefix() { return 0x1A; } };
};

#endif // DEFI_MASTERNODES_LOAN_H
Loading

0 comments on commit 1a03b0c

Please sign in to comment.