2019-08-27 00:12:43 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
namespace ircd::m::vm::fetch
|
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
static void prev_check(const event &, vm::eval &);
|
|
|
|
static std::forward_list<ctx::future<m::fetch::result>> prev_fetch(const event &, vm::eval &, const room &);
|
|
|
|
static void prev(const event &, vm::eval &, const room &);
|
2019-09-06 22:29:54 +02:00
|
|
|
static std::forward_list<ctx::future<m::fetch::result>> state_fetch(const event &, vm::eval &, const room &);
|
|
|
|
static void state(const event &, vm::eval &, const room &);
|
2019-09-06 01:30:51 +02:00
|
|
|
static void auth_chain_eval(const event &, vm::eval &, const room &, const json::array &);
|
|
|
|
static void auth_chain(const event &, vm::eval &, const room &);
|
|
|
|
static void auth(const event &, vm::eval &, const room &);
|
|
|
|
static void handle(const event &, vm::eval &);
|
|
|
|
|
|
|
|
extern conf::item<size_t> prev_backfill_limit;
|
2019-08-28 04:34:46 +02:00
|
|
|
extern conf::item<seconds> auth_timeout;
|
2019-08-27 00:12:43 +02:00
|
|
|
extern conf::item<bool> enable;
|
|
|
|
extern hookfn<vm::eval &> hook;
|
|
|
|
extern log::log log;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Matrix VM Fetch Unit"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::fetch::log)
|
|
|
|
ircd::m::vm::fetch::log
|
|
|
|
{
|
|
|
|
"m.vm.fetch"
|
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
decltype(ircd::m::vm::fetch::hook)
|
|
|
|
ircd::m::vm::fetch::hook
|
|
|
|
{
|
|
|
|
handle,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.fetch" }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-27 00:12:43 +02:00
|
|
|
decltype(ircd::m::vm::fetch::enable)
|
|
|
|
ircd::m::vm::fetch::enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.vm.fetch.enable" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
2019-08-28 04:34:46 +02:00
|
|
|
decltype(ircd::m::vm::fetch::auth_timeout)
|
|
|
|
ircd::m::vm::fetch::auth_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.vm.fetch.auth.timeout" },
|
|
|
|
{ "default", 15L },
|
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
decltype(ircd::m::vm::fetch::prev_backfill_limit)
|
|
|
|
ircd::m::vm::fetch::prev_backfill_limit
|
2019-08-27 00:12:43 +02:00
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
{ "name", "ircd.m.vm.fetch.prev.backfill.limit" },
|
|
|
|
{ "default", 128L },
|
2019-08-27 00:12:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// fetch_phase
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2019-09-06 01:30:51 +02:00
|
|
|
ircd::m::vm::fetch::handle(const event &event,
|
|
|
|
vm::eval &eval)
|
2019-08-27 00:12:43 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(eval.opts);
|
|
|
|
assert(eval.opts->fetch);
|
2019-09-06 01:30:51 +02:00
|
|
|
const auto &opts
|
|
|
|
{
|
|
|
|
*eval.opts
|
|
|
|
};
|
2019-08-27 00:12:43 +02:00
|
|
|
|
|
|
|
const auto &type
|
|
|
|
{
|
|
|
|
at<"type"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(type == "m.room.create")
|
|
|
|
return;
|
|
|
|
|
|
|
|
const m::event::id &event_id
|
|
|
|
{
|
|
|
|
event.event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
at<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Can't construct m::room with the event_id argument because it
|
|
|
|
// won't be found (we're evaluating that event here!) so we just set
|
|
|
|
// the member manually to make further use of the room struct.
|
|
|
|
m::room room{room_id};
|
|
|
|
room.event_id = event_id;
|
|
|
|
|
2019-09-05 22:17:04 +02:00
|
|
|
if(opts.fetch_auth)
|
2019-09-06 01:30:51 +02:00
|
|
|
auth(event, eval, room);
|
2019-08-27 00:12:43 +02:00
|
|
|
|
2019-09-05 22:17:04 +02:00
|
|
|
if(opts.fetch_prev)
|
2019-09-06 01:30:51 +02:00
|
|
|
prev(event, eval, room);
|
2019-09-06 22:29:54 +02:00
|
|
|
|
|
|
|
if(opts.fetch_state)
|
|
|
|
state(event, eval, room);
|
2019-08-27 00:12:43 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "%s :%s",
|
|
|
|
loghead(eval),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
//
|
|
|
|
// auth_events handler stack
|
|
|
|
//
|
|
|
|
|
2019-08-27 00:12:43 +02:00
|
|
|
void
|
2019-09-06 01:30:51 +02:00
|
|
|
ircd::m::vm::fetch::auth(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
const room &room)
|
2019-08-27 00:12:43 +02:00
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
try
|
2019-08-27 00:12:43 +02:00
|
|
|
{
|
|
|
|
// Count how many of the auth_events provided exist locally.
|
|
|
|
const auto &opts{*eval.opts};
|
|
|
|
const event::prev prev{event};
|
2019-09-06 01:30:51 +02:00
|
|
|
const size_t auth_count
|
|
|
|
{
|
|
|
|
prev.auth_events_count()
|
|
|
|
};
|
2019-09-05 04:26:46 +02:00
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
size_t auth_exists{0};
|
|
|
|
for(size_t i(0); i < auth_count; ++i)
|
2019-08-27 00:12:43 +02:00
|
|
|
{
|
|
|
|
const auto &auth_id
|
|
|
|
{
|
|
|
|
prev.auth_event(i)
|
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
auth_exists += bool(m::exists(auth_id));
|
2019-08-27 00:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We are satisfied at this point if all auth_events for this event exist,
|
|
|
|
// as those events have themselves been successfully evaluated and so forth.
|
2019-09-06 01:30:51 +02:00
|
|
|
assert(auth_exists <= auth_count);
|
|
|
|
if(auth_exists == auth_count)
|
2019-08-27 00:12:43 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// At this point we are missing one or more auth_events for this event.
|
|
|
|
log::dwarning
|
|
|
|
{
|
2019-09-05 04:26:46 +02:00
|
|
|
log, "%s auth_events:%zu miss:%zu",
|
2019-08-27 00:12:43 +02:00
|
|
|
loghead(eval),
|
2019-09-06 01:30:51 +02:00
|
|
|
auth_count,
|
2019-09-06 21:15:30 +02:00
|
|
|
auth_count - auth_exists,
|
2019-08-27 00:12:43 +02:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
if(!bool(m::vm::fetch::enable))
|
|
|
|
throw vm::error
|
|
|
|
{
|
|
|
|
vm::fault::AUTH, "Fetching auth_events disabled by configuration",
|
|
|
|
};
|
2019-08-27 00:12:43 +02:00
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
if(!opts.fetch_auth)
|
2019-08-27 00:12:43 +02:00
|
|
|
throw vm::error
|
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
vm::fault::AUTH, "Not fetching auth_events for this evaluation",
|
2019-08-27 00:12:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is a blocking call to recursively fetch and evaluate the auth_chain
|
|
|
|
// for this event. Upon return all of the auth_events for this event will
|
2019-09-06 01:30:51 +02:00
|
|
|
// have themselves been fetched and auth'ed recursively.
|
|
|
|
auth_chain(event, eval, room);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw vm::error
|
|
|
|
{
|
|
|
|
vm::fault::AUTH, "Failed to fetch all auth_events :%s",
|
|
|
|
string_view{event.event_id},
|
|
|
|
json::get<"room_id"_>(event),
|
|
|
|
e.what()
|
|
|
|
};
|
2019-08-27 00:12:43 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 04:34:46 +02:00
|
|
|
void
|
2019-09-06 01:30:51 +02:00
|
|
|
ircd::m::vm::fetch::auth_chain(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
const room &room)
|
2019-08-28 04:34:46 +02:00
|
|
|
try
|
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
assert(eval.opts);
|
|
|
|
m::fetch::opts opts;
|
|
|
|
opts.op = m::fetch::op::auth;
|
|
|
|
opts.room_id = room.room_id;
|
|
|
|
opts.event_id = room.event_id;
|
|
|
|
|
|
|
|
// Figure out a remote hint as the primary target to request the missing
|
|
|
|
// auth events from; if provided, m::fetch will ask this remote first. We
|
|
|
|
// try to use the eval.node_id, which is set to a server that is conducting
|
|
|
|
// the eval (i.e in a /send/ or when processing some response data from
|
|
|
|
// them); next we try the origin of the event itself. These remotes are
|
|
|
|
// most likely to provide a satisfying response.
|
|
|
|
opts.hint =
|
|
|
|
{
|
|
|
|
eval.opts->node_id?
|
|
|
|
eval.opts->node_id:
|
|
|
|
!my_host(json::get<"origin"_>(event))?
|
|
|
|
string_view(json::get<"origin"_>(event)):
|
|
|
|
string_view{}
|
|
|
|
};
|
|
|
|
|
2019-08-28 04:34:46 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
log, "Fetching auth chain for %s in %s hint:%s",
|
2019-08-28 04:34:46 +02:00
|
|
|
string_view{room.event_id},
|
|
|
|
string_view{room.room_id},
|
2019-09-06 01:30:51 +02:00
|
|
|
opts.hint,
|
2019-08-30 04:38:53 +02:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
// send
|
2019-08-30 04:38:53 +02:00
|
|
|
auto future
|
|
|
|
{
|
|
|
|
m::fetch::start(opts)
|
2019-08-28 04:34:46 +02:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
// recv
|
2019-08-30 04:38:53 +02:00
|
|
|
const auto result
|
2019-08-28 04:34:46 +02:00
|
|
|
{
|
2019-08-30 04:38:53 +02:00
|
|
|
future.get(seconds(auth_timeout))
|
2019-08-28 04:34:46 +02:00
|
|
|
};
|
|
|
|
|
2019-08-30 04:38:53 +02:00
|
|
|
const json::object response
|
2019-08-28 04:34:46 +02:00
|
|
|
{
|
2019-08-30 04:38:53 +02:00
|
|
|
result
|
2019-08-28 04:34:46 +02:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
// parse
|
2019-08-30 04:38:53 +02:00
|
|
|
const json::array &auth_chain
|
2019-08-28 04:34:46 +02:00
|
|
|
{
|
2019-08-30 04:38:53 +02:00
|
|
|
response["auth_chain"]
|
2019-08-28 04:34:46 +02:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
auth_chain_eval(event, eval, room, auth_chain);
|
|
|
|
}
|
|
|
|
catch(const vm::error &e)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Fetching auth chain for %s in %s :%s",
|
|
|
|
string_view{room.event_id},
|
|
|
|
string_view{room.room_id},
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::vm::fetch::auth_chain_eval(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
const room &room,
|
|
|
|
const json::array &auth_chain)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(eval.opts);
|
2019-09-13 22:27:50 +02:00
|
|
|
auto opts(*eval.opts);
|
2019-09-06 01:30:51 +02:00
|
|
|
opts.fetch_prev = false;
|
|
|
|
opts.fetch_state = false;
|
|
|
|
opts.infolog_accept = true;
|
|
|
|
opts.warnlog &= ~vm::fault::EXISTS;
|
2019-08-28 04:34:46 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
log, "Evaluating auth chain for %s in %s events:%zu",
|
2019-08-28 04:34:46 +02:00
|
|
|
string_view{room.event_id},
|
|
|
|
string_view{room.room_id},
|
2019-09-06 01:30:51 +02:00
|
|
|
auth_chain.size(),
|
2019-08-28 04:34:46 +02:00
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
// eval
|
2019-08-28 04:34:46 +02:00
|
|
|
m::vm::eval
|
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
auth_chain, opts
|
2019-08-28 04:34:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
log, "Evaluating auth chain for %s in %s :%s",
|
2019-08-28 04:34:46 +02:00
|
|
|
string_view{room.event_id},
|
|
|
|
string_view{room.room_id},
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-09-06 22:29:54 +02:00
|
|
|
//
|
|
|
|
// state handler stack
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::vm::fetch::state(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
const room &room)
|
|
|
|
{
|
|
|
|
const event::prev prev{event};
|
|
|
|
const size_t prev_miss
|
|
|
|
{
|
|
|
|
prev.prev_events_count() - prev.prev_events_exist()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!prev_miss)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto &[sounding_depth, sounding_idx]
|
|
|
|
{
|
|
|
|
m::sounding(room)
|
|
|
|
};
|
|
|
|
|
2019-09-14 01:24:18 +02:00
|
|
|
if(at<"depth"_>(event) > sounding_depth)
|
2019-09-06 22:29:54 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto futures
|
|
|
|
{
|
|
|
|
state_fetch(event, eval, room)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!std::distance(begin(futures), end(futures)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto fetching
|
|
|
|
{
|
|
|
|
ctx::when_all(begin(futures), end(futures))
|
|
|
|
};
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "%s fetching %zu missing state events in %s",
|
|
|
|
loghead(eval),
|
|
|
|
std::distance(begin(futures), end(futures)),
|
|
|
|
string_view{room.room_id},
|
|
|
|
};
|
|
|
|
|
|
|
|
// yields context
|
|
|
|
fetching.wait();
|
|
|
|
|
|
|
|
// evaluate results
|
|
|
|
size_t good(0), fail(0);
|
|
|
|
for(auto &future : futures) try
|
|
|
|
{
|
|
|
|
m::fetch::result result
|
|
|
|
{
|
|
|
|
future.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &pdus
|
|
|
|
{
|
|
|
|
json::object(result).get("pdus")
|
|
|
|
};
|
|
|
|
|
|
|
|
auto opts(*eval.opts);
|
|
|
|
opts.fetch_prev = false;
|
|
|
|
opts.fetch_state = false;
|
|
|
|
vm::eval
|
|
|
|
{
|
|
|
|
pdus, opts
|
|
|
|
};
|
|
|
|
|
|
|
|
++good;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
++fail;
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "%s state eval :%s",
|
|
|
|
loghead(eval),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "%s evaluated missing state in %s fetched:%zu good:%zu fail:%zu",
|
|
|
|
loghead(eval),
|
|
|
|
string_view{room.room_id},
|
|
|
|
std::distance(begin(futures), end(futures)),
|
|
|
|
good,
|
|
|
|
fail,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::forward_list
|
|
|
|
<
|
|
|
|
ircd::ctx::future<ircd::m::fetch::result>
|
|
|
|
>
|
|
|
|
ircd::m::vm::fetch::state_fetch(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
const room &room)
|
|
|
|
{
|
|
|
|
feds::opts opts;
|
|
|
|
opts.op = feds::op::state;
|
|
|
|
opts.event_id = room.event_id;
|
|
|
|
opts.room_id = room.room_id;
|
|
|
|
opts.arg[0] = "ids";
|
|
|
|
opts.exclude_myself = true;
|
|
|
|
opts.closure_errors = false;
|
|
|
|
opts.nothrow_closure = true;
|
|
|
|
log::debug
|
|
|
|
{
|
2019-12-04 05:24:11 +01:00
|
|
|
log, "%s acquire state event ids in %s...",
|
2019-09-06 22:29:54 +02:00
|
|
|
loghead(eval),
|
|
|
|
string_view{room.room_id},
|
|
|
|
};
|
|
|
|
|
|
|
|
std::set<std::string, std::less<>> req;
|
|
|
|
std::forward_list<ctx::future<m::fetch::result>> ret;
|
|
|
|
feds::execute(opts, [&eval, &ret, &req]
|
|
|
|
(const auto &result)
|
|
|
|
{
|
|
|
|
const auto each_state_id{[&eval, &ret, &req, &result]
|
|
|
|
(const m::event::id &event_id)
|
|
|
|
{
|
|
|
|
if(m::exists(event_id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto it(req.lower_bound(event_id));
|
|
|
|
if(it != end(req) && *it == event_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
req.emplace_hint(it, event_id);
|
|
|
|
|
|
|
|
m::fetch::opts opts;
|
|
|
|
opts.op = m::fetch::op::event;
|
|
|
|
opts.room_id = result.request->room_id;
|
|
|
|
opts.event_id = event_id;
|
|
|
|
ret.emplace_front(m::fetch::start(opts));
|
|
|
|
|
|
|
|
assert(std::distance(begin(ret), end(ret)) <= ssize_t(req.size()));
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s requesting state event %s off %s in %s reqs:%zu",
|
|
|
|
loghead(eval),
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{result.request->event_id},
|
|
|
|
string_view{result.request->room_id},
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
const json::array &auth_chain_ids
|
|
|
|
{
|
|
|
|
result.object["auth_chain_ids"]
|
|
|
|
};
|
|
|
|
|
2019-12-04 05:29:58 +01:00
|
|
|
const json::array &pdu_ids
|
2019-09-06 22:29:54 +02:00
|
|
|
{
|
2019-12-04 05:29:58 +01:00
|
|
|
result.object["pdu_ids"]
|
2019-09-06 22:29:54 +02:00
|
|
|
};
|
|
|
|
|
2019-12-04 05:29:58 +01:00
|
|
|
for(const json::string &auth_chain_id : auth_chain_ids)
|
|
|
|
each_state_id(auth_chain_id);
|
|
|
|
|
|
|
|
for(const json::string &pdu_id : pdu_ids)
|
|
|
|
each_state_id(pdu_id);
|
2019-09-06 22:29:54 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
//
|
|
|
|
// prev_events handler stack
|
|
|
|
//
|
|
|
|
|
2019-08-27 00:12:43 +02:00
|
|
|
void
|
2019-09-06 01:30:51 +02:00
|
|
|
ircd::m::vm::fetch::prev(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
const room &room)
|
2019-08-27 00:12:43 +02:00
|
|
|
{
|
|
|
|
const auto &opts{*eval.opts};
|
|
|
|
const event::prev prev{event};
|
2019-09-05 04:26:46 +02:00
|
|
|
const size_t prev_count
|
|
|
|
{
|
|
|
|
prev.prev_events_count()
|
|
|
|
};
|
2019-08-31 10:38:20 +02:00
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
const size_t prev_exists
|
2019-08-27 00:12:43 +02:00
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
prev.prev_events_exist()
|
|
|
|
};
|
2019-08-31 10:38:20 +02:00
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
assert(prev_exists <= prev_count);
|
|
|
|
if(prev_count == prev_exists)
|
|
|
|
return;
|
2019-08-27 00:12:43 +02:00
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
if(!opts.fetch_prev || !m::vm::fetch::enable)
|
|
|
|
{
|
|
|
|
prev_check(event, eval);
|
|
|
|
return;
|
2019-08-27 00:12:43 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
auto futures
|
|
|
|
{
|
|
|
|
prev_fetch(event, eval, room)
|
|
|
|
};
|
2019-08-27 00:12:43 +02:00
|
|
|
|
|
|
|
// At this point one or more prev_events are missing; the fetches were
|
|
|
|
// launched asynchronously if the options allowed for it.
|
|
|
|
log::dwarning
|
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
log, "%s depth:%ld prev_events:%zu miss:%zu fetching:%zu fetching ...",
|
2019-08-27 00:12:43 +02:00
|
|
|
loghead(eval),
|
2019-09-06 01:30:51 +02:00
|
|
|
at<"depth"_>(event),
|
2019-09-05 04:26:46 +02:00
|
|
|
prev_count,
|
|
|
|
prev_count - prev_exists,
|
2019-09-06 01:30:51 +02:00
|
|
|
std::distance(begin(futures), end(futures)),
|
2019-08-27 00:12:43 +02:00
|
|
|
};
|
|
|
|
|
2019-08-31 10:38:20 +02:00
|
|
|
auto fetching
|
|
|
|
{
|
|
|
|
ctx::when_all(begin(futures), end(futures))
|
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
// yields context
|
2019-08-31 10:38:20 +02:00
|
|
|
fetching.wait();
|
2019-09-06 01:30:51 +02:00
|
|
|
|
|
|
|
// evaluate results
|
2019-08-31 10:38:20 +02:00
|
|
|
for(auto &future : futures) try
|
|
|
|
{
|
|
|
|
m::fetch::result result
|
|
|
|
{
|
|
|
|
future.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &pdus
|
|
|
|
{
|
|
|
|
json::object(result).get("pdus")
|
|
|
|
};
|
|
|
|
|
2019-09-06 02:39:28 +02:00
|
|
|
auto opts(*eval.opts);
|
|
|
|
opts.fetch_prev = false;
|
2019-09-13 23:17:11 +02:00
|
|
|
opts.fetch_state = false;
|
2019-08-31 10:38:20 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s fetched %zu pdus; evaluating...",
|
|
|
|
loghead(eval),
|
|
|
|
pdus.size(),
|
|
|
|
};
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
vm::eval
|
2019-08-31 10:38:20 +02:00
|
|
|
{
|
|
|
|
pdus, opts
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2019-09-06 22:29:54 +02:00
|
|
|
log, "%s prev fetch/eval :%s",
|
2019-08-31 10:38:20 +02:00
|
|
|
loghead(eval),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
// check if result evals have satisfied this eval now; or throw
|
|
|
|
prev_check(event, eval);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::forward_list
|
|
|
|
<
|
|
|
|
ircd::ctx::future<ircd::m::fetch::result>
|
|
|
|
>
|
|
|
|
ircd::m::vm::fetch::prev_fetch(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
const room &room)
|
|
|
|
{
|
|
|
|
const long room_depth
|
|
|
|
{
|
|
|
|
m::depth(std::nothrow, room)
|
|
|
|
};
|
|
|
|
|
|
|
|
const long viewport_depth
|
2019-08-27 00:12:43 +02:00
|
|
|
{
|
2019-09-06 02:31:54 +02:00
|
|
|
room_depth - room::events::viewport_size
|
2019-09-05 04:26:46 +02:00
|
|
|
};
|
2019-08-27 00:12:43 +02:00
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
std::forward_list
|
|
|
|
<
|
|
|
|
ctx::future<m::fetch::result>
|
|
|
|
>
|
|
|
|
ret;
|
|
|
|
const event::prev prev{event};
|
|
|
|
for(size_t i(0); i < prev.prev_events_count(); ++i)
|
2019-09-05 04:26:46 +02:00
|
|
|
{
|
2019-09-06 01:30:51 +02:00
|
|
|
const auto &prev_id
|
|
|
|
{
|
|
|
|
prev.prev_event(i)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(m::exists(prev_id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const long depth_gap
|
|
|
|
{
|
|
|
|
std::max(std::abs(at<"depth"_>(event) - room_depth), 1L)
|
|
|
|
};
|
|
|
|
|
|
|
|
m::fetch::opts opts;
|
|
|
|
opts.op = m::fetch::op::backfill;
|
|
|
|
opts.room_id = room.room_id;
|
|
|
|
opts.event_id = prev_id;
|
2019-09-16 21:06:48 +02:00
|
|
|
opts.backfill_limit = size_t(depth_gap);
|
|
|
|
opts.backfill_limit = std::min(opts.backfill_limit, eval.opts->fetch_prev_limit);
|
|
|
|
opts.backfill_limit = std::min(opts.backfill_limit, size_t(prev_backfill_limit));
|
2019-09-06 01:30:51 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s requesting backfill off %s; depth:%ld viewport:%ld room:%ld gap:%ld limit:%zu",
|
|
|
|
loghead(eval),
|
|
|
|
string_view{prev_id},
|
|
|
|
at<"depth"_>(event),
|
|
|
|
viewport_depth,
|
|
|
|
room_depth,
|
|
|
|
depth_gap,
|
2019-09-16 21:06:48 +02:00
|
|
|
opts.backfill_limit,
|
2019-09-06 01:30:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ret.emplace_front(m::fetch::start(opts));
|
2019-08-27 00:12:43 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 01:30:51 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::vm::fetch::prev_check(const event &event,
|
|
|
|
vm::eval &eval)
|
|
|
|
{
|
|
|
|
const auto &opts
|
|
|
|
{
|
|
|
|
*eval.opts
|
|
|
|
};
|
|
|
|
|
|
|
|
const event::prev prev
|
|
|
|
{
|
|
|
|
event
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t prev_exists
|
|
|
|
{
|
|
|
|
prev.prev_events_exist()
|
|
|
|
};
|
|
|
|
|
2019-08-27 00:12:43 +02:00
|
|
|
// Aborts this event if the options want us to guarantee at least one
|
|
|
|
// prev_event was fetched and evaluated for this event. This is generally
|
|
|
|
// used in conjunction with the fetch_prev_wait option to be effective.
|
2019-09-05 04:26:46 +02:00
|
|
|
if(opts.fetch_prev_any && !prev_exists)
|
2019-08-27 00:12:43 +02:00
|
|
|
throw vm::error
|
|
|
|
{
|
2019-09-05 04:26:46 +02:00
|
|
|
vm::fault::EVENT, "Failed to fetch any of the %zu prev_events for %s in %s",
|
2019-09-06 01:30:51 +02:00
|
|
|
prev.prev_events_count(),
|
2019-08-27 00:12:43 +02:00
|
|
|
string_view{event.event_id},
|
|
|
|
json::get<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Aborts this event if the options want us to guarantee ALL of the
|
|
|
|
// prev_events were fetched and evaluated for this event.
|
2019-09-06 01:30:51 +02:00
|
|
|
if(opts.fetch_prev_all && prev_exists < prev.prev_events_count())
|
2019-08-27 00:12:43 +02:00
|
|
|
throw vm::error
|
|
|
|
{
|
2019-09-05 04:26:46 +02:00
|
|
|
vm::fault::EVENT, "Missing %zu of %zu required prev_events for %s in %s",
|
|
|
|
prev_exists,
|
2019-09-06 01:30:51 +02:00
|
|
|
prev.prev_events_count(),
|
2019-08-27 00:12:43 +02:00
|
|
|
string_view{event.event_id},
|
|
|
|
json::get<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
}
|