forked from MultiChain/multichain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better separation between V8 and non-V8 filter code.
Better separation between Windows and Linux filter code.
- Loading branch information
Showing
18 changed files
with
1,330 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) 2014-2018 Coin Sciences Ltd | ||
// MultiChain code distributed under the GPLv3 license, see COPYING file. | ||
|
||
#include "v8/callbacks.h" | ||
#include "v8/v8engine.h" | ||
#include "v8/v8utils.h" | ||
#include "v8/v8ubjson.h" | ||
#include "v8/v8blob.h" | ||
#include <cassert> | ||
|
||
namespace mc_v8 | ||
{ | ||
/** | ||
* Call an RPC function from a V8 JS callback. | ||
* | ||
* Marshal the arguments and the return value between V8 and json_spirit using intermediate JSON strings. | ||
* Optionally filter the result before returning it to JS. | ||
* | ||
* @param name The name of the RPC function. | ||
* @param args The V8 arguments/return value. | ||
*/ | ||
void CallRpcFunction(std::string name, const v8::FunctionCallbackInfo<v8::Value> &args) | ||
{ | ||
v8::Isolate *isolate = args.GetIsolate(); | ||
v8::Locker locker(isolate); | ||
v8::Isolate::Scope isolateScope(isolate); | ||
v8::HandleScope handleScope(isolate); | ||
v8::Local<v8::Context> context(isolate->GetCurrentContext()); | ||
v8::Context::Scope contextScope(context); | ||
|
||
IFilterCallback *filterCallback = static_cast<IFilterCallback *>(args.Data().As<v8::External>()->Value()); | ||
|
||
auto v8args = v8::Array::New(isolate, args.Length()); | ||
for (int i = 0; i < args.Length(); ++i) | ||
{ | ||
v8args->Set(static_cast<unsigned>(i), args[i]); | ||
} | ||
|
||
BlobPtr argsBlob = Blob::Instance("args"); | ||
BlobPtr resultBlob = Blob::Instance("result"); | ||
V82Ubj(isolate, v8args, argsBlob); | ||
unsigned char *result; | ||
size_t resultSize; | ||
|
||
filterCallback->UbjCallback(name.c_str(), argsBlob->Data(), &result, &resultSize); | ||
resultBlob->Set(result, resultSize); | ||
|
||
if (resultBlob->IsEmpty()) | ||
{ | ||
args.GetReturnValue().SetUndefined(); | ||
} | ||
else | ||
{ | ||
int err; | ||
args.GetReturnValue().Set(Ubj2V8(isolate, resultBlob, &err)); | ||
} | ||
|
||
if (result != nullptr) | ||
{ | ||
delete [] result; | ||
} | ||
} | ||
|
||
// clang-format off | ||
#define FILTER_FUNCTION(name) \ | ||
void filter_##name(const v8::FunctionCallbackInfo<v8::Value> &args) \ | ||
{ \ | ||
CallRpcFunction(#name, args); \ | ||
} | ||
|
||
FILTER_FUNCTION(getfiltertxid) | ||
FILTER_FUNCTION(getfiltertransaction) | ||
FILTER_FUNCTION(getfilterstreamitem) | ||
FILTER_FUNCTION(getfilterassetbalances) | ||
FILTER_FUNCTION(setfilterparam) | ||
FILTER_FUNCTION(getfiltertxinput) | ||
FILTER_FUNCTION(getlastblockinfo) | ||
FILTER_FUNCTION(getassetinfo) | ||
FILTER_FUNCTION(getstreaminfo) | ||
FILTER_FUNCTION(verifypermission) | ||
FILTER_FUNCTION(verifymessage) | ||
|
||
#define FILTER_LOOKUP(name) { #name, filter_##name } | ||
|
||
std::map<std::string, v8::FunctionCallback> callbackLookup{ | ||
FILTER_LOOKUP(getfiltertxid), | ||
FILTER_LOOKUP(getfiltertransaction), | ||
FILTER_LOOKUP(getfilterstreamitem), | ||
FILTER_LOOKUP(getfilterassetbalances), | ||
FILTER_LOOKUP(setfilterparam), | ||
FILTER_LOOKUP(getfiltertxinput), | ||
FILTER_LOOKUP(getlastblockinfo), | ||
FILTER_LOOKUP(getassetinfo), | ||
FILTER_LOOKUP(getstreaminfo), | ||
FILTER_LOOKUP(verifypermission), | ||
FILTER_LOOKUP(verifymessage) | ||
}; | ||
// clang-format on | ||
} // namespace mc_v8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "v8blob.h" | ||
#include <cstring> | ||
|
||
namespace mc_v8 | ||
{ | ||
BlobPtr Blob::Instance(std::string name) | ||
{ | ||
auto it = m_instances.find(name); | ||
if (it == m_instances.end()) | ||
{ | ||
it = m_instances.insert(std::make_pair(name, std::make_shared<Blob>())).first; | ||
} | ||
return it->second; | ||
} | ||
|
||
Blob::~Blob() | ||
{ | ||
if (m_buffer != nullptr) | ||
{ | ||
delete[] m_buffer; | ||
} | ||
} | ||
|
||
void Blob::Reset() | ||
{ | ||
m_size = 0; | ||
} | ||
|
||
void Blob::Set(void *data, size_t size) | ||
{ | ||
this->Reset(); | ||
this->Append(data, size); | ||
} | ||
|
||
void Blob::Append(void *data, size_t size) | ||
{ | ||
this->Resize(size); | ||
std::memcpy(m_buffer + m_size, data, size); | ||
m_size += size; | ||
} | ||
|
||
void Blob::Remove(std::string name) | ||
{ | ||
m_instances.equal_range(name); | ||
} | ||
|
||
void Blob::Resize(size_t add_size) | ||
{ | ||
if (m_size + add_size > m_allocated) | ||
{ | ||
size_t new_size = ((m_size + add_size - 1) / size_increment + 1) * size_increment; | ||
auto new_buffer = new unsigned char[new_size]; | ||
if (m_allocated > 0) | ||
{ | ||
std::memcpy(new_buffer, m_buffer, m_allocated); | ||
delete[] m_buffer; | ||
} | ||
m_buffer = new_buffer; | ||
m_allocated = new_size; | ||
} | ||
} | ||
|
||
std::map<std::string, BlobPtr> Blob::m_instances; | ||
} // namespace mc_v8 |
Oops, something went wrong.