Skip to content

Commit

Permalink
Fix ExistsInMempool && ExistsInChain
Browse files Browse the repository at this point in the history
  • Loading branch information
brangr committed Sep 9, 2022
1 parent 3dfa432 commit a57e806
Showing 1 changed file with 70 additions and 28 deletions.
98 changes: 70 additions & 28 deletions src/pocketdb/repositories/ConsensusRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,38 +405,60 @@ namespace PocketDb

bool ConsensusRepository::ExistsInMempool(const string& string1, const vector<TxType>& types)
{
return ExistsInMempool(string1, "", types);
assert(string1 != "");
bool result = false;

string sql = R"sql(
select 1
from Transactions indexed by Transactions_Type_String1_Height_Time_Int1
where Type in ( )sql" + join(vector<string>(types.size(), "?"), ",") + R"sql( )
and String1 = ?
and Height is null
limit 1
)sql";

TryTransactionStep(__func__, [&]()
{
auto stmt = SetupSqlStatement(sql);

int i = 1;
for (const auto& type: types)
TryBindStatementInt(stmt, i++, type);
TryBindStatementText(stmt, i++, string1);

if (sqlite3_step(*stmt) == SQLITE_ROW)
result = true;

FinalizeSqlStatement(*stmt);
});

return result;
}

bool ConsensusRepository::ExistsInMempool(const string& string1, const string& string2, const vector<TxType>& types)
{
assert(string1 != "");
assert(string2 != "");
bool result = false;

string sql = R"sql(
select 1
from Transactions indexed by Transactions_Type_String1_String2_Height
where Type in ( )sql" + join(vector<string>(types.size(), "?"), ",") + R"sql( )
and String1 = ?
and String2 = ?
and Height is null
)sql";

if (string1 != "")
sql += " and String1 = ?";

if (string2 != "")
sql += " and String2 = ?";

sql += " limit 1 ";

TryTransactionStep(__func__, [&]()
{
auto stmt = SetupSqlStatement(sql);

int i = 1;
for (const auto& type: types)
TryBindStatementInt(stmt, i++, type);
if (string1 != "")
TryBindStatementText(stmt, i++, string1);
if (string2 != "")
TryBindStatementText(stmt, i++, string2);
TryBindStatementText(stmt, i++, string1);
TryBindStatementText(stmt, i++, string2);

if (sqlite3_step(*stmt) == SQLITE_ROW)
result = true;
Expand All @@ -449,39 +471,59 @@ namespace PocketDb

bool ConsensusRepository::ExistsInChain(const string& string1, const vector<TxType>& types)
{
return ExistsInChain(string1, "", types);
assert(string1 != "");
bool result = false;

string sql = R"sql(
select 1
from Transactions indexed by Transactions_Type_String1_Height_Time_Int1
where Type in ( )sql" + join(vector<string>(types.size(), "?"), ",") + R"sql( )
and String1 = ?
and Height is not null
)sql";

TryTransactionStep(__func__, [&]()
{
auto stmt = SetupSqlStatement(sql);

int i = 1;
for (const auto& type: types)
TryBindStatementInt(stmt, i++, type);
TryBindStatementText(stmt, i++, string1);

if (sqlite3_step(*stmt) == SQLITE_ROW)
result = true;

FinalizeSqlStatement(*stmt);
});

return result;
}

bool ConsensusRepository::ExistsInChain(const string& string1, const string& string2, const vector<TxType>& types)
{
assert(string1 != "");
assert(string2 != "");
bool result = false;

string sql = R"sql(
select 1
from Transactions indexed by Transactions_Type_Last_String1_String2_Height
from Transactions indexed by Transactions_Type_String1_String2_Height
where Type in ( )sql" + join(vector<string>(types.size(), "?"), ",") + R"sql( )
and Last = 1
and String1 = ?
and String2 = ?
and Height is not null
)sql";

if (string1 != "")
sql += " and String1 = ?";

if (string2 != "")
sql += " and String2 = ?";

sql += " limit 1 ";

TryTransactionStep(__func__, [&]()
{
auto stmt = SetupSqlStatement(sql);

int i = 1;
for (const auto& type: types)
TryBindStatementInt(stmt, i++, type);
if (string1 != "")
TryBindStatementText(stmt, i++, string1);
if (string2 != "")
TryBindStatementText(stmt, i++, string2);
TryBindStatementText(stmt, i++, string1);
TryBindStatementText(stmt, i++, string2);

if (sqlite3_step(*stmt) == SQLITE_ROW)
result = true;
Expand Down

0 comments on commit a57e806

Please sign in to comment.