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
|
|
|
|
|
|
|
decltype(ircd::m::vm::log)
|
|
|
|
ircd::m::vm::log
|
|
|
|
{
|
|
|
|
"vm", 'v'
|
|
|
|
};
|
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
decltype(ircd::m::vm::accept)
|
|
|
|
ircd::m::vm::accept
|
2018-02-08 22:23:01 +01:00
|
|
|
{};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
decltype(ircd::m::vm::current_sequence)
|
2018-02-08 22:23:01 +01:00
|
|
|
ircd::m::vm::current_sequence
|
|
|
|
{};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
decltype(ircd::m::vm::default_opts)
|
|
|
|
ircd::m::vm::default_opts
|
|
|
|
{};
|
|
|
|
|
2018-04-06 08:07:55 +02:00
|
|
|
decltype(ircd::m::vm::default_commit_opts)
|
|
|
|
ircd::m::vm::default_commit_opts
|
|
|
|
{};
|
|
|
|
|
2018-04-16 23:21:54 +02:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::vm::init::init()
|
|
|
|
{
|
|
|
|
id::event::buf event_id;
|
2018-04-18 09:26:14 +02:00
|
|
|
current_sequence = retired_sequence(event_id);
|
2018-04-16 23:21:54 +02:00
|
|
|
|
2018-04-23 01:23:22 +02:00
|
|
|
log.info("BOOT %s @%lu [%s]",
|
|
|
|
string_view{m::my_node.node_id},
|
2018-04-16 23:21:54 +02:00
|
|
|
current_sequence,
|
|
|
|
current_sequence? string_view{event_id} : "NO EVENTS"_sv);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::init::~init()
|
|
|
|
{
|
|
|
|
id::event::buf event_id;
|
|
|
|
const auto current_sequence
|
|
|
|
{
|
2018-04-18 09:26:14 +02:00
|
|
|
retired_sequence(event_id)
|
2018-04-16 23:21:54 +02:00
|
|
|
};
|
|
|
|
|
2018-04-23 01:23:22 +02:00
|
|
|
log.info("HLT '%s' @%lu [%s]",
|
|
|
|
string_view{m::my_node.node_id},
|
2018-04-16 23:21:54 +02:00
|
|
|
current_sequence,
|
|
|
|
current_sequence? string_view{event_id} : "NO EVENTS"_sv);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// commit
|
|
|
|
//
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
/// This function takes an event object vector and adds our origin and event_id
|
|
|
|
/// and hashes and signature and attempts to inject the event into the core.
|
2017-11-16 02:37:09 +01:00
|
|
|
///
|
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:26:09 +01:00
|
|
|
ircd::m::vm::commit(json::iov &event,
|
2018-03-08 18:44:44 +01:00
|
|
|
const json::iov &contents,
|
2018-04-06 08:07:55 +02:00
|
|
|
const opts::commit &opts)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-14 23:06:35 +01:00
|
|
|
const json::iov::add_if _origin
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-14 23:06:35 +01:00
|
|
|
event, opts.origin,
|
|
|
|
{
|
|
|
|
"origin", my_host()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::add_if _origin_server_ts
|
|
|
|
{
|
|
|
|
event, opts.origin_server_ts,
|
|
|
|
{
|
|
|
|
"origin_server_ts", ircd::time<milliseconds>()
|
|
|
|
}
|
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-03-14 23:06:35 +01:00
|
|
|
if(opts.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-07 20:54:56 +01:00
|
|
|
event::id::buf eid_buf;
|
2018-03-14 23:06:35 +01:00
|
|
|
const string_view event_id
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-14 23:06:35 +01:00
|
|
|
opts.event_id?
|
2018-04-18 23:26:25 +02:00
|
|
|
make_id(event, eid_buf, event_id_hash):
|
2018-03-14 23:06:35 +01:00
|
|
|
string_view{}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-03-14 23:06:35 +01:00
|
|
|
const json::iov::add_if _event_id
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-14 23:06:35 +01:00
|
|
|
event, opts.event_id, { "event_id", event_id }
|
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-03-08 18:54:39 +01:00
|
|
|
opts.hash?
|
|
|
|
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-03-08 18:54:39 +01:00
|
|
|
const json::iov::add_if _hashes
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-08 18:54:39 +01:00
|
|
|
event, opts.hash, { "hashes", hashes }
|
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-03-08 18:54:39 +01:00
|
|
|
opts.sign?
|
|
|
|
m::event::signatures(sigs_buf, event, contents):
|
|
|
|
string_view{}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-03-08 18:54:39 +01:00
|
|
|
const json::iov::add_if _sigs
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-08 18:54:39 +01:00
|
|
|
event, opts.sign, { "signatures", sigs }
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::push _content
|
|
|
|
{
|
|
|
|
event, { "content", content },
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-03-16 21:22:00 +01:00
|
|
|
commit(event, opts);
|
|
|
|
return eid_buf;
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
namespace ircd::m::vm
|
|
|
|
{
|
|
|
|
extern hook::site commit_hook;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::commit_hook)
|
|
|
|
ircd::m::vm::commit_hook
|
|
|
|
{
|
2018-03-16 21:27:25 +01:00
|
|
|
{ "name", "vm.commit" }
|
2018-02-28 07:53:44 +01:00
|
|
|
};
|
|
|
|
|
2017-11-16 02:37:09 +01:00
|
|
|
/// Insert a new event originating from this server.
|
|
|
|
///
|
|
|
|
/// Figure 1:
|
2017-12-12 21:33:14 +01:00
|
|
|
/// in . <-- injection
|
2018-03-08 18:44:44 +01:00
|
|
|
/// ===:::::::==//
|
2017-12-12 21:33:14 +01:00
|
|
|
/// | ||||||| // <-- this function
|
2017-11-16 02:37:09 +01:00
|
|
|
/// | \\|// //|
|
2018-01-26 21:26:09 +01:00
|
|
|
/// | ||| // | | acceleration
|
2017-12-12 21:33:14 +01:00
|
|
|
/// | |||// | |
|
2018-01-26 21:26:09 +01:00
|
|
|
/// | |||/ | |
|
|
|
|
/// | ||| | V
|
2017-11-16 02:37:09 +01:00
|
|
|
/// | !!! |
|
2017-12-12 21:33:14 +01:00
|
|
|
/// | * | <----- nozzle
|
|
|
|
/// | ///|||\\\ |
|
|
|
|
/// |/|/|/|\|\|\| <---- propagation cone
|
|
|
|
/// _/|/|/|/|\|\|\|\_
|
2017-11-16 02:37:09 +01:00
|
|
|
/// out
|
|
|
|
///
|
2018-03-16 21:22:00 +01:00
|
|
|
ircd::m::vm::fault
|
2018-03-08 18:54:39 +01:00
|
|
|
ircd::m::vm::commit(const event &event,
|
2018-04-06 08:07:55 +02:00
|
|
|
const opts::commit &opts)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-08 18:54:39 +01:00
|
|
|
if(opts.debuglog_precommit)
|
2018-04-16 23:21:54 +02:00
|
|
|
log.debug("injecting event(mark +%ld) %s",
|
2018-03-08 18:54:39 +01:00
|
|
|
vm::current_sequence,
|
|
|
|
pretty_oneline(event));
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-03-09 17:23:39 +01:00
|
|
|
check_size(event);
|
|
|
|
commit_hook(event);
|
|
|
|
|
2018-04-06 08:07:55 +02:00
|
|
|
auto opts_{opts};
|
2018-03-22 09:31:17 +01:00
|
|
|
opts_.verify = false;
|
2018-03-09 17:23:39 +01:00
|
|
|
|
|
|
|
// Some functionality on this server may create an event on behalf
|
|
|
|
// of remote users. It's safe for us to mask this here, but eval'ing
|
|
|
|
// this event in any replay later will require special casing.
|
|
|
|
opts_.non_conform |= event::conforms::MISMATCH_ORIGIN_SENDER;
|
|
|
|
|
|
|
|
//TODO: X
|
2018-03-08 18:54:39 +01:00
|
|
|
opts_.non_conform |= event::conforms::MISSING_PREV_STATE;
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-03-16 21:22:00 +01:00
|
|
|
vm::eval eval{opts_};
|
|
|
|
const fault ret
|
2018-03-09 17:23:39 +01:00
|
|
|
{
|
2018-03-16 21:22:00 +01:00
|
|
|
eval(event)
|
2018-03-09 17:23:39 +01:00
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-04-16 23:21:54 +02:00
|
|
|
if(opts_.infolog_postcommit)
|
|
|
|
log.info("%s @%lu %s",
|
|
|
|
reflect(ret),
|
|
|
|
sequence(eval),
|
2018-03-16 21:19:52 +01:00
|
|
|
pretty_oneline(event, false));
|
|
|
|
|
2018-03-16 21:22:00 +01:00
|
|
|
return ret;
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Eval
|
|
|
|
//
|
|
|
|
// Processes any event from any place from any time and does whatever is
|
|
|
|
// necessary to validate, reject, learn from new information, ignore old
|
|
|
|
// information and advance the state of IRCd as best as possible.
|
|
|
|
|
|
|
|
namespace ircd::m::vm
|
|
|
|
{
|
2018-03-07 20:02:23 +01:00
|
|
|
extern hook::site eval_hook;
|
2018-02-28 07:53:44 +01:00
|
|
|
extern hook::site notify_hook;
|
2018-02-26 09:13:58 +01:00
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
void _tmp_effects(const m::event &event); //TODO: X
|
2017-11-16 02:37:09 +01:00
|
|
|
void write(eval &);
|
2018-03-07 20:02:23 +01:00
|
|
|
fault _eval_edu(eval &, const event &);
|
|
|
|
fault _eval_pdu(eval &, const event &);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
decltype(ircd::m::vm::eval_hook)
|
|
|
|
ircd::m::vm::eval_hook
|
|
|
|
{
|
|
|
|
{ "name", "vm.eval" }
|
|
|
|
};
|
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
decltype(ircd::m::vm::notify_hook)
|
|
|
|
ircd::m::vm::notify_hook
|
|
|
|
{
|
2018-03-16 21:27:25 +01:00
|
|
|
{ "name", "vm.notify" }
|
2018-02-28 07:53:44 +01:00
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-02-28 07:53:44 +01:00
|
|
|
ircd::m::vm::eval::eval(const vm::opts &opts)
|
2018-04-17 02:28:26 +02:00
|
|
|
:opts{&opts}
|
|
|
|
,txn{nullptr}
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-08 22:23:01 +01:00
|
|
|
ircd::m::vm::eval::eval(const event &event,
|
2018-02-28 07:53:44 +01:00
|
|
|
const vm::opts &opts)
|
2018-04-16 23:24:56 +02:00
|
|
|
:eval{opts}
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-02-08 22:23:01 +01:00
|
|
|
operator()(event);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 20:01:14 +01:00
|
|
|
enum ircd::m::vm::fault
|
|
|
|
ircd::m::vm::eval::operator()(const event &event)
|
2018-02-09 08:21:15 +01:00
|
|
|
try
|
2017-11-30 20:01:14 +01:00
|
|
|
{
|
2018-03-13 03:34:54 +01:00
|
|
|
assert(opts);
|
|
|
|
const event::conforms &report
|
2018-02-09 08:21:15 +01:00
|
|
|
{
|
2018-03-13 03:34:54 +01:00
|
|
|
opts->conforming && !opts->conformed?
|
|
|
|
event::conforms{event, opts->non_conform.report}:
|
|
|
|
opts->report
|
2018-02-09 08:21:15 +01:00
|
|
|
};
|
|
|
|
|
2018-03-13 03:34:54 +01: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)?
|
|
|
|
_eval_pdu(*this, event):
|
|
|
|
_eval_edu(*this, event)
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
event, opts, &report
|
|
|
|
};
|
|
|
|
|
2018-04-17 02:28:26 +02:00
|
|
|
if(opts->effects)
|
2018-03-13 03:07:41 +01:00
|
|
|
notify_hook(event);
|
2018-04-17 02:28:26 +02:00
|
|
|
|
|
|
|
if(opts->notify)
|
2018-03-27 09:52:02 +02:00
|
|
|
vm::accept(accepted);
|
2018-03-13 03:07:41 +01:00
|
|
|
|
|
|
|
if(opts->effects)
|
|
|
|
_tmp_effects(event);
|
|
|
|
|
|
|
|
if(opts->debuglog_accept)
|
|
|
|
log.debug("%s", pretty_oneline(event));
|
|
|
|
|
|
|
|
if(opts->infolog_accept)
|
|
|
|
log.info("%s", pretty_oneline(event));
|
|
|
|
|
|
|
|
return ret;
|
2018-03-07 20:02:23 +01:00
|
|
|
}
|
|
|
|
catch(const error &e)
|
|
|
|
{
|
|
|
|
if(opts->errorlog & e.code)
|
|
|
|
log.error("eval %s: %s %s",
|
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
|
|
|
e.what(),
|
|
|
|
e.content);
|
|
|
|
|
|
|
|
if(opts->warnlog & e.code)
|
|
|
|
log.warning("eval %s: %s %s",
|
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
|
|
|
e.what(),
|
|
|
|
e.content);
|
|
|
|
|
|
|
|
if(opts->nothrows & e.code)
|
|
|
|
return e.code;
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-04-13 08:06:02 +02:00
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
if(opts->errorlog & fault::INTERRUPT)
|
|
|
|
log.error("eval %s: #NMI: %s",
|
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
|
|
|
e.what());
|
|
|
|
|
|
|
|
if(opts->warnlog & fault::INTERRUPT)
|
|
|
|
log.warning("eval %s: #NMI: %s",
|
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
|
|
|
e.what());
|
|
|
|
throw;
|
|
|
|
}
|
2018-03-07 20:02:23 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
if(opts->errorlog & fault::GENERAL)
|
|
|
|
log.error("eval %s: #GP: %s",
|
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
|
|
|
e.what());
|
|
|
|
|
|
|
|
if(opts->warnlog & fault::GENERAL)
|
|
|
|
log.warning("eval %s: #GP: %s",
|
|
|
|
json::get<"event_id"_>(event)?: json::string{"<edu>"},
|
|
|
|
e.what());
|
|
|
|
|
|
|
|
if(opts->nothrows & fault::GENERAL)
|
|
|
|
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;
|
|
|
|
const unwind cleartxn{[&eval]
|
|
|
|
{
|
|
|
|
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-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-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}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-09 00:55:54 +01:00
|
|
|
int64_t top;
|
2018-04-06 06:57:17 +02:00
|
|
|
id::event::buf head;
|
2018-04-19 00:04:46 +02:00
|
|
|
std::tie(head, top, std::ignore) = m::top(std::nothrow, room_id);
|
2018-03-09 00:55:54 +01:00
|
|
|
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
|
|
|
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-04-17 02:28:26 +02:00
|
|
|
auto &txn(*eval.txn);
|
2018-03-13 03:07:41 +01:00
|
|
|
if(eval.opts->debuglog_accept)
|
|
|
|
log.debug("Committing %zu cells in %zu bytes to events database...",
|
2018-04-17 02:28:26 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-04-18 09:26:14 +02:00
|
|
|
const uint64_t &
|
2018-04-16 23:21:54 +02:00
|
|
|
ircd::m::vm::sequence(const eval &eval)
|
|
|
|
{
|
2018-04-18 09:26:14 +02:00
|
|
|
return eval.sequence;
|
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-01-26 21:26:09 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::vm::reflect(const enum fault &code)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-01-26 21:26:09 +01:00
|
|
|
switch(code)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-01-26 21:26:09 +01:00
|
|
|
case fault::ACCEPT: return "ACCEPT";
|
2018-02-28 07:53:44 +01:00
|
|
|
case fault::EXISTS: return "EXISTS";
|
|
|
|
case fault::INVALID: return "INVALID";
|
2018-01-26 21:26:09 +01:00
|
|
|
case fault::DEBUGSTEP: return "DEBUGSTEP";
|
|
|
|
case fault::BREAKPOINT: return "BREAKPOINT";
|
2018-02-28 07:53:44 +01:00
|
|
|
case fault::GENERAL: return "GENERAL";
|
2018-01-26 21:26:09 +01:00
|
|
|
case fault::EVENT: return "EVENT";
|
2018-02-28 07:53:44 +01:00
|
|
|
case fault::STATE: return "STATE";
|
2018-04-16 22:48:16 +02:00
|
|
|
case fault::INTERRUPT: return "INTERRUPT";
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
2018-01-26 21:26:09 +01:00
|
|
|
return "??????";
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
2018-02-28 07:53:44 +01:00
|
|
|
|
|
|
|
//TODO: X
|
|
|
|
void
|
|
|
|
ircd::m::vm::_tmp_effects(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &type{at<"type"_>(event)};
|
|
|
|
|
|
|
|
//TODO: X
|
2018-04-13 23:54:11 +02:00
|
|
|
if(type == "m.room.join_rules")
|
2018-02-28 07:53:44 +01:00
|
|
|
{
|
2018-03-13 03:07:41 +01:00
|
|
|
const m::room::id room_id{at<"room_id"_>(event)};
|
|
|
|
const m::user::id sender{at<"sender"_>(event)};
|
2018-04-13 23:54:11 +02:00
|
|
|
if(my_host(sender.host()))
|
|
|
|
send(room::id{"!public:zemos.net"}, sender, "ircd.room", room_id, {});
|
2018-02-28 07:53:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: X
|
2018-04-13 23:54:11 +02:00
|
|
|
if(type == "m.room.create")
|
2018-02-28 07:53:44 +01:00
|
|
|
{
|
2018-04-13 23:54:11 +02:00
|
|
|
const string_view local{m::room::id{at<"room_id"_>(event)}.localname()};
|
|
|
|
if(local != "users") //TODO: circ dep
|
|
|
|
send(my_room, at<"sender"_>(event), "ircd.room", at<"room_id"_>(event), json::object{});
|
2018-02-28 07:53:44 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-13 03:07:41 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// accepted
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::vm::accepted::accepted(const m::event &event,
|
|
|
|
const vm::opts *const &opts,
|
|
|
|
const event::conforms *const &report)
|
|
|
|
:m::event{event}
|
|
|
|
,context{ctx::current}
|
|
|
|
,opts{opts}
|
|
|
|
,report{report}
|
|
|
|
{
|
|
|
|
}
|