Skip to content

Commit

Permalink
Events for single address
Browse files Browse the repository at this point in the history
  • Loading branch information
lostystyg committed May 6, 2022
1 parent b7ec0f7 commit 4030e74
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 103 deletions.
118 changes: 37 additions & 81 deletions src/pocketdb/repositories/web/WebRpcRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ namespace PocketDb
class EventsReconstructor : public RowAccessor
{
public:
EventsReconstructor(const vector<string>& addresses)
{
for (const auto& address: addresses) {
// Preinitializing results for all addresses so we can fill it with common pocknetteam content
m_result.insert({address, {}});
}
}
EventsReconstructor()
: m_result(UniValue::VARR)
{}
bool FeedRow(sqlite3_stmt* stmt)
{
auto [ok0, type] = TryGetColumnString(stmt, 0);
Expand All @@ -32,30 +28,18 @@ namespace PocketDb
txUni.pushKV("blockNum", blockNum);
txUni.pushKV("hash", hash);

if (type == "pocketnetteam") {
for (auto& addressEntry : m_result) {
addressEntry.second.emplace_back(txUni);
}
} else {
auto [ok, address] = TryGetColumnString(stmt, 4);
if (!ok) return false;
if (auto addressEntry = m_result.find(address); addressEntry != m_result.end()) {
addressEntry->second.emplace_back(txUni);
} else {
return false;
}
}
m_result.push_back(txUni);

return true;
}

map<string, vector<UniValue>> GetResult() const
UniValue GetResult() const
{
return m_result;
}

private:
map<string, vector<UniValue>> m_result;
UniValue m_result;
};


Expand Down Expand Up @@ -4227,16 +4211,15 @@ namespace PocketDb
return result;
};

std::map<std::string, std::vector<UniValue>> WebRpcRepository::GetEventsForAddresses(const std::vector<std::string>& addresses, int64_t heightMax, int64_t heightMin, int64_t blockNumMax, const std::set<std::string>& filters)
UniValue WebRpcRepository::GetEventsForAddresses(const std::string& address, int64_t heightMax, int64_t heightMin, int64_t blockNumMax, const std::set<std::string>& filters)
{
static const auto pocketnetteam = R"sql(
-- Pocket posts
select
('pocketnetteam')TP,
t.Height as Height,
t.BlockNum as BlockNum,
t.Hash,
null
t.Hash
from Transactions t indexed by Transactions_Type_Last_String1_Height_Id
Expand All @@ -4253,8 +4236,7 @@ namespace PocketDb
('money')TP,
t.Height as Height,
t.BlockNum as BlockNum,
t.Hash,
o.AddressHash
t.Hash
from TxOutputs o indexed by TxOutputs_AddressHash_TxHeight_TxHash
Expand All @@ -4268,7 +4250,7 @@ namespace PocketDb
on i.SpentTxHash = o.TxHash
and i.AddressHash != o.AddressHash
where o.AddressHash in ( )sql" + join(vector<string>(addresses.size(), "?"), ",") + R"sql( )
where o.AddressHash = ?
and o.TxHeight > ?
and o.TxHeight < ?
)sql";
Expand All @@ -4278,14 +4260,13 @@ namespace PocketDb
('referals')TP,
t.Height as Height,
t.BlockNum as BlockNum,
t.Hash,
t.String2
t.Hash
from Transactions t --indexed by Transactions_Type_Last_String2_Height
where t.Type = 100
and t.Last = 1
and t.String2 in ( )sql" + join(vector<string>(addresses.size(), "?"), ",") + R"sql( )
and t.String2 = ?
and t.Height > ?
and (t.Height < ? or (t.Height = ? and t.BlockNum < ?))
and t.ROWID = (select min(tt.ROWID) from Transactions tt where tt.Id = t.Id)
Expand Down Expand Up @@ -4314,16 +4295,15 @@ namespace PocketDb
where c.Type in (204, 205)
and c.Last = 1
and c.Height is not null
and c.String1 in ( )sql" + join(vector<string>(addresses.size(), "?"), ",") + R"sql( )
and c.String1 = ?
)sql";
static const auto comments = R"sql(
-- Comments for my content
select
('comments')TP,
c.Height as Height,
c.BlockNum as BlockNum,
c.Hash,
p.String1
c.Hash
from Transactions p indexed by Transactions_String1_Last_Height
Expand All @@ -4340,16 +4320,15 @@ namespace PocketDb
where p.Type in (200,201,202)
and p.Last = 1
and p.Height > ?
and p.String1 in ( )sql" + join(vector<string>(addresses.size(), "?"), ",") + R"sql( )
and p.String1 = ?
)sql";
static const auto subscribers = R"sql(
-- Subscribers
select
('subscribers')TP,
subs.Height as Height,
subs.BlockNum as BlockNum,
subs.Hash,
subs.String2
subs.Hash
from Transactions subs --indexed by Transactions_Type_Last_String2_Height
Expand All @@ -4361,7 +4340,7 @@ namespace PocketDb
where subs.Type in (302, 303) -- Ignoring unsubscribers?
and subs.Last = 1
and subs.String2 in ( )sql" + join(vector<string>(addresses.size(), "?"), ",") + R"sql( )
and subs.String2 = ?
and subs.Height > ?
and (subs.Height < ? or (subs.Height = ? and subs.BlockNum < ?))
)sql";
Expand All @@ -4371,8 +4350,7 @@ namespace PocketDb
('commentscores')TP,
s.Height as Height,
s.BlockNum as BlockNum,
s.Hash,
c.String1
s.Hash
from Transactions c indexed by Transactions_Type_Last_String1_Height_Id
Expand All @@ -4386,16 +4364,15 @@ namespace PocketDb
where c.Type in (204,205)
and c.Last = 1
and c.Height > ?
and c.String1 in ( )sql" + join(vector<string>(addresses.size(), "?"), ",") + R"sql( )
and c.String1 = ?
)sql";
static const auto contentscores = R"sql(
-- Content scores
select
('contentscores')TP,
s.Height as Height,
s.BlockNum as BlockNum,
s.Hash,
c.String1
s.Hash
from Transactions c indexed by Transactions_Type_Last_String1_Height_Id
Expand All @@ -4406,7 +4383,7 @@ namespace PocketDb
where c.Type in (200, 201, 202)
and c.Last = 1
and c.String1 in ( )sql" + join(vector<string>(addresses.size(), "?"), ",") + R"sql( )
and c.String1 = ?
and c.Height > ?
)sql";
static const auto privatecontent = R"sql(
Expand All @@ -4415,8 +4392,7 @@ namespace PocketDb
('privatecontent')TP,
cps.Height as Height,
cps.BlockNum as BlockNum,
cps.Hash,
subs.String1
cps.Hash
from Transactions subs indexed by Transactions_Type_Last_String1_Height_Id -- Subscribers private
Expand All @@ -4434,24 +4410,23 @@ namespace PocketDb
where subs.Type = 303
and subs.Last = 1
and subs.Height > ?
and subs.String1 in ( )sql" + join(vector<string>(addresses.size(), "?"), ",") + R"sql( )
and subs.String1 = ?
)sql";
static const auto boost = R"sql(
-- Boosts for my content
select
('boost')TP,
tBoost.Height as Height,
tBoost.BlockNum as BlockNum,
tBoost.Hash,
tContent.String1
tBoost.Hash
from Transactions tBoost indexed by Transactions_Type_Last_Height_Id
join Transactions tContent indexed by Transactions_Type_Last_String1_String2_Height
on tContent.Type in (200,201,202)
and tContent.Last in (0,1)
and tContent.Height > ?
and tContent.String1 in ( )sql" + join(vector<string>(addresses.size(), "?"), ",") + R"sql( )
and tContent.String1 = ?
and tContent.String2 = tBoost.String2
where tBoost.Type in (208)
Expand All @@ -4466,8 +4441,7 @@ namespace PocketDb
('reposts')TP,
r.Height as Height,
r.BlockNum as BlockNum,
r.Hash,
p.String1
r.Hash
from Transactions p indexed by Transactions_Type_Last_String1_Height_Id
join Transactions r indexed by Transactions_Type_Last_String3_Height
Expand All @@ -4480,7 +4454,7 @@ namespace PocketDb
where p.Type in (200,201,202)
and p.Last = 1
and p.Height > ?
and p.String1 in ( )sql" + join(vector<string>(addresses.size(), "?"), ",") + R"sql( )
and p.String1 = ?
)sql";

static const auto footer = R"sql(
Expand Down Expand Up @@ -4530,7 +4504,7 @@ namespace PocketDb

std::string pocketnetteamAddress = GetPocketnetteamAddress();

EventsReconstructor reconstructor(addresses);
EventsReconstructor reconstructor;
TryTransactionStep(__func__, [&]()
{
auto stmt = SetupSqlStatement(sql);
Expand All @@ -4550,17 +4524,13 @@ namespace PocketDb
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, blockNumMax);
for (auto& address : addresses) {
TryBindStatementText(stmt, i++, address);
}
TryBindStatementText(stmt, i++, address);
TryBindStatementInt64(stmt, i++, heightMin);
TryBindStatementInt64(stmt, i++, heightMax);
}
// Referals
if (filters.empty() || filters.find("referals") != filters.end()) {
for (auto& address : addresses) {
TryBindStatementText(stmt, i++, address);
}
TryBindStatementText(stmt, i++, address);
TryBindStatementInt64(stmt, i++, heightMin);
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, heightMax);
Expand All @@ -4580,15 +4550,11 @@ namespace PocketDb
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, blockNumMax);
TryBindStatementInt64(stmt, i++, heightMin);
for (auto& address : addresses) {
TryBindStatementText(stmt, i++, address);
}
TryBindStatementText(stmt, i++, address);
}
// Subscribers and unsubscribers
if (filters.empty() || filters.find("subscribers") != filters.end()) {
for (auto& address : addresses) {
TryBindStatementText(stmt, i++, address);
}
TryBindStatementText(stmt, i++, address);
TryBindStatementInt64(stmt, i++, heightMin);
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, heightMax);
Expand All @@ -4601,19 +4567,15 @@ namespace PocketDb
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, blockNumMax);
TryBindStatementInt64(stmt, i++, heightMin);
for (auto& address : addresses) {
TryBindStatementText(stmt, i++, address);
}
TryBindStatementText(stmt, i++, address);
}
// Content scores
if (filters.empty() || filters.find("contentscores") != filters.end()) {
TryBindStatementInt64(stmt, i++, heightMin);
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, blockNumMax);
for (auto& address : addresses) {
TryBindStatementText(stmt, i++, address);
}
TryBindStatementText(stmt, i++, address);
TryBindStatementInt64(stmt, i++, heightMin);
}
// Content from private subscribers
Expand All @@ -4623,17 +4585,13 @@ namespace PocketDb
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, blockNumMax);
TryBindStatementInt64(stmt, i++, heightMin);
for (auto& address : addresses) {
TryBindStatementText(stmt, i++, address);
}
TryBindStatementText(stmt, i++, address);
}

// Boosts
if (filters.empty() || filters.find("boost") != filters.end()) {
TryBindStatementInt64(stmt, i++, heightMin);
for (auto& address : addresses) {
TryBindStatementText(stmt, i++, address);
}
TryBindStatementText(stmt, i++, address);
TryBindStatementInt64(stmt, i++, heightMin);
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, heightMax);
Expand All @@ -4646,9 +4604,7 @@ namespace PocketDb
TryBindStatementInt64(stmt, i++, heightMax);
TryBindStatementInt64(stmt, i++, blockNumMax);
TryBindStatementInt64(stmt, i++, heightMin);
for (auto& address : addresses) {
TryBindStatementText(stmt, i++, address);
}
TryBindStatementText(stmt, i++, address);
}

while (sqlite3_step(*stmt) == SQLITE_ROW)
Expand Down
2 changes: 1 addition & 1 deletion src/pocketdb/repositories/web/WebRpcRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ namespace PocketDb
/**
* Returns map where key is address. Value is map, where key - height, value - vector of transactions for this height.
*/
std::map<std::string, std::vector<UniValue>> GetEventsForAddresses(const std::vector<std::string>& address, int64_t heightMax, int64_t heightMin, int64_t blockNum, const std::set<std::string>& filters);
UniValue GetEventsForAddresses(const std::string& address, int64_t heightMax, int64_t heightMin, int64_t blockNum, const std::set<std::string>& filters);

// TODO (o1q): Remove this two methods when the client gui switches to new methods
UniValue GetProfileFeedOld(const string& addressFrom, const string& addressTo, int64_t topContentId, int count, const string& lang, const vector<string>& tags, const vector<int>& contentTypes);
Expand Down
24 changes: 3 additions & 21 deletions src/pocketdb/web/PocketContentRpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,15 +958,9 @@ namespace PocketWeb::PocketWebRpc
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
RPCTypeCheck(request.params, {UniValue::VARR});

auto arr = request.params[0].get_array();
vector<string> addresses;
addresses.reserve(arr.size());
for (int i = 0; i < arr.size(); i++) {
addresses.emplace_back(arr[i].get_str());
}
RPCTypeCheck(request.params, {UniValue::VSTR});

auto address = request.params[0].get_str();

int64_t heightMax = ChainActive().Height(); // TODO (losty): deadlock here wtf
if (request.params.size() > 1 && request.params[1].isNum()) {
Expand Down Expand Up @@ -994,19 +988,7 @@ namespace PocketWeb::PocketWebRpc
}
}

auto events = request.DbConnection()->WebRpcRepoInst->GetEventsForAddresses(addresses, heightMax, heightMin, blockNum, filters);

UniValue result(UniValue::VOBJ);
for (auto& entry: events) {
auto address = entry.first;
UniValue txs(UniValue::VARR);
for (const auto& tx : entry.second) {
txs.push_back(tx);
}
result.pushKV(address, txs);
}

return result;
return request.DbConnection()->WebRpcRepoInst->GetEventsForAddresses(address, heightMax, heightMin, blockNum, filters);
},
};
}
Expand Down

0 comments on commit 4030e74

Please sign in to comment.