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
|
|
|
{
|
2018-05-27 07:05:56 +02:00
|
|
|
extern hook::site<> commit_hook;
|
|
|
|
extern hook::site<> eval_hook;
|
|
|
|
extern hook::site<> notify_hook;
|
2018-05-29 13:04:32 +02:00
|
|
|
extern phase enter;
|
|
|
|
extern phase leave;
|
2018-04-06 08:07:55 +02:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
static void write(eval &);
|
|
|
|
static fault _eval_edu(eval &, const event &);
|
|
|
|
static fault _eval_pdu(eval &, const event &);
|
2018-04-16 23:21:54 +02:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
extern "C" fault eval__event(eval &, const event &);
|
|
|
|
extern "C" fault eval__commit(eval &, json::iov &, const json::iov &);
|
|
|
|
extern "C" fault eval__commit_room(eval &, const room &, json::iov &, const json::iov &);
|
2018-04-16 23:21:54 +02:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
static void init();
|
|
|
|
static void fini();
|
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
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
decltype(ircd::m::vm::commit_hook)
|
|
|
|
ircd::m::vm::commit_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.commit" }
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::eval_hook)
|
|
|
|
ircd::m::vm::eval_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.eval" }
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::notify_hook)
|
|
|
|
ircd::m::vm::notify_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.notify" }
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
current_sequence = retired_sequence(event_id);
|
2018-05-07 01:04:51 +02:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "BOOT %s @%lu [%s]",
|
|
|
|
string_view{m::my_node.node_id},
|
|
|
|
current_sequence,
|
|
|
|
current_sequence? string_view{event_id} : "NO EVENTS"_sv
|
|
|
|
};
|
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
|
|
|
{
|
2018-06-12 07:43:19 +02:00
|
|
|
assert(eval::list.empty());
|
2018-06-03 19:02:19 +02:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
id::event::buf event_id;
|
|
|
|
const auto current_sequence
|
|
|
|
{
|
|
|
|
retired_sequence(event_id)
|
|
|
|
};
|
2018-05-07 01:04:51 +02:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "HLT '%s' @%lu [%s]",
|
|
|
|
string_view{m::my_node.node_id},
|
|
|
|
current_sequence,
|
|
|
|
current_sequence? string_view{event_id} : "NO EVENTS"_sv
|
|
|
|
};
|
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
|
2018-05-07 06:15:25 +02:00
|
|
|
ircd::m::vm::eval__commit_room(eval &eval,
|
|
|
|
const room &room,
|
|
|
|
json::iov &event,
|
|
|
|
const json::iov &contents)
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2018-05-29 13:04:32 +02:00
|
|
|
// This eval entry point is only used for commits. We try to find the
|
|
|
|
// 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.
|
|
|
|
eval.issue = &event;
|
|
|
|
eval.room_id = room.room_id;
|
|
|
|
eval.phase = &vm::enter;
|
|
|
|
const unwind deissue{[&eval]
|
|
|
|
{
|
|
|
|
eval.phase = &vm::leave;
|
|
|
|
eval.room_id = {};
|
|
|
|
eval.issue = nullptr;
|
|
|
|
}};
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
const json::iov::push room_id
|
|
|
|
{
|
|
|
|
event, { "room_id", room.room_id }
|
|
|
|
};
|
|
|
|
|
2018-06-02 01:46:58 +02:00
|
|
|
using prev_prototype = std::pair<json::array, int64_t> (const m::room &,
|
|
|
|
const mutable_buffer &,
|
|
|
|
const size_t &,
|
|
|
|
const bool &);
|
|
|
|
static m::import<prev_prototype> make_prev__buf
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2018-06-02 01:46:58 +02:00
|
|
|
"m_room", "make_prev__buf"
|
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};
|
|
|
|
const auto prev
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2018-06-02 01:46:58 +02:00
|
|
|
make_prev__buf(room, prev_buf, 16, 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-02 01:46:58 +02:00
|
|
|
using auth_prototype = json::array (const m::room &,
|
|
|
|
const mutable_buffer &,
|
|
|
|
const vector_view<const string_view> &,
|
|
|
|
const string_view &);
|
|
|
|
|
|
|
|
static m::import<auth_prototype> make_auth__buf
|
|
|
|
{
|
|
|
|
"m_room", "make_auth__buf"
|
|
|
|
};
|
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
char ae_buf[512];
|
2018-05-12 11:27:39 +02:00
|
|
|
json::array auth_events;
|
|
|
|
if(depth != -1 && opts.add_auth_events)
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2018-06-02 01:46:58 +02:00
|
|
|
static const string_view types[]
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2018-06-02 01:46:58 +02:00
|
|
|
"m.room.create",
|
|
|
|
"m.room.join_rules",
|
|
|
|
"m.room.power_levels",
|
|
|
|
};
|
2018-05-07 01:04:51 +02:00
|
|
|
|
2018-06-02 01:46:58 +02:00
|
|
|
const auto member
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
2018-06-02 01:46:58 +02:00
|
|
|
event.at("type") != "m.room.member"?
|
|
|
|
string_view{event.at("sender")}:
|
|
|
|
string_view{}
|
|
|
|
};
|
2018-05-07 01:04:51 +02:00
|
|
|
|
2018-06-02 01:46:58 +02:00
|
|
|
auth_events = make_auth__buf(room, ae_buf, types, member);
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::add prev_state_
|
|
|
|
{
|
|
|
|
event, opts.add_prev_state,
|
2018-05-12 11:27:39 +02:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"prev_state", []
|
2018-05-12 11:27:39 +02:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
return json::empty_array;
|
2018-05-12 11:27:39 +02:00
|
|
|
}
|
2018-06-05 20:19:40 +02:00
|
|
|
}
|
2018-05-07 01:04:51 +02:00
|
|
|
};
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
return 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
|
2018-05-07 06:15:25 +02:00
|
|
|
ircd::m::vm::eval__commit(eval &eval,
|
|
|
|
json::iov &event,
|
|
|
|
const json::iov &contents)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
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;
|
|
|
|
eval.phase = &enter;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.phase = &leave;
|
|
|
|
eval.issue = nullptr;
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
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
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-03-07 20:54:56 +01:00
|
|
|
// event_id
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-03-07 20:54:56 +01:00
|
|
|
sha256::buf event_id_hash;
|
2018-05-12 06:21:15 +02:00
|
|
|
if(opts.add_event_id)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-15 01:56:50 +01:00
|
|
|
const json::iov::push _content
|
|
|
|
{
|
|
|
|
event, { "content", content },
|
|
|
|
};
|
|
|
|
|
2018-03-07 20:54:56 +01:00
|
|
|
thread_local char preimage_buf[64_KiB];
|
2018-03-14 23:06:35 +01:00
|
|
|
event_id_hash = sha256
|
|
|
|
{
|
|
|
|
stringify(mutable_buffer{preimage_buf}, event)
|
|
|
|
};
|
2018-03-07 20:54:56 +01:00
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-03-14 23:06:35 +01:00
|
|
|
const string_view event_id
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-05-12 06:21:15 +02:00
|
|
|
opts.add_event_id?
|
2018-05-07 06:15:25 +02:00
|
|
|
make_id(event, eval.event_id, event_id_hash):
|
2018-03-14 23:06:35 +01:00
|
|
|
string_view{}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add event_id_
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-05-12 06:21:15 +02:00
|
|
|
event, opts.add_event_id,
|
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"event_id", [&event_id]() -> json::value
|
|
|
|
{
|
|
|
|
return event_id;
|
|
|
|
}
|
2018-05-12 06:21:15 +02:00
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-03-07 20:54:56 +01:00
|
|
|
// hashes
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
char hashes_buf[128];
|
2018-03-07 20:54:56 +01:00
|
|
|
const string_view hashes
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-05-12 06:21:15 +02:00
|
|
|
opts.add_hash?
|
2018-03-08 18:54:39 +01:00
|
|
|
m::event::hashes(hashes_buf, event, content):
|
|
|
|
string_view{}
|
2018-03-07 20:54:56 +01:00
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add hashes_
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-05-12 06:21:15 +02:00
|
|
|
event, opts.add_hash,
|
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"hashes", [&hashes]() -> json::value
|
|
|
|
{
|
|
|
|
return hashes;
|
|
|
|
}
|
2018-05-12 06:21:15 +02:00
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
return eval(event);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 20:01:14 +01:00
|
|
|
enum ircd::m::vm::fault
|
2018-05-07 06:15:25 +02:00
|
|
|
ircd::m::vm::eval__event(eval &eval,
|
|
|
|
const event &event)
|
2018-02-09 08:21:15 +01:00
|
|
|
try
|
2017-11-30 20:01:14 +01:00
|
|
|
{
|
2018-05-29 13:04:32 +02:00
|
|
|
// Set a member pointer to the event currently being evaluated. This
|
|
|
|
// allows other parallel evals to have deep access to exactly what this
|
|
|
|
// eval is working on. The pointer must be nulled on the way out.
|
|
|
|
eval.event_ = &event;
|
|
|
|
if(!eval.issue)
|
|
|
|
eval.phase = &enter;
|
|
|
|
|
|
|
|
const unwind null_event{[&eval]
|
|
|
|
{
|
|
|
|
if(!eval.issue)
|
|
|
|
eval.phase = &leave;
|
|
|
|
|
|
|
|
eval.event_ = nullptr;
|
|
|
|
}};
|
|
|
|
|
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
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
if(eval.copts)
|
2018-05-07 01:04:51 +02:00
|
|
|
{
|
|
|
|
if(unlikely(!my_host(at<"origin"_>(event))))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
fault::GENERAL, "Committing event for origin: %s", at<"origin"_>(event)
|
|
|
|
};
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
if(eval.copts->debuglog_precommit)
|
2018-08-16 09:46:46 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "injecting event(mark +%ld) %s",
|
|
|
|
vm::current_sequence,
|
|
|
|
pretty_oneline(event)
|
|
|
|
};
|
2018-05-07 01:04:51 +02:00
|
|
|
|
|
|
|
check_size(event);
|
|
|
|
commit_hook(event);
|
|
|
|
}
|
|
|
|
|
2018-03-13 03:34:54 +01:00
|
|
|
const event::conforms &report
|
2018-02-09 08:21:15 +01:00
|
|
|
{
|
2018-05-07 06:15:25 +02:00
|
|
|
opts.conforming && !opts.conformed?
|
|
|
|
event::conforms{event, opts.non_conform.report}:
|
|
|
|
opts.report
|
2018-02-09 08:21:15 +01:00
|
|
|
};
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
if(opts.conforming && !report.clean())
|
2018-02-28 07:53:44 +01:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
fault::INVALID, "Non-conforming event: %s", string(report)
|
|
|
|
};
|
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
// A conforming (with lots of masks) event without an event_id is an EDU.
|
2018-03-13 03:07:41 +01:00
|
|
|
const fault ret
|
|
|
|
{
|
|
|
|
json::get<"event_id"_>(event)?
|
2018-05-07 06:15:25 +02:00
|
|
|
_eval_pdu(eval, event):
|
|
|
|
_eval_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-03-13 03:07:41 +01:00
|
|
|
vm::accepted accepted
|
|
|
|
{
|
2018-05-07 06:15:25 +02:00
|
|
|
event, &opts, &report
|
2018-03-13 03:07:41 +01:00
|
|
|
};
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
if(opts.effects)
|
2018-03-13 03:07:41 +01:00
|
|
|
notify_hook(event);
|
2018-04-17 02:28:26 +02:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
if(opts.notify)
|
2018-03-27 09:52:02 +02:00
|
|
|
vm::accept(accepted);
|
2018-03-13 03:07:41 +01:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
if(opts.debuglog_accept)
|
2018-08-16 09:46:46 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s", pretty_oneline(event)
|
|
|
|
};
|
2018-03-13 03:07:41 +01:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
if(opts.infolog_accept)
|
2018-08-16 09:46:46 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
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 ctx::interrupted &e) // INTERRUPTION
|
|
|
|
{
|
|
|
|
if(eval.opts->errorlog & fault::INTERRUPT)
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "eval %s: #NMI: %s",
|
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(eval.opts->warnlog & fault::INTERRUPT)
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "eval %s: #NMI: %s",
|
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const error &e) // VM FAULT CODE
|
2018-03-07 20:02:23 +01:00
|
|
|
{
|
2018-05-07 06:15:25 +02:00
|
|
|
if(eval.opts->errorlog & e.code)
|
|
|
|
log::error
|
|
|
|
{
|
2018-06-05 20:21:07 +02:00
|
|
|
log, "eval %s: %s: %s %s",
|
2018-05-07 06:15:25 +02:00
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
2018-06-05 20:21:07 +02:00
|
|
|
reflect(e.code),
|
2018-05-07 06:15:25 +02:00
|
|
|
e.what(),
|
|
|
|
e.content
|
|
|
|
};
|
|
|
|
|
|
|
|
if(eval.opts->warnlog & e.code)
|
|
|
|
log::warning
|
|
|
|
{
|
2018-06-05 20:21:07 +02:00
|
|
|
log, "eval %s: %s: %s %s",
|
2018-05-07 06:15:25 +02:00
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
2018-06-05 20:21:07 +02:00
|
|
|
reflect(e.code),
|
2018-05-07 06:15:25 +02:00
|
|
|
e.what(),
|
|
|
|
e.content
|
|
|
|
};
|
|
|
|
|
|
|
|
if(eval.opts->nothrows & e.code)
|
2018-03-07 20:02:23 +01:00
|
|
|
return e.code;
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
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-05-15 07:24:03 +02:00
|
|
|
if(eval.opts->errorlog & fault::GENERAL)
|
2018-05-07 06:15:25 +02:00
|
|
|
log::error
|
|
|
|
{
|
2018-05-15 07:24:03 +02:00
|
|
|
log, "eval %s: #GP: %s :%s",
|
2018-05-07 06:15:25 +02:00
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
2018-05-15 07:24:03 +02:00
|
|
|
e.what(),
|
|
|
|
e.content
|
2018-05-07 06:15:25 +02:00
|
|
|
};
|
|
|
|
|
2018-05-15 07:24:03 +02:00
|
|
|
if(eval.opts->warnlog & fault::GENERAL)
|
2018-05-07 06:15:25 +02:00
|
|
|
log::warning
|
|
|
|
{
|
2018-05-15 07:24:03 +02:00
|
|
|
log, "eval %s: #GP: %s :%s",
|
2018-05-07 06:15:25 +02:00
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
2018-05-15 07:24:03 +02:00
|
|
|
e.what(),
|
|
|
|
e.content
|
2018-05-07 06:15:25 +02:00
|
|
|
};
|
|
|
|
|
2018-05-15 07:24:03 +02:00
|
|
|
if(eval.opts->nothrows & fault::GENERAL)
|
|
|
|
return fault::GENERAL;
|
|
|
|
|
2018-04-13 08:06:02 +02:00
|
|
|
throw;
|
|
|
|
}
|
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-05-07 06:15:25 +02:00
|
|
|
if(eval.opts->errorlog & fault::GENERAL)
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "eval %s: #GP: %s",
|
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
|
|
|
e.what()
|
|
|
|
};
|
2018-03-07 20:02:23 +01:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
if(eval.opts->warnlog & fault::GENERAL)
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "eval %s: #GP: %s",
|
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
|
|
|
e.what()
|
|
|
|
};
|
2018-03-07 20:02:23 +01:00
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
if(eval.opts->nothrows & fault::GENERAL)
|
2018-03-07 20:02:23 +01:00
|
|
|
return fault::GENERAL;
|
|
|
|
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
fault::GENERAL, "%s", e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ircd::m::vm::fault
|
|
|
|
ircd::m::vm::_eval_edu(eval &eval,
|
|
|
|
const event &event)
|
|
|
|
{
|
|
|
|
eval_hook(event);
|
|
|
|
return fault::ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ircd::m::vm::fault
|
|
|
|
ircd::m::vm::_eval_pdu(eval &eval,
|
|
|
|
const event &event)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2018-02-08 22:23:01 +01:00
|
|
|
at<"event_id"_>(event)
|
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-03-07 20:02:23 +01:00
|
|
|
if(!opts.replays && exists(event_id)) //TODO: exclusivity
|
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
|
|
|
};
|
|
|
|
|
2018-03-22 09:31:17 +01:00
|
|
|
if(opts.verify)
|
2018-03-23 07:53:11 +01:00
|
|
|
if(!verify(event))
|
2018-03-22 09:31:17 +01:00
|
|
|
throw m::BAD_SIGNATURE
|
|
|
|
{
|
|
|
|
"Signature verification failed"
|
|
|
|
};
|
|
|
|
|
2018-04-17 02:28:26 +02:00
|
|
|
const size_t reserve_bytes
|
|
|
|
{
|
|
|
|
opts.reserve_bytes == size_t(-1)?
|
|
|
|
json::serialized(event):
|
|
|
|
opts.reserve_bytes
|
|
|
|
};
|
|
|
|
|
|
|
|
db::txn txn
|
|
|
|
{
|
|
|
|
*dbs::events, db::txn::opts
|
|
|
|
{
|
|
|
|
reserve_bytes + opts.reserve_index, // reserve_bytes
|
|
|
|
0, // max_bytes (no max)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
eval.txn = &txn;
|
2018-06-12 07:43:19 +02:00
|
|
|
const unwind clear{[&eval]
|
2018-04-17 02:28:26 +02:00
|
|
|
{
|
|
|
|
eval.txn = nullptr;
|
|
|
|
}};
|
|
|
|
|
2018-04-16 23:21:54 +02:00
|
|
|
// Obtain sequence number here
|
2018-04-18 09:26:14 +02:00
|
|
|
eval.sequence = ++vm::current_sequence;
|
2018-04-16 23:21:54 +02:00
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
m::dbs::write_opts wopts;
|
|
|
|
wopts.present = opts.present;
|
|
|
|
wopts.history = opts.history;
|
2018-05-05 05:42:25 +02:00
|
|
|
wopts.head = opts.head;
|
|
|
|
wopts.refs = opts.refs;
|
2018-04-18 09:26:14 +02:00
|
|
|
wopts.idx = eval.sequence;
|
2018-04-16 23:21:54 +02:00
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
eval_hook(event);
|
2018-02-09 08:21:15 +01:00
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
const auto &depth
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-02-28 07:53:44 +01:00
|
|
|
json::get<"depth"_>(event)
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
const auto &type
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-02-28 07:53:44 +01:00
|
|
|
unquote(at<"type"_>(event))
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
const event::prev prev
|
2017-11-30 20:01:14 +01:00
|
|
|
{
|
2018-02-28 07:53:44 +01:00
|
|
|
event
|
2017-11-30 20:01:14 +01:00
|
|
|
};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
const size_t prev_count
|
2017-11-30 20:01:14 +01:00
|
|
|
{
|
2018-02-28 07:53:44 +01:00
|
|
|
size(json::get<"prev_events"_>(prev))
|
2017-11-30 20:01:14 +01:00
|
|
|
};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
//TODO: ex
|
2018-03-07 20:02:23 +01:00
|
|
|
if(opts.write && prev_count)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-05-15 07:24:50 +02:00
|
|
|
int64_t top;
|
|
|
|
id::event::buf head;
|
|
|
|
std::tie(head, top, std::ignore) = m::top(std::nothrow, room_id);
|
|
|
|
if(top < 0 && (opts.head_must_exist || opts.history))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
fault::STATE, "Found nothing for room %s", string_view{room_id}
|
|
|
|
};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
for(size_t i(0); i < prev_count; ++i)
|
|
|
|
{
|
|
|
|
const auto prev_id{prev.prev_event(i)};
|
2018-03-10 23:26:18 +01:00
|
|
|
if(opts.prev_check_exists && !exists(prev_id))
|
2018-02-28 07:53:44 +01:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
fault::EVENT, "Missing prev event %s", string_view{prev_id}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
m::room room{room_id, head};
|
|
|
|
m::room::state state{room};
|
|
|
|
m::state::id_buffer new_root_buf;
|
2018-03-03 07:03:06 +01:00
|
|
|
wopts.root_in = state.root_id;
|
|
|
|
wopts.root_out = new_root_buf;
|
2018-02-28 07:53:44 +01:00
|
|
|
const auto new_root
|
|
|
|
{
|
2018-04-17 02:28:26 +02:00
|
|
|
dbs::write(*eval.txn, event, wopts)
|
2018-02-28 07:53:44 +01:00
|
|
|
};
|
|
|
|
}
|
2018-03-07 20:02:23 +01:00
|
|
|
else if(opts.write)
|
2018-02-28 07:53:44 +01:00
|
|
|
{
|
|
|
|
m::state::id_buffer new_root_buf;
|
2018-03-03 07:03:06 +01:00
|
|
|
wopts.root_out = new_root_buf;
|
2018-02-28 07:53:44 +01:00
|
|
|
const auto new_root
|
|
|
|
{
|
2018-04-17 02:28:26 +02:00
|
|
|
dbs::write(*eval.txn, event, wopts)
|
2018-02-28 07:53:44 +01:00
|
|
|
};
|
|
|
|
}
|
2018-02-08 22:23:01 +01:00
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
if(opts.write)
|
|
|
|
write(eval);
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
return fault::ACCEPT;
|
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
|
|
|
|
ircd::m::vm::write(eval &eval)
|
|
|
|
{
|
2018-06-12 07:43:19 +02:00
|
|
|
assert(eval.txn);
|
2018-04-17 02:28:26 +02:00
|
|
|
auto &txn(*eval.txn);
|
2018-03-13 03:07:41 +01:00
|
|
|
if(eval.opts->debuglog_accept)
|
2018-05-07 06:15:25 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Committing %zu cells in %zu bytes to events database...",
|
|
|
|
txn.size(),
|
|
|
|
txn.bytes()
|
|
|
|
};
|
2017-11-30 20:01:14 +01:00
|
|
|
|
2018-04-17 02:28:26 +02:00
|
|
|
txn();
|
2018-04-16 23:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2018-04-18 09:26:14 +02:00
|
|
|
ircd::m::vm::retired_sequence()
|
2018-04-16 23:21:54 +02:00
|
|
|
{
|
2018-04-18 09:26:14 +02:00
|
|
|
event::id::buf event_id;
|
|
|
|
return retired_sequence(event_id);
|
2018-04-16 23:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2018-04-18 09:26:14 +02:00
|
|
|
ircd::m::vm::retired_sequence(event::id::buf &event_id)
|
2018-04-16 23:21:54 +02:00
|
|
|
{
|
2018-04-18 09:26:14 +02:00
|
|
|
static constexpr auto column_idx
|
|
|
|
{
|
|
|
|
json::indexof<event, "event_id"_>()
|
|
|
|
};
|
|
|
|
|
2018-04-16 23:21:54 +02:00
|
|
|
auto &column
|
|
|
|
{
|
2018-04-18 09:26:14 +02:00
|
|
|
dbs::event_column.at(column_idx)
|
2018-04-16 23:21:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
column.rbegin()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!it)
|
|
|
|
{
|
|
|
|
// If this iterator is invalid the events db should
|
|
|
|
// be completely fresh.
|
|
|
|
assert(db::sequence(*dbs::events) == 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
byte_view<uint64_t>(it->first)
|
|
|
|
};
|
|
|
|
|
2018-04-18 09:26:14 +02:00
|
|
|
event_id = it->second;
|
2018-04-18 04:14:39 +02:00
|
|
|
return ret;
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
2018-05-29 13:04:32 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// phase enter
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::enter)
|
|
|
|
ircd::m::vm::enter
|
|
|
|
{
|
|
|
|
"enter"
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// phase leave
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::leave)
|
|
|
|
ircd::m::vm::leave
|
|
|
|
{
|
|
|
|
"leave"
|
|
|
|
};
|