您正在查看: Surou 发布的文章

HD钱包地址生成JavaScript库

支持的加密货币

BTC,ETH,BCH,SLP,BSV,DASH,LTC,DOGE,EOS,XRP,NAV ,STRAT

开源库地址:https://github.com/lpopo0856/cryptohdwallet

合约推送Action参数为time_point的序列化处理

将时间转换为微妙(uint64)推送,链上会自动转为time_point类型

使用history-tool 同步链上数据到PostgreSQL

由于官方的工具history-tool,对比mongo插件,history-tool效率更高,支持PostgreSQL/RocksDB。并且避免了因为意外写入导致链程序意外退出后,数据脏的麻烦问题,并且pg会修复微分叉的交易,所以今天试用下history-tool方案

编译 fill-pg

主要参考 https://eosio.github.io/history-tools/build-ubuntu-1804.html

安装环境依赖

安装Clang 8 和其他需要的工具

sudo apt update && sudo apt install -y wget gnupg

cd ~
sudo wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -

sudo vi /etc/apt/sources.list
## 文件尾部添加
deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic main
deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic main
deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-8 main
deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic-8 main

sudo apt update && sudo apt install -y \
    autoconf2.13        \
    build-essential     \
    bzip2               \
    cargo               \
    clang-8             \
    git                 \
    libgmp-dev          \
    libpq-dev           \
    lld-8               \
    lldb-8              \
    ninja-build         \
    nodejs              \
    npm                 \
    pkg-config          \
    postgresql-server-dev-all \
    python2.7-dev       \
    python3-dev         \
    rustc               \
    zlib1g-dev

sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-8 100
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-8 100

生成并安装Boost 1.70。调整-j10以匹配您的机器。如果您没有足够的RAM用于所使用的内核数量,则会发生不好的事情

cd ~
wget https://dl.bintray.com/boostorg/release/1.70.0/source/boost_1_70_0.tar.gz
tar xf boost_1_70_0.tar.gz
cd boost_1_70_0
./bootstrap.sh
sudo ./b2 toolset=clang -j10 install

生成并安装CMake 3.14.5。调整--parallel=并-j匹配您的机器。如果您没有足够的RAM用于所使用的内核数量,则会发生不好的事情:

cd ~
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
tar xf cmake-3.14.5.tar.gz
cd cmake-3.14.5
./bootstrap --parallel=10
make -j10
sudo make -j10 install

编译history-tools

cd ~
git clone --recursive https://github.com/EOSIO/history-tools.git
cd history-tools
mkdir build
cd build
cmake -GNinja -DCMAKE_CXX_COMPILER=clang++-8 -DCMAKE_C_COMPILER=clang-8 ..
git submodule update --init --recursive
bash -c "cd ../src && npm install node-fetch"
ninja

此时查看build目录则生成了fill-pg

配置使用

参考 https://eosio.github.io/history-tools/database-fillers.html
首次运行需要创建所需的表,执行参数--fpg-create
如果肺首次执行,需要清理,则添加参数--fpg-drop --fpg-create
--fill-connect-to 需要连接的state-history-plugin endpoint,默认值127.0.0.1:8080
我们先演示不加其他参数的运行实例,如果需要其他的参数,比如过滤,请查看文档--fill-trx

测试运行的命令行参数如下

export PGUSER=       // PostgreSQL用户名
export PGPASSWORD=   // PostgreSQL密码
export PGDATABASE=   // PostgreSQL数据库名
export PGHOST=       // PostgreSQL访问host
export PGPORT=       // PostgreSQL端口

./fill-pg --fill-connect-to 127.0.0.1:8080 --fpg-create

连接后创建的表如下

表名 介绍
account
account_metadata
action_trace
action_trace_auth_sequence
action_trace_authorization
action_trace_ram_delta
action_trace_v1
block_info
code
contract_index_double
contract_index_long_double
contract_index64
contract_index128
contract_index256
contract_row
contract_table
fill_status
generated_transaction
permission
permission_link
protocol_state
received_block
resource_limits
resource_limits_config
resource_limits_state
resource_usage
transaction_trace

测试

查看端口有没有正常监听

sudo lsof -i -P -n | grep LISTEN

如果想测试history节点ws端口可以使用

wget https://github.com/vi/websocat/releases/download/v1.6.0/websocat_arm-linux-static
mv websocat_amd64-linux-static websocat
./websocat ws://127.0.0.1:8080/

注意

目前history-tools方案还处于试验阶段,目前测试遇到问题
https://github.com/EOSIO/history-tools/issues/103
等待后面有时间再继续跟进

根据公钥生成账户名

/**
 * @module AccountName
 */
import * as bs58 from 'bs58';

import * as Long from 'long';

const { PublicKey } = require('./ecc');

/**
    Hashes a public key to a valid FIOIO account name.
    @arg {string} pubkey
    @return {string} valid FIOIO account name
*/
export function accountHash(pubkey : string) : string {
    if(!PublicKey.isValid(pubkey, 'EOS')) {
        throw new TypeError('invalid public key');
    }

    pubkey = pubkey.substring('EOS'.length, pubkey.length);

    const decoded58 = bs58.decode(pubkey);
    const long = shortenKey(decoded58);

    const output = stringFromUInt64T(long);
    return output;
}

function shortenKey(key : [number]) : any {
  var res = Long.fromValue (0, true);
  var temp = Long.fromValue (0, true);
  var toShift = 0;
  var i = 1;
  var len = 0;

  while (len <= 12) {
      //assert(i < 33, "Means the key has > 20 bytes with trailing zeroes...")
      temp = Long.fromValue(key[i], true).and(len == 12 ? 0x0f : 0x1f);
      if (temp == 0) {
          i+=1
          continue
      }
      if (len == 12){
        toShift = 0;
      }
      else{
        toShift = (5 * (12 - len) - 1);
      }
      temp = Long.fromValue(temp, true).shiftLeft(toShift);

      res = Long.fromValue(res, true).or(temp);
      len+=1
      i+=1
  }

  return res;
}

function stringFromUInt64T(temp : any) : string{
  var charmap = ".12345abcdefghijklmnopqrstuvwxyz".split('');

  var str = new Array(13);
  str[12] = charmap[Long.fromValue(temp, true).and(0x0f)];

  temp = Long.fromValue(temp, true).shiftRight(4);
  for (var i = 1; i <= 12; i++) {
      var c = charmap[Long.fromValue(temp, true).and(0x1f)];
      str[12 - i] = c;
      temp = Long.fromValue(temp, true).shiftRight(5);
  }
  var result = str.join('');
  if (result.length > 12) {
      result = result.substring(0, 12);
  }
  return result;
}

测试

describe('accountname', () => {
    it('matches', () => {
        const samplekey = "EOS7isxEua78KPVbGzKemH4nj2bWE52gqj8Hkac3tc7jKNvpfWzYS";
        const accountHash = AccountName.accountHash(samplekey);
        expect(accountHash).toEqual('p4hc54ppiofx');
    })
})

激活 ACTION_RETURN_VALUE

激活动作返回值协议

cleos push action eosio activate '["c3a6138c5061cf291310887c0b5c71fcaffeab90d5deb50d3b9e687cead45071"]'

查询是否已开启

curl -X POST http://localhost:8888/v1/producer/get_supported_protocol_features | jq -r
...
{                                                                                                                                      
    "feature_digest": "c3a6138c5061cf291310887c0b5c71fcaffeab90d5deb50d3b9e687cead45071",
    "subjective_restrictions": {
      "enabled": true,
      "preactivation_required": true,
      "earliest_allowed_activation_time": "1970-01-01T00:00:00.000"
    },
    "description_digest": "69b064c5178e2738e144ed6caa9349a3995370d78db29e494b3126ebd9111966",
    "dependencies": [],
    "protocol_feature_type": "builtin",
    "specification": [
      {
        "name": "builtin_feature_codename",
        "value": "ACTION_RETURN_VALUE"
      }
    ]
  }
...