您正在查看: EOS 分类下的文章

关于EOS出块BP备份的想法

假设使用一台机器作为BP节点机器。大家都知道eos的data目录超级大,备份的话只能全量备份。cp 的时间过于长,影响出块和RPC业务等提供。

暂时想到的方案如下

机器上跑2个nodeos进程,一个跑BP账号做出块节点,一个做同步几点。
保留三份data数据。出块节点初始化占一个,剩余两个,其中一个同步节点同步使用,同步节点定期切换剩余两个目录,保持2个目录数据与主网持平,当出块节点出问题导致data脏时,时切换到当时没有占用的data目录。然后删除脏目录,停止同步节点,并cp 同步节点前面用的data目录补齐第三目录。然后选一data重新运行同步节点。

且如果此机器提供RPC服务的话,可以做高可用。

优点

本方案可保证出块节点最大的运行时间。

缺点

data 三份的占用。

关于资源的抵押赎回,及查询抵押列表

关于抵押和赎回的方法,拿eosjs举例
https://eosio.github.io/eosjs/guides/2.-Transaction-Examples.html

Stake 抵押

const result = await api.transact({
  actions: [{
    account: 'eosio',
    name: 'delegatebw',
    authorization: [{
      actor: 'useraaaaaaaa',
      permission: 'active',
    }],
    data: {
      from: 'useraaaaaaaa',
      receiver: 'useraaaaaaaa',
      stake_net_quantity: '1.0000 SYS',
      stake_cpu_quantity: '1.0000 SYS',
      transfer: false,
    }
  }]
}, {
  blocksBehind: 3,
  expireSeconds: 30,
});

Unstake 赎回

const result = await api.transact({
  actions: [{
    account: 'eosio',
    name: 'undelegatebw',
    authorization: [{
      actor: 'useraaaaaaaa',
      permission: 'active',
    }],
    data: {
      from: 'useraaaaaaaa',
      receiver: 'useraaaaaaaa',
      unstake_net_quantity: '1.0000 SYS',
      unstake_cpu_quantity: '1.0000 SYS',
      transfer: false,
    }
  }]
}, {
  blocksBehind: 3,
  expireSeconds: 30,
});

操作细节看以上demo即可,下面我们说下抵押时transfer这个参数。
先看合约 (跳转代码)

if ( transfer ) {
    from = receiver;
}

如果设置transfer参数为true,及把来源的账户修改为接收账户。
也就变成了接收账户自己给自己抵押,由来源账户付钱

查看数据

查看代码
抵押数据表明为delband
(查看代码)

del_bandwidth_table     del_tbl( _self, from.value );

是以来源账户为查询的scope
所以RPC查询如下
post: https://api.eoslaomao.com/v1/chain/get_table_rows
data:

{
    "scope": "bcskillsurou",
    "code": "eosio",
    "table": "delband",
    "json": true
}

返回

{
    "rows": [
        {
            "from": "bcskillsurou",
            "to": "bcskillsurou",
            "net_weight": "0.0046 EOS",
            "cpu_weight": "0.0147 EOS"
        }
    ],
    "more": false
}

EOS 修改链代码 默认公钥前缀

https://github.com/EOSIO/fc/blob/df5a17ef0704d7dd96c444bfd9a70506bcfbc057/include/fc/crypto/public_key.hpp#L11

namespace fc { namespace crypto {
   namespace config {
      constexpr const char* public_key_legacy_prefix = "EOS"; // 修改EOS为需要的前缀即可,如BSC

EOS合约中使用nlohmann/json解析 float类型报"wabt out of bounds memory access"

由于合约中使用nlohmann/json解析json时,当遇到float类型时报"wabt out of bounds memory access"
不断调试代码,找到如下位置
https://github.com/nlohmann/json/blob/d2a08ddafd503b02b3418194c31e121e105411c6/include/nlohmann/detail/input/lexer.hpp#L1005
由于合约内不会考虑float类型,项目较急,先直接不解析此类型,下面位置添加return token_type::parse_error;

scan_number_decimal1:
    return token_type::parse_error; //在这加一个return
    // state: we just parsed a decimal point
    number_type = token_type::value_float;
    switch (get())
    {
        case '0':
        case '1':
        case '2':
        case '3':

并且由于合约内不允许使用try,修改下
https://github.com/nlohmann/json/blob/b52c3638f58291c2f2564aa31ad0a4a15cd01c84/include/nlohmann/detail/macro_scope.hpp#L68

// allow to disable exceptions
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
    // #define JSON_THROW(exception) throw exception 修改为如下
    #define JSON_THROW(exception) eosio_assert(false, exception.c_str());
    #define JSON_TRY try
    #define JSON_CATCH(exception) catch(exception)
    #define JSON_INTERNAL_CATCH(exception) catch(exception)
#else
    #include <cstdlib>
    // #define JSON_THROW(exception) std::abort() 修改为如下
    #define JSON_THROW(exception) eosio_assert(false, exception.c_str());
    #define JSON_TRY if(true)
    #define JSON_CATCH(exception) if(false)
    #define JSON_INTERNAL_CATCH(exception) if(false)
#endif

// override exception macros
#if defined(JSON_THROW_USER)
    #undef JSON_THROW
    #define JSON_THROW JSON_THROW_USER
#endif

EOS 签名 ECDSA sign 椭圆曲线数字签名

先零散的记一下,后面再整理

js ecc

https://github.com/EOSIO/eosjs-ecc/blob/7ec577cad54e17da6168fdfb11ec2b09d6f0e7f0/src/signature.js

sign

https://github.com/EOSIO/eosjs-ecc/blob/7ec577cad54e17da6168fdfb11ec2b09d6f0e7f0/src/signature.js#L177

Signature.sign = function(data, privateKey, encoding = 'utf8') {
    if(typeof data === 'string') {
        data = Buffer.from(data, encoding)
    }
    assert(Buffer.isBuffer(data), 'data is a required String or Buffer')
    data = hash.sha256(data)
    return Signature.signHash(data, privateKey)
}

https://github.com/EOSIO/eosjs-ecc/blob/7ec577cad54e17da6168fdfb11ec2b09d6f0e7f0/src/signature.js#L210

 ecsignature = ecdsa.sign(curve, dataSha256, privateKey.d, nonce++);

java

https://github.com/adyliu/jeos/blob/53dbd027cd59d367d9a197cbff5a58bdd9bf7195/src/main/java/io/jafka/jeos/impl/LocalApiImpl.java#L68

private String sign(String privateKey, SignArg arg, PackedTransaction t) {
    Raw raw = Packer.packPackedTransaction(arg.getChainId(), t);
    raw.pack(ByteBuffer.allocate(33).array());// TODO: what's this?
    String hash = KeyUtil.signHash(privateKey, raw.bytes());
    return hash;
}

https://github.com/adyliu/jeos/blob/4eaa9fb4555129f3d0baa56e7ffb70cac213a009/src/main/java/io/jafka/jeos/util/ecc/Ecdsa.java

https://github.com/starkbank/ecdsa-java/blob/master/src/main/java/com/starkbank/ellipticcurve/Ecdsa.java

参考

ECDSA — The art of cryptographic signatures
https://medium.com/coinmonks/ecdsa-the-art-of-cryptographic-signatures-d0bb254c8b96