以太坊节点相关知识

1.如何搭建以太坊节点

geth是什么

  1. go-ethereum就是通常所说的 geth ,它是一个用Go语言实现运行在以太坊完整节点上的命令行接口,安装并运行了geth,你可以成为以太坊正式链的节点并且可以:

  2. 挖矿得到真实的以太币

  3. 在账户地址之间转移资金
  4. 创建智能合约和发起交易
  5. 查看所有历史区块
  6. 还有很多很多功能
客户端种类

客户端 语言 开发者 最新版本 go-ethereum Go 以太坊基金会 go-ethereum-v1.4.9 Parity Rust Ethcore Parity-v1.2.1 cpp-ethereum C++ 以太坊基金会 cpp-ethereum-v1.2.9 pyethapp Python 以太坊基金会 pyethapp-v1.2.3 ethereumjs-lib Javascript 以太坊基金会 ethereumjs-lib-v3.0.0 Ethereum(J) Java ethereumJ-v1.3.0-RC3-daoRescue2 ruby-ethereum Ruby Jan Xie ruby-ethereum-v0.9.3 ethereumH Haskell BlockApps 尚无Homestead 版本

使用go-ethereum

1. git clone https://github.com/ethereum/go-ethereum
2. cd go-ethereum 
3. make geth
4. build/bin/geth
  • 注意:服务器需要centos7 并且安装好go (运维)

启动节点

   nohup go-ethereum/build/bin/geth --testnet --rpc --rpcaddr 0.0.0.0  --rpcapi eth,net,web3 --syncmode fast --cache 1028 --datadir /data/block/ > /data/block/geth.log 2>&1 &
  • testnet (Ropsten network: pre-configured proof-of-work test network)
  • rpc (Enable the HTTP-RPC server)
  • rpcaddr (HTTP-RPC server listening interface (default: "localhost"))
  • rpcapi (API's offered over the HTTP-RPC interface)
  • rpcport (HTTP-RPC server listening port (default: 8545))
  • port (Network listening port (default: 30303))
  • datadir (Data directory for the databases and keystore)
  • syncmode (Blockchain sync mode ("fast", "full", or "light"))
  • cache value (Megabytes of memory allocated to internal caching (min 16MB / database forced) (default: 128)) 优化性能

参考命令

2.web3.js

介绍

  • node web3模块: This is the Ethereum compatible JavaScript API which implements the Generic JSON RPC spec
  • 通过web3能够方便的与以太坊交互,开发者不用关心JSON RPC实现细节。
  • geth提供了以下的api模块: "admin":"1.0", "db":"1.0", "debug":"1.0", "eth":"1.0", "miner":"1.0", "net":"1.0", "personal":"1.0", "shh":"1.0", "txpool":"1.0", "web3":"1.0"

    1. By default Geth enables all APIs over the IPC (ipc) interface and only the db, eth, net and web3 APIs over the HTTP and WebSocket interfaces.
    2. 最重要的是eth这个模块

使用

  • 注意事项

    1. 默认用同步的方式,同时也支持异步。 As this API is designed to work with a local RPC node, all its functions use synchronous HTTP requests by default. If you want to make an asynchronous request, you can pass an optional callback as the last parameter to most functions.
    2. bignumber 依赖bignumber这个模块 wei You will always get a BigNumber object for number values as JavaScript is not able to handle big numbers correctly.
  • 充值提现中用到的api

    设置provider(某个以太坊节点提供的HTTP服务)

    ```
    var Web3 = require('web3');
    var web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/"))
    
    ```
    

    监听事件

    ```
    

    var rechargeContract = web3.eth.contract(contract_abi); var instance = rechargeContract.at(config.sc_address); var event = instance.Transfer({},{fromBlock: 2200000, toBlock: 'latest'}); event.watch(function(err, result){ })

    result: { "address":"0xb8ddf2301719f6aeaf7863aa97be2250e89a0034", "blockNumber"2403211, "transactionHash":"0x98ad87b4af6ba6ae422e2879a0aceca3b0a13e7b93ec603e65b8c5fd91724c35", "transactionIndex"2, "blockHash":"0xf0c632e9f616d1a308a17f543c0d3d008b8541c909b883790dbb0fcf782028c4", "logIndex":0, "removed":false, "event":"Transfer", "args": {"from":"0x0d1d4e623d10f9fba5db95830f7d3839406c6af2","to":"0x627306090abab3a6e1400e9345bc60c78a8bef57","orderId":"1000001389","value":"1000000000000000000","sucess":true} }

    
    #### 用私钥加密发起交易
    

    var Tx = require('ethereumjs-tx'); var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')

    var rawTx = {

    nonce: '0x00',    //
    gasPrice: '0x09184e72a000', 
    gasLimit: '0x2710',
    to: '0x0000000000000000000000000000000000000000', 
    value: '0x00', 
    data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
    

    }

    var tx = new Tx(rawTx); tx.sign(privateKey);

    var serializedTx = tx.serialize();

    //console.log(serializedTx.toString('hex')); //f889808609184e72a00082271094000000000000000000000000000000000000000080a47f74657374320000000000000000000000000000000000000000000000000000006000571ca08a8bbf888cfa37bbf0bb965423625641fc956967b81d12e23709cead01446075a01ce999b56a8a88504be365442ea61239198e23d1fce7d00fcfc5cd3b44b7215f

    web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {

    if (!err)
      console.log(hash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385"
    

    });

       - var nonce = web3.eth.getTransactionCount(from); //The number of transactions sent from the given address
      - var nonceHex = web3.toHex(nonce) ;  // 转16进制
      - var gasPrice = web3.eth.gasPrice;  //A BigNumber instance of the current gas price in wei
    
    #### 验证交易成功  
     - web3.eth.getTransaction
    

    {

        "hash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
        "nonce": 2,
        "blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
        "blockNumber": 3,
        "transactionIndex": 0,
        "from": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
        "to": "0x6295ee1b4f6dd65047762f924ecd367c17eabf8f",
        "value": BigNumber,
        "gas": 314159,
        "gasPrice": BigNumber,
        "input": "0x57cb2fc4"
    

    } ```

    • web3.eth.blockNumber //The number of the most recent block

账户安全

  1. 私钥:
  2. 私钥文件: 用密码加密私钥生成本地JSON文件 需要密码+文件

event && logs

  • Events, logs, and event logs are usually Ethereum terms that are interchangeable (in some contexts a particular term is favored, for example events in Solidity and web3.js, and logs as in the EVM and Yellow Paper).

  • event.watch 实际上调用了两个方法(JSON RPC api)

    1. eth_getFilterLogs 查看历史
    2. eth_getFilterChanges 轮询

参考文档

1. https://github.com/ethereum/go-ethereum/wiki/Management-APIs
2. https://github.com/ethereum/wiki/wiki/JSON-RPC
3. https://github.com/ethereum/wiki/wiki/JavaScript-API
2018-3-22 15:21:39 浏览(428)
Copyright ©leiwei | 京ICP备18013719号