Skip to content

Commit

Permalink
Get intended reuse from CommonContext (#6233)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyashton authored Jun 6, 2024
1 parent d26271f commit 43ae533
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 62 deletions.
21 changes: 19 additions & 2 deletions src/js/common_context.h → include/ccf/js/common_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace ccf::js
{
// This is intended to extend a js::core::Context with various CCF-specific
// extensions, expected to be accessible in every execution context (eg -
// ccf.bufToStr converters, ccf.crypto helpers, kv access). This is
// ccf.bufToStr converters, ccf.crypto helpers). This is
// implemented as a CRTP mixin so that you could build your own hierarchy.
template <typename Base>
class WithCommonExtensions : public Base
{
public:
WithCommonExtensions(TxAccess acc, kv::Tx* tx) : Base(acc)
WithCommonExtensions(TxAccess acc) : Base(acc)
{
// override Math.random
Base::add_extension(
Expand All @@ -39,11 +39,28 @@ namespace ccf::js
Base::add_extension(
std::make_shared<ccf::js::extensions::CryptoExtension>());

// add openenclave.*
Base::add_extension(
std::make_shared<ccf::js::extensions::OpenEnclaveExtension>());

// add snp_attestation.*
Base::add_extension(
std::make_shared<ccf::js::extensions::SnpAttestationExtension>());
}
};

template <typename Base>
class WithKVExtension : public Base
{
public:
WithKVExtension(TxAccess acc, kv::Tx* tx) : Base(acc)
{
// add ccf.kv.*
Base::add_extension(
std::make_shared<ccf::js::extensions::KvExtension>(tx));
}
};

using CommonContext = WithCommonExtensions<js::core::Context>;
using CommonContextWithLocalTx = WithKVExtension<CommonContext>;
}
29 changes: 4 additions & 25 deletions src/apps/js_generic/js_generic_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@
#include "ccf/crypto/rsa_key_pair.h"
#include "ccf/endpoints/authentication/all_of_auth.h"
#include "ccf/historical_queries_adapter.h"
#include "ccf/js/common_context.h"
#include "ccf/js/core/context.h"
#include "ccf/js/core/wrapped_property_enum.h"
#include "ccf/js/extensions/ccf/consensus.h"
#include "ccf/js/extensions/ccf/converters.h"
#include "ccf/js/extensions/ccf/crypto.h"
#include "ccf/js/extensions/ccf/historical.h"
#include "ccf/js/extensions/ccf/host.h"
#include "ccf/js/extensions/ccf/kv.h"
#include "ccf/js/extensions/ccf/request.h"
#include "ccf/js/extensions/ccf/rpc.h"
#include "ccf/js/extensions/console.h"
#include "ccf/js/extensions/math/random.h"
#include "ccf/js/extensions/openenclave.h"
#include "ccf/js/extensions/snp_attestation.h"
#include "ccf/js/interpreter_cache_interface.h"
#include "ccf/js/modules/chained_module_loader.h"
#include "ccf/js/modules/kv_bytecode_module_loader.h"
Expand Down Expand Up @@ -504,18 +499,7 @@ namespace ccfapp
// Install dependency-less (ie reusable) extensions on interpreters _at
// creation_, rather than on every run
js::extensions::Extensions extensions;
// override Math.random
extensions.emplace_back(
std::make_shared<ccf::js::extensions::MathRandomExtension>());
// add console.[debug|log|...]
extensions.emplace_back(
std::make_shared<ccf::js::extensions::ConsoleExtension>());
// add ccf.[strToBuf|bufToStr|...]
extensions.emplace_back(
std::make_shared<ccf::js::extensions::ConvertersExtension>());
// add ccf.crypto.*
extensions.emplace_back(
std::make_shared<ccf::js::extensions::CryptoExtension>());

// add ccf.consensus.*
extensions.emplace_back(
std::make_shared<ccf::js::extensions::ConsensusExtension>(this));
Expand All @@ -527,16 +511,11 @@ namespace ccfapp
extensions.emplace_back(
std::make_shared<ccf::js::extensions::HistoricalExtension>(
&context.get_historical_state()));
// add openenclave.*
extensions.emplace_back(
std::make_shared<ccf::js::extensions::OpenEnclaveExtension>());
// add snp_attestation.*
extensions.emplace_back(
std::make_shared<ccf::js::extensions::SnpAttestationExtension>());

interpreter_cache->set_interpreter_factory(
[extensions](ccf::js::TxAccess access) {
auto interpreter = std::make_shared<js::core::Context>(access);
// CommonContext also adds many extensions
auto interpreter = std::make_shared<js::CommonContext>(access);

for (auto extension : extensions)
{
Expand Down
29 changes: 4 additions & 25 deletions src/js/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,15 @@
#include "ccf/endpoint.h"
#include "ccf/endpoints/authentication/js.h"
#include "ccf/js/bundle.h"
#include "ccf/js/common_context.h"
#include "ccf/js/core/context.h"
#include "ccf/js/core/wrapped_property_enum.h"
#include "ccf/js/extensions/ccf/consensus.h"
#include "ccf/js/extensions/ccf/converters.h"
#include "ccf/js/extensions/ccf/crypto.h"
#include "ccf/js/extensions/ccf/historical.h"
#include "ccf/js/extensions/ccf/host.h"
#include "ccf/js/extensions/ccf/kv.h"
#include "ccf/js/extensions/ccf/request.h"
#include "ccf/js/extensions/ccf/rpc.h"
#include "ccf/js/extensions/console.h"
#include "ccf/js/extensions/math/random.h"
#include "ccf/js/extensions/openenclave.h"
#include "ccf/js/extensions/snp_attestation.h"
#include "ccf/js/interpreter_cache_interface.h"
#include "ccf/js/modules/chained_module_loader.h"
#include "ccf/js/modules/kv_bytecode_module_loader.h"
Expand Down Expand Up @@ -428,18 +423,7 @@ namespace ccf::js
// Install dependency-less (ie reusable) extensions on interpreters _at
// creation_, rather than on every run
ccf::js::extensions::Extensions extensions;
// override Math.random
extensions.emplace_back(
std::make_shared<ccf::js::extensions::MathRandomExtension>());
// add console.[debug|log|...]
extensions.emplace_back(
std::make_shared<ccf::js::extensions::ConsoleExtension>());
// add ccf.[strToBuf|bufToStr|...]
extensions.emplace_back(
std::make_shared<ccf::js::extensions::ConvertersExtension>());
// add ccf.crypto.*
extensions.emplace_back(
std::make_shared<ccf::js::extensions::CryptoExtension>());

// add ccf.consensus.*
extensions.emplace_back(
std::make_shared<ccf::js::extensions::ConsensusExtension>(this));
Expand All @@ -451,16 +435,11 @@ namespace ccf::js
extensions.emplace_back(
std::make_shared<ccf::js::extensions::HistoricalExtension>(
&context.get_historical_state()));
// add openenclave.*
extensions.emplace_back(
std::make_shared<ccf::js::extensions::OpenEnclaveExtension>());
// add snp_attestation.*
extensions.emplace_back(
std::make_shared<ccf::js::extensions::SnpAttestationExtension>());

interpreter_cache->set_interpreter_factory(
[extensions](ccf::js::TxAccess access) {
auto interpreter = std::make_shared<ccf::js::core::Context>(access);
// CommonContext also adds many extensions
auto interpreter = std::make_shared<ccf::js::CommonContext>(access);

for (auto extension : extensions)
{
Expand Down
10 changes: 5 additions & 5 deletions src/node/gov/handlers/proposals.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#pragma once

#include "ccf/base_endpoint_registry.h"
#include "ccf/js/common_context.h"
#include "ccf/js/extensions/ccf/gov_effects.h"
#include "js/common_context.h"
#include "js/extensions/ccf/network.h"
#include "js/extensions/ccf/node.h"
#include "node/gov/api_version.h"
Expand Down Expand Up @@ -151,7 +151,7 @@ namespace ccf::gov::endpoints
// Evaluate ballots
for (const auto& [mid, mb] : proposal_info.ballots)
{
js::CommonContext js_context(js::TxAccess::GOV_RO, &tx);
js::CommonContextWithLocalTx js_context(js::TxAccess::GOV_RO, &tx);

auto ballot_func = js_context.get_exported_function(
mb,
Expand Down Expand Up @@ -193,7 +193,7 @@ namespace ccf::gov::endpoints
// votes, there is no change to the proposal stored in the KV.
{
{
js::CommonContext js_context(js::TxAccess::GOV_RO, &tx);
js::CommonContextWithLocalTx js_context(js::TxAccess::GOV_RO, &tx);

auto resolve_func = js_context.get_exported_function(
constitution,
Expand Down Expand Up @@ -297,7 +297,7 @@ namespace ccf::gov::endpoints
"Unexpected: Could not access GovEffects subsytem");
}

js::CommonContext js_context(js::TxAccess::GOV_RW, &tx);
js::CommonContextWithLocalTx js_context(js::TxAccess::GOV_RW, &tx);

js_context.add_extension(
std::make_shared<ccf::js::extensions::NodeExtension>(
Expand Down Expand Up @@ -445,7 +445,7 @@ namespace ccf::gov::endpoints
return;
}

js::CommonContext context(js::TxAccess::GOV_RO, &ctx.tx);
js::CommonContextWithLocalTx context(js::TxAccess::GOV_RO, &ctx.tx);

auto validate_func = context.get_exported_function(
constitution.value(),
Expand Down
11 changes: 6 additions & 5 deletions src/node/rpc/member_frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#include "ccf/crypto/sha256.h"
#include "ccf/ds/nonstd.h"
#include "ccf/http_query.h"
#include "ccf/js/common_context.h"
#include "ccf/json_handler.h"
#include "ccf/node/quote.h"
#include "ccf/service/tables/gov.h"
#include "ccf/service/tables/jwt.h"
#include "ccf/service/tables/members.h"
#include "ccf/service/tables/nodes.h"
#include "frontend.h"
#include "js/common_context.h"
#include "js/extensions/ccf/network.h"
#include "js/extensions/ccf/node.h"
#include "node/gov/gov_endpoint_registry.h"
Expand Down Expand Up @@ -153,7 +153,7 @@ namespace ccf
std::optional<ccf::jsgov::VoteFailures> vote_failures = std::nullopt;
for (const auto& [mid, mb] : pi_->ballots)
{
js::CommonContext context(js::TxAccess::GOV_RO, &tx);
js::CommonContextWithLocalTx context(js::TxAccess::GOV_RO, &tx);

auto ballot_func = context.get_exported_function(
mb,
Expand Down Expand Up @@ -200,7 +200,7 @@ namespace ccf
}

{
js::CommonContext js_context(js::TxAccess::GOV_RO, &tx);
js::CommonContextWithLocalTx js_context(js::TxAccess::GOV_RO, &tx);

auto resolve_func = js_context.get_exported_function(
constitution,
Expand Down Expand Up @@ -307,7 +307,8 @@ namespace ccf
"Unexpected: Could not access GovEffects subsytem");
}

js::CommonContext apply_js_context(js::TxAccess::GOV_RW, &tx);
js::CommonContextWithLocalTx apply_js_context(
js::TxAccess::GOV_RW, &tx);

apply_js_context.add_extension(
std::make_shared<ccf::js::extensions::NodeExtension>(
Expand Down Expand Up @@ -1171,7 +1172,7 @@ namespace ccf

auto validate_script = constitution.value();

js::CommonContext context(js::TxAccess::GOV_RO, &ctx.tx);
js::CommonContextWithLocalTx context(js::TxAccess::GOV_RO, &ctx.tx);

auto validate_func = context.get_exported_function(
validate_script,
Expand Down

0 comments on commit 43ae533

Please sign in to comment.