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
|
|
|
{
|
2019-04-30 23:08:00 +02:00
|
|
|
template<class... args> static fault handle_error(const opts &, const fault &, const string_view &fmt, args&&... a);
|
2019-07-06 06:33:58 +02:00
|
|
|
template<class T> static void call_hook(hook::site<T> &, eval &, const event &, T&& data);
|
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 &);
|
|
|
|
static void write_append(eval &, const event &);
|
|
|
|
static void write_prepare(eval &, const event &);
|
2019-03-21 23:00:58 +01:00
|
|
|
static fault execute_edu(eval &, const event &);
|
|
|
|
static fault execute_pdu(eval &, const event &);
|
|
|
|
fault execute(eval &, const event &);
|
|
|
|
fault inject(eval &, json::iov &, const json::iov &);
|
|
|
|
fault inject(eval &, const room &, 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
|
|
|
|
2019-03-09 22:12:58 +01: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
|
2019-05-27 05:57:55 +02:00
|
|
|
extern hook::site<eval &> access_hook; ///< Called for access control checking
|
2019-03-09 21:08:05 +01:00
|
|
|
extern hook::site<eval &> fetch_hook; ///< Called to resolve dependencies
|
2019-03-09 22:12:58 +01:00
|
|
|
extern hook::site<eval &> eval_hook; ///< Called for final event evaluation
|
2019-03-09 21:08:05 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
2018-04-16 23:21:54 +02:00
|
|
|
{
|
2018-05-07 06:15:25 +02:00
|
|
|
"Matrix Virtual Machine",
|
|
|
|
ircd::m::vm::init, ircd::m::vm::fini
|
|
|
|
};
|
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" },
|
|
|
|
{ "default", false },
|
|
|
|
};
|
|
|
|
|
|
|
|
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" }
|
|
|
|
};
|
|
|
|
|
2018-09-06 10:21:37 +02:00
|
|
|
decltype(ircd::m::vm::fetch_hook)
|
|
|
|
ircd::m::vm::fetch_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.fetch" }
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
{ "name", "vm.notify" }
|
|
|
|
};
|
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
decltype(ircd::m::vm::effect_hook)
|
|
|
|
ircd::m::vm::effect_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.effect" }
|
|
|
|
};
|
|
|
|
|
2018-05-07 02:07:18 +02:00
|
|
|
//
|
2018-05-07 06:15:25 +02:00
|
|
|
// init
|
2018-05-07 02:07:18 +02:00
|
|
|
//
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
void
|
|
|
|
ircd::m::vm::init()
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2018-05-07 06:15:25 +02:00
|
|
|
id::event::buf event_id;
|
2019-03-19 19:45:01 +01:00
|
|
|
sequence::retired = sequence::get(event_id);
|
|
|
|
sequence::committed = sequence::retired;
|
|
|
|
sequence::uncommitted = sequence::committed;
|
2018-05-07 01:04:51 +02:00
|
|
|
|
2019-03-21 23:58:18 +01:00
|
|
|
vm::ready = true;
|
|
|
|
vm::dock.notify_all();
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "BOOT %s @%lu [%s]",
|
|
|
|
string_view{m::my_node.node_id},
|
2019-03-19 19:45:01 +01:00
|
|
|
sequence::retired,
|
|
|
|
sequence::retired? string_view{event_id} : "NO EVENTS"_sv
|
2018-05-07 06:15:25 +02:00
|
|
|
};
|
2018-05-07 01:04:51 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
void
|
|
|
|
ircd::m::vm::fini()
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2019-03-21 23:58:18 +01:00
|
|
|
vm::ready = false;
|
2019-03-20 02:02:35 +01:00
|
|
|
|
2019-03-21 23:58:18 +01:00
|
|
|
if(!eval::list.empty())
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "Waiting for %zu evals (exec:%zu inject:%zu room:%zu pending:%zu)",
|
|
|
|
eval::list.size(),
|
|
|
|
eval::executing,
|
|
|
|
eval::injecting,
|
|
|
|
eval::injecting_room,
|
|
|
|
sequence::pending,
|
|
|
|
};
|
|
|
|
|
|
|
|
vm::dock.wait([]
|
|
|
|
{
|
|
|
|
return !eval::executing && !eval::injecting && !eval::injecting_room;
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(!sequence::pending);
|
2018-06-03 19:02:19 +02:00
|
|
|
|
2019-03-19 19:45:01 +01:00
|
|
|
event::id::buf event_id;
|
|
|
|
const auto retired
|
2018-05-07 06:15:25 +02:00
|
|
|
{
|
2019-03-19 19:45:01 +01:00
|
|
|
sequence::get(event_id)
|
2018-05-07 06:15:25 +02:00
|
|
|
};
|
2018-05-07 01:04:51 +02:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
log::info
|
|
|
|
{
|
2019-03-19 19:45:01 +01:00
|
|
|
log, "HLT '%s' @%lu [%s] %lu:%lu:%lu",
|
2018-05-07 06:15:25 +02:00
|
|
|
string_view{m::my_node.node_id},
|
2019-03-19 19:45:01 +01:00
|
|
|
retired,
|
|
|
|
retired? string_view{event_id} : "NO EVENTS"_sv,
|
|
|
|
sequence::retired,
|
|
|
|
sequence::committed,
|
|
|
|
sequence::uncommitted
|
2018-05-07 06:15:25 +02:00
|
|
|
};
|
2019-03-21 23:58:18 +01:00
|
|
|
|
|
|
|
assert(retired == sequence::retired);
|
2018-05-07 02:35:12 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
//
|
|
|
|
// eval
|
|
|
|
//
|
2018-05-07 01:04:51 +02:00
|
|
|
|
|
|
|
enum ircd::m::vm::fault
|
2019-03-21 23:00:58 +01:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::vm::inject(eval &eval,
|
|
|
|
const room &room,
|
|
|
|
json::iov &event,
|
|
|
|
const json::iov &contents)
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2019-03-21 23:58:18 +01:00
|
|
|
// m::vm bookkeeping that someone entered this function
|
|
|
|
const scope_count injecting_room{eval::injecting_room};
|
|
|
|
const scope_notify notify{vm::dock};
|
|
|
|
|
2019-05-09 12:43:34 +02:00
|
|
|
// This eval entry point is only used for injections. We try to find the
|
2018-05-29 13:04:32 +02:00
|
|
|
// commit opts the user supplied directly to this eval or with the room.
|
|
|
|
if(!eval.copts)
|
|
|
|
eval.copts = room.copts;
|
|
|
|
|
|
|
|
if(!eval.copts)
|
|
|
|
eval.copts = &vm::default_copts;
|
|
|
|
|
|
|
|
// Note that the regular opts is unconditionally overridden because the
|
|
|
|
// user should have provided copts instead.
|
2018-06-12 07:43:19 +02:00
|
|
|
assert(!eval.opts || eval.opts == eval.copts);
|
2018-05-29 13:04:32 +02:00
|
|
|
eval.opts = eval.copts;
|
|
|
|
|
|
|
|
// Set a member pointer to the json::iov currently being composed. This
|
|
|
|
// allows other parallel evals to have deep access to exactly what this
|
|
|
|
// eval is attempting to do.
|
2019-03-02 21:33:32 +01:00
|
|
|
const scope_restore eval_issue
|
2018-05-29 13:04:32 +02:00
|
|
|
{
|
2019-02-05 10:53:20 +01:00
|
|
|
eval.issue, &event
|
|
|
|
};
|
|
|
|
|
2019-03-02 21:33:32 +01:00
|
|
|
const scope_restore eval_room_id
|
2019-02-05 10:53:20 +01:00
|
|
|
{
|
|
|
|
eval.room_id, room.room_id
|
|
|
|
};
|
2018-05-29 13:04:32 +02:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
assert(eval.issue);
|
|
|
|
assert(eval.room_id);
|
|
|
|
assert(eval.copts);
|
|
|
|
assert(eval.opts);
|
2018-05-07 03:10:01 +02:00
|
|
|
assert(room.room_id);
|
2018-05-07 06:15:25 +02:00
|
|
|
|
|
|
|
const auto &opts
|
|
|
|
{
|
|
|
|
*eval.copts
|
|
|
|
};
|
|
|
|
|
2019-03-06 08:19:16 +01:00
|
|
|
const m::room::head head
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2019-03-06 08:19:16 +01:00
|
|
|
room
|
2018-05-07 01:04:51 +02:00
|
|
|
};
|
|
|
|
|
2018-06-02 01:46:58 +02:00
|
|
|
const bool need_tophead{event.at("type") != "m.room.create"};
|
|
|
|
const unique_buffer<mutable_buffer> prev_buf{8192};
|
2019-03-06 08:19:16 +01:00
|
|
|
static const size_t prev_limit{16};
|
2018-06-02 01:46:58 +02:00
|
|
|
const auto prev
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2019-03-06 08:19:16 +01:00
|
|
|
head.make_refs(prev_buf, prev_limit, need_tophead)
|
2018-05-07 01:04:51 +02:00
|
|
|
};
|
|
|
|
|
2018-06-02 01:46:58 +02:00
|
|
|
const auto &prev_events{prev.first};
|
|
|
|
const auto &depth{prev.second};
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::set depth_
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
|
|
|
event, !event.has("depth"),
|
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"depth", [&depth]
|
|
|
|
{
|
|
|
|
return json::value
|
|
|
|
{
|
|
|
|
depth == std::numeric_limits<int64_t>::max()? depth : depth + 1
|
|
|
|
};
|
|
|
|
}
|
2018-05-07 01:04:51 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-06 07:49:05 +01:00
|
|
|
const m::room::auth auth
|
2018-06-02 01:46:58 +02:00
|
|
|
{
|
2019-03-06 07:49:05 +01:00
|
|
|
room
|
2018-06-02 01:46:58 +02:00
|
|
|
};
|
|
|
|
|
2019-03-06 07:49:05 +01:00
|
|
|
char ae_buf[1024];
|
2019-03-06 23:09:54 +01:00
|
|
|
json::array auth_events{json::empty_array};
|
|
|
|
if(depth != -1 && event.at("type") != "m.room.create" && opts.add_auth_events)
|
2019-07-09 04:58:04 +02:00
|
|
|
auth_events = auth.make_refs(ae_buf, m::event{event});
|
2018-05-07 01:04:51 +02:00
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add auth_events_
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
event, opts.add_auth_events,
|
2018-05-12 11:27:39 +02:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"auth_events", [&auth_events]() -> json::value
|
2018-05-12 11:27:39 +02:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
return auth_events;
|
2018-05-12 11:27:39 +02:00
|
|
|
}
|
2018-06-05 20:19:40 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::add prev_events_
|
|
|
|
{
|
|
|
|
event, opts.add_prev_events,
|
2018-05-12 11:27:39 +02:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"prev_events", [&prev_events]() -> json::value
|
2018-05-12 11:27:39 +02:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
return prev_events;
|
2018-05-12 11:27:39 +02:00
|
|
|
}
|
2018-06-05 20:19:40 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-21 23:00:58 +01:00
|
|
|
return inject(eval, event, contents);
|
2018-05-07 01:04:51 +02:00
|
|
|
}
|
2018-04-16 23:21:54 +02:00
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
enum ircd::m::vm::fault
|
2019-03-21 23:00:58 +01:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::vm::inject(eval &eval,
|
|
|
|
json::iov &event,
|
|
|
|
const json::iov &contents)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-03-21 23:58:18 +01:00
|
|
|
// m::vm bookkeeping that someone entered this function
|
|
|
|
const scope_count injecting{eval::injecting};
|
|
|
|
const scope_notify notify{vm::dock};
|
|
|
|
|
2018-05-29 13:04:32 +02:00
|
|
|
// This eval entry point is only used for commits. If the user did not
|
|
|
|
// supply commit opts we supply the default ones here.
|
|
|
|
if(!eval.copts)
|
|
|
|
eval.copts = &vm::default_copts;
|
|
|
|
|
|
|
|
// Note that the regular opts is unconditionally overridden because the
|
|
|
|
// user should have provided copts instead.
|
2018-06-12 07:43:19 +02:00
|
|
|
assert(!eval.opts || eval.opts == eval.copts);
|
2018-05-29 13:04:32 +02:00
|
|
|
eval.opts = eval.copts;
|
|
|
|
|
|
|
|
// Set a member pointer to the json::iov currently being composed. This
|
|
|
|
// allows other parallel evals to have deep access to exactly what this
|
|
|
|
// eval is attempting to do.
|
|
|
|
assert(!eval.room_id || eval.issue == &event);
|
|
|
|
if(!eval.room_id)
|
|
|
|
eval.issue = &event;
|
|
|
|
|
|
|
|
const unwind deissue{[&eval]
|
|
|
|
{
|
|
|
|
// issue is untouched when room_id is set; that indicates it was set
|
|
|
|
// and will be unset by another eval function (i.e above).
|
|
|
|
if(!eval.room_id)
|
|
|
|
eval.issue = nullptr;
|
|
|
|
}};
|
|
|
|
|
2019-07-12 04:21:11 +02:00
|
|
|
const scope_restore eval_room_id
|
|
|
|
{
|
|
|
|
eval.room_id,
|
|
|
|
!eval.room_id &&
|
|
|
|
event.has("room_id")?
|
|
|
|
string_view{event.at("room_id")}:
|
|
|
|
string_view{eval.room_id}
|
|
|
|
};
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
assert(eval.issue);
|
|
|
|
assert(eval.copts);
|
|
|
|
assert(eval.opts);
|
|
|
|
assert(eval.copts);
|
|
|
|
const auto &opts
|
|
|
|
{
|
|
|
|
*eval.copts
|
|
|
|
};
|
2018-05-07 01:04:51 +02:00
|
|
|
|
2019-07-12 04:21:11 +02:00
|
|
|
assert(eval.room_id);
|
|
|
|
const json::iov::push room_id
|
|
|
|
{
|
|
|
|
event, { "room_id", eval.room_id }
|
|
|
|
};
|
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add origin_
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-05-12 06:21:15 +02:00
|
|
|
event, opts.add_origin,
|
2018-03-14 23:06:35 +01:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"origin", []() -> json::value
|
|
|
|
{
|
|
|
|
return my_host();
|
|
|
|
}
|
2018-03-14 23:06:35 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add origin_server_ts_
|
2018-03-14 23:06:35 +01:00
|
|
|
{
|
2018-05-12 06:21:15 +02:00
|
|
|
event, opts.add_origin_server_ts,
|
2018-03-14 23:06:35 +01:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"origin_server_ts", []
|
|
|
|
{
|
|
|
|
return json::value{ircd::time<milliseconds>()};
|
|
|
|
}
|
2018-03-14 23:06:35 +01:00
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const json::strung content
|
|
|
|
{
|
2018-01-26 21:26:09 +01:00
|
|
|
contents
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2019-07-10 11:14:38 +02:00
|
|
|
// hashes
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2019-07-11 23:56:03 +02:00
|
|
|
char hashes_buf[384];
|
2019-07-10 11:14:38 +02:00
|
|
|
const string_view hashes
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-07-11 23:56:03 +02:00
|
|
|
opts.add_hash && eval.room_version != "1"?
|
2019-07-10 11:14:38 +02:00
|
|
|
m::event::hashes(hashes_buf, event, content):
|
|
|
|
string_view{}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2019-07-10 11:14:38 +02:00
|
|
|
const json::iov::add hashes_
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-07-11 23:56:03 +02:00
|
|
|
event, opts.add_hash && eval.room_version != "1",
|
2018-05-12 06:21:15 +02:00
|
|
|
{
|
2019-07-10 11:14:38 +02:00
|
|
|
"hashes", [&hashes]() -> json::value
|
2018-06-05 20:19:40 +02:00
|
|
|
{
|
2019-07-10 11:14:38 +02:00
|
|
|
return hashes;
|
2018-06-05 20:19:40 +02:00
|
|
|
}
|
2018-05-12 06:21:15 +02:00
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2019-07-10 11:14:38 +02:00
|
|
|
// event_id
|
2018-03-07 20:54:56 +01:00
|
|
|
|
2019-07-10 11:14:38 +02:00
|
|
|
char room_version_buf[32];
|
|
|
|
const scope_restore eval_room_version
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-07-10 11:14:38 +02:00
|
|
|
eval.room_version,
|
|
|
|
eval.opts->room_version?:
|
|
|
|
m::version(room_version_buf, room{eval.room_id}, std::nothrow)
|
2018-03-07 20:54:56 +01:00
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2019-07-10 11:14:38 +02:00
|
|
|
assert(eval.room_version);
|
|
|
|
const event::id &event_id_v1
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-07-10 11:14:38 +02:00
|
|
|
opts.add_event_id && (eval.room_version == "1" || eval.room_version == "2")?
|
|
|
|
make_id(m::event{event}, eval.room_version, eval.event_id):
|
|
|
|
event::id{}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::add event_id_
|
|
|
|
{
|
|
|
|
event, !empty(event_id_v1),
|
2018-05-12 06:21:15 +02:00
|
|
|
{
|
2019-07-10 11:14:38 +02:00
|
|
|
"event_id", [&event_id_v1]() -> json::value
|
2018-06-05 20:19:40 +02:00
|
|
|
{
|
2019-07-10 11:14:38 +02:00
|
|
|
return event_id_v1;
|
2018-06-05 20:19:40 +02:00
|
|
|
}
|
2018-05-12 06:21:15 +02:00
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2019-07-11 23:56:03 +02:00
|
|
|
// hashes
|
|
|
|
|
|
|
|
const string_view hashes__
|
|
|
|
{
|
|
|
|
opts.add_hash && eval.room_version == "1"?
|
|
|
|
m::event::hashes(hashes_buf, event, content):
|
|
|
|
string_view{}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::set hashes___
|
|
|
|
{
|
|
|
|
event, opts.add_hash && eval.room_version == "1",
|
|
|
|
{
|
|
|
|
"hashes", [&hashes__]() -> json::value
|
|
|
|
{
|
|
|
|
return hashes__;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-07 20:54:56 +01:00
|
|
|
// sigs
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-03-07 20:54:56 +01:00
|
|
|
char sigs_buf[384];
|
|
|
|
const string_view sigs
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-05-12 06:21:15 +02:00
|
|
|
opts.add_sig?
|
2018-03-08 18:54:39 +01:00
|
|
|
m::event::signatures(sigs_buf, event, contents):
|
|
|
|
string_view{}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add sigs_
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-05-12 06:21:15 +02:00
|
|
|
event, opts.add_sig,
|
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"signatures", [&sigs]() -> json::value
|
|
|
|
{
|
|
|
|
return sigs;
|
|
|
|
}
|
2018-05-12 06:21:15 +02:00
|
|
|
}
|
2018-03-08 18:54:39 +01:00
|
|
|
};
|
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::push content_
|
2018-03-08 18:54:39 +01:00
|
|
|
{
|
|
|
|
event, { "content", content },
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2019-07-10 11:14:38 +02:00
|
|
|
const event::id &event_id
|
|
|
|
{
|
|
|
|
event_id_v1?
|
|
|
|
event_id_v1:
|
|
|
|
opts.add_event_id?
|
|
|
|
make_id(m::event{event}, eval.room_version, eval.event_id):
|
|
|
|
event::id{}
|
|
|
|
};
|
|
|
|
|
2019-03-26 04:13:03 +01:00
|
|
|
const m::event event_tuple
|
|
|
|
{
|
2019-07-10 11:14:38 +02:00
|
|
|
event, event_id
|
2019-03-26 04:13:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(opts.debuglog_precommit)
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Issuing: %s", pretty_oneline(event_tuple)
|
|
|
|
};
|
|
|
|
|
|
|
|
return execute(eval, event_tuple);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 20:01:14 +01:00
|
|
|
enum ircd::m::vm::fault
|
2019-03-21 23:00:58 +01:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::vm::execute(eval &eval,
|
|
|
|
const event &event)
|
2018-02-09 08:21:15 +01:00
|
|
|
try
|
2017-11-30 20:01:14 +01:00
|
|
|
{
|
2019-03-21 23:58:18 +01:00
|
|
|
// m::vm bookkeeping that someone entered this function
|
|
|
|
const scope_count executing{eval::executing};
|
|
|
|
const scope_notify notify{vm::dock};
|
|
|
|
|
2018-05-29 13:04:32 +02:00
|
|
|
// Set a member pointer to the event currently being evaluated. This
|
2019-05-26 10:48:27 +02:00
|
|
|
// allows other parallel evals to have deep access to this eval.
|
2019-02-05 10:53:20 +01:00
|
|
|
assert(!eval.event_);
|
2019-03-02 21:33:32 +01:00
|
|
|
const scope_restore eval_event
|
2018-05-29 13:04:32 +02:00
|
|
|
{
|
2019-02-05 10:53:20 +01:00
|
|
|
eval.event_, &event
|
|
|
|
};
|
2018-05-29 13:04:32 +02:00
|
|
|
|
2019-07-10 11:14:38 +02:00
|
|
|
const scope_restore eval_room_id
|
|
|
|
{
|
|
|
|
eval.room_id,
|
|
|
|
eval.room_id?
|
|
|
|
eval.room_id:
|
|
|
|
string_view(json::get<"room_id"_>(event))
|
|
|
|
};
|
|
|
|
|
|
|
|
char room_version_buf[32];
|
|
|
|
const scope_restore eval_room_version
|
|
|
|
{
|
|
|
|
eval.room_version,
|
|
|
|
eval.opts->room_version?
|
|
|
|
eval.opts->room_version:
|
|
|
|
eval.room_id?
|
|
|
|
m::version(room_version_buf, room{eval.room_id}, std::nothrow):
|
|
|
|
string_view{}
|
|
|
|
};
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
assert(eval.opts);
|
|
|
|
assert(eval.event_);
|
|
|
|
assert(eval.id);
|
|
|
|
assert(eval.ctx);
|
|
|
|
const auto &opts
|
2018-05-07 03:10:01 +02:00
|
|
|
{
|
2018-05-07 06:15:25 +02:00
|
|
|
*eval.opts
|
|
|
|
};
|
2018-05-07 03:10:01 +02:00
|
|
|
|
2019-03-09 22:12:58 +01:00
|
|
|
if(eval.copts && eval.copts->issue)
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(issue_hook, eval, event, eval);
|
2019-03-09 22:12:58 +01:00
|
|
|
|
|
|
|
if(opts.conform)
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(conform_hook, eval, event, eval);
|
2019-03-09 22:12:58 +01:00
|
|
|
|
2018-03-13 03:07:41 +01:00
|
|
|
const fault ret
|
|
|
|
{
|
2019-07-06 06:09:07 +02:00
|
|
|
event.event_id?
|
2019-03-21 23:00:58 +01:00
|
|
|
execute_pdu(eval, event):
|
|
|
|
execute_edu(eval, event)
|
2018-03-13 03:07:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(ret != fault::ACCEPT)
|
|
|
|
return ret;
|
2018-03-07 20:02:23 +01:00
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
if(opts.notify)
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(notify_hook, eval, event, eval);
|
2018-03-13 03:07:41 +01:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
if(opts.effects)
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(effect_hook, eval, event, eval);
|
2018-03-13 03:07:41 +01:00
|
|
|
|
2019-03-01 01:53:03 +01:00
|
|
|
if(opts.debuglog_accept || bool(log_accept_debug))
|
2018-08-16 09:46:46 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s", pretty_oneline(event)
|
|
|
|
};
|
2018-03-13 03:07:41 +01:00
|
|
|
|
2019-03-01 01:53:03 +01: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
|
|
|
}
|
2018-05-15 07:24:03 +02:00
|
|
|
catch(const error &e) // VM FAULT CODE
|
2018-03-07 20:02:23 +01:00
|
|
|
{
|
2018-12-10 22:03:15 +01:00
|
|
|
return handle_error
|
|
|
|
(
|
|
|
|
*eval.opts, e.code,
|
2019-04-20 23:51:42 +02:00
|
|
|
"eval %s :%s",
|
2019-07-06 06:09:07 +02:00
|
|
|
event.event_id? string_view{event.event_id}: "<edu>"_sv,
|
2018-12-31 01:10:28 +01:00
|
|
|
unquote(json::object(e.content).get("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
|
|
|
{
|
2018-12-10 22:03:15 +01:00
|
|
|
return handle_error
|
|
|
|
(
|
|
|
|
*eval.opts, fault::GENERAL,
|
2019-07-14 22:50:33 +02:00
|
|
|
"eval %s (General Protection) :%s %s :%s",
|
2019-07-06 06:09:07 +02:00
|
|
|
event.event_id? string_view{event.event_id}: "<edu>"_sv,
|
2018-12-10 22:03:15 +01:00
|
|
|
e.what(),
|
2018-12-31 01:10:28 +01:00
|
|
|
unquote(json::object(e.content).get("errcode")),
|
|
|
|
unquote(json::object(e.content).get("error"))
|
2018-12-10 22:03:15 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &e) // INTERRUPTION
|
|
|
|
{
|
|
|
|
return handle_error
|
|
|
|
(
|
|
|
|
*eval.opts, fault::INTERRUPT,
|
2019-04-20 23:51:42 +02:00
|
|
|
"eval %s :%s",
|
2019-07-06 06:09:07 +02:00
|
|
|
event.event_id? string_view{event.event_id}: "<edu>"_sv,
|
2018-12-10 22:03:15 +01:00
|
|
|
e.what()
|
|
|
|
);
|
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
|
|
|
{
|
2018-12-10 22:03:15 +01:00
|
|
|
return handle_error
|
|
|
|
(
|
|
|
|
*eval.opts, fault::GENERAL,
|
2019-07-14 22:50:33 +02:00
|
|
|
"eval %s (General Protection) :%s",
|
2019-07-06 06:09:07 +02:00
|
|
|
event.event_id? string_view{event.event_id}: "<edu>"_sv,
|
2018-12-10 22:03:15 +01:00
|
|
|
e.what()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
enum 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
|
|
|
{
|
2018-10-11 10:18:21 +02:00
|
|
|
if(eval.opts->eval)
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(eval_hook, eval, event, eval);
|
2018-10-11 10:18:21 +02:00
|
|
|
|
2019-04-07 03:18:17 +02:00
|
|
|
if(eval.opts->post)
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(post_hook, eval, event, eval);
|
2019-04-07 03:18:17 +02:00
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
return fault::ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum 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-03-30 23:18:22 +01:00
|
|
|
const scope_restore remove_txn
|
|
|
|
{
|
|
|
|
eval.txn, std::shared_ptr<db::txn>{}
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2018-10-11 10:18:21 +02:00
|
|
|
const bool already_exists
|
|
|
|
{
|
|
|
|
exists(event_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: ABA
|
|
|
|
if(already_exists && !opts.replays)
|
2018-02-28 07:53:44 +01:00
|
|
|
throw error
|
2018-02-09 08:21:15 +01:00
|
|
|
{
|
2018-02-28 07:53:44 +01:00
|
|
|
fault::EXISTS, "Event has already been evaluated."
|
2018-02-09 08:21:15 +01:00
|
|
|
};
|
|
|
|
|
2019-05-27 05:57:55 +02:00
|
|
|
if(opts.access)
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(access_hook, eval, event, eval);
|
2019-05-27 05:36:26 +02:00
|
|
|
|
2019-04-28 05:01:27 +02:00
|
|
|
if(opts.verify && !verify(event))
|
|
|
|
throw m::BAD_SIGNATURE
|
|
|
|
{
|
|
|
|
"Signature verification failed"
|
|
|
|
};
|
2018-03-22 09:31:17 +01:00
|
|
|
|
2018-10-31 19:37:40 +01:00
|
|
|
// Fetch dependencies
|
|
|
|
if(opts.fetch)
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(fetch_hook, eval, event, eval);
|
2018-04-17 02:28:26 +02:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
// Obtain sequence number here.
|
|
|
|
const auto *const &top(eval::seqmax());
|
|
|
|
eval.sequence_shared[0] = 0;
|
|
|
|
eval.sequence_shared[1] = 0;
|
2019-06-02 02:03:17 +02:00
|
|
|
eval.sequence = 0;
|
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?
|
2019-03-19 19:45:01 +01:00
|
|
|
std::max(sequence::get(*top) + 1, sequence::committed + 1):
|
2019-03-21 22:13:45 +01:00
|
|
|
sequence::committed + 1
|
|
|
|
};
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s | acquire", loghead(eval)
|
|
|
|
};
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
assert(eval.sequence != 0);
|
|
|
|
assert(sequence::uncommitted <= sequence::get(eval));
|
|
|
|
assert(sequence::committed < sequence::get(eval));
|
|
|
|
assert(sequence::retired < sequence::get(eval));
|
|
|
|
assert(eval::sequnique(sequence::get(eval)));
|
|
|
|
sequence::uncommitted = sequence::get(eval);
|
2018-09-05 08:28:01 +02:00
|
|
|
|
2018-09-06 10:21:37 +02:00
|
|
|
// Evaluation by module hooks
|
2018-10-11 10:18:21 +02:00
|
|
|
if(opts.eval)
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(eval_hook, eval, event, eval);
|
2018-09-05 08:28:01 +02:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
// Wait until this is the lowest sequence number
|
|
|
|
sequence::dock.wait([&eval]
|
2019-03-19 19:45:01 +01:00
|
|
|
{
|
2019-03-21 22:13:45 +01:00
|
|
|
return eval::seqnext(sequence::committed) == &eval;
|
|
|
|
});
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s | commit", loghead(eval)
|
|
|
|
};
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
assert(sequence::committed < sequence::get(eval));
|
|
|
|
assert(sequence::retired < sequence::get(eval));
|
|
|
|
sequence::committed = sequence::get(eval);
|
|
|
|
sequence::dock.notify_all();
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
if(opts.write)
|
|
|
|
write_prepare(eval, event);
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
if(opts.write)
|
|
|
|
write_append(eval, event);
|
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.
|
2019-03-20 02:02:35 +01:00
|
|
|
if(opts.post)
|
2019-07-06 06:33:58 +02:00
|
|
|
call_hook(post_hook, eval, event, eval);
|
2018-12-31 02:02:19 +01:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
// Commit the transaction to database iff this eval is at the stack base.
|
|
|
|
if(opts.write && !eval.sequence_shared[0])
|
|
|
|
write_commit(eval);
|
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.
|
|
|
|
if(!eval.sequence_shared[0])
|
2019-03-20 02:02:35 +01:00
|
|
|
{
|
2019-03-19 19:45:01 +01:00
|
|
|
sequence::dock.wait([&eval]
|
|
|
|
{
|
|
|
|
return eval::seqnext(sequence::retired) == &eval;
|
|
|
|
});
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
2019-03-21 22:13:45 +01:00
|
|
|
log, "%s | retire %lu:%lu",
|
|
|
|
loghead(eval),
|
2019-03-19 19:45:01 +01:00
|
|
|
sequence::get(eval),
|
2019-03-21 22:13:45 +01:00
|
|
|
eval.sequence_shared[1],
|
2019-03-19 19:45:01 +01:00
|
|
|
};
|
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
assert(sequence::retired < sequence::get(eval));
|
|
|
|
sequence::retired = std::max(eval.sequence_shared[1], sequence::get(eval));
|
|
|
|
sequence::dock.notify_all();
|
2019-03-20 02:02:35 +01:00
|
|
|
}
|
2019-03-19 19:45:01 +01:00
|
|
|
|
2018-12-31 02:02:19 +01:00
|
|
|
return fault::ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-03-21 22:13:45 +01:00
|
|
|
ircd::m::vm::write_prepare(eval &eval,
|
|
|
|
const event &event)
|
2018-12-31 02:02:19 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
assert(eval.opts);
|
2019-03-21 22:13:45 +01:00
|
|
|
const auto &opts{*eval.opts};
|
|
|
|
|
2019-03-29 00:22:19 +01:00
|
|
|
// Share a transaction with any other unretired evals on this stack. This
|
2019-03-21 22:13:45 +01:00
|
|
|
// should mean the bottom-most/lowest-sequence eval on this ctx.
|
|
|
|
const auto get_other_txn{[&eval]
|
|
|
|
(auto &other)
|
2018-12-31 02:02:19 +01:00
|
|
|
{
|
2019-03-29 00:22:19 +01:00
|
|
|
if(&other == &eval)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!other.txn)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(sequence::get(other) <= sequence::retired)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
other.sequence_shared[1] = std::max(other.sequence_shared[1], sequence::get(eval));
|
|
|
|
eval.sequence_shared[0] = sequence::get(other);
|
|
|
|
eval.txn = other.txn;
|
|
|
|
return false;
|
2019-03-21 22:13:45 +01:00
|
|
|
}};
|
|
|
|
|
|
|
|
// If we broke from the iteration then this eval is sharing a transaction
|
|
|
|
// from another eval on this stack.
|
|
|
|
if(!eval.for_each(eval.ctx, get_other_txn))
|
|
|
|
return;
|
2018-09-05 08:28:01 +02:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
eval.txn = std::make_shared<db::txn>
|
|
|
|
(
|
2018-04-17 02:28:26 +02:00
|
|
|
*dbs::events, db::txn::opts
|
|
|
|
{
|
2019-04-30 23:08:00 +02:00
|
|
|
calc_txn_reserve(opts, event), // reserve_bytes
|
|
|
|
0, // max_bytes (no max)
|
2018-04-17 02:28:26 +02:00
|
|
|
}
|
2019-03-21 22:13:45 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::vm::write_append(eval &eval,
|
|
|
|
const event &event)
|
2018-04-17 02:28:26 +02:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
{
|
|
|
|
assert(eval.opts);
|
|
|
|
assert(eval.txn);
|
|
|
|
|
|
|
|
const auto &opts{*eval.opts};
|
|
|
|
auto &txn{*eval.txn};
|
|
|
|
|
|
|
|
log::debug
|
2018-04-17 02:28:26 +02:00
|
|
|
{
|
2019-03-21 22:13:45 +01:00
|
|
|
log, "%s | append", loghead(eval)
|
2019-02-05 10:53:20 +01:00
|
|
|
};
|
2018-04-17 02:28:26 +02:00
|
|
|
|
2018-09-05 08:28:01 +02:00
|
|
|
// Preliminary write_opts
|
2019-05-12 00:30:40 +02:00
|
|
|
m::dbs::write_opts wopts(opts.wopts);
|
2019-05-13 21:12:23 +02:00
|
|
|
wopts.appendix.set(dbs::appendix::ROOM_STATE, opts.present);
|
|
|
|
wopts.appendix.set(dbs::appendix::ROOM_JOINED, opts.present);
|
|
|
|
wopts.appendix.set(dbs::appendix::ROOM_STATE_SPACE, opts.history);
|
2019-05-08 00:19:38 +02:00
|
|
|
wopts.appendix.set(dbs::appendix::ROOM_HEAD, opts.room_head);
|
2019-05-12 00:20:58 +02:00
|
|
|
wopts.appendix.set(dbs::appendix::ROOM_HEAD_RESOLVE, opts.room_head_resolve);
|
2019-01-24 20:32:57 +01:00
|
|
|
wopts.json_source = opts.json_source;
|
2018-09-05 08:28:01 +02:00
|
|
|
wopts.event_idx = eval.sequence;
|
2019-03-21 22:13:45 +01:00
|
|
|
dbs::write(*eval.txn, event, wopts);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
2017-11-30 20:01:14 +01:00
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
void
|
2019-03-21 22:13:45 +01:00
|
|
|
ircd::m::vm::write_commit(eval &eval)
|
2018-02-28 07:53:44 +01:00
|
|
|
{
|
2018-06-12 07:43:19 +02:00
|
|
|
assert(eval.txn);
|
2019-03-21 22:13:45 +01:00
|
|
|
assert(eval.txn.use_count() == 1);
|
|
|
|
assert(eval.sequence_shared[0] == 0);
|
2018-04-17 02:28:26 +02:00
|
|
|
auto &txn(*eval.txn);
|
2018-12-31 02:02:19 +01:00
|
|
|
|
2019-03-16 20:38:14 +01:00
|
|
|
#ifdef RB_DEBUG
|
2019-03-19 19:45:01 +01:00
|
|
|
const auto db_seq_before(db::sequence(*m::dbs::events));
|
2019-03-16 20:38:14 +01:00
|
|
|
#endif
|
|
|
|
|
2018-12-31 02:02:19 +01:00
|
|
|
txn();
|
2019-03-16 20:38:14 +01:00
|
|
|
|
|
|
|
#ifdef RB_DEBUG
|
2019-03-19 19:45:01 +01:00
|
|
|
const auto db_seq_after(db::sequence(*m::dbs::events));
|
2019-03-16 20:38:14 +01:00
|
|
|
|
2019-03-21 22:13:45 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s | wrote %lu:%lu | db seq %lu:%lu %zu cells in %zu bytes to events database ...",
|
|
|
|
loghead(eval),
|
|
|
|
sequence::get(eval),
|
|
|
|
eval.sequence_shared[1],
|
|
|
|
db_seq_before,
|
|
|
|
db_seq_after,
|
|
|
|
txn.size(),
|
|
|
|
txn.bytes()
|
|
|
|
};
|
2019-03-26 04:17:49 +01:00
|
|
|
#endif
|
2017-11-16 02:37:09 +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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
hook(event, std::forward<T>(data));
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
"%s | phase:%s :%s",
|
|
|
|
loghead(eval),
|
|
|
|
unquote(hook.feature.get("name")),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:08:00 +02:00
|
|
|
template<class... args>
|
|
|
|
ircd::m::vm::fault
|
|
|
|
ircd::m::vm::handle_error(const vm::opts &opts,
|
|
|
|
const vm::fault &code,
|
|
|
|
const string_view &fmt,
|
|
|
|
args&&... a)
|
|
|
|
{
|
|
|
|
if(opts.errorlog & code)
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, fmt, std::forward<args>(a)...
|
|
|
|
};
|
2019-05-13 21:31:34 +02:00
|
|
|
else if(~opts.warnlog & code)
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, fmt, std::forward<args>(a)...
|
|
|
|
};
|
2019-04-30 23:08:00 +02:00
|
|
|
|
|
|
|
if(opts.warnlog & code)
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, fmt, std::forward<args>(a)...
|
|
|
|
};
|
|
|
|
|
|
|
|
if(~opts.nothrows & code)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
code, fmt, std::forward<args>(a)...
|
|
|
|
};
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|