我自己可以挖礦了!使用Ethereum C++客戶端Aleth建一個私有網絡,並使用Remix部署一個智能合約



本文是按照這個教程執行的結果記錄:Creating a private network and deploying a contract with Remix

Ethereum Aleth在Wins上面的編譯和安裝請參考本人上一篇文章。https://blog.csdn.net/liangyihuai/article/details/103737223

本人只在這裏講注意事項,具體的步驟其參考上面第一個link。

  • genesis 區塊的配置文件config.json的賬號需要替換成自己的。同時把/**/和中間的文字刪除。
  • 執行下面命令的時候,需要替換其中的賬號爲自己的。
aleth/aleth --config /Users/johndoe/Documents/config.json -m on -a 008a78302c6fe24cc74008c7bdae27b7243a7066 --no-discovery --pin --unsafe-transactions
  • 在win下運行命令scripts/dopple.py是有問題的,解決的方法是,使用anaconda的命令行窗口,在窗口中進入下圖所在的目錄,執行python dopple.py。如下面三張圖片所示。
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
    經過運行dopply.py文件,我們把Dopple JSON-RPC代理跑起來了。使用瀏覽器驗證一下。
    在這裏插入圖片描述
    到這裏,我們已經把兩個服務分別在兩個cmd窗口中執行起來了,一個是aleth客戶端服務,另一個是json-rpc 代理服務。分別使用的是系統的cmd和anaconda的cmd窗口。
    在這裏插入圖片描述

下面便是使用remix編寫和部署一個智能合約。remix是在線的IDE,輸入下面的網址即可:http://remix.ethereum.org/, 這裏建議使用http,而不是https。不過,都可以試一下。下面給出一個簡單的智能合約代碼:

pragma solidity ^0.4.0;

contract Greeter{
    string public yourName;
    
    function Greeter() public {
        yourName = "World";
    }
    
    
    function set(string name) public{
        yourName = name;
    }
    
    function hello() constant public returns (string){
        return yourName;
    }
}

下圖顯示了三個注意點,1,需要選擇Web3Provider環境。2,選擇相應的智能合約。3.部署和操作智能合約。
在這裏插入圖片描述
在點擊部署按鈕之後,需要回到命令行窗口輸入密碼,手速快一點輸入就okay。
下面是本人智能合約的執行結果
在這裏插入圖片描述

謝謝

還可參考:https://github.com/ethereum/aleth/blob/master/doc/index.rst

Creating a private network and deploying a contract with Remix
This guide describes creating a private network with a single node and connecting it to Remix through an HTTP RPC interface.

Initializing Aleth using the command-line interface
1. Create a new Aleth account:

aleth/aleth account new
Enter and confirm a new password for this account when prompted.

2. Create a JSON file (e.g. config.json). Its content should look roughly like this:

{
  "sealEngine": "Ethash",
  "params": {
    "accountStartNonce": "0x00",
    "maximumExtraDataSize": "0x20",
    "homesteadForkBlock": "0x00",
    "daoHardforkBlock": "0x00",
    "EIP150ForkBlock": "0x00",
    "EIP158ForkBlock": "0x00",
    "byzantiumForkBlock": "0x00",
    "constantinopleForkBlock": "0x00",
    "constantinopleFixForkBlock": "0x00",
    "istanbulForkBlock": "0x00",
    "minGasLimit": "0x5208",
    "maxGasLimit": "0x7fffffffffffffff",
    "tieBreakingGas": false,
    "gasLimitBoundDivisor": "0x0400",
    "minimumDifficulty": "0x100000",
    "difficultyBoundDivisor": "0x0800",
    "durationLimit": "0x0d",
    "blockReward": "0x4563918244F40000",
    "networkID" : "0x42",
    "chainID": "0x42",
    "allowFutureBlocks" : false
  },
  "genesis": {
    "nonce": "0x0000000000000042",
    "difficulty": "0x100000",
    "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "author": "0x0000000000000000000000000000000000000000",
    "timestamp": "0x5A5B92D7",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x655741534d2074657374206e6574776f726b2030",
    "gasLimit": "0x989680"
  },
  "accounts": {
    "008a78302c6fe24cc74008c7bdae27b7243a7066" /* <= Enter your account address here */: {
        "balance" : "0x200000000000000000000000000000000000000000000000000000000000000"
    }
  }
}
This includes all forks activated at genesis and the initial balance for your account.

Note that this format can change occasionally. For the latest format, please consult the source files in https://github.com/ethereum/aleth/tree/master/libethashseal/genesis.

3. Run Aleth with config.json:

aleth/aleth --config /Users/johndoe/Documents/config.json -m on -a 008a78302c6fe24cc74008c7bdae27b7243a7066 --no-discovery --pin --unsafe-transactions
Argument Descriptions:

-m on enables CPU mining

-a 008a78302c6fe24cc74008c7bdae27b7243a7066 sets the beneficiary of mined blocks

--no-discovery --pin effectively disables networking; we have only a single node, we don't need to discover other ones, and we don't allow other nodes to connect to us

--unsafe-transactions disables additional prompt before sending each transaction, we don't need it in the testing environment

It can take a while to mine the first block, but block generation latency should decrease with time.

4. While Aleth is mining, open a new window in your CLI, navigate to the aleth directory, and run

scripts/dopple.py
This enables HTTP RPC at http://127.0.0.1:8545 by running a proxy which forwards all HTTP requests to Aleth's IPC interface.

Connecting Remix
This assumes that the contract code has already been successfully compiled.

1. On the Run tab, choose Web3 Provider in the Environment list.

2. After connecting to your node, it should fetch your account's address and automatically select it in the Account list.

3. Use the Deploy button to deploy a compiled contract into the blockchain. Aleth requests the confirmation and the account's password in its CLI - switch to the Aleth window and confirm the transaction when prompted (our tests currently show that Remix may not wait for the confirmation and consider the transaction failed. If this occurs, try redeploying the contract. Aleth will have cached your password and won't prompt for confirmation a second time.)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章