Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

needs init() function to reduce boilerplate in other libraries #68

Open
coolaj86 opened this issue Apr 9, 2024 · 0 comments · Fixed by dashhive/DashRPC.js#6 · May be fixed by #72
Open

needs init() function to reduce boilerplate in other libraries #68

coolaj86 opened this issue Apr 9, 2024 · 0 comments · Fixed by dashhive/DashRPC.js#6 · May be fixed by #72

Comments

@coolaj86
Copy link
Contributor

coolaj86 commented Apr 9, 2024

Update This is in #72, and readily available in https://github.com/dashhive/DashRPC.js

This can't be used reliably without waiting for initialization. Therefore, it should include an init() function that provides that boilerplate once, rather than leaving it up to every downstream dev to implement it, possibly incorrectly / missing edge cases.

(in fact, there have already been bugs in dashcore-lib related to this problem)

Example Usage

    let rpc = new RpcClient(rpcConfig);
    await rpc.init({ retry: 5000 }).catch(function (err) {
        throw err;
    });

    // (if using the shim below)
    rpc.onconnected = rpcConfig.onconnected;

Example Shim

dashd-rpc-shim.js:

'use strict';

let RpcClient = require('@dashevo/dashd-rpc');

RpcClient.E_IN_WARMUP = -28;

RpcClient.prototype.init = async function (opts) {
        let rpc = this;
        rpc._connected = false;

        let retry = opts?.retry || 5000;

        let height = 0;
        for (;;) {
                height = await RpcClient._getHeight(rpc);
                if (height) {
                        break;
                }
                await sleep(retry);
        }

        return height;
};

function sleep(ms) {
    return new Promise(function (resolve) {
        setTimeout(resolve, ms);
    });
}

RpcClient._getHeight = async function (rpc) {
        let warn = null;
        let tip = await rpc
                .getChainTips()
                //.getBestBlockHash()
                .then(function (result) {
                        // { id, error, result }
                        if (result.error) {
                                // impossible, but we'll check anyway
                                throw new Error(result.error);
                        }

                        if (!result.result?.[0].height) {
                                // also impossible, and we still check anyway
                                throw new Error('Sanity Fail: missing tip');
                        }

                        return result.result[0].height;
                })
                .catch(function (e) {
                        if (e.code === RpcClient.E_IN_WARMUP) {
                                warn = e;
                                return 0;
                        }

                        throw e;
                });

        if (!rpc._connected) {
                rpc._connected = true;
                let onconnected = rpc.onconnected || RpcClient._onconnected;
                void onconnected.call(rpc, warn);
        }

        return tip;
};

RpcClient._onconnected = function () {
        let rpc = this;
        console.info(`[dashd-rpc] client connected to ${rpc.host}:${rpc.port}`);
};

Example Internal changes

function RpcClient(opts) {
  opts = opts || {};
  // ...
  this.onconnected = opts.onconnected;
  // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant