0

Code: https://github.com/t348575/blockchain-api-testing/tree/master

Blocks are added using genesis(). A thread watches the worker queue, and sends blocks to be computed and added to the chain. Order needs to be preserved, which is why i have a queuing system. Does the Queue() in AsyncWorker take care of this? This example works if my queuing system is removed. BlockChainWrapper holds all the functions, and inherits from ObjectWrap. When an item from the queue is to be executed, AsyncBlockChainWrapper is used, which implements PromiseWorker, which does some block chain work and returns a string to block_as_json_string, after this the promise needs to be resolved. Running BlockchChainAPI.js throws a V8 error, while test.js gives no output at all. What is going on? I am relativity new to node-addon-api. Any suggestions on how to proceed?

Store Napi::Env and the object for later use (blockchainWrapper.cpp)

Napi::Value BlockChainWrapper::genesis(const Napi::CallbackInfo& info) {
    Napi::Object input_obj = info[0].As<Napi::Object>();
    std::lock_guard<std::mutex> guard_ready_queue(ready_queue_mutex);
    this->ready_queue_data.push_back(input_obj);
    this->ready_queue_func.push_back(BlockChainWrapperTypes::_genesis_ready);
    this->ready_queue_env.push_back(info.Env());
    Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(info.Env());
    return deferred.Promise();
}

Vectors (blockchainWrapper.h):

std::vector<Napi::Env> ready_queue_env;
std::vector<Napi::Object> ready_queue_data;

Every 200 milliseconds, the work queue is scanned, and AsyncFunctions (the AsyncWorker class) is called.

t348575
  • 674
  • 8
  • 19
  • 1
    Please remember to read [how to ask a good question](/help/how-to-ask): if you have a problem with code, show that code in your post _even if_ you link out to it. And ideally, don't show your code: show your code _after_ you turned it into a [mcve] because that way (a) you put in the effort to figure out exactly what causes the problem (and you probably solve the problem entirely on your own before you even need to post to SO) and (b) it gives others concise code that they can actually comment on, instead of getting lost in your code. – Mike 'Pomax' Kamermans Mar 13 '20 at 22:30
  • @Mike'Pomax'Kamermans I have changed the repo to a minimal reproducible, and added code – t348575 Mar 14 '20 at 03:26
  • 1
    Again as per the guidelines, please put your MCVE in your post. By all means link out to the code elsewhere, too, but put all the code relevant to your question, in your question. – Mike 'Pomax' Kamermans Mar 14 '20 at 03:32

1 Answers1

0

While further stressing the point in the comments, to further reduce your example, so that you are able to just post the code in the question and just keep a link for reproducing, here is already a preliminary answer to your question:

First:

Order needs to be preserved, which is why i have a queuing system. Does the Queue() in AsyncWorker take care of this?

No, NAPI does not document any order guarantee, and I would be surprised if it would, since this "queue" is just in the mean of deferring the work, to been taken by one of the many worker threads.

And also your are not using NAPI right, you should read the documentation of AsyncWorker and the whole Lifetime shebang, for example you wrote in https://github.com/t348575/blockchain-api-testing/blob/master/cpp/asyncFunctions.h#L11

class AsyncFunctions : public PromiseWorker {
    public:
        AsyncFunctions(Napi::Promise::Deferred const &d, Napi::Object& resource) : PromiseWorker(d), resource(resource) {};
        virtual ~AsyncFunctions() {};
        void Execute() {
            std::this_thread::sleep_for(std::chrono::seconds(3));
            std::string input = resource.Get('data').As<Napi::String>();
            this->result = "result is: " + input;
        }

this std::string input = resource.Get('data').As<Napi::String> is strictly forbidden and leads to undefined behavior, since NAPI clearly documents that you must not call NAPI functions in the execute function.

Superlokkus
  • 4,731
  • 1
  • 25
  • 57