BCSkill (Block chain skill )
区块链中文技术社区

只讨论区块链底层技术
遵守一切相关法律政策!

Ubuntu 安装 Mongodb 3+

方式一:从apt-get安装

  1. 添加mongodb签名到APT
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
  2. 创建/etc/apt/sources.list.d/mongodb-org-3.2.list文件并写入命令
    echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
  3. 更新软件源列表
    sudo apt-get update
  4. 安装mongodb(默认是安装稳定版)
    sudo apt-get install -y mongodb-org

    或者安装指定版本:

    sudo apt-get install -y mongodb-org=3.2.9 mongodb-org-server=3.2.9 mongodb-org-shell=3.2.9 mongodb-org-mongos=3.2.9 mongodb-org-tools=3.2.9

方式二:从 .tar.gz 二进制包安装

  1. 下载最新安装包
    官网下载地址:https://www.mongodb.com/download-center?jmp=nav#community.
    curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.2.9.tgz
  2. 解压
    tar -zxvf mongodb-linux-x86_64-ubuntu1404-3.2.9.tgz
  3. 重命名并移动到安装目录(可自定义)
    sudo mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb   # 将解压包拷贝到指定目录进行安装
  4. 创建 mongo 的数据目录
    MongoDB 默认的数据目录是 /data/db/ ,就直接使用默认的。
    sudo mkdir -p /data/db    #创建数据存储目录
    sudo chmod 755 /data/*    #赋予目录权限
  5. 启动
    mongod    #启动服务端
    mongo    #启动客户端

    配置MongoDB

    控制的配置文件在:/etc/mongodb.conf
    控制脚本在:/etc/init.d/mongodb
    MongoDB实例会把数据存放在:/var/lib/mongodb
    日志存放在:/var/log/mongodb
    默认由mongodb用户运行。
    如果如果显示目录不存在的话,需要运行下列命令指定文件夹:

    mongod --dbpath /var/lib/mongodb

    如果想要切换用户运行MongoDB的话,需要设置 /var/lib/mongodb 、 /var/log/mongodb两个目录的权限

    启动、停止、重启

    sudo service mongodb start
    sudo service mongodb stop
    sudo service mongodb restart

    通过mongodb shell测试安装结果

    mongodb

卸载MongoDB

  1. 停止MongoDB
    sudo service mongod stop
  2. 移除包文件
    sudo apt-get purge mongodb-org*
  3. 删除存放数据的目录
    sudo rm -r /var/log/mongodbsudo rm -r /var/lib/mongodb

    参考文档

    MongoDB官方文档:https://docs.mongodb.com/getting-started/shell/tutorial/install-mongodb-on-ubuntu/
    Ubuntu下MongoDB 安装教程及简单操作:http://www.linuxidc.com/Linux/2016-07/133254.htm

买卖内存的最小字节数 (token amount received from selling ram is too low)

当卖内存字节数按当时内存价格所值的EOS数,如果小于0.0001,则交易出错

root@iZj6cbx3duprxf6dasczbpZ:~# cleos system sellram dapp.exec 1
1483958ms thread-0   main.cpp:438                  create_action        ] result: {"binargs":"000040aa2b50ab490100000000000000"} arg: {"code":"eosio","action":"sellram","args":{"account":"dapp.exec","bytes":1}} 
Error 3050003: eosio_assert_message assertion failure
Error Details:
assertion failure with message: token amount received from selling ram is too low
pending console output:
void system_contract::sellram( account_name account, int64_t bytes ) {
      require_auth( account );
      eosio_assert( bytes > 0, "cannot sell negative byte" );

      user_resources_table  userres( _self, account );
      auto res_itr = userres.find( account );
      eosio_assert( res_itr != userres.end(), "no resource row" );
      eosio_assert( res_itr->ram_bytes >= bytes, "insufficient quota" );

      asset tokens_out;
      auto itr = _rammarket.find(S(4,RAMCORE));
      _rammarket.modify( itr, 0, [&]( auto& es ) {
          /// the cast to int64_t of bytes is safe because we certify bytes is <= quota which is limited by prior purchases
          tokens_out = es.convert( asset(bytes,S(0,RAM)), CORE_SYMBOL);
      });

      eosio_assert( tokens_out.amount > 1, "token amount received from selling ram is too low" );

当按EOS买内存时,如果所支出的EOS扣除0.5%手续费后的余额,按当时内存价格所购买的字节数小于1,则出错"must reserve a positive amount"

void system_contract::buyram( account_name payer, account_name receiver, asset quant )
   {
      require_auth( payer );
      eosio_assert( quant.amount > 0, "must purchase a positive amount" );

      auto fee = quant;
      fee.amount = ( fee.amount + 199 ) / 200; /// .5% fee (round up)
      // fee.amount cannot be 0 since that is only possible if quant.amount is 0 which is not allowed by the assert above.
      // If quant.amount == 1, then fee.amount == 1,
      // otherwise if quant.amount > 1, then 0 < fee.amount < quant.amount.
      auto quant_after_fee = quant;
      quant_after_fee.amount -= fee.amount;
      // quant_after_fee.amount should be > 0 if quant.amount > 1.
      // If quant.amount == 1, then quant_after_fee.amount == 0 and the next inline transfer will fail causing the buyram action to fail.

      INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {payer,N(active)},
         { payer, N(eosio.ram), quant_after_fee, std::string("buy ram") } );

      if( fee.amount > 0 ) {
         INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {payer,N(active)},
                                                       { payer, N(eosio.ramfee), fee, std::string("ram fee") } );
      }

      int64_t bytes_out;

      const auto& market = _rammarket.get(S(4,RAMCORE), "ram market does not exist");
      _rammarket.modify( market, 0, [&]( auto& es ) {
          bytes_out = es.convert( quant_after_fee,  S(0,RAM) ).amount;
      });

      eosio_assert( bytes_out > 0, "must reserve a positive amount" );

当按字节数买内存时,内部会先按当时内存价格计算出所需的EOS价格,如果计算出的EOS扣除0.5%手续费后的余额,按当时内存价格所购买的字节数小于1,也会出错"must reserve a positive amount"

EOS traces 多个相同的 txid数据

{
    "id": "fe804e9a9ab2a9f8b674bb490ac2c89d4f70247f82bc506adc7285cb82e3c991",
    "trx": {
        "receipt": {
            "status": "executed",
            "cpu_usage_us": 688,
            "net_usage_words": 16,
            "trx": [
                1,
                {
                    "signatures": [
                        "SIG_K1_KUypLM5YSdRRZMTuTDUAu4fr4V3FhabKJdGdNcZTaTj5zbsB1xfta3FvL5XEGkaduako2yVaHhQxLc5hm8TjpmGarXDRoM"
                    ],
                    "compression": "none",
                    "packed_context_free_data": "",
                    "packed_trx": "d27c555bc5a54ba71421000000000100a6823403ea3055000000572d3ccdcd0130c618630c05a77900000000a8ed32322130c618630c05a77990b1cadec45aab49204e00000000000004454f53000000000000"
                }
            ]
        },
        "trx": {
            "expiration": "2018-07-23T06:59:30",
            "ref_block_num": 42437,
            "ref_block_prefix": 555001675,
            "max_net_usage_words": 0,
            "max_cpu_usage_ms": 0,
            "delay_sec": 0,
            "context_free_actions": [],
            "actions": [
                {
                    "account": "eosio.token",
                    "name": "transfer",
                    "authorization": [
                        {
                            "actor": "bcskill3333333",
                            "permission": "active"
                        }
                    ],
                    "data": {
                        "from": "bcskill3333333",
                        "to": "dappplaytest",
                        "quantity": "2.0000 EOS",
                        "memo": ""
                    },
                    "hex_data": "30c618630c05a77990b1cadec45aab49204e00000000000004454f530000000000"
                }
            ],
            "transaction_extensions": [],
            "signatures": [
                "SIG_K1_KUypLM5YSdRRZMTuTDUAu4fr4V3FhabKJdGdNcZTaTj5zbsB1xfta3FvL5XEGkaduako2yVaHhQxLc5hm8TjpmGarXDRoM"
            ],
            "context_free_data": []
        }
    },
    "block_time": "2018-07-23T06:58:32.000",
    "block_num": 7317258,
    "last_irreversible_block": 8006080,
    "traces": [
        {
            "receipt": {
                "receiver": "eosio.token",
                "act_digest": "c6c9aa38acd0c3940de511b6ee69b1831f047f895b4aa94a6cde8ae80dfdd6a5",
                "global_sequence": 74737287,
                "recv_sequence": 2145761,
                "auth_sequence": [
                    [
                        "bcskill3333333",
                        6
                    ]
                ],
                "code_sequence": 1,
                "abi_sequence": 1
            },
            "act": {
                "account": "eosio.token",
                "name": "transfer",
                "authorization": [
                    {
                        "actor": "bcskill3333333",
                        "permission": "active"
                    }
                ],
                "data": {
                    "from": "bcskill3333333",
                    "to": "dappplaytest",
                    "quantity": "2.0000 EOS",
                    "memo": ""
                },
                "hex_data": "30c618630c05a77990b1cadec45aab49204e00000000000004454f530000000000"
            },
            "elapsed": 42,
            "cpu_usage": 0,
            "console": "",
            "total_cpu_usage": 0,
            "trx_id": "fe804e9a9ab2a9f8b674bb490ac2c89d4f70247f82bc506adc7285cb82e3c991",
            "inline_traces": [
                {
                    "receipt": {
                        "receiver": "bcskill3333333",
                        "act_digest": "c6c9aa38acd0c3940de511b6ee69b1831f047f895b4aa94a6cde8ae80dfdd6a5",
                        "global_sequence": 74737288,
                        "recv_sequence": 19,
                        "auth_sequence": [
                            [
                                "bcskill3333333",
                                7
                            ]
                        ],
                        "code_sequence": 1,
                        "abi_sequence": 1
                    },
                    "act": {
                        "account": "eosio.token",
                        "name": "transfer",
                        "authorization": [
                            {
                                "actor": "bcskill3333333",
                                "permission": "active"
                            }
                        ],
                        "data": {
                            "from": "bcskill3333333",
                            "to": "dappplaytest",
                            "quantity": "2.0000 EOS",
                            "memo": ""
                        },
                        "hex_data": "30c618630c05a77990b1cadec45aab49204e00000000000004454f530000000000"
                    },
                    "elapsed": 3,
                    "cpu_usage": 0,
                    "console": "",
                    "total_cpu_usage": 0,
                    "trx_id": "fe804e9a9ab2a9f8b674bb490ac2c89d4f70247f82bc506adc7285cb82e3c991",
                    "inline_traces": []
                },
                {
                    "receipt": {
                        "receiver": "dappplaytest",
                        "act_digest": "c6c9aa38acd0c3940de511b6ee69b1831f047f895b4aa94a6cde8ae80dfdd6a5",
                        "global_sequence": 74737289,
                        "recv_sequence": 1,
                        "auth_sequence": [
                            [
                                "bcskill3333333",
                                8
                            ]
                        ],
                        "code_sequence": 1,
                        "abi_sequence": 1
                    },
                    "act": {
                        "account": "eosio.token",
                        "name": "transfer",
                        "authorization": [
                            {
                                "actor": "bcskill3333333",
                                "permission": "active"
                            }
                        ],
                        "data": {
                            "from": "bcskill3333333",
                            "to": "dappplaytest",
                            "quantity": "2.0000 EOS",
                            "memo": ""
                        },
                        "hex_data": "30c618630c05a77990b1cadec45aab49204e00000000000004454f530000000000"
                    },
                    "elapsed": 5,
                    "cpu_usage": 0,
                    "console": "",
                    "total_cpu_usage": 0,
                    "trx_id": "fe804e9a9ab2a9f8b674bb490ac2c89d4f70247f82bc506adc7285cb82e3c991",
                    "inline_traces": []
                }
            ]
        },
        {
            "receipt": {
                "receiver": "bcskill3333333",
                "act_digest": "c6c9aa38acd0c3940de511b6ee69b1831f047f895b4aa94a6cde8ae80dfdd6a5",
                "global_sequence": 74737288,
                "recv_sequence": 19,
                "auth_sequence": [
                    [
                        "bcskill3333333",
                        7
                    ]
                ],
                "code_sequence": 1,
                "abi_sequence": 1
            },
            "act": {
                "account": "eosio.token",
                "name": "transfer",
                "authorization": [
                    {
                        "actor": "bcskill3333333",
                        "permission": "active"
                    }
                ],
                "data": {
                    "from": "bcskill3333333",
                    "to": "dappplaytest",
                    "quantity": "2.0000 EOS",
                    "memo": ""
                },
                "hex_data": "30c618630c05a77990b1cadec45aab49204e00000000000004454f530000000000"
            },
            "elapsed": 3,
            "cpu_usage": 0,
            "console": "",
            "total_cpu_usage": 0,
            "trx_id": "fe804e9a9ab2a9f8b674bb490ac2c89d4f70247f82bc506adc7285cb82e3c991",
            "inline_traces": []
        },
        {
            "receipt": {
                "receiver": "dappplaytest",
                "act_digest": "c6c9aa38acd0c3940de511b6ee69b1831f047f895b4aa94a6cde8ae80dfdd6a5",
                "global_sequence": 74737289,
                "recv_sequence": 1,
                "auth_sequence": [
                    [
                        "bcskill3333333",
                        8
                    ]
                ],
                "code_sequence": 1,
                "abi_sequence": 1
            },
            "act": {
                "account": "eosio.token",
                "name": "transfer",
                "authorization": [
                    {
                        "actor": "bcskill3333333",
                        "permission": "active"
                    }
                ],
                "data": {
                    "from": "bcskill3333333",
                    "to": "dappplaytest",
                    "quantity": "2.0000 EOS",
                    "memo": ""
                },
                "hex_data": "30c618630c05a77990b1cadec45aab49204e00000000000004454f530000000000"
            },
            "elapsed": 5,
            "cpu_usage": 0,
            "console": "",
            "total_cpu_usage": 0,
            "trx_id": "fe804e9a9ab2a9f8b674bb490ac2c89d4f70247f82bc506adc7285cb82e3c991",
            "inline_traces": []
        }
    ]
}

traces里面的你可以理解为是给参与这次交易的各个角色的发票清单 每一次EOS的转账都涉及到3个角色:转账方、接受方、执行转账操作的token发行方,如果是收款方查到的只是其中一个

EOS 新建账户合约,转账到执行账户,创建账户

查看主要代码

//signdappplay.cpp

#include "signdappplay.hpp"

void signdappplay::transfer(account_name from, account_name to, asset quantity, string memo)
{
    if (from == _self || to != _self) {
        return;
    }

    // don't do anything on transfers from our reference account
    if (from == N(signdappplay)) {
      return;
    }

    eosio_assert(quantity.symbol == CORE_SYMBOL, "only core token allowed"); //string_to_symbol(4, "EOS")
    eosio_assert(quantity.is_valid(), "Invalid token transfer");
    eosio_assert(quantity.amount > 0, "Quantity must be positive");

    //memo "your_account_name-your_owner_public_key-your_active_public_key" 分隔符支持“-” “:” 空格
    //去掉memo前面的空格
    memo.erase(memo.begin(), find_if(memo.begin(), memo.end(), [](int ch) {
        return !isspace(ch);
    }));
    //去掉memo后面的空格
    memo.erase(find_if(memo.rbegin(), memo.rend(), [](int ch) {
        return !isspace(ch);
    }).base(), memo.end());

    eosio_assert(memo.length() == 120 || memo.length() == 66, "Malformed Memo (not right length)");
    const string account_string = memo.substr(0, 12);
    const account_name new_account_name = string_to_name(account_string.c_str());
    eosio_assert(memo[12] == ':' || memo[12] == '-' || memo[12] == ' ', "Malformed Memo [12] == : or - or space");

    const string owner_key_str = memo.substr(13, 53);
    string active_key_str;

    if(memo[66] == ':' || memo[66] == '-' || memo[66] == ' ') {
      // active key provided
      active_key_str = memo.substr(67, 53);
    } else {
      // active key is the same as owner
      active_key_str = owner_key_str;
    }

    const abieos::public_key owner_pubkey =
        abieos::string_to_public_key(owner_key_str);
    const abieos::public_key active_pubkey =
        abieos::string_to_public_key(active_key_str);

    array<char, 33> owner_pubkey_char;
    copy(owner_pubkey.data.begin(), owner_pubkey.data.end(),
         owner_pubkey_char.begin());

    array<char, 33> active_pubkey_char;
    copy(active_pubkey.data.begin(), active_pubkey.data.end(),
         active_pubkey_char.begin());

    key_weight owner_pubkey_weight = {
        .key = {0, owner_pubkey_char},
        .weight = 1
    };

    key_weight active_pubkey_weight = {
        .key = {0, owner_pubkey_char},
        .weight = 1
    };

    authority owner = authority{
        .threshold = 1,
        .keys = {owner_pubkey_weight},
        .accounts = {},
        .waits = {}
    };

    authority active = authority{
        .threshold = 1,
        .keys = {active_pubkey_weight},
        .accounts = {},
        .waits = {}
    };

    newaccount new_account = newaccount{
        .creator = _self,
        .name = new_account_name,
        .owner = owner,
        .active = active
    };

    asset stake_net(1000, CORE_SYMBOL);
    asset stake_cpu(1000, CORE_SYMBOL);
    asset buy_ram = quantity - stake_net - stake_cpu;
    eosio_assert(buy_ram.amount > 0, "Not enough balance to buy ram");

    // create account
    action(
            permission_level{ _self, N(active) },
            N(eosio),
            N(newaccount),
            new_account
    ).send();
    // buy ram
    action(
            permission_level{ _self, N(active)},
            N(eosio),
            N(buyram),
            make_tuple(_self, new_account_name, buy_ram)
    ).send();
    // delegate and transfer cpu and net
    action(
            permission_level{ _self, N(active)},
            N(eosio),
            N(delegatebw),
            make_tuple(_self, new_account_name, stake_net, stake_cpu, true)
    ).send();
}

开始部署

  • 下载代码
    git clone https://github.com/cppfuns/signdappplay.git
  • 编译代码
    //进入源码目录
    cd signdappplay/
    //生成abi文件
    eosiocpp -g signdappplay.abi signdappplay.cpp
    //生成wast文件
    eosiocpp -o signdappplay.wast signdappplay.cpp
  • 部署合约
    //提前创建好signdappplay 将合约部署到此账号
    cleos set contract signdappplay signdappplay/ -p signdappplay
  • signdappplay@active里添加signdappplay@eosio.code授权
    cleos set account permission signdappplay active '{"threshold": 1,"keys": [{"key": "EOS7nK2w6ZT8hKdrxr48xdt3CFj1MXpaDV6jagRHKayFhqJBX5GEf","weight": 1}],"accounts": [{"permission":{"actor":"signdappplay","permission":"eosio.code"},"weight":1}]}' owner -p signdappplay

开始转账创建账号

signdappplay账号转账,并按规则(新建账户名:owner公钥:active公钥)填写memo。(分隔符支持"-" ":"或者空格,如果owner公钥与active公钥一致,可以只填写一个,如实例)。

root@iZj6cbx3duprxf6dasczbpZ:~# cleos push action eosio.token transfer '["dapp.exec", "signdappplay","100.0000 EOS","dingtet12345:EOS8Hdw6vismBgoYPzfLhr2rtHrdsR3F8UYAL23LSc9wdV8eNhNH8"]' -p dapp.exec
executed transaction: e6d94f39de7e6508e06b469327eb63eb6100ce129deb152a484acc0399bae292  192 bytes  12152 us
#   eosio.token <= eosio.token::transfer        {"from":"dapp.exec","to":"signdappplay","quantity":"100.0000 EOS","memo":"dingtet12345:EOS8Hdw6vismB...
#     dapp.exec <= eosio.token::transfer        {"from":"dapp.exec","to":"signdappplay","quantity":"100.0000 EOS","memo":"dingtet12345:EOS8Hdw6vismB...
#  signdappplay <= eosio.token::transfer        {"from":"dapp.exec","to":"signdappplay","quantity":"100.0000 EOS","memo":"dingtet12345:EOS8Hdw6vismB...
#         eosio <= eosio::newaccount            {"creator":"signdappplay","name":"dingtet12345","owner":{"threshold":1,"keys":[{"key":"EOS8Hdw6vismB...
#         eosio <= eosio::buyram                {"payer":"signdappplay","receiver":"dingtet12345","quant":"99.8000 EOS"}
#   eosio.token <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ram","quantity":"99.3010 EOS","memo":"buy ram"}
#  signdappplay <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ram","quantity":"99.3010 EOS","memo":"buy ram"}
#     eosio.ram <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ram","quantity":"99.3010 EOS","memo":"buy ram"}
#   eosio.token <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ramfee","quantity":"0.4990 EOS","memo":"ram fee"}
#  signdappplay <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ramfee","quantity":"0.4990 EOS","memo":"ram fee"}
#  eosio.ramfee <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ramfee","quantity":"0.4990 EOS","memo":"ram fee"}
#         eosio <= eosio::delegatebw            {"from":"signdappplay","receiver":"dingtet12345","stake_net_quantity":"0.1000 EOS","stake_cpu_quanti...
#   eosio.token <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.stake","quantity":"0.2000 EOS","memo":"stake bandwidth"}
#  signdappplay <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.stake","quantity":"0.2000 EOS","memo":"stake bandwidth"}
#   eosio.stake <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.stake","quantity":"0.2000 EOS","memo":"stake bandwidth"}
warning: transaction executed locally, but may not be confirmed by the network yet

账户向signdappplay账号转一定量的EOS,合约将会自动创建新账户,为CPU和NET各抵押0.1EOS,剩余EOS全部为新账户购买RAM(0.5%手续费)。

参考:

Ubuntu 切换 PHP 版本

PHP5.6 -> 7.0

# 禁用 Apache 中的 PHP5

sudo a2dismod php5.6

# 启用 PHP7

sudo a2enmod php7

# 重启 Apache

sudo systemctl restart apache2.service



# 切换 CLI 

sudo update-alternatives --set php /usr/bin/php7.0