0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-10 14:08:56 +02:00
construct/ircd/m/vm.cc

450 lines
9.1 KiB
C++
Raw Normal View History

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
2017-11-30 20:44:23 +01:00
#include <ircd/m/m.h>
2017-11-16 02:37:09 +01:00
decltype(ircd::m::vm::log)
ircd::m::vm::log
{
"vm", 'v'
};
decltype(ircd::m::vm::inserted)
ircd::m::vm::inserted
{};
decltype(ircd::m::vm::current_sequence)
ircd::m::vm::current_sequence
{};
decltype(ircd::m::vm::default_opts)
ircd::m::vm::default_opts
{};
/// 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.
/// The caller is expected to have done their best to check if this event will
/// succeed once it hits the core because failures blow all this effort. The
/// caller's ircd::ctx will obviously yield for evaluation, which may involve
/// requests over the internet in the worst case. Nevertheless, the evaluation,
/// write and release sequence of the core commitment is designed to allow the
/// caller to service a usual HTTP request conveying success or error without
/// hanging their client too much.
2017-11-16 02:37:09 +01:00
///
ircd::m::event::id::buf
ircd::m::vm::commit(json::iov &event,
const json::iov &contents)
2017-11-16 02:37:09 +01:00
{
const auto &room_id
2017-11-16 02:37:09 +01:00
{
event.at("room_id")
2017-11-16 02:37:09 +01:00
};
// derp
2017-11-16 02:37:09 +01:00
const json::strung content
{
contents
2017-11-16 02:37:09 +01:00
};
const json::iov::set set[]
2017-11-16 02:37:09 +01:00
{
{ event, { "origin_server_ts", ircd::time<milliseconds>() }},
{ event, { "origin", my_host() }},
2017-11-16 02:37:09 +01:00
};
thread_local char preimage_buf[64_KiB];
const_buffer preimage
2017-11-16 02:37:09 +01:00
{
stringify(mutable_buffer{preimage_buf}, event)
2017-11-16 02:37:09 +01:00
};
sha256::buf hash
2017-11-16 02:37:09 +01:00
{
sha256{preimage}
2017-11-16 02:37:09 +01:00
};
event::id::buf eid_buf;
const json::iov::set _event_id
2017-11-16 02:37:09 +01:00
{
event, { "event_id", m::event_id(event, eid_buf, hash) }
2017-11-16 02:37:09 +01:00
};
char hashes_buf[128];
string_view hashes;
2017-11-16 02:37:09 +01:00
{
const json::iov::push _content
2017-11-16 02:37:09 +01:00
{
event, { "content", string_view{content} }
};
2017-11-16 02:37:09 +01:00
// derp
preimage = stringify(mutable_buffer{preimage_buf}, event);
hash = sha256{preimage};
2017-11-16 02:37:09 +01:00
// derp
thread_local char hashb64[hash.size() * 2];
hashes = stringify(mutable_buffer{hashes_buf}, json::members
{
{ "sha256", b64encode_unpadded(hashb64, hash) }
});
}
2017-11-16 02:37:09 +01:00
const json::iov::push _hashes
2017-11-16 02:37:09 +01:00
{
event, { "hashes", hashes }
2017-11-16 02:37:09 +01:00
};
// derp
ed25519::sig sig;
2017-11-16 02:37:09 +01:00
{
const json::iov::push _content
{
event, { "content", "{}" }
};
2017-11-16 02:37:09 +01:00
preimage = stringify(mutable_buffer{preimage_buf}, event);
sig = self::secret_key.sign(preimage);
assert(self::public_key.verify(preimage, sig));
}
2017-11-16 02:37:09 +01:00
char sigb64[size_t(size(sig) * 1.34) + 1];
const json::members sigs
2017-11-16 02:37:09 +01:00
{
{ my_host(), json::members
{
{ self::public_key_id, b64encode_unpadded(sigb64, sig) }
}}
2017-11-16 02:37:09 +01:00
};
const json::iov::push _final[]
2017-11-16 02:37:09 +01:00
{
{ event, { "signatures", sigs }},
{ event, { "content", content }},
2017-11-16 02:37:09 +01:00
};
return commit(event);
2017-11-16 02:37:09 +01:00
}
namespace ircd::m::vm
{
extern hook::site commit_hook;
}
decltype(ircd::m::vm::commit_hook)
ircd::m::vm::commit_hook
{
{ "name", "vm commit" }
};
2017-11-16 02:37:09 +01:00
/// Insert a new event originating from this server.
///
/// Figure 1:
/// in . <-- injection
/// ___:::::::__//
/// | ||||||| // <-- this function
2017-11-16 02:37:09 +01:00
/// | \\|// //|
/// | ||| // | | acceleration
/// | |||// | |
/// | |||/ | |
/// | ||| | V
2017-11-16 02:37:09 +01:00
/// | !!! |
/// | * | <----- nozzle
/// | ///|||\\\ |
/// |/|/|/|\|\|\| <---- propagation cone
/// _/|/|/|/|\|\|\|\_
2017-11-16 02:37:09 +01:00
/// out
///
ircd::m::event::id::buf
ircd::m::vm::commit(json::iov &iov)
{
const m::event event
2017-11-30 20:01:14 +01:00
{
iov
2017-11-30 20:01:14 +01:00
};
check_size(event);
2017-11-16 02:37:09 +01:00
log.debug("injecting event(mark: %ld) %s",
vm::current_sequence,
pretty_oneline(event));
//TODO: X
vm::opts opts;
opts.non_conform |= event::conforms::MISSING_PREV_STATE;
vm::eval eval{opts};
2017-11-16 02:37:09 +01:00
ircd::timer timer;
commit_hook(event);
eval(event);
2017-11-16 02:37:09 +01:00
log.debug("committed event %s (mark: %ld time: %ld$ms)",
at<"event_id"_>(event),
vm::current_sequence,
timer.at<milliseconds>().count());
return unquote(at<"event_id"_>(event));
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
{
extern hook::site notify_hook;
void _tmp_effects(const m::event &event); //TODO: X
2017-11-16 02:37:09 +01:00
void write(eval &);
}
decltype(ircd::m::vm::notify_hook)
ircd::m::vm::notify_hook
{
{ "name", "vm notify" }
};
2017-11-16 02:37:09 +01:00
ircd::m::vm::eval::eval(const vm::opts &opts)
2017-11-16 02:37:09 +01:00
:opts{&opts}
{
}
ircd::m::vm::eval::eval(const event &event,
const vm::opts &opts)
:opts{&opts}
2017-11-16 02:37:09 +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)
try
2017-11-30 20:01:14 +01:00
{
const event::conforms report
{
event, opts->non_conform.report
};
if(!report.clean())
throw error
{
fault::INVALID, "Non-conforming event: %s", string(report)
};
const m::event::id &event_id
2017-11-16 02:37:09 +01:00
{
at<"event_id"_>(event)
2017-11-16 02:37:09 +01:00
};
const m::room::id &room_id
2017-11-16 02:37:09 +01:00
{
at<"room_id"_>(event)
};
2017-11-16 02:37:09 +01:00
if(!opts->replays && exists(event_id)) //TODO: exclusivity
throw error
{
fault::EXISTS, "Event has already been evaluated."
};
const auto &depth
2017-11-16 02:37:09 +01:00
{
json::get<"depth"_>(event)
2017-11-16 02:37:09 +01:00
};
const auto &type
2017-11-16 02:37:09 +01:00
{
unquote(at<"type"_>(event))
2017-11-16 02:37:09 +01:00
};
const event::prev prev
2017-11-30 20:01:14 +01:00
{
event
2017-11-30 20:01:14 +01:00
};
const size_t prev_count
2017-11-30 20:01:14 +01:00
{
size(json::get<"prev_events"_>(prev))
2017-11-30 20:01:14 +01:00
};
//TODO: ex
if(opts->write && prev_count)
2017-11-16 02:37:09 +01:00
{
for(size_t i(0); i < prev_count; ++i)
{
const auto prev_id{prev.prev_event(i)};
if(opts->prev_check_exists && !dbs::exists(prev_id))
throw error
{
fault::EVENT, "Missing prev event %s", string_view{prev_id}
};
}
uint64_t top;
const id::event::buf head
{
m::head(room_id, top)
};
2017-11-16 02:37:09 +01:00
m::room room{room_id, head};
m::room::state state{room};
m::state::id_buffer new_root_buf;
m::dbs::write_opts wopts;
wopts.root_in = state.root_id;
wopts.root_out = new_root_buf;
wopts.present = true;
const auto new_root
{
dbs::write(txn, event, wopts)
};
}
else if(opts->write)
{
m::state::id_buffer new_root_buf;
m::dbs::write_opts wopts;
wopts.root_out = new_root_buf;
wopts.present = true;
const auto new_root
{
dbs::write(txn, event, wopts)
};
}
if(opts->write)
write(*this);
2017-11-16 02:37:09 +01:00
if(opts->notify)
{
notify_hook(event);
vm::inserted.notify(event);
}
2017-11-16 02:37:09 +01:00
if(opts->effects)
_tmp_effects(event);
2017-11-16 02:37:09 +01:00
if(opts->debuglog_accept)
log.debug("%s", pretty_oneline(event));
2017-11-16 02:37:09 +01:00
if(opts->infolog_accept)
log.info("%s", pretty_oneline(event));
2017-11-16 02:37:09 +01:00
return fault::ACCEPT;
2017-11-16 02:37:09 +01:00
}
catch(const error &e)
2017-11-16 02:37:09 +01:00
{
if(opts->errorlog & e.code)
log.error("eval %s: %s %s",
json::get<"event_id"_>(event),
e.what(),
e.content);
2017-11-16 02:37:09 +01:00
if(opts->warnlog & e.code)
log.warning("eval %s: %s %s",
json::get<"event_id"_>(event),
e.what(),
e.content);
2017-11-16 02:37:09 +01:00
if(opts->nothrows & e.code)
return e.code;
2017-11-16 02:37:09 +01:00
throw;
2017-11-16 02:37:09 +01:00
}
catch(const std::exception &e)
2017-11-16 02:37:09 +01:00
{
if(opts->errorlog & fault::GENERAL)
log.error("eval %s: #GP: %s",
json::get<"event_id"_>(event),
e.what());
2017-11-16 02:37:09 +01:00
if(opts->warnlog & fault::GENERAL)
log.warning("eval %s: #GP: %s",
json::get<"event_id"_>(event),
e.what());
2017-11-16 02:37:09 +01:00
if(opts->nothrows & fault::GENERAL)
return fault::GENERAL;
2017-11-16 02:37:09 +01:00
throw error
2017-11-16 02:37:09 +01:00
{
fault::GENERAL, "%s", e.what()
2017-11-16 02:37:09 +01:00
};
}
2017-11-30 20:01:14 +01:00
void
ircd::m::vm::write(eval &eval)
{
log.debug("Committing %zu cells in %zu bytes to events database...",
eval.txn.size(),
eval.txn.bytes());
2017-11-30 20:01:14 +01:00
eval.txn();
vm::current_sequence++;
eval.txn.clear();
2017-11-16 02:37:09 +01:00
}
ircd::string_view
ircd::m::vm::reflect(const enum fault &code)
2017-11-16 02:37:09 +01:00
{
switch(code)
2017-11-16 02:37:09 +01:00
{
case fault::ACCEPT: return "ACCEPT";
case fault::EXISTS: return "EXISTS";
case fault::INVALID: return "INVALID";
case fault::DEBUGSTEP: return "DEBUGSTEP";
case fault::BREAKPOINT: return "BREAKPOINT";
case fault::GENERAL: return "GENERAL";
case fault::EVENT: return "EVENT";
case fault::STATE: return "STATE";
2017-11-16 02:37:09 +01:00
}
return "??????";
2017-11-16 02:37:09 +01:00
}
//TODO: X
void
ircd::m::vm::_tmp_effects(const m::event &event)
{
const m::room::id room_id{at<"room_id"_>(event)};
const m::user::id sender{at<"sender"_>(event)};
const auto &type{at<"type"_>(event)};
//TODO: X
/*
if(type == "m.room.create" && my_host(user::id{at<"sender"_>(event)}.host()))
{
const m::room room{room::id{room_id}};
send(room, at<"sender"_>(event), "m.room.power_levels", {}, json::members
{
{ "users", json::members
{
{ at<"sender"_>(event), 100L }
}}
});
}
*/
//TODO: X
if(type == "m.room.member" && my_host(sender.host()))
{
m::user::room user_room{sender};
send(user_room, sender, "ircd.member", room_id, at<"content"_>(event));
}
//TODO: X
if(type == "m.room.join_rules" && my_host(sender.host()))
{
send(room::id{"!public:zemos.net"}, sender, "ircd.room", room_id, {});
}
}