dApp和app的EOS API最佳实践

作者: ce6c3d7bcac1 | 来源:发表于2018-11-26 22:57 被阅读13次

    dApp和app的EOS API最佳实践

    原文链接:EOS API Best Practices for dApps and Apps

    作者:Ben Sigman(shEOS 的技术指导)

    译者:听风(已获得shEOS翻译授权)

    shEOS简介:shEOS 是由来自全球的女性企业家、投资人和工程师组成——这是一个由女性运营的 EOS 区块生产商——专注于为全球的 EOS.IO 区块链的成功启动和稳定发展提供支持。欢迎关注币乎主页:https://bihu.com/people/1120964


    It’s important for developers of wallets, scripts, apps, and dApps to understand how to properly setup their API functions to get info from the EOS chain. I have information below on how to setup failover / fallback servers.

    对于钱包、脚本、app和dApp的开发人员来说,了解如何正确设置其API函数以便从EOS链中获取信息是非常重要的。关于如何设置失效转移/备用服务器,我将分享以下内容。

    Background on EOS API

    EOS has a built-in API plugin for the main node software, nodeos. There are various filters that can be applied to the transactional history that gets filtered through this process. There are extremely high costs for running an API node that has a full transactional history. Currently, the RAM usage for full history on the nodeos plugin was 800GB on Nov 2, and over 1.2 TB today on Nov 13. Servers with this capacity of RAM cost over $30k per server, but with the rate of growth in the memory usage, running nodeos is not sustainable. Therefore, the plugin is being refactored by Block.One’s development team to better integrate with more scalable database platforms — such as postgresql.

    EOS API背景

    EOS有一个用于主节点软件的内置API插件——nodeos。通过此进程过滤的交易历史记录,有各种各样的过滤器可以应用于此。运行拥有完整的交易历史记录的API节点的成本极高。目前,nodeos插件中完整历史记录的RAM使用量在11月2日为800GB,而11月13日则达到了1.2 TB。拥有此RAM容量的服务器每台的成本超过3万美元,但随着内存使用量增长速度的提高 ,运行nodeos是不可持续的。 因此,Block.One的开发团队正在重构该插件,以便更好地与扩展性更好的数据库平台兼容 - 例如postgresql。

    Bad Practice

    There are currently too many examples of people hard-coding a single API (most commonly Greymass). This is NOT a good practice. There is no API node that will have 100% uptime. I have seen a lot of apps and scripts that are depending on Greymass and their node will go down from time to time. (It’s actually down this morning as I’m writing this.)

    糟糕的做法

    目前有许多人对单个API进行硬编码(最常见的是Greymass)。 这不是一个好的做法。没有任何API节点可以永远正常运行时间。我见过许多依赖于Greymass的应用程序和脚本,他们的节点会时不时出现故障。(今天早上我这篇文章时就宕机了。)

    EOS API Proxy

    The Blockmatrix team has created an excellent geo-DNS redundant proxy for EOS API nodes. The proxy will automatically route requests to the best servers that can handle those requests and he has served over 1.2 billion requests in the past 2 weeks.

    EOS API代理

    [Blockmatrix](http://blockmatrix.network/)团队为EOS API节点创建了一个优秀的“地域DNS冗余代理”(听风注:geo-redundant意为地域冗余)。代理将自动将请求路由到可以处理这些请求的最佳服务器,在过去两周内,该工具服务了超过12亿次请求。

    1_Jz3TSwJdLbRQGMjNwYgHNQ.jpeg

    Config Example: Javascript Function for EOS API Failover

    Here is an example of how we recommend setting up a failover/fallback for API calls in a js function. The primary API I recommend using is https://proxy.eosnode.tools – secondarily there should be at least one fallback API node (ideally another proxy).

    In this example, I have referenced the fallback proxy to shEOS high-availability bare metal servers, but you can use any nodes listed here on the EOS Nation report.

    Config示例:EOS API失效转移的Javascript函数

    以下的示例展示了我们如何建议在Javascript函数中为API调用设置失效转移/备用机制。 我首先推荐使用的API是https://proxy.eosnode.tools,其次应该至少有一个备用API节点(最好是有另一个代理)。

    在此示例中,我将备用代理引用到shEOS的高可用性裸机服务器,但您可以使用EOS Nation 报告列出的任一节点。

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n31" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Roboto, "Courier New", Consolas, Inconsolata, Courier, monospace; font-size: 0.9rem; margin: 0px 5px; line-height: 1.4em; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; width: inherit;">EosRequest("/v1/chain/get_info", "https://proxy.eosnode.tools", "https://api.proxy1a.sheos.org/");
    function EosRequest(path, url, fallback, retry = false) {
    let query = retry === false ? url : fallback;
    fetch(query + path)
    .then(function(response) {
    if (!response.ok) {
    if (retry === true) {
    throw Error(response.statusText);
    } else {
    return EosRequest(path, url, fallback, true);
    }
    }
    return response;
    }).then(function(response) {
    response.json().then(function(data) {
    console.log(data)
    });
    }).catch(function(error) {
    if (retry === true) {
    console.log(error);
    } else {
    return EosRequest(path, url, fallback, true);
    }
    });
    }</pre>

    Any questions or comments? You can reach me on Twitter — @bensig.

    如有任何疑问和意见,你可以随时在Twitter联系我Twitter — @bensig.!

    相关文章

      网友评论

      本文标题:dApp和app的EOS API最佳实践

      本文链接:https://www.haomeiwen.com/subject/kwziqqtx.html