记录下搭建以太坊私有链的步骤

猿哥的环境是 Linux,实际操作时,需要替换路径、钱包地址等。

1. 到  https://geth.ethereum.org/downloads/ 下载编译好的 geth

2. 创建 genesis.json,内容如下:

{
  "config": {
    "chainId": 2206,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0
  },
  "alloc": {},
  "coinbase": "0x0000000000000000000000000000000000000000",
  "difficulty": "0x20000",
  "extraData": "",
  "gasLimit": "0xffffffff",
  "nonce": "0x0000000000000042",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp": "0x00"
}

 

注意事项: 

 

  • 上述内容修改了https://github.com/ethereum/go-ethereum 中的的chainId和nonce, 不修改chainId的话,可能在交易时会出现错误“ Error: insufficient funds for gas * price + value ”。
  • 猿哥原先的 gasLimit 是  0x2fefd8,但使用 truffle 发布合约到私有链的时候出现了“Error encountered, bailing. Network state unknown. Review successful transactions manually. Error: exceeds block gas limit” 这样的错误。后来修改genesis.json 中的 gasLimit 为 “0xffffffff” ,删除区块文件并重新开始挖矿,之后可以正常发布合约到私有链了。

 

3. 生成创世区块:

/data/eth/geth --datadir "/data/eth/database"    init /data/eth/database/genesis.json

 

4. 创建账户,使用其它工具如 MyEtherWalletMetaMask 创建新帐号,获得钱包地址和私钥。

5. 启动节点

nohup /data/eth/geth --gcmode archive --networkid 2006 --mine --minerthreads=1 --etherbase=你的挖矿收益钱包地址 --rpcapi admin,eth,web3,personal --verbosity 0 --rpc --rpcport 8545  --nodiscover --datadir "/data/eth/database" &

上述命令中 --nodiscover 表示不链接其它节点;--gcmode archive 表示即时将内存中的数据写入到文件中,否则重启节点可能会导致区块高度归零而丢失数据。

 

6. 通过私钥导导入帐号到本地

/data/eth/geth --datadir "/data/eth/database" account import ./key.txt

 

key.txt 的内容只有一行,就是钱包的私钥。

7. 使用 console 模式启动命令行工具

/data/eth/geth  attach http://127.0.0.1:8545

之后就可以在命令行模式下使用命令了, 具体命令可以参考 https://github.com/ethereum/wiki/wiki/JavaScript-API

 

发布了9 篇原创文章 · 获赞 2 · 访问量 1万+
展开阅读全文

geth转出代币开发时遇到 exceeds block gas limit

07-11

geth转出代币开发时遇到 exceeds block gas limit 有大神帮帮忙吗?感激不尽! 补充:本地建一个测试网络没问题, 但是用正式网络就出现这个问题. 提示:**exceeds block gas limit** 代码如下: ``` <?php class EthCommon { protected $host, $port, $version; protected $id = 0; //public $base = 1000000000000000000;//1e18 wei 基本单位 public $base = 100000000; //0000000000;//1e18 wei 基本单位 /** * 构造函数 * Common constructor. * @param $host * @param string $port * @param string $version */ function __construct($host, $port = "80", $version = "2.0") { //$host = "http://eth.weihuwei.com/";//geth地址 $this->host = $host; $this->port = $port; $this->version = $version; } /** * 发送请求 * @author qiuphp2 * @since 2017-9-21 * @param $method * @param array $params * @return mixed */ function request($method, $params = array()) { $data = array(); $data['jsonrpc'] = $this->version; $data['id'] = $this->id + 1; $data['method'] = $method; $data['params'] = $params; // echo json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->host); curl_setopt($ch, CURLOPT_PORT, $this->port); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $ret = curl_exec($ch); //返回结果 if ($ret) { curl_close($ch); return json_decode($ret, true); } else { $error = curl_errno($ch); curl_close($ch); // throw new Exception("curl出错,错误码:$error"); } } function eth_sendTransactionraw($from='',$to='',$password='',$data='') { if($from=='' || $to=='' || $password=='' || $data==''){ // echo '传入参数缺失'; // return '传入参数缺失!'; return false; } $status = $this->personal_unlockAccount($from, $password);//解锁 if (!$status) { return false; // return '解锁失败!'; } $params = array( "from" => $from, "to" => $to, "data" => $data, ); $data = $this->request('eth_sendTransaction', [$params]); return $data; } /** * 解锁账号 此函数可能比较耗时 * @author qiuphp2 * @since 2017-9-15 */ function personal_unlockAccount($account, $password) { $params = array( $account, $password, 100, ); $data = $this->request(__FUNCTION__, $params); if (!empty($data['error'])) { return $data['error']['message'];//解锁失败 } else { return $data['result'];//成功返回true } } } /** * 10为转16位 * 2018-07-05 */ function NumToStr($num){ if(stripos($num,'e')===false){ return $num; } $num = trim(preg_replace('/[=\'"]/','',$num,1),'"');//出现科学计数法,还原成字符串 $result = ""; while ($num > 0){ $v = $num - floor($num / 10)*10; $num = floor($num / 10); $result = $v . $result; } return $result; } /** * 金额转合约币 * $amount 是10进制的金额 * 2018-07-05 */ function amountToHex($amount){ //$amount = $amount * 1000000000000000000; // 转成 wei $amount = $amount * 100000000; //0000000000; // 转成 wei $amount = NumToStr($amount); // 转成字符串,而不是用科学计数法 $amount = number_format($amount, 0, '', ''); // 防止转成科学计数法 $amount = base_convert($amount, 10, 16); // 把10进制转成16进制 return $amount; } $rpc_address = $_POST['rpc_address']; $rpc_port = $_POST['rpc_port']; $receive_address = $_POST['receive_address']; $contract_address = $_POST['contract_address']; $send_address = $_POST['send_address']; $password = $_POST['password']; $amount = $_POST['amount']; /* $EthClient = EthCommon($dj_address, $dj_port); $mum=$myzc['mum']; $mum=dechex($mum*100000000);//代币的位数8 $amounthex=sprintf("%064s",$mum); //转换为64位 $addr2=explode('0x', $myzc['username'])[1];//接受地址 $dataraw='0xa9059cbb000000000000000000000000'.$addr2.$amounthex;//拼接data $constadd='$heyue';//合约地址 $sendrs = $EthClient->eth_sendTransactionraw($dj_username,$constadd,'密码',$dataraw);//转出账户,合约地址,转出账户解锁密码,data值 */ $EthClient = new EthCommon($rpc_address, $rpc_port); //$mum = $amount; //$mum = dechex($mum * 100000000);//代币的位数8 //$amounthex = sprintf("%064s", $mum); //转换为64位 $amounthex = amountToHex($amount); $amounthex = substr('0000000000000000000000000000000000000000000000000000000000000000' . $amounthex, -64); $addr2 = $receive_address;//接受地址 $dataraw = '0xa9059cbb000000000000000000000000' . $addr2 . $amounthex;//拼接data $constadd = $contract_address;//合约地址 $sendrs = $EthClient->eth_sendTransactionraw($send_address, $constadd, $password, $dataraw);//转出账户,合约地址,转出账户解锁密码,data值 echo "<pre>"; print_r($sendrs); ?> ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览