区块链中文技术社区

EOS 合约中使用eosio::checksum256作为查询索引,查询不到问题

演示合约代码

合约中Table表结构

struct [[eosio::table, eosio::contract("bcskillsurou")]] extract_token {
    uint64_t id;
    eosio::checksum256 transaction_id;                   // 交易单id

    auto primary_key() const { return id; }
    eosio::checksum256 second_key() const { return transaction_id; } // 已hash作为二级索引

    EOSLIB_SERIALIZE(extract_token, (id)(transaction_id))
};
typedef eosio::multi_index<"xxxxtb"_n, extract_token, eosio::indexed_by<"bysubkey"_n, eosio::const_mem_fun<extract_token, eosio::checksum256, &extract_token::second_key>>> xxxx_table;

合约中查询代码

ACTION findtrxid(eosio::checksum256 trx_id){
    ....
    xxx_table xxxtb(_self, _self.value);
    auto sub_extract_index = xxxtb.get_index<"bysubkey"_n>();
    auto itr_xxx = sub_xxx_index.find(trx_id);

    eosio_assert(itr_extract != sub_xxx_index.end(), "Record not found");
    ....
}

使用Cleos 复现问题

cleos push action 合约账户 findtrxid '{"trx_id":0bb0e5901c3b627484c6a1a473ba8e3e3a3ffb4b96f849e08afb2d1e2671b88d}' -p 合约账户

返回

Error 3050003: eosio_assert_message assertion failure
Error Details:
assertion failure with message: {"code":102,"msg":"Record not found 563"}

经过测试发现是因为eosio::checksum256 trx_id传参最终打包不是已字符串打包导致,交易id上传后被识别成了

1116029241376226714100000000000000000000000000000000000000000000

解决方案

修改eosio::checksum256 trx_id为字符串即可

cleos push action 合约账户 findtrxid '{"trx_id":"0bb0e5901c3b627484c6a1a473ba8e3e3a3ffb4b96f849e08afb2d1e2671b88d"}' -p 合约账户

对于其他语言封装,处理eosio::checksum256 trx_id统一字符串打包即可

备注

特殊hash复现

参考

https://eosio.github.io/eosio.cdt/1.6.0/group__multiindex.html

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »