2018-02-04 03:22:01 +01: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.
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
namespace ircd::m::vm
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
template<class... args> static bool output(const vm::opts &, const vm::fault &, const string_view &event_id, const string_view &fmt, args&&...);
|
|
|
|
template<class... args> static fault handle_fault(const opts &, const fault &, const string_view &event_id, const string_view &fmt, args&&...);
|
2019-07-06 06:33:58 +02:00
|
|
|
template<class T> static void call_hook(hook::site<T> &, eval &, const event &, T&& data);
|
2020-12-18 09:50:16 +01:00
|
|
|
static void retire(eval &, const event &);
|
|
|
|
static void emption_check(eval &, const event &);
|
2019-04-30 23:08:00 +02:00
|
|
|
static size_t calc_txn_reserve(const opts &, const event &);
|
2019-03-21 22:13:45 +01:00
|
|
|
static void write_commit(eval &);
|
2020-12-06 17:05:33 +01:00
|
|
|
static void write_append(eval &, const event &, const bool &);
|
2019-03-21 23:00:58 +01:00
|
|
|
static fault execute_edu(eval &, const event &);
|
|
|
|
static fault execute_pdu(eval &, const event &);
|
2020-03-20 20:44:23 +01:00
|
|
|
static fault execute_du(eval &, const event &);
|
2020-12-01 05:47:57 +01:00
|
|
|
static fault execute(eval &, const event &);
|
2019-07-17 21:15:17 +02:00
|
|
|
static fault inject3(eval &, json::iov &, const json::iov &);
|
|
|
|
static fault inject1(eval &, json::iov &, const json::iov &);
|
2018-05-07 06:15:25 +02:00
|
|
|
static void fini();
|
2019-05-26 10:48:27 +02:00
|
|
|
static void init();
|
2019-03-01 01:53:03 +01:00
|
|
|
|
2020-05-12 05:24:54 +02:00
|
|
|
extern hook::site<eval &> issue_hook; ///< Called when this server is issuing event
|
|
|
|
extern hook::site<eval &> conform_hook; ///< Called for static evaluations of event
|
|
|
|
extern hook::site<eval &> access_hook; ///< Called for access control checking
|
|
|
|
extern hook::site<eval &> fetch_auth_hook; ///< Called to resolve dependencies
|
|
|
|
extern hook::site<eval &> fetch_prev_hook; ///< Called to resolve dependencies
|
|
|
|
extern hook::site<eval &> fetch_state_hook; ///< Called to resolve dependencies
|
|
|
|
extern hook::site<eval &> eval_hook; ///< Called for final event evaluation
|
|
|
|
extern hook::site<eval &> post_hook; ///< Called to apply effects pre-notify
|
|
|
|
extern hook::site<eval &> notify_hook; ///< Called to broadcast successful eval
|
|
|
|
extern hook::site<eval &> effect_hook; ///< Called to apply effects post-notify
|
2019-03-01 01:53:03 +01:00
|
|
|
|
2019-03-16 20:38:14 +01:00
|
|
|
extern conf::item<bool> log_commit_debug;
|
2019-03-01 01:53:03 +01:00
|
|
|
extern conf::item<bool> log_accept_debug;
|
|
|
|
extern conf::item<bool> log_accept_info;
|
2018-04-16 23:21:54 +02:00
|
|
|
}
|
|
|
|
|
2019-03-16 20:38:14 +01:00
|
|
|
decltype(ircd::m::vm::log_commit_debug)
|
|
|
|
ircd::m::vm::log_commit_debug
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.vm.log.commit.debug" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
2019-03-01 01:53:03 +01:00
|
|
|
decltype(ircd::m::vm::log_accept_debug)
|
|
|
|
ircd::m::vm::log_accept_debug
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.vm.log.accept.debug" },
|
2019-10-03 18:26:18 +02:00
|
|
|
{ "default", true },
|
2019-03-01 01:53:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::log_accept_info)
|
|
|
|
ircd::m::vm::log_accept_info
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.vm.log.accept.info" },
|
|
|
|
{ "default", false },
|
|
|
|
};
|
|
|
|
|
2019-03-09 21:13:21 +01:00
|
|
|
decltype(ircd::m::vm::issue_hook)
|
|
|
|
ircd::m::vm::issue_hook
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2019-03-09 21:13:21 +01:00
|
|
|
{ "name", "vm.issue" }
|
2018-05-07 01:04:51 +02:00
|
|
|
};
|
|
|
|
|
2019-03-09 22:12:58 +01:00
|
|
|
decltype(ircd::m::vm::conform_hook)
|
|
|
|
ircd::m::vm::conform_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.conform" }
|
|
|
|
};
|
|
|
|
|
2019-05-27 05:57:55 +02:00
|
|
|
decltype(ircd::m::vm::access_hook)
|
|
|
|
ircd::m::vm::access_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.access" }
|
|
|
|
};
|
|
|
|
|
2020-05-12 05:24:54 +02:00
|
|
|
decltype(ircd::m::vm::fetch_auth_hook)
|
|
|
|
ircd::m::vm::fetch_auth_hook
|
2018-09-06 10:21:37 +02:00
|
|
|
{
|
2020-05-12 05:24:54 +02:00
|
|
|
{ "name", "vm.fetch.auth" }
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::fetch_prev_hook)
|
|
|
|
ircd::m::vm::fetch_prev_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.fetch.prev" }
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::fetch_state_hook)
|
|
|
|
ircd::m::vm::fetch_state_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.fetch.state" }
|
2018-09-06 10:21:37 +02:00
|
|
|
};
|
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
decltype(ircd::m::vm::eval_hook)
|
|
|
|
ircd::m::vm::eval_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.eval" }
|
|
|
|
};
|
|
|
|
|
2019-03-09 21:08:05 +01:00
|
|
|
decltype(ircd::m::vm::post_hook)
|
|
|
|
ircd::m::vm::post_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.post" }
|
|
|
|
};
|
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
decltype(ircd::m::vm::notify_hook)
|
|
|
|
ircd::m::vm::notify_hook
|
|
|
|
{
|
2019-07-20 05:12:05 +02:00
|
|
|
{ "name", "vm.notify" },
|
|
|
|
{ "exceptions", false },
|
2020-05-25 05:56:30 +02:00
|
|
|
{ "interrupts", false },
|
2018-05-07 01:04:51 +02:00
|
|
|
};
|
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
decltype(ircd::m::vm::effect_hook)
|
|
|
|
ircd::m::vm::effect_hook
|
|
|
|
{
|
2019-07-20 05:12:05 +02:00
|
|
|
{ "name", "vm.effect" },
|
|
|
|
{ "exceptions", false },
|
2020-05-25 05:56:30 +02:00
|
|
|
{ "interrupts", false },
|
2018-10-07 07:17:46 +02:00
|
|
|
};
|
|
|
|
|
2018-05-07 02:07:18 +02:00
|
|
|
//
|
2019-10-03 19:51:29 +02:00
|
|
|
// execute
|
2018-05-07 06:15:25 +02:00
|
|
|
//
|
2018-05-07 01:04:51 +02:00
|
|
|
|
2020-12-01 05:47:57 +01:00
|
|
|
ircd::m::vm::fault
|
|
|
|
ircd::m::vm::execute(eval &eval,
|
|
|
|
const json::array &pdus)
|
|
|
|
{
|
|
|
|
assert(eval.opts);
|
|
|
|
const auto &opts
|
|
|
|
{
|
|
|
|
*eval.opts
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it(begin(pdus));
|
|
|
|
const bool empty
|
|
|
|
{
|
|
|
|
it == end(pdus)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(empty))
|
|
|
|
return fault::ACCEPT;
|
|
|
|
|
|
|
|
// Determine iff pdus.size()==1 without iterating the whole array
|
|
|
|
const bool single
|
|
|
|
{
|
|
|
|
std::next(begin(pdus)) == end(pdus)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(likely(single))
|
|
|
|
{
|
|
|
|
const m::event event
|
|
|
|
{
|
|
|
|
json::object(*it)
|
|
|
|
};
|
|
|
|
|
|
|
|
return execute(eval, vector_view(&event, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
fault ret{fault::ACCEPT};
|
|
|
|
std::vector<m::event> eventv(64); do
|
|
|
|
{
|
|
|
|
size_t i(0);
|
|
|
|
for(; i < eventv.size() && it != end(pdus); ++i, ++it)
|
|
|
|
eventv[i] = json::object(*it);
|
|
|
|
|
|
|
|
if(likely(!opts.ordered))
|
|
|
|
std::sort(begin(eventv), begin(eventv) + i);
|
|
|
|
|
|
|
|
assert(i <= eventv.size());
|
|
|
|
execute(eval, vector_view(eventv.data(), i)); //XXX
|
|
|
|
}
|
|
|
|
while(it != end(pdus) && eval.evaluated < opts.limit);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::fault
|
2020-09-14 00:23:42 +02:00
|
|
|
ircd::m::vm::execute(eval &eval,
|
|
|
|
const vector_view<const event> &events)
|
|
|
|
{
|
|
|
|
assert(eval.opts);
|
|
|
|
const auto &opts
|
|
|
|
{
|
|
|
|
*eval.opts
|
|
|
|
};
|
|
|
|
|
|
|
|
const scope_restore eval_pdus
|
|
|
|
{
|
|
|
|
eval.pdus, events
|
|
|
|
};
|
|
|
|
|
2020-12-10 11:15:26 +01:00
|
|
|
const scope_count executing
|
|
|
|
{
|
|
|
|
eval::executing
|
|
|
|
};
|
|
|
|
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::EXECUTE
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool prefetch_keys
|
|
|
|
{
|
|
|
|
opts.phase[phase::VERIFY]
|
|
|
|
&& opts.mfetch_keys
|
|
|
|
&& events.size() > 1
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t prefetched_keys
|
|
|
|
{
|
|
|
|
prefetch_keys?
|
2020-12-19 08:10:03 +01:00
|
|
|
fetch_keys(eval): 0UL
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool prefetch_refs
|
|
|
|
{
|
|
|
|
opts.phase[phase::PREINDEX]
|
|
|
|
&& opts.mprefetch_refs
|
|
|
|
&& events.size() > 1
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t prefetched_refs
|
|
|
|
{
|
|
|
|
prefetch_refs?
|
|
|
|
vm::prefetch_refs(eval): 0UL
|
2020-12-10 11:15:26 +01:00
|
|
|
};
|
2020-09-14 00:23:42 +02:00
|
|
|
|
2020-09-14 23:14:24 +02:00
|
|
|
size_t accepted(0), existed(0), i, j, k;
|
2020-12-01 05:47:57 +01:00
|
|
|
for(i = 0; i < events.size(); i += j)
|
2020-09-14 00:23:42 +02:00
|
|
|
{
|
2020-09-14 23:14:24 +02:00
|
|
|
id::event ids[64];
|
2020-12-01 05:47:57 +01:00
|
|
|
for(j = 0; j < 64 && i + j < events.size() && eval.evaluated + j < opts.limit; ++j)
|
2020-09-14 23:14:24 +02:00
|
|
|
ids[j] = events[i + j].event_id;
|
|
|
|
|
|
|
|
// Bitset indicating which events already exist.
|
2020-12-15 20:06:48 +01:00
|
|
|
const uint64_t existing
|
2020-09-14 00:23:42 +02:00
|
|
|
{
|
2020-09-14 23:14:24 +02:00
|
|
|
!opts.replays?
|
|
|
|
m::exists(vector_view<const id::event>(ids, j)):
|
|
|
|
0UL
|
2020-09-14 00:23:42 +02:00
|
|
|
};
|
|
|
|
|
2020-12-01 05:47:57 +01:00
|
|
|
for(k = 0; k < j; ++k, ++eval.evaluated) try
|
2020-09-14 00:23:42 +02:00
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
const bool exists
|
|
|
|
(
|
|
|
|
existing & (1 << k)
|
|
|
|
);
|
|
|
|
|
|
|
|
const auto &event
|
|
|
|
{
|
|
|
|
events[i + k]
|
|
|
|
};
|
2020-09-14 00:23:42 +02:00
|
|
|
|
2020-12-15 20:06:48 +01:00
|
|
|
const auto fault
|
2020-09-14 23:14:24 +02:00
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
!exists?
|
|
|
|
execute(eval, event):
|
|
|
|
fault::EXISTS
|
2020-09-14 23:14:24 +02:00
|
|
|
};
|
|
|
|
|
2020-12-15 20:06:48 +01:00
|
|
|
existed += exists;
|
|
|
|
accepted += fault == fault::ACCEPT;
|
|
|
|
eval.accepted += fault == fault::ACCEPT;
|
|
|
|
eval.faulted += fault != fault::ACCEPT;
|
|
|
|
|
|
|
|
// If handle_fault() was not previously called about this eval
|
|
|
|
if(likely(fault == fault::ACCEPT || exists))
|
|
|
|
handle_fault(opts, fault, event.event_id?: eval.event_id, string_view{});
|
2020-09-14 23:14:24 +02:00
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
2020-12-01 05:47:57 +01:00
|
|
|
++eval.faulted;
|
2020-09-14 23:14:24 +02:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &)
|
|
|
|
{
|
2020-12-01 05:47:57 +01:00
|
|
|
++eval.faulted;
|
2020-09-14 23:14:24 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-12-01 05:47:57 +01:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
++eval.faulted;
|
|
|
|
throw;
|
|
|
|
}
|
2020-09-14 00:23:42 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 05:47:57 +01:00
|
|
|
return fault::ACCEPT;
|
2020-09-14 00:23:42 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 19:51:29 +02:00
|
|
|
ircd::m::vm::fault
|
2019-03-21 23:00:58 +01:00
|
|
|
ircd::m::vm::execute(eval &eval,
|
|
|
|
const event &event)
|
2020-03-26 18:24:35 +01:00
|
|
|
try
|
2017-11-30 20:01:14 +01:00
|
|
|
{
|
2019-09-09 21:53:34 +02:00
|
|
|
// This assertion is tripped if the end of your context's stack is
|
|
|
|
// danger close; try increasing your stack size.
|
|
|
|
const ctx::stack_usage_assertion sua;
|
|
|
|
|
|
|
|
const scope_notify notify
|
|
|
|
{
|
|
|
|
vm::dock
|
|
|
|
};
|
2019-03-21 23:58:18 +01:00
|
|
|
|
2020-03-20 20:44:23 +01:00
|
|
|
assert(eval.opts);
|
|
|
|
const auto &opts
|
2019-09-14 00:27:02 +02:00
|
|
|
{
|
2020-03-20 20:44:23 +01:00
|
|
|
*eval.opts
|
2019-09-14 00:27:02 +02:00
|
|
|
};
|
|
|
|
|
2020-09-17 11:09:40 +02:00
|
|
|
// Determine if this is an internal room creation event
|
|
|
|
const bool is_internal_room_create
|
|
|
|
{
|
2020-12-16 22:19:20 +01:00
|
|
|
json::get<"type"_>(event) == "m.room.create"
|
|
|
|
&& json::get<"sender"_>(event)
|
|
|
|
&& m::myself(json::get<"sender"_>(event))
|
2020-09-17 11:09:40 +02:00
|
|
|
};
|
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// Query for whether the room apropos is an internal room. Note that the
|
|
|
|
// room_id at this point may not be canonical; however, internal rooms
|
|
|
|
// do not and never will never use non-canonical room_id's.
|
2020-09-17 11:09:40 +02:00
|
|
|
const scope_restore room_internal
|
|
|
|
{
|
|
|
|
eval.room_internal,
|
2020-11-30 03:33:15 +01:00
|
|
|
|
|
|
|
// Retain any existing true value from predetermination.
|
2020-09-17 11:09:40 +02:00
|
|
|
eval.room_internal?
|
|
|
|
eval.room_internal:
|
2020-11-30 03:33:15 +01:00
|
|
|
|
|
|
|
// Case for creating an internal room (as a query will fail)
|
2020-09-17 11:09:40 +02:00
|
|
|
is_internal_room_create?
|
|
|
|
true:
|
2020-11-30 03:33:15 +01:00
|
|
|
|
|
|
|
// Query to find out if internal room.
|
2020-11-30 11:08:58 +01:00
|
|
|
json::get<"room_id"_>(event) && my(room::id(json::get<"room_id"_>(event)))?
|
|
|
|
m::internal(json::get<"room_id"_>(event)):
|
2020-11-30 03:33:15 +01:00
|
|
|
|
|
|
|
// Default to false
|
|
|
|
false
|
2020-09-17 11:09:40 +02:00
|
|
|
};
|
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// Reset the conformity report before and after this event's eval.
|
2020-11-30 11:08:58 +01:00
|
|
|
const scope_restore eval_report
|
|
|
|
{
|
|
|
|
eval.report, event::conforms{}
|
|
|
|
};
|
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// Conformity checks only require the event data itself; note that some
|
|
|
|
// local queries may still be made by the hook, such as m::redacted().
|
2020-11-30 04:46:20 +01:00
|
|
|
if(likely(opts.phase[phase::CONFORM]) && !opts.edu)
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::CONFORM
|
|
|
|
};
|
|
|
|
|
|
|
|
call_hook(conform_hook, eval, event, eval);
|
|
|
|
}
|
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// If the event is simply missing content while not being authoritatively
|
|
|
|
// redacted, the conformity phase would have thrown a prior exception. Now
|
|
|
|
// we know if the event is legitimately missing content.
|
2020-11-30 04:46:20 +01:00
|
|
|
const bool redacted
|
|
|
|
{
|
2020-12-01 10:50:37 +01:00
|
|
|
eval.report.has(event::conforms::MISMATCH_HASHES)
|
2020-11-30 04:46:20 +01:00
|
|
|
};
|
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// If the input JSON is insufficient we'll need a buffer to rewrite the
|
|
|
|
// event. This buffer can be reused by subsequent events in the eval.
|
2020-12-01 10:50:37 +01:00
|
|
|
assert(!eval.buf || size(eval.buf) >= event::MAX_SIZE);
|
2020-11-30 04:46:20 +01:00
|
|
|
if(!opts.edu && !eval.buf && (!opts.json_source || redacted))
|
|
|
|
eval.buf = unique_mutable_buffer
|
|
|
|
{
|
2020-12-16 22:19:20 +01:00
|
|
|
event::MAX_SIZE, simd::alignment
|
2020-11-30 04:46:20 +01:00
|
|
|
};
|
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// Conjure a view of the correct canonical JSON representation. This will
|
|
|
|
// either reference the input directly or the rewrite into eval.buf.
|
2020-11-30 11:08:58 +01:00
|
|
|
const json::object event_source
|
2020-11-30 04:46:20 +01:00
|
|
|
{
|
|
|
|
// Canonize from some other serialized source.
|
2020-12-01 10:50:37 +01:00
|
|
|
likely(!opts.edu && !opts.json_source && event.source && !redacted)?
|
2020-11-30 04:46:20 +01:00
|
|
|
json::stringify(mutable_buffer{eval.buf}, event.source):
|
|
|
|
|
|
|
|
// Canonize from no source; usually taken when my(event).
|
|
|
|
// XXX elision conditions go here
|
2020-12-01 10:50:37 +01:00
|
|
|
likely(!opts.edu && !opts.json_source && !redacted)?
|
2020-11-30 04:46:20 +01:00
|
|
|
json::stringify(mutable_buffer{eval.buf}, event):
|
|
|
|
|
2020-12-01 10:50:37 +01:00
|
|
|
// Canonize and redact from some other serialized source.
|
|
|
|
!opts.edu && !opts.json_source && event.source?
|
2020-12-21 19:26:59 +01:00
|
|
|
json::stringify(mutable_buffer{eval.buf}, m::essential(event.source, event::buf[0], true)):
|
2020-12-01 10:50:37 +01:00
|
|
|
|
|
|
|
// Canonize and redact from no source.
|
|
|
|
!opts.edu && !opts.json_source?
|
2020-12-21 19:26:59 +01:00
|
|
|
json::stringify(mutable_buffer{eval.buf}, m::essential(event, event::buf[0], true)):
|
2020-12-01 10:50:37 +01:00
|
|
|
|
2020-11-30 04:46:20 +01:00
|
|
|
// Use the input directly.
|
|
|
|
string_view{event.source}
|
|
|
|
};
|
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// Create a new event tuple from the canonical source, otherwise reference
|
|
|
|
// the existing input tuple directly. From now on we'll be referencing
|
|
|
|
// `_event` instead of `event` to ensure we have canonical values.
|
|
|
|
const m::event &_event
|
2020-11-30 11:08:58 +01:00
|
|
|
{
|
2020-12-16 22:19:20 +01:00
|
|
|
event_source?
|
|
|
|
m::event{event_source}:
|
|
|
|
event
|
2020-11-30 11:08:58 +01:00
|
|
|
};
|
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// Now conjure the corrected room_id and reference that for the duration
|
|
|
|
// of this event's eval.
|
2020-11-30 11:08:58 +01:00
|
|
|
const scope_restore eval_room_id
|
2020-11-30 04:46:20 +01:00
|
|
|
{
|
|
|
|
eval.room_id,
|
|
|
|
|
2020-11-30 11:08:58 +01:00
|
|
|
// Reassigns reference after any prior rewrites
|
|
|
|
likely(json::get<"room_id"_>(_event))?
|
|
|
|
string_view(json::get<"room_id"_>(_event)):
|
2020-11-30 04:46:20 +01:00
|
|
|
|
|
|
|
// No action
|
|
|
|
eval.room_id,
|
|
|
|
};
|
|
|
|
|
2019-07-17 21:15:17 +02:00
|
|
|
// Procure the room version.
|
2019-07-25 23:02:25 +02:00
|
|
|
char room_version_buf[room::VERSION_MAX_SIZE];
|
2019-07-10 11:14:38 +02:00
|
|
|
const scope_restore eval_room_version
|
|
|
|
{
|
|
|
|
eval.room_version,
|
2019-07-17 21:15:17 +02:00
|
|
|
|
|
|
|
// The room version was supplied by the user in the options structure
|
|
|
|
// because they know better.
|
2019-07-10 11:14:38 +02:00
|
|
|
eval.opts->room_version?
|
|
|
|
eval.opts->room_version:
|
2019-07-17 21:15:17 +02:00
|
|
|
|
|
|
|
// The room version was already computed; probably by vm::inject().
|
|
|
|
eval.room_version?
|
|
|
|
eval.room_version:
|
|
|
|
|
|
|
|
// There's no room version because there's no room!
|
|
|
|
!eval.room_id?
|
|
|
|
string_view{}:
|
|
|
|
|
2020-12-01 08:50:56 +01:00
|
|
|
// Special case for create event
|
|
|
|
json::get<"type"_>(event) == "m.room.create"?
|
|
|
|
json::string(json::get<"content"_>(_event).get("room_version", "1"_sv)):
|
|
|
|
|
|
|
|
// Special case for v1 distinguishable event_id's
|
|
|
|
_event.event_id && _event.event_id.version() == "1"_sv?
|
|
|
|
"1"_sv:
|
|
|
|
|
2019-07-17 21:15:17 +02:00
|
|
|
// Make a query for the room version into the stack buffer.
|
|
|
|
m::version(room_version_buf, room{eval.room_id}, std::nothrow)
|
2019-07-10 11:14:38 +02:00
|
|
|
};
|
|
|
|
|
2020-11-30 01:43:52 +01:00
|
|
|
// Copy the event_id into the eval buffer
|
|
|
|
eval.event_id =
|
|
|
|
{
|
2020-11-30 11:08:58 +01:00
|
|
|
!opts.edu && !_event.event_id && eval.room_version == "3"?
|
|
|
|
event::id::buf{event::id::v3{eval.event_id, _event}}:
|
2020-11-30 01:43:52 +01:00
|
|
|
|
2020-11-30 11:08:58 +01:00
|
|
|
!opts.edu && !_event.event_id?
|
|
|
|
event::id::buf{event::id::v4{eval.event_id, _event}}:
|
2020-11-30 01:43:52 +01:00
|
|
|
|
|
|
|
!opts.edu?
|
2020-11-30 11:08:58 +01:00
|
|
|
event::id::buf{_event.event_id}:
|
2020-11-30 01:43:52 +01:00
|
|
|
|
|
|
|
event::id::buf{}
|
|
|
|
};
|
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// Enter the phase to check and hold for other evals with the same event_id
|
|
|
|
// to prevent race-conditions. Note that duplicates are blocked but never
|
|
|
|
// rejected here, as the first eval might fail and the second might not.
|
2020-12-10 10:49:27 +01:00
|
|
|
if(likely(opts.phase[phase::DUPWAIT]) && eval.event_id)
|
2020-12-03 03:54:20 +01:00
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
2020-12-10 10:49:27 +01:00
|
|
|
eval.phase, phase::DUPWAIT
|
2020-12-03 03:54:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Prevent more than one event with the same event_id from
|
|
|
|
// being evaluated at the same time.
|
|
|
|
if(likely(opts.unique))
|
|
|
|
vm::dock.wait([&eval]
|
|
|
|
{
|
|
|
|
assert(eval::count(eval.event_id) <= 1);
|
|
|
|
return eval::count(eval.event_id) == 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Point to the new event_id.
|
|
|
|
const scope_restore event_event_id
|
|
|
|
{
|
|
|
|
mutable_cast(_event).event_id,
|
|
|
|
event::id{eval.event_id}
|
|
|
|
};
|
|
|
|
|
2020-11-30 01:43:52 +01:00
|
|
|
// Set a member pointer to the event currently being evaluated. This
|
|
|
|
// allows other parallel evals to have deep access to this eval. It also
|
|
|
|
// will be used to count this event as currently being evaluated.
|
|
|
|
assert(!eval.event_);
|
|
|
|
const scope_restore eval_event
|
|
|
|
{
|
2020-11-30 11:08:58 +01:00
|
|
|
eval.event_, &_event
|
2020-11-30 01:43:52 +01:00
|
|
|
};
|
2020-03-20 20:44:23 +01:00
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// Now that the final input is constructed and everything is known about it,
|
|
|
|
// the next frame's exception handlers will build and propagate much better
|
|
|
|
// error messages.
|
2020-11-30 11:08:58 +01:00
|
|
|
return execute_du(eval, _event);
|
2020-03-26 01:11:39 +01:00
|
|
|
}
|
2020-03-26 18:24:35 +01:00
|
|
|
catch(const vm::error &e)
|
|
|
|
{
|
|
|
|
throw; // propagate from execute_du
|
|
|
|
}
|
|
|
|
catch(const m::error &e)
|
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
const ctx::exception_handler eh;
|
2020-03-26 18:24:35 +01:00
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
e.content
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(eval.opts);
|
2020-12-15 20:06:48 +01:00
|
|
|
return handle_fault
|
2020-03-26 18:24:35 +01:00
|
|
|
(
|
2020-12-15 20:06:48 +01:00
|
|
|
*eval.opts, fault::GENERAL, event.event_id,
|
2020-03-26 18:24:35 +01:00
|
|
|
"eval %s %s :%s :%s :%s",
|
|
|
|
event.event_id?
|
|
|
|
string_view{event.event_id}:
|
|
|
|
"<unknown>"_sv,
|
|
|
|
json::get<"room_id"_>(event)?
|
|
|
|
string_view{json::get<"room_id"_>(event)}:
|
|
|
|
"<unknown>"_sv,
|
|
|
|
e.what(),
|
|
|
|
json::string{content["errcode"]},
|
|
|
|
json::string{content["error"]}
|
|
|
|
);
|
|
|
|
}
|
2020-04-06 19:37:51 +02:00
|
|
|
catch(const ctx::interrupted &)
|
2020-03-26 18:24:35 +01:00
|
|
|
{
|
2020-04-06 19:37:51 +02:00
|
|
|
throw;
|
2020-03-26 18:24:35 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
const ctx::exception_handler eh;
|
|
|
|
|
2020-03-26 18:24:35 +01:00
|
|
|
assert(eval.opts);
|
2020-12-15 20:06:48 +01:00
|
|
|
return handle_fault
|
2020-03-26 18:24:35 +01:00
|
|
|
(
|
2020-12-15 20:06:48 +01:00
|
|
|
*eval.opts, fault::GENERAL, event.event_id,
|
2020-03-26 18:24:35 +01:00
|
|
|
"eval %s %s (General Protection) :%s",
|
|
|
|
event.event_id?
|
|
|
|
string_view{event.event_id}:
|
|
|
|
"<unknown>"_sv,
|
|
|
|
json::get<"room_id"_>(event)?
|
|
|
|
string_view{json::get<"room_id"_>(event)}:
|
|
|
|
"<unknown>"_sv,
|
|
|
|
e.what()
|
|
|
|
);
|
|
|
|
}
|
2020-03-26 01:11:39 +01:00
|
|
|
|
|
|
|
ircd::m::vm::fault
|
|
|
|
ircd::m::vm::execute_du(eval &eval,
|
|
|
|
const event &event)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(eval.id);
|
|
|
|
assert(eval.ctx);
|
|
|
|
assert(eval.opts);
|
2020-03-20 20:44:23 +01:00
|
|
|
assert(eval.opts->edu || event.event_id);
|
|
|
|
assert(eval.opts->edu || eval.event_id);
|
|
|
|
assert(eval.event_id == event.event_id);
|
|
|
|
assert(eval.event_);
|
2020-03-26 01:11:39 +01:00
|
|
|
const auto &opts
|
|
|
|
{
|
|
|
|
*eval.opts
|
|
|
|
};
|
2020-03-20 20:44:23 +01:00
|
|
|
|
2020-04-21 05:18:02 +02:00
|
|
|
const scope_restore eval_sequence
|
|
|
|
{
|
|
|
|
eval.sequence, 0UL
|
|
|
|
};
|
|
|
|
|
2019-07-17 21:15:17 +02:00
|
|
|
// The issue hook is only called when this server is injecting a newly
|
|
|
|
// created event.
|
2020-05-12 04:37:55 +02:00
|
|
|
if(opts.phase[phase::ISSUE] && eval.copts && eval.copts->issue)
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::ISSUE
|
|
|
|
};
|
|
|
|
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(issue_hook, eval, event, eval);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2019-03-09 22:12:58 +01:00
|
|
|
|
2019-07-17 21:15:17 +02:00
|
|
|
// Branch on whether the event is an EDU or a PDU
|
2018-03-13 03:07:41 +01:00
|
|
|
const fault ret
|
|
|
|
{
|
2020-12-01 11:24:40 +01:00
|
|
|
event.event_id && !opts.edu?
|
2019-03-21 23:00:58 +01:00
|
|
|
execute_pdu(eval, event):
|
|
|
|
execute_edu(eval, event)
|
2018-03-13 03:07:41 +01:00
|
|
|
};
|
|
|
|
|
2019-07-17 21:15:17 +02:00
|
|
|
// ret can be a fault code if the user masked the exception from being
|
|
|
|
// thrown. If there's an error code here nothing further is done.
|
2018-03-13 03:07:41 +01:00
|
|
|
if(ret != fault::ACCEPT)
|
|
|
|
return ret;
|
2018-03-07 20:02:23 +01:00
|
|
|
|
2019-10-03 19:51:29 +02:00
|
|
|
if(opts.debuglog_accept || bool(log_accept_debug))
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s", pretty_oneline(event)
|
|
|
|
};
|
|
|
|
|
2019-07-17 21:15:17 +02:00
|
|
|
// The event was executed; now we broadcast the good news. This will
|
|
|
|
// include notifying client `/sync` and the federation sender.
|
2020-05-12 04:37:55 +02:00
|
|
|
if(likely(opts.phase[phase::NOTIFY]))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::NOTIFY
|
|
|
|
};
|
|
|
|
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(notify_hook, eval, event, eval);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2018-03-13 03:07:41 +01:00
|
|
|
|
2019-07-17 21:15:17 +02:00
|
|
|
// The "effects" of the event are created by listeners on the effect hook.
|
|
|
|
// These can include the creation of even more events, such as creating a
|
|
|
|
// PDU out of an EDU, etc. Unlike the post_hook in execute_pdu(), the
|
|
|
|
// notify for the event at issue here has already been made.
|
2020-05-12 04:37:55 +02:00
|
|
|
if(likely(opts.phase[phase::EFFECTS]))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::EFFECTS
|
|
|
|
};
|
|
|
|
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(effect_hook, eval, event, eval);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2018-03-13 03:07:41 +01:00
|
|
|
|
2019-09-11 20:27:42 +02:00
|
|
|
if(opts.infolog_accept || bool(log_accept_info))
|
|
|
|
log::info
|
2018-08-16 09:46:46 +02:00
|
|
|
{
|
|
|
|
log, "%s", pretty_oneline(event)
|
|
|
|
};
|
2018-03-13 03:07:41 +01:00
|
|
|
|
|
|
|
return ret;
|
2018-03-07 20:02:23 +01:00
|
|
|
}
|
2020-03-04 04:31:21 +01:00
|
|
|
catch(const vm::error &e) // VM FAULT CODE
|
2018-03-07 20:02:23 +01:00
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
const ctx::exception_handler eh;
|
2020-03-04 04:31:21 +01:00
|
|
|
const json::object &content{e.content};
|
|
|
|
const json::string &error
|
|
|
|
{
|
|
|
|
content["error"]
|
|
|
|
};
|
|
|
|
|
2020-12-15 20:06:48 +01:00
|
|
|
assert(eval.opts);
|
|
|
|
return handle_fault
|
2020-03-26 18:24:35 +01:00
|
|
|
(
|
2020-12-15 20:06:48 +01:00
|
|
|
*eval.opts, e.code, event.event_id,
|
2020-03-26 18:24:35 +01:00
|
|
|
"execute %s %s :%s",
|
2020-03-04 04:31:21 +01:00
|
|
|
event.event_id?
|
|
|
|
string_view{event.event_id}:
|
2020-03-26 18:24:35 +01:00
|
|
|
"<edu>"_sv,
|
2020-03-04 04:31:21 +01:00
|
|
|
eval.room_id?
|
|
|
|
eval.room_id:
|
|
|
|
"<edu>"_sv,
|
|
|
|
error
|
2018-12-10 22:03:15 +01:00
|
|
|
);
|
2018-03-07 20:02:23 +01:00
|
|
|
}
|
2018-05-15 07:24:03 +02:00
|
|
|
catch(const m::error &e) // GENERAL MATRIX ERROR
|
2018-04-13 08:06:02 +02:00
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
const ctx::exception_handler eh;
|
2020-03-04 04:31:21 +01:00
|
|
|
const json::object &content{e.content};
|
|
|
|
const json::string error[]
|
|
|
|
{
|
|
|
|
content["errcode"],
|
|
|
|
content["error"]
|
|
|
|
};
|
|
|
|
|
2020-12-15 20:06:48 +01:00
|
|
|
assert(eval.opts);
|
|
|
|
return handle_fault
|
2020-03-26 18:24:35 +01:00
|
|
|
(
|
2020-12-15 20:06:48 +01:00
|
|
|
*eval.opts, fault::GENERAL, event.event_id,
|
2020-06-05 02:55:36 +02:00
|
|
|
"execute %s %s :%s :%s",
|
2020-03-04 04:31:21 +01:00
|
|
|
event.event_id?
|
|
|
|
string_view{event.event_id}:
|
2020-03-26 18:24:35 +01:00
|
|
|
"<edu>"_sv,
|
2020-03-04 04:31:21 +01:00
|
|
|
eval.room_id?
|
|
|
|
eval.room_id:
|
|
|
|
"<edu>"_sv,
|
|
|
|
error[0],
|
|
|
|
error[1]
|
2018-12-10 22:03:15 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &e) // INTERRUPTION
|
|
|
|
{
|
2020-04-06 19:37:51 +02:00
|
|
|
throw;
|
2018-04-13 08:06:02 +02:00
|
|
|
}
|
2018-05-15 07:24:03 +02:00
|
|
|
catch(const std::exception &e) // ALL OTHER ERRORS
|
2018-03-07 20:02:23 +01:00
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
const ctx::exception_handler eh;
|
|
|
|
|
|
|
|
assert(eval.opts);
|
|
|
|
return handle_fault
|
2020-03-26 18:24:35 +01:00
|
|
|
(
|
2020-12-15 20:06:48 +01:00
|
|
|
*eval.opts, fault::GENERAL, event.event_id,
|
2020-03-26 18:24:35 +01:00
|
|
|
"execute %s %s (General Protection) :%s",
|
2020-03-04 04:31:21 +01:00
|
|
|
event.event_id?
|
|
|
|
string_view{event.event_id}:
|
2020-03-26 18:24:35 +01:00
|
|
|
"<edu>"_sv,
|
2020-03-04 04:31:21 +01:00
|
|
|
eval.room_id?
|
|
|
|
eval.room_id:
|
|
|
|
"<edu>"_sv,
|
2018-12-10 22:03:15 +01:00
|
|
|
e.what()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:51:29 +02:00
|
|
|
ircd::m::vm::fault
|
2019-03-21 23:00:58 +01:00
|
|
|
ircd::m::vm::execute_edu(eval &eval,
|
2019-07-06 06:33:58 +02:00
|
|
|
const event &event)
|
2018-03-07 20:02:23 +01:00
|
|
|
{
|
2020-05-12 04:37:55 +02:00
|
|
|
if(likely(eval.opts->phase[phase::EVALUATE]))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::EVALUATE
|
|
|
|
};
|
|
|
|
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(eval_hook, eval, event, eval);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(likely(eval.opts->phase[phase::POST]))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::POST
|
|
|
|
};
|
2018-10-11 10:18:21 +02:00
|
|
|
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(post_hook, eval, event, eval);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2019-04-07 03:18:17 +02:00
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
return fault::ACCEPT;
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:51:29 +02:00
|
|
|
ircd::m::vm::fault
|
2019-03-21 23:00:58 +01:00
|
|
|
ircd::m::vm::execute_pdu(eval &eval,
|
2019-03-29 00:00:54 +01:00
|
|
|
const event &event)
|
2018-03-07 20:02:23 +01:00
|
|
|
{
|
2019-03-21 23:58:18 +01:00
|
|
|
const scope_count pending
|
|
|
|
{
|
|
|
|
sequence::pending
|
|
|
|
};
|
|
|
|
|
2019-09-09 21:16:48 +02:00
|
|
|
const scope_notify sequence_dock
|
|
|
|
{
|
|
|
|
sequence::dock, scope_notify::all
|
|
|
|
};
|
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
assert(eval.opts);
|
|
|
|
const auto &opts
|
|
|
|
{
|
|
|
|
*eval.opts
|
|
|
|
};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
const m::event::id &event_id
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-07-06 06:09:07 +02:00
|
|
|
event.event_id
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
const m::room::id &room_id
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-02-28 07:53:44 +01:00
|
|
|
at<"room_id"_>(event)
|
2018-02-08 22:23:01 +01:00
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-09-05 08:28:01 +02:00
|
|
|
const string_view &type
|
|
|
|
{
|
|
|
|
at<"type"_>(event)
|
|
|
|
};
|
|
|
|
|
2019-09-17 01:28:55 +02:00
|
|
|
const bool authenticate
|
|
|
|
{
|
2020-03-22 01:23:08 +01:00
|
|
|
opts.auth && !eval.room_internal
|
2019-09-17 01:28:55 +02:00
|
|
|
};
|
|
|
|
|
2020-12-16 22:19:20 +01:00
|
|
|
// There is no reason for an event from another origin to be sent to an
|
|
|
|
// internal room. This boxes internal room access as a local problem, with
|
|
|
|
// local mistakes.
|
|
|
|
if(unlikely(eval.room_internal && !my(event)))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
fault::GENERAL, "Internal room event denied from external source."
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if an event with the same ID was already accepted.
|
2020-12-15 22:08:38 +01:00
|
|
|
if(likely(opts.phase[phase::DUPCHK]))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::DUPCHK
|
|
|
|
};
|
|
|
|
|
|
|
|
// Prevent the same event from being accepted twice.
|
|
|
|
if(likely(!opts.replays) && m::exists(event_id))
|
|
|
|
{
|
|
|
|
if(unlikely(~opts.nothrows & fault::EXISTS))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
fault::EXISTS, "Event has already been evaluated."
|
|
|
|
};
|
|
|
|
|
|
|
|
return fault::EXISTS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 03:54:20 +01:00
|
|
|
assert(!opts.unique || eval::count(event_id) == 1);
|
|
|
|
assert(opts.replays || !m::exists(event_id));
|
2020-12-16 22:19:20 +01:00
|
|
|
|
|
|
|
// Check if event's proprietor is denied by the room ACL.
|
2020-05-12 04:37:55 +02:00
|
|
|
if(likely(opts.phase[phase::ACCESS]))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::ACCESS
|
|
|
|
};
|
|
|
|
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(access_hook, eval, event, eval);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2019-05-27 05:36:26 +02:00
|
|
|
|
2020-12-16 23:22:21 +01:00
|
|
|
// Check if this event is relevant to this server.
|
|
|
|
if(likely(opts.phase[phase::EMPTION]) && !eval.room_internal)
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::EMPTION
|
|
|
|
};
|
|
|
|
|
2020-12-18 09:50:16 +01:00
|
|
|
emption_check(eval, event);
|
2020-12-16 23:22:21 +01:00
|
|
|
}
|
|
|
|
|
2020-05-12 09:13:24 +02:00
|
|
|
if(likely(opts.phase[phase::VERIFY]))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
2019-04-28 05:01:27 +02:00
|
|
|
{
|
2020-05-12 09:13:24 +02:00
|
|
|
eval.phase, phase::VERIFY
|
2019-04-28 05:01:27 +02:00
|
|
|
};
|
2018-03-22 09:31:17 +01:00
|
|
|
|
2020-05-13 02:15:01 +02:00
|
|
|
if(!verify(event))
|
2020-05-12 09:13:24 +02:00
|
|
|
throw m::BAD_SIGNATURE
|
|
|
|
{
|
|
|
|
"Signature verification failed."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-12 05:24:54 +02:00
|
|
|
if(likely(opts.phase[phase::FETCH_AUTH] && opts.fetch))
|
2020-05-12 04:37:55 +02:00
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
2020-05-12 05:24:54 +02:00
|
|
|
eval.phase, phase::FETCH_AUTH
|
2020-05-12 04:37:55 +02:00
|
|
|
};
|
|
|
|
|
2020-05-12 05:24:54 +02:00
|
|
|
call_hook(fetch_auth_hook, eval, event, eval);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2018-04-17 02:28:26 +02:00
|
|
|
|
2019-08-17 15:37:43 +02:00
|
|
|
// Evaluation by auth system; throws
|
2020-05-12 05:24:54 +02:00
|
|
|
if(likely(opts.phase[phase::AUTH_STATIC]) && authenticate)
|
2020-04-28 04:54:54 +02:00
|
|
|
{
|
2020-05-12 04:37:55 +02:00
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
2020-05-12 05:24:54 +02:00
|
|
|
eval.phase, phase::AUTH_STATIC
|
2020-05-12 04:37:55 +02:00
|
|
|
};
|
|
|
|
|
2020-04-28 04:54:54 +02:00
|
|
|
const auto &[pass, fail]
|
|
|
|
{
|
|
|
|
room::auth::check_static(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!pass)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
fault::AUTH, "Fails against provided auth_events :%s",
|
|
|
|
what(fail)
|
|
|
|
};
|
|
|
|
}
|
2019-08-17 15:37:43 +02:00
|
|
|
|
2020-05-12 05:24:54 +02:00
|
|
|
if(likely(opts.phase[phase::FETCH_PREV] && opts.fetch))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::FETCH_PREV
|
|
|
|
};
|
|
|
|
|
|
|
|
call_hook(fetch_prev_hook, eval, event, eval);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(likely(opts.phase[phase::FETCH_STATE] && opts.fetch))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::FETCH_STATE
|
|
|
|
};
|
|
|
|
|
|
|
|
call_hook(fetch_state_hook, eval, event, eval);
|
|
|
|
}
|
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
// Obtain sequence number here.
|
2020-12-14 12:56:03 +01:00
|
|
|
const auto top(eval::seqmax());
|
2019-03-21 22:13:45 +01:00
|
|
|
eval.sequence =
|
2019-03-19 19:45:01 +01:00
|
|
|
{
|
2019-03-21 22:13:45 +01:00
|
|
|
top?
|
2020-05-13 04:18:23 +02:00
|
|
|
std::max(sequence::get(*top) + 1, sequence::uncommitted + 1):
|
2020-12-14 12:56:03 +01:00
|
|
|
sequence::uncommitted + 1
|
2019-03-21 22:13:45 +01:00
|
|
|
};
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2019-09-17 01:42:22 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2020-05-13 05:23:29 +02:00
|
|
|
log, "%s event sequenced",
|
|
|
|
loghead(eval)
|
2019-09-17 01:42:22 +02:00
|
|
|
};
|
|
|
|
|
2020-12-14 12:56:03 +01:00
|
|
|
assert(eval::sequnique(eval.sequence));
|
2020-05-13 04:18:23 +02:00
|
|
|
const auto &parent_phase
|
|
|
|
{
|
|
|
|
eval.parent?
|
|
|
|
eval.parent->phase:
|
|
|
|
phase::NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &parent_post
|
|
|
|
{
|
|
|
|
parent_phase == phase::POST
|
|
|
|
&& eval.parent->event_
|
|
|
|
&& eval.parent->event_->event_id
|
|
|
|
};
|
|
|
|
|
2020-12-05 14:12:45 +01:00
|
|
|
// Allocate transaction; prefetch dependencies.
|
2020-12-19 08:10:03 +01:00
|
|
|
if(likely(opts.phase[phase::PREINDEX]) && !opts.mprefetch_refs)
|
2020-12-05 14:12:45 +01:00
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::PREINDEX
|
|
|
|
};
|
|
|
|
|
|
|
|
dbs::write_opts wopts(opts.wopts);
|
|
|
|
wopts.event_idx = eval.sequence;
|
|
|
|
const size_t prefetched
|
|
|
|
{
|
|
|
|
dbs::prefetch(event, wopts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-12 04:37:55 +02:00
|
|
|
const scope_restore eval_phase_precommit
|
|
|
|
{
|
|
|
|
eval.phase, phase::PRECOMMIT
|
|
|
|
};
|
|
|
|
|
2019-09-17 01:28:55 +02:00
|
|
|
// Wait until this is the lowest sequence number
|
2020-05-13 04:18:23 +02:00
|
|
|
sequence::dock.wait([&eval, &parent_post]
|
2019-09-17 01:28:55 +02:00
|
|
|
{
|
2020-05-13 04:18:23 +02:00
|
|
|
return false
|
|
|
|
|| parent_post
|
|
|
|
|| eval::seqnext(sequence::committed) == &eval
|
|
|
|
|| eval::seqnext(sequence::uncommitted) == &eval
|
|
|
|
;
|
2019-09-17 01:28:55 +02:00
|
|
|
});
|
|
|
|
|
2020-05-13 02:15:01 +02:00
|
|
|
if(likely(opts.phase[phase::AUTH_RELA] && authenticate))
|
2020-04-28 04:54:54 +02:00
|
|
|
{
|
2020-05-12 05:24:54 +02:00
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::AUTH_RELA
|
|
|
|
};
|
|
|
|
|
2020-04-28 04:54:54 +02:00
|
|
|
const auto &[pass, fail]
|
|
|
|
{
|
|
|
|
room::auth::check_relative(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!pass)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
fault::AUTH, "Fails relative to the state at the event :%s",
|
|
|
|
what(fail)
|
|
|
|
};
|
|
|
|
}
|
2019-09-17 01:28:55 +02:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
assert(eval.sequence != 0);
|
|
|
|
assert(eval::sequnique(sequence::get(eval)));
|
2020-05-13 04:18:23 +02:00
|
|
|
//assert(sequence::uncommitted <= sequence::get(eval));
|
|
|
|
//assert(sequence::committed < sequence::get(eval));
|
|
|
|
assert(sequence::retired < sequence::get(eval));
|
|
|
|
sequence::uncommitted = std::max(sequence::get(eval), sequence::uncommitted);
|
2018-09-05 08:28:01 +02:00
|
|
|
|
2020-05-12 04:37:55 +02:00
|
|
|
const scope_restore eval_phase_commit
|
|
|
|
{
|
|
|
|
eval.phase, phase::COMMIT
|
|
|
|
};
|
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
// Wait until this is the lowest sequence number
|
2020-05-13 04:18:23 +02:00
|
|
|
sequence::dock.wait([&eval, &parent_post]
|
2019-03-19 19:45:01 +01:00
|
|
|
{
|
2020-05-13 03:19:58 +02:00
|
|
|
return false
|
2020-05-13 04:18:23 +02:00
|
|
|
|| parent_post
|
2020-05-13 03:19:58 +02:00
|
|
|
|| eval::seqnext(sequence::committed) == &eval
|
|
|
|
;
|
2019-03-21 22:13:45 +01:00
|
|
|
});
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2019-09-17 01:28:55 +02:00
|
|
|
// Reevaluation of auth against the present state of the room.
|
2020-05-13 02:15:01 +02:00
|
|
|
if(likely(opts.phase[phase::AUTH_PRES] && authenticate))
|
2020-05-12 04:37:55 +02:00
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
2020-05-12 05:24:54 +02:00
|
|
|
eval.phase, phase::AUTH_PRES
|
2020-05-12 04:37:55 +02:00
|
|
|
};
|
|
|
|
|
2019-09-17 01:28:55 +02:00
|
|
|
room::auth::check_present(event);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2019-09-17 01:28:55 +02:00
|
|
|
|
|
|
|
// Evaluation by module hooks
|
2020-05-12 04:37:55 +02:00
|
|
|
if(likely(opts.phase[phase::EVALUATE]))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::EVALUATE
|
|
|
|
};
|
|
|
|
|
2019-09-17 01:28:55 +02:00
|
|
|
call_hook(eval_hook, eval, event, eval);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2019-09-17 01:28:55 +02:00
|
|
|
|
2020-05-12 11:47:48 +02:00
|
|
|
// Allocate transaction; discover shared-sequenced evals.
|
|
|
|
if(likely(opts.phase[phase::INDEX]))
|
|
|
|
{
|
2020-08-28 11:14:46 +02:00
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::INDEX
|
|
|
|
};
|
|
|
|
|
|
|
|
// Transaction composition.
|
2020-12-06 17:05:33 +01:00
|
|
|
write_append(eval, event, parent_post);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2019-03-20 02:02:35 +01:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
// Generate post-eval/pre-notify effects. This function may conduct
|
|
|
|
// an entire eval of several more events recursively before returning.
|
2020-05-12 04:37:55 +02:00
|
|
|
if(likely(opts.phase[phase::POST]))
|
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::POST
|
|
|
|
};
|
|
|
|
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(post_hook, eval, event, eval);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2018-12-31 02:02:19 +01:00
|
|
|
|
2020-05-13 04:18:23 +02:00
|
|
|
assert(sequence::committed < sequence::get(eval));
|
|
|
|
assert(sequence::retired < sequence::get(eval));
|
|
|
|
sequence::committed = !parent_post?
|
|
|
|
sequence::get(eval):
|
|
|
|
sequence::committed;
|
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
// Commit the transaction to database iff this eval is at the stack base.
|
2020-05-13 04:18:23 +02:00
|
|
|
if(likely(opts.phase[phase::WRITE] && !parent_post))
|
2020-05-12 04:37:55 +02:00
|
|
|
{
|
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::WRITE
|
|
|
|
};
|
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
write_commit(eval);
|
2020-05-12 04:37:55 +02:00
|
|
|
}
|
2019-03-20 02:02:35 +01:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
// Wait for sequencing only if this is the stack base, otherwise we'll
|
|
|
|
// never return back to that stack base.
|
2020-05-13 04:18:23 +02:00
|
|
|
if(likely(!parent_post))
|
2019-03-20 02:02:35 +01:00
|
|
|
{
|
2020-05-12 04:37:55 +02:00
|
|
|
const scope_restore eval_phase
|
|
|
|
{
|
|
|
|
eval.phase, phase::RETIRE
|
|
|
|
};
|
|
|
|
|
2020-12-18 09:50:16 +01:00
|
|
|
retire(eval, event);
|
|
|
|
}
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2020-12-18 09:50:16 +01:00
|
|
|
return fault::ACCEPT;
|
|
|
|
}
|
2020-05-13 03:19:58 +02:00
|
|
|
|
2020-12-18 09:50:16 +01:00
|
|
|
void
|
|
|
|
ircd::m::vm::retire(eval &eval,
|
|
|
|
const event &event)
|
2020-04-21 05:25:12 +02:00
|
|
|
|
2020-12-18 09:50:16 +01:00
|
|
|
{
|
|
|
|
sequence::dock.wait([&eval]
|
|
|
|
{
|
|
|
|
return eval::seqnext(sequence::retired) == std::addressof(eval);
|
|
|
|
});
|
2020-04-21 05:25:12 +02:00
|
|
|
|
2020-12-18 09:50:16 +01:00
|
|
|
const auto next
|
|
|
|
{
|
|
|
|
eval::seqnext(eval.sequence)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto highest
|
|
|
|
{
|
|
|
|
next?
|
|
|
|
sequence::get(*next):
|
|
|
|
sequence::get(eval)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto retire
|
|
|
|
{
|
|
|
|
std::clamp
|
|
|
|
(
|
2019-03-19 19:45:01 +01:00
|
|
|
sequence::get(eval),
|
2020-12-18 09:50:16 +01:00
|
|
|
sequence::retired + 1,
|
|
|
|
highest
|
|
|
|
)
|
|
|
|
};
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2020-12-18 09:50:16 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s %lu:%lu release %lu",
|
|
|
|
loghead(eval),
|
|
|
|
sequence::get(eval),
|
|
|
|
retire,
|
|
|
|
highest,
|
|
|
|
};
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2020-12-18 09:50:16 +01:00
|
|
|
assert(sequence::get(eval) <= retire);
|
|
|
|
assert(sequence::retired < sequence::get(eval));
|
|
|
|
assert(sequence::retired < retire);
|
|
|
|
sequence::retired = retire;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::vm::write_commit(eval &eval)
|
|
|
|
{
|
|
|
|
assert(eval.txn);
|
|
|
|
assert(eval.txn.use_count() == 1);
|
|
|
|
auto &txn
|
|
|
|
{
|
|
|
|
*eval.txn
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef RB_DEBUG
|
|
|
|
const auto db_seq_before(db::sequence(*m::dbs::events));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
txn();
|
|
|
|
|
|
|
|
#ifdef RB_DEBUG
|
|
|
|
const auto db_seq_after(db::sequence(*m::dbs::events));
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s wrote %lu | db seq %lu:%lu %zu cells in %zu bytes to events database ...",
|
|
|
|
loghead(eval),
|
|
|
|
sequence::get(eval),
|
|
|
|
db_seq_before,
|
|
|
|
db_seq_after,
|
|
|
|
txn.size(),
|
|
|
|
txn.bytes()
|
|
|
|
};
|
|
|
|
#endif
|
2018-12-31 02:02:19 +01:00
|
|
|
}
|
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
void
|
|
|
|
ircd::m::vm::write_append(eval &eval,
|
2020-12-06 17:05:33 +01:00
|
|
|
const event &event,
|
|
|
|
const bool &parent_post)
|
2018-04-17 02:28:26 +02:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
{
|
|
|
|
assert(eval.opts);
|
2020-03-04 04:53:14 +01:00
|
|
|
const auto &opts
|
|
|
|
{
|
|
|
|
*eval.opts
|
|
|
|
};
|
|
|
|
|
2020-12-07 00:05:51 +01:00
|
|
|
assert(eval.room_id);
|
|
|
|
const room room
|
|
|
|
{
|
|
|
|
eval.room_id
|
|
|
|
};
|
|
|
|
|
2020-12-06 17:05:33 +01:00
|
|
|
if(eval.txn)
|
|
|
|
eval.txn->clear();
|
|
|
|
|
|
|
|
if(!eval.txn && parent_post)
|
|
|
|
eval.txn = eval.parent->txn;
|
|
|
|
|
|
|
|
if(!eval.txn)
|
|
|
|
eval.txn = std::make_shared<db::txn>
|
|
|
|
(
|
|
|
|
*dbs::events, db::txn::opts
|
|
|
|
{
|
|
|
|
calc_txn_reserve(opts, event), // reserve_bytes
|
|
|
|
0, // max_bytes (no max)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
assert(eval.txn);
|
2020-03-04 04:53:14 +01:00
|
|
|
auto &txn
|
|
|
|
{
|
|
|
|
*eval.txn
|
|
|
|
};
|
2019-03-21 22:13:45 +01:00
|
|
|
|
2019-05-12 00:30:40 +02:00
|
|
|
m::dbs::write_opts wopts(opts.wopts);
|
2020-05-13 04:18:23 +02:00
|
|
|
wopts.interpose = eval.txn.get();
|
2019-08-24 01:56:58 +02:00
|
|
|
wopts.event_idx = eval.sequence;
|
2020-11-30 01:18:40 +01:00
|
|
|
wopts.json_source = true;
|
2020-03-04 04:53:14 +01:00
|
|
|
|
2020-03-04 21:48:37 +01:00
|
|
|
// Don't update or resolve the room head with this shit.
|
2020-09-12 05:10:14 +02:00
|
|
|
const bool dummy_event
|
|
|
|
{
|
|
|
|
json::get<"type"_>(event) == "org.matrix.dummy_event"
|
|
|
|
};
|
|
|
|
|
|
|
|
wopts.appendix.set
|
|
|
|
(
|
|
|
|
dbs::appendix::ROOM_HEAD,
|
|
|
|
wopts.appendix[dbs::appendix::ROOM_HEAD] && !dummy_event
|
|
|
|
);
|
2020-03-04 21:48:37 +01:00
|
|
|
|
2020-12-07 00:05:51 +01:00
|
|
|
const auto state_candidate
|
2019-08-24 01:56:58 +02:00
|
|
|
{
|
2020-12-07 00:05:51 +01:00
|
|
|
opts.present
|
|
|
|
&& json::get<"state_key"_>(event)
|
|
|
|
};
|
2019-08-24 01:56:58 +02:00
|
|
|
|
2020-12-07 00:05:51 +01:00
|
|
|
const auto state_idx
|
|
|
|
{
|
|
|
|
state_candidate?
|
|
|
|
room.get(std::nothrow, at<"type"_>(event), at<"state_key"_>(event)):
|
|
|
|
0UL
|
|
|
|
};
|
2019-08-24 01:56:58 +02:00
|
|
|
|
2020-12-07 00:05:51 +01:00
|
|
|
const int64_t state_depth
|
|
|
|
{
|
|
|
|
m::get<int64_t>(std::nothrow, state_idx, "depth", 0L)
|
|
|
|
};
|
2019-08-24 01:56:58 +02:00
|
|
|
|
2020-12-07 00:05:51 +01:00
|
|
|
const auto state_present
|
|
|
|
{
|
|
|
|
!state_depth || state_depth < json::get<"depth"_>(event)
|
|
|
|
};
|
2019-08-24 01:56:58 +02:00
|
|
|
|
2020-12-07 00:05:51 +01:00
|
|
|
const bool authenticate
|
|
|
|
{
|
|
|
|
opts.auth
|
|
|
|
&& opts.phase[phase::AUTH_PRES]
|
|
|
|
&& state_present
|
|
|
|
&& !eval.room_internal
|
|
|
|
};
|
2019-08-24 01:56:58 +02:00
|
|
|
|
2020-12-07 00:05:51 +01:00
|
|
|
const auto &[pass, fail]
|
|
|
|
{
|
|
|
|
authenticate?
|
|
|
|
room::auth::check_present(event):
|
|
|
|
room::auth::passfail{true, {}}
|
|
|
|
};
|
2019-08-24 01:56:58 +02:00
|
|
|
|
2020-12-07 00:05:51 +01:00
|
|
|
if(state_present && fail)
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "%s fails auth for present state of %s :%s",
|
|
|
|
loghead(eval),
|
|
|
|
string_view{room.room_id},
|
|
|
|
what(fail),
|
|
|
|
};
|
2020-09-12 05:10:14 +02:00
|
|
|
|
2020-12-07 00:05:51 +01:00
|
|
|
wopts.appendix.set
|
|
|
|
(
|
|
|
|
dbs::appendix::ROOM_STATE,
|
|
|
|
wopts.appendix[dbs::appendix::ROOM_STATE] && state_present && pass
|
|
|
|
);
|
|
|
|
|
|
|
|
wopts.appendix.set
|
|
|
|
(
|
|
|
|
dbs::appendix::ROOM_JOINED,
|
|
|
|
wopts.appendix[dbs::appendix::ROOM_JOINED] && state_present && pass
|
|
|
|
);
|
2019-08-24 01:56:58 +02:00
|
|
|
|
2020-12-05 14:13:11 +01:00
|
|
|
const size_t wrote
|
|
|
|
{
|
2020-12-07 00:05:51 +01:00
|
|
|
dbs::write(txn, event, wopts)
|
2020-12-05 14:13:11 +01:00
|
|
|
};
|
2019-08-24 01:56:58 +02:00
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
2020-12-07 00:05:51 +01:00
|
|
|
log, "%s composed transaction wrote:%zu state:%b pres:%b prev:%lu @%ld",
|
2019-08-24 01:56:58 +02:00
|
|
|
loghead(eval),
|
2020-12-05 14:13:11 +01:00
|
|
|
wrote,
|
2020-12-07 00:05:51 +01:00
|
|
|
state_candidate,
|
|
|
|
state_present,
|
|
|
|
state_idx,
|
|
|
|
state_depth,
|
2019-08-24 01:56:58 +02:00
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
2017-11-30 20:01:14 +01:00
|
|
|
|
2019-04-30 23:08:00 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::vm::calc_txn_reserve(const opts &opts,
|
|
|
|
const event &event)
|
|
|
|
{
|
|
|
|
const size_t reserve_event
|
|
|
|
{
|
|
|
|
opts.reserve_bytes == size_t(-1)?
|
|
|
|
size_t(json::serialized(event) * 1.66):
|
|
|
|
opts.reserve_bytes
|
|
|
|
};
|
|
|
|
|
|
|
|
return reserve_event + opts.reserve_index;
|
|
|
|
}
|
|
|
|
|
2020-12-18 09:50:16 +01:00
|
|
|
void
|
|
|
|
ircd::m::vm::emption_check(eval &eval,
|
|
|
|
const m::event &event)
|
|
|
|
{
|
|
|
|
const bool my_target_member_event
|
|
|
|
{
|
|
|
|
json::get<"type"_>(event) == "m.room.member"
|
|
|
|
&& my(m::user(json::get<"state_key"_>(event)))
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool allow
|
|
|
|
{
|
|
|
|
my(event)
|
|
|
|
|| my_target_member_event
|
|
|
|
|| m::local_joined(room::id(json::get<"room_id"_>(event)))
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!allow))
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"No users require events of type=%s%s%s in %s on this server.",
|
|
|
|
json::get<"type"_>(event),
|
|
|
|
json::get<"state_key"_>(event)?
|
|
|
|
",state_key="_sv:
|
|
|
|
string_view{},
|
|
|
|
json::get<"state_key"_>(event),
|
|
|
|
json::get<"room_id"_>(event),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-06 06:33:58 +02:00
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
ircd::m::vm::call_hook(hook::site<T> &hook,
|
|
|
|
eval &eval,
|
|
|
|
const event &event,
|
|
|
|
T&& data)
|
|
|
|
try
|
|
|
|
{
|
2019-12-12 05:53:52 +01:00
|
|
|
#if 0
|
2019-09-17 01:42:22 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2020-05-13 05:23:29 +02:00
|
|
|
log, "%s hook:%s enter",
|
2019-09-17 01:42:22 +02:00
|
|
|
loghead(eval),
|
2020-03-03 04:36:49 +01:00
|
|
|
hook.name(),
|
2019-09-17 01:42:22 +02:00
|
|
|
};
|
2019-12-12 05:53:52 +01:00
|
|
|
#endif
|
2019-09-17 01:42:22 +02:00
|
|
|
|
2020-05-13 01:31:54 +02:00
|
|
|
// Providing a pointer to the eval.hook pointer allows the hook site to
|
|
|
|
// provide updates for observers in other contexts for which hook is
|
|
|
|
// currently entered.
|
|
|
|
auto **const cur
|
2020-03-03 04:37:38 +01:00
|
|
|
{
|
2020-05-13 01:31:54 +02:00
|
|
|
std::addressof(eval.hook)
|
2020-03-03 04:37:38 +01:00
|
|
|
};
|
|
|
|
|
2020-05-13 01:31:54 +02:00
|
|
|
hook(cur, event, std::forward<T>(data));
|
2019-09-17 01:42:22 +02:00
|
|
|
|
2019-12-12 05:53:52 +01:00
|
|
|
#if 0
|
2019-09-17 01:42:22 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2020-05-13 05:23:29 +02:00
|
|
|
log, "%s hook:%s leave",
|
2019-09-17 01:42:22 +02:00
|
|
|
loghead(eval),
|
2020-03-03 04:36:49 +01:00
|
|
|
hook.name(),
|
2019-09-17 01:42:22 +02:00
|
|
|
};
|
2019-12-12 05:53:52 +01:00
|
|
|
#endif
|
2019-07-06 06:33:58 +02:00
|
|
|
}
|
2019-07-16 01:41:04 +02:00
|
|
|
catch(const m::error &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2020-06-05 02:55:36 +02:00
|
|
|
log, "%s hook:%s :%s :%s",
|
2019-07-16 01:41:04 +02:00
|
|
|
loghead(eval),
|
2020-03-04 04:15:02 +01:00
|
|
|
hook.name(),
|
2019-07-16 01:41:04 +02:00
|
|
|
e.errcode(),
|
|
|
|
e.errstr(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const http::error &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2020-05-13 05:23:29 +02:00
|
|
|
log, "%s hook:%s :%s :%s",
|
2019-07-16 01:41:04 +02:00
|
|
|
loghead(eval),
|
2020-03-04 04:15:02 +01:00
|
|
|
hook.name(),
|
2019-07-16 01:41:04 +02:00
|
|
|
e.what(),
|
|
|
|
e.content,
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2019-07-06 06:33:58 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2020-05-13 05:23:29 +02:00
|
|
|
log, "%s hook:%s :%s",
|
2019-07-06 06:33:58 +02:00
|
|
|
loghead(eval),
|
2020-03-04 04:15:02 +01:00
|
|
|
hook.name(),
|
2019-07-06 06:33:58 +02:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:08:00 +02:00
|
|
|
template<class... args>
|
|
|
|
ircd::m::vm::fault
|
2020-12-15 20:06:48 +01:00
|
|
|
ircd::m::vm::handle_fault(const vm::opts &opts,
|
2019-04-30 23:08:00 +02:00
|
|
|
const vm::fault &code,
|
2020-12-15 20:06:48 +01:00
|
|
|
const string_view &event_id,
|
2019-04-30 23:08:00 +02:00
|
|
|
const string_view &fmt,
|
|
|
|
args&&... a)
|
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
if(code != fault::ACCEPT && fmt)
|
|
|
|
{
|
|
|
|
if(opts.errorlog & code)
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, fmt, std::forward<args>(a)...
|
|
|
|
};
|
|
|
|
else if(~opts.warnlog & code)
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, fmt, std::forward<args>(a)...
|
|
|
|
};
|
|
|
|
|
|
|
|
if(opts.warnlog & code)
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, fmt, std::forward<args>(a)...
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(likely(opts.outlog & code))
|
|
|
|
output(opts, code, event_id, fmt, std::forward<args>(a)...);
|
2019-04-30 23:08:00 +02:00
|
|
|
|
2020-12-15 20:06:48 +01:00
|
|
|
if(code != fault::ACCEPT && fmt)
|
|
|
|
if(unlikely(~opts.nothrows & code))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
code, fmt, std::forward<args>(a)...
|
|
|
|
};
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
//NOTE: may yield on a json::stack flush
|
|
|
|
template<class... args>
|
|
|
|
bool
|
|
|
|
ircd::m::vm::output(const vm::opts &opts,
|
|
|
|
const vm::fault &code,
|
|
|
|
const string_view &event_id,
|
|
|
|
const string_view &fmt,
|
|
|
|
args&&... a)
|
|
|
|
{
|
|
|
|
if(!opts.out)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(unlikely(!event_id))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
json::stack::object object
|
|
|
|
{
|
|
|
|
*opts.out, event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(code != fault::ACCEPT)
|
|
|
|
json::stack::member
|
2019-04-30 23:08:00 +02:00
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
object, "errcode", json::value
|
|
|
|
{
|
|
|
|
reflect(code), json::STRING
|
|
|
|
}
|
2019-04-30 23:08:00 +02:00
|
|
|
};
|
|
|
|
|
2020-12-15 20:06:48 +01:00
|
|
|
if(!fmt)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const fmt::bsprintf<1024> text
|
|
|
|
{
|
|
|
|
fmt, std::forward<args>(a)...
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "error", json::value
|
2019-04-30 23:08:00 +02:00
|
|
|
{
|
2020-12-15 20:06:48 +01:00
|
|
|
string_view{text}, json::STRING
|
|
|
|
}
|
|
|
|
};
|
2019-04-30 23:08:00 +02:00
|
|
|
|
2020-12-15 20:06:48 +01:00
|
|
|
return true;
|
2019-04-30 23:08:00 +02:00
|
|
|
}
|