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-09-26 06:42:07 +02:00
|
|
|
|
|
|
|
#pragma once
|
2017-10-25 18:47:03 +02:00
|
|
|
#define HAVE_IRCD_M_VM_H
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
/// Matrix Virtual Machine
|
|
|
|
///
|
2017-10-25 18:47:03 +02:00
|
|
|
namespace ircd::m::vm
|
2017-09-26 06:42:07 +02:00
|
|
|
{
|
2018-02-28 07:53:44 +01:00
|
|
|
struct error; // custom exception
|
|
|
|
struct opts;
|
2018-05-07 01:04:51 +02:00
|
|
|
struct copts;
|
2017-11-16 02:48:25 +01:00
|
|
|
struct eval;
|
2018-04-16 23:21:54 +02:00
|
|
|
enum fault :uint;
|
2018-02-28 07:53:44 +01:00
|
|
|
using fault_t = std::underlying_type<fault>::type;
|
2017-09-26 06:42:07 +02:00
|
|
|
|
2018-05-08 01:08:52 +02:00
|
|
|
string_view reflect(const fault &);
|
2019-03-09 22:51:03 +01:00
|
|
|
http::code http_code(const fault &);
|
2019-03-21 20:49:02 +01:00
|
|
|
string_view loghead(const mutable_buffer &, const eval &);
|
|
|
|
string_view loghead(const eval &); // single tls buffer
|
2019-03-21 23:58:18 +01:00
|
|
|
|
|
|
|
extern const opts default_opts;
|
|
|
|
extern const copts default_copts;
|
|
|
|
extern log::log log;
|
|
|
|
extern ctx::dock dock;
|
|
|
|
extern bool ready;
|
2017-11-16 02:48:25 +01:00
|
|
|
}
|
|
|
|
|
2019-03-19 19:45:01 +01:00
|
|
|
namespace ircd::m::vm::sequence
|
|
|
|
{
|
2019-03-21 23:58:18 +01:00
|
|
|
extern ctx::dock dock;
|
2019-03-19 19:45:01 +01:00
|
|
|
extern uint64_t retired; // already written; always monotonic
|
|
|
|
extern uint64_t committed; // pending write; usually monotonic
|
|
|
|
extern uint64_t uncommitted; // evaluating; not monotonic
|
2019-03-26 04:17:49 +01:00
|
|
|
static size_t pending;
|
2019-03-19 19:45:01 +01:00
|
|
|
|
|
|
|
const uint64_t &get(const eval &);
|
2019-03-21 22:11:42 +01:00
|
|
|
uint64_t get(id::event::buf &); // [GET]
|
2019-03-19 19:45:01 +01:00
|
|
|
uint64_t max();
|
|
|
|
uint64_t min();
|
|
|
|
};
|
|
|
|
|
2018-02-08 22:23:01 +01:00
|
|
|
/// Event Evaluation Device
|
|
|
|
///
|
|
|
|
/// This object conducts the evaluation of an event or a tape of multiple
|
|
|
|
/// events. An event is evaluated in an attempt to execute it. Events which
|
|
|
|
/// fail during evaluation won't be executed; such is the case for events which
|
|
|
|
/// have already been executed, or events which are invalid or lead to invalid
|
|
|
|
/// transitions or actions of the machine etc.
|
|
|
|
///
|
|
|
|
struct ircd::m::vm::eval
|
2018-05-07 02:07:18 +02:00
|
|
|
:instance_list<eval>
|
2018-02-08 22:23:01 +01:00
|
|
|
{
|
2019-03-19 19:45:01 +01:00
|
|
|
static uint64_t id_ctr;
|
2019-03-21 23:58:18 +01:00
|
|
|
static uint executing;
|
|
|
|
static uint injecting;
|
|
|
|
static uint injecting_room;
|
2019-02-07 05:54:04 +01:00
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
const vm::opts *opts {&default_opts};
|
|
|
|
const vm::copts *copts {nullptr};
|
2019-03-19 19:45:01 +01:00
|
|
|
ctx::ctx *ctx {ctx::current};
|
|
|
|
uint64_t id {++id_ctr};
|
2019-02-07 05:54:04 +01:00
|
|
|
uint64_t sequence {0};
|
2019-03-21 22:13:45 +01:00
|
|
|
uint64_t sequence_shared[2] {0}; // min, max
|
|
|
|
std::shared_ptr<db::txn> txn;
|
2019-02-07 05:54:04 +01:00
|
|
|
|
2018-05-07 03:10:01 +02:00
|
|
|
const json::iov *issue {nullptr};
|
|
|
|
const event *event_ {nullptr};
|
2019-02-07 05:54:04 +01:00
|
|
|
json::array pdus;
|
|
|
|
|
2019-03-19 19:45:01 +01:00
|
|
|
string_view room_id;
|
2018-05-07 01:04:51 +02:00
|
|
|
event::id::buf event_id;
|
2019-03-19 19:45:01 +01:00
|
|
|
event::conforms report;
|
|
|
|
|
|
|
|
static bool for_each_pdu(const std::function<bool (const json::object &)> &);
|
2018-05-07 01:04:51 +02:00
|
|
|
|
2018-05-08 01:08:52 +02:00
|
|
|
public:
|
2018-05-07 01:04:51 +02:00
|
|
|
operator const event::id::buf &() const;
|
2018-02-08 22:23:01 +01:00
|
|
|
|
|
|
|
fault operator()(const event &);
|
2018-05-07 01:04:51 +02:00
|
|
|
fault operator()(json::iov &event, const json::iov &content);
|
|
|
|
fault operator()(const room &, json::iov &event, const json::iov &content);
|
2018-02-08 22:23:01 +01:00
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
eval(const vm::opts &);
|
|
|
|
eval(const vm::copts &);
|
2018-02-28 07:53:44 +01:00
|
|
|
eval(const event &, const vm::opts & = default_opts);
|
2019-02-07 05:54:04 +01:00
|
|
|
eval(const json::array &event, const vm::opts & = default_opts);
|
2018-05-07 01:04:51 +02:00
|
|
|
eval(json::iov &event, const json::iov &content, const vm::copts & = default_copts);
|
|
|
|
eval(const room &, json::iov &event, const json::iov &content);
|
|
|
|
eval() = default;
|
2018-04-16 23:24:56 +02:00
|
|
|
eval(eval &&) = delete;
|
|
|
|
eval(const eval &) = delete;
|
2018-05-07 02:35:12 +02:00
|
|
|
~eval() noexcept;
|
2019-02-07 05:54:21 +01:00
|
|
|
|
2019-03-21 22:11:42 +01:00
|
|
|
static bool for_each(const ctx::ctx *const &, const std::function<bool (eval &)> &);
|
2019-02-07 05:54:21 +01:00
|
|
|
static bool for_each(const std::function<bool (eval &)> &);
|
2019-03-21 22:11:42 +01:00
|
|
|
static size_t count(const ctx::ctx *const &);
|
2019-02-07 05:54:21 +01:00
|
|
|
static eval *find(const event::id &);
|
|
|
|
static eval &get(const event::id &);
|
2019-03-19 19:45:01 +01:00
|
|
|
static bool sequnique(const uint64_t &seq);
|
|
|
|
static eval *seqnext(const uint64_t &seq);
|
|
|
|
static eval *seqmax();
|
|
|
|
static eval *seqmin();
|
|
|
|
static void seqsort();
|
2018-02-08 22:23:01 +01:00
|
|
|
};
|
|
|
|
|
2017-11-30 20:01:14 +01:00
|
|
|
/// Evaluation faults. These are reasons which evaluation has halted but may
|
|
|
|
/// continue after the user defaults the fault. They are basically types of
|
2018-02-28 07:53:44 +01:00
|
|
|
/// interrupts and traps, which are supposed to be recoverable. Only the
|
|
|
|
/// GENERAL protection fault (#gp) is an abort and is not supposed to be
|
|
|
|
/// recoverable. The fault codes have the form of bitflags so they can be
|
|
|
|
/// used in masks; outside of that case only one fault is dealt with at
|
|
|
|
/// a time so they can be switched as they appear in the enum.
|
2017-11-30 20:01:14 +01:00
|
|
|
///
|
|
|
|
enum ircd::m::vm::fault
|
|
|
|
:uint
|
|
|
|
{
|
2018-02-28 07:53:44 +01:00
|
|
|
ACCEPT = 0x00, ///< No fault.
|
|
|
|
EXISTS = 0x01, ///< Replaying existing event. (#ex)
|
|
|
|
INVALID = 0x02, ///< Non-conforming event format. (#ud)
|
2019-04-08 05:57:43 +02:00
|
|
|
GENERAL = 0x04, ///< General protection fault. (#gp)
|
|
|
|
STATE = 0x10, ///< Required state is missing (#st)
|
2018-02-28 07:53:44 +01:00
|
|
|
EVENT = 0x20, ///< Eval requires addl events in the ef register (#ef)
|
2019-04-08 05:57:43 +02:00
|
|
|
INTERRUPT = 0x40, ///< ctx::interrupted (#nmi)
|
2017-11-30 20:01:14 +01:00
|
|
|
};
|
2018-02-28 07:53:44 +01:00
|
|
|
|
|
|
|
/// Evaluation Options
|
|
|
|
struct ircd::m::vm::opts
|
|
|
|
{
|
2019-03-09 22:12:58 +01:00
|
|
|
/// Call conform hooks (detailed behavior can be tweaked below)
|
|
|
|
bool conform {true};
|
|
|
|
|
2018-10-11 10:18:21 +02:00
|
|
|
/// Make fetches or false to bypass fetch stage.
|
|
|
|
bool fetch {true};
|
|
|
|
|
|
|
|
/// Call eval hooks or false to bypass this stage.
|
|
|
|
bool eval {true};
|
|
|
|
|
2019-03-09 21:08:05 +01:00
|
|
|
/// Make writes to database
|
|
|
|
bool write {true};
|
|
|
|
|
|
|
|
/// Call post hooks or false to bypass post-write / pre-notify effects.
|
|
|
|
bool post {true};
|
2018-02-28 07:53:44 +01:00
|
|
|
|
2018-05-28 04:56:04 +02:00
|
|
|
/// Broadcast to clients/servers. When true, individual notify options
|
|
|
|
/// that follow are considered. When false, no notifications occur.
|
|
|
|
short notify {true};
|
|
|
|
|
|
|
|
/// Broadcast to local clients (/sync stream).
|
|
|
|
bool notify_clients {true};
|
|
|
|
|
|
|
|
/// Broadcast to federation servers (/federation/send/).
|
|
|
|
bool notify_servers {true};
|
2018-02-28 07:53:44 +01:00
|
|
|
|
2019-03-09 21:08:05 +01:00
|
|
|
/// Apply effects of this event or false to bypass this stage.
|
|
|
|
bool effects {true};
|
|
|
|
|
2018-03-13 03:34:54 +01:00
|
|
|
/// False to allow a dirty conforms report (not recommended).
|
|
|
|
bool conforming {true};
|
|
|
|
|
|
|
|
/// Mask of conformity failures to allow without considering dirty.
|
2018-02-28 07:53:44 +01:00
|
|
|
event::conforms non_conform;
|
|
|
|
|
2018-03-13 03:34:54 +01:00
|
|
|
/// If the event was already checked before the eval, set this to true
|
|
|
|
/// and include the report (see below).
|
|
|
|
bool conformed {false};
|
|
|
|
|
|
|
|
/// When conformed=true, this report will be included instead of generating
|
|
|
|
/// one during the eval. This is useful if a conformity check was already
|
|
|
|
/// done before eval.
|
|
|
|
event::conforms report;
|
|
|
|
|
2018-03-09 00:55:54 +01:00
|
|
|
/// Toggles whether event may be considered a "present event" and may
|
|
|
|
/// update the optimized present state table of the room if it is proper.
|
|
|
|
bool present {true};
|
|
|
|
|
2018-05-05 05:42:25 +02:00
|
|
|
/// Toggles whether event may be added to the room head table which means
|
|
|
|
/// it is considered unreferenced by any other event at this time. It is
|
|
|
|
/// safe for this to always be true if events are evaluated in order. If
|
|
|
|
/// `present` is false this should be set to false but they are not tied.
|
2019-02-06 02:15:17 +01:00
|
|
|
bool room_head {true};
|
2018-05-05 05:42:25 +02:00
|
|
|
|
|
|
|
/// Toggles whether the prev_events of this event are removed from the
|
|
|
|
/// room head table, now that this event has referenced them. It is safe
|
|
|
|
/// for this to always be true.
|
2019-02-06 02:15:17 +01:00
|
|
|
bool room_refs {true};
|
2018-05-05 05:42:25 +02:00
|
|
|
|
2018-03-09 00:55:54 +01:00
|
|
|
/// Toggles whether the state btree is updated; this should be consistently
|
|
|
|
/// true or false for all events in a room.
|
|
|
|
bool history {true};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
/// Bypass check for event having already been evaluated so it can be
|
|
|
|
/// replayed through the system (not recommended).
|
|
|
|
bool replays {false};
|
|
|
|
|
2019-01-24 20:32:57 +01:00
|
|
|
/// If the input event has a reference to already-strung json we can use
|
|
|
|
/// that directly when writing to the DB. When this is false we will
|
|
|
|
/// re-stringify the event internally either from a referenced source or
|
|
|
|
/// the tuple if no source is referenced. This should only be set to true
|
|
|
|
/// if the evaluator already performed this and the json source is good.
|
|
|
|
bool json_source {false};
|
|
|
|
|
2018-03-22 09:31:17 +01:00
|
|
|
// Verify the origin signature
|
|
|
|
bool verify {true};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
/// TODO: Y
|
|
|
|
bool prev_check_exists {true};
|
|
|
|
|
2018-03-09 00:55:54 +01:00
|
|
|
/// TODO: Y
|
|
|
|
bool head_must_exist {false};
|
|
|
|
|
2019-03-27 21:45:21 +01:00
|
|
|
/// Whether to automatically fetch and evaluate the auth chain for the
|
|
|
|
/// event; effective when there is no state for this room on the server.
|
|
|
|
bool fetch_auth_chain {true};
|
|
|
|
|
2018-04-16 23:24:56 +02:00
|
|
|
/// Evaluators can set this value to optimize the creation of the database
|
|
|
|
/// transaction where the event will be stored. This value should be set
|
|
|
|
/// to the amount of space the event consumes; the JSON-serialized size is
|
2018-04-17 02:28:26 +02:00
|
|
|
/// a good value here. Default of -1 will automatically use serialized().
|
|
|
|
size_t reserve_bytes = -1;
|
2018-04-16 23:24:56 +02:00
|
|
|
|
|
|
|
/// This value is added to reserve_bytes to account for indexing overhead
|
|
|
|
/// in the database transaction allocation. Most evaluators have little
|
|
|
|
/// reason to ever adjust this.
|
2018-06-04 00:18:53 +02:00
|
|
|
size_t reserve_index {1024};
|
2018-04-16 23:24:56 +02:00
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
/// Mask of faults that are not thrown as exceptions out of eval(). If
|
|
|
|
/// masked, the fault is returned from eval(). By default, the EXISTS
|
2019-04-08 05:57:43 +02:00
|
|
|
/// fault is masked which means existing events won't kill eval loops.
|
2018-02-28 07:53:44 +01:00
|
|
|
fault_t nothrows
|
|
|
|
{
|
2019-04-08 05:57:43 +02:00
|
|
|
EXISTS
|
2018-02-28 07:53:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Mask of faults that are logged to the error facility in vm::log.
|
|
|
|
fault_t errorlog
|
|
|
|
{
|
2019-04-08 05:57:43 +02:00
|
|
|
~(EXISTS)
|
2018-02-28 07:53:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Mask of faults that are logged to the warning facility in vm::log
|
|
|
|
fault_t warnlog
|
|
|
|
{
|
|
|
|
EXISTS
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Whether to log a debug message on successful eval.
|
|
|
|
bool debuglog_accept {false};
|
|
|
|
|
|
|
|
/// Whether to log an info message on successful eval.
|
|
|
|
bool infolog_accept {false};
|
|
|
|
};
|
|
|
|
|
2018-04-06 08:07:55 +02:00
|
|
|
/// Extension structure to vm::opts which includes additional options for
|
|
|
|
/// commissioning events originating from this server which are then passed
|
2019-03-21 23:58:18 +01:00
|
|
|
/// through eval (this process is also known as issuing).
|
2018-05-07 01:04:51 +02:00
|
|
|
struct ircd::m::vm::copts
|
2018-04-06 08:07:55 +02:00
|
|
|
:opts
|
|
|
|
{
|
2018-09-06 04:32:14 +02:00
|
|
|
/// A matrix-spec opaque token from a client identifying this eval.
|
|
|
|
string_view client_txnid;
|
|
|
|
|
2018-05-12 06:07:53 +02:00
|
|
|
/// Hash and include hashes object.
|
2018-05-12 06:21:15 +02:00
|
|
|
bool add_hash {true};
|
2018-04-06 08:07:55 +02:00
|
|
|
|
2018-05-12 06:07:53 +02:00
|
|
|
/// Sign and include signatures object
|
2018-05-12 06:21:15 +02:00
|
|
|
bool add_sig {true};
|
2018-04-06 08:07:55 +02:00
|
|
|
|
2018-05-12 06:07:53 +02:00
|
|
|
/// Generate and include event_id
|
2018-05-12 06:21:15 +02:00
|
|
|
bool add_event_id {true};
|
2018-04-06 08:07:55 +02:00
|
|
|
|
2018-05-12 06:07:53 +02:00
|
|
|
/// Include our origin
|
2018-05-12 06:21:15 +02:00
|
|
|
bool add_origin {true};
|
2018-04-06 08:07:55 +02:00
|
|
|
|
2018-05-12 06:07:53 +02:00
|
|
|
/// Include origin_server_ts
|
2018-05-12 06:21:15 +02:00
|
|
|
bool add_origin_server_ts {true};
|
2018-04-06 08:07:55 +02:00
|
|
|
|
2018-05-12 11:27:39 +02:00
|
|
|
/// Add prev_events
|
|
|
|
bool add_prev_events {true};
|
|
|
|
|
|
|
|
/// Add prev_state
|
|
|
|
bool add_prev_state {true};
|
|
|
|
|
|
|
|
/// Add auth_events
|
|
|
|
bool add_auth_events {true};
|
|
|
|
|
2018-04-06 08:07:55 +02:00
|
|
|
/// Whether to log a debug message before commit
|
|
|
|
bool debuglog_precommit {false};
|
|
|
|
|
|
|
|
/// Whether to log an info message after commit accepted
|
|
|
|
bool infolog_postcommit {false};
|
2019-03-09 21:13:21 +01:00
|
|
|
|
|
|
|
/// Call the issue hook or bypass
|
|
|
|
bool issue {true};
|
2018-04-06 08:07:55 +02:00
|
|
|
};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
struct ircd::m::vm::error
|
|
|
|
:m::error
|
|
|
|
{
|
|
|
|
vm::fault code;
|
|
|
|
|
2018-12-31 01:22:18 +01:00
|
|
|
template<class... args> error(const http::code &, const fault &, const string_view &fmt, args&&... a);
|
|
|
|
template<class... args> error(const fault &, const string_view &fmt, args&&... a);
|
2018-12-10 22:08:35 +01:00
|
|
|
template<class... args> error(const string_view &fmt, args&&... a);
|
2018-02-28 07:53:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class... args>
|
2018-12-31 01:22:18 +01:00
|
|
|
ircd::m::vm::error::error(const string_view &fmt,
|
2018-02-28 07:53:44 +01:00
|
|
|
args&&... a)
|
2018-12-31 01:22:18 +01:00
|
|
|
:error
|
2018-02-28 07:53:44 +01:00
|
|
|
{
|
2018-12-31 01:22:18 +01:00
|
|
|
http::INTERNAL_SERVER_ERROR, fault::GENERAL, fmt, std::forward<args>(a)...
|
2018-02-28 07:53:44 +01:00
|
|
|
}
|
2018-12-31 01:22:18 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
template<class... args>
|
|
|
|
ircd::m::vm::error::error(const fault &code,
|
|
|
|
const string_view &fmt,
|
|
|
|
args&&... a)
|
|
|
|
:error
|
2018-02-28 07:53:44 +01:00
|
|
|
{
|
2019-03-09 22:51:03 +01:00
|
|
|
http_code(code), code, fmt, std::forward<args>(a)...
|
2018-02-28 07:53:44 +01:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class... args>
|
2018-12-31 01:22:18 +01:00
|
|
|
ircd::m::vm::error::error(const http::code &httpcode,
|
|
|
|
const fault &code,
|
|
|
|
const string_view &fmt,
|
2018-02-28 07:53:44 +01:00
|
|
|
args&&... a)
|
|
|
|
:m::error
|
|
|
|
{
|
2018-12-31 01:22:18 +01:00
|
|
|
child, httpcode, "M_VM_FAULT", fmt, std::forward<args>(a)...
|
2018-02-28 07:53:44 +01:00
|
|
|
}
|
|
|
|
,code
|
|
|
|
{
|
2018-12-31 01:22:18 +01:00
|
|
|
code
|
2018-02-28 07:53:44 +01:00
|
|
|
}
|
|
|
|
{}
|