Go-Ethereum

將智能合約部署到乙太坊測試網

  • March 7, 2017

在嘗試將簡單的智能合約部署到乙太坊測試網時,我已遵循本指南https://ethereum.stackexchange.com/a/2787/2426(或等效的https://www.ethereum.org/greeter )。我在測試網上使用 geth CLI。一切正常,除了最後一步,在區塊鏈上上傳程式碼。這就是我所做的:

GETH SECOND INSTANCE:

geth --testnet --unlock 0 attach ipc:.ethereum/testnet/geth.ipc
   instance: Geth/v1.4.10-stable-5f55d95a/linux/go1.6.2
   coinbase: 0x47978a69f410d0f61850c92acdb0d4c464d70937
   at block: 1697809 (Mon, 26 Sep 2016 07:10:20 UTC)
    datadir: .ethereum/testnet
    modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> web3.fromWei(eth.getBalance(eth.accounts[0]).toNumber(), "ether")
"5.024681313417504"

編譯:

> var greeterSource = 'contract mortal { address owner; function mortal() { owner = msg.sender; } function kill() { if (msg.sender == owner) suicide(owner); } } contract greeter is mortal { string greeting; function greeter(string _greeting) public { greeting = _greeting; } function greet() constant returns (string) { return greeting; } }'

> var greeterCompiled = web3.eth.compile.solidity(greeterSource)

將合約程式碼插入區塊鏈:

> var _greeting = "Good Morning, Smart Contract!"

> var greeterContract = web3.eth.contract(greeterCompiled.greeter.info.abiDefinition);

> var greeter = greeterContract.new(_greeting, {from: eth.accounts[0], data: greeterCompiled.greeter.code, gas: 4000000}, function(e, contract) { if (!e) { if (!contract.address) { console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined..."); } else { console.log("Contract mined! Address: " + contract.address); console.log(contract); } } })

> debug.verbosity(4)

探索 greeter 時,輸出如下:

> greeter
   {
     abi: [{
         constant: false,
         inputs: [],
         name: "kill",
         outputs: [],
         payable: false,
         type: "function"
     }, {
         constant: true,
         inputs: [],
         name: "greet",
         outputs: [{...}],
         payable: false,
         type: "function"
     }, {
         inputs: [{...}],
         type: "constructor"
     }],
     address: undefined,
     transactionHash: null
   }

> greeter.greet();
   TypeError: 'greet' is not a function
       at <anonymous>:1:1

我等了一會兒,沒有收到這樣的消息:

Contract mined! address: 0x...

因此,合約似乎已正確編譯,但上傳到區塊鏈後我無法獲取或地址,也無法獲取 txHash。也許它沒有被上傳或探勘。我怎樣才能弄清楚?我錯過了什麼重要的事情嗎?

編輯:在嘗試部署日誌後立即報告了很多這樣的消息:

I0926 10:31:13.097817 core/state/state_object.go:160] 47978a69f410d0f61850c92acdb0d4c464d70937: #1048576 631783523629846744286 (+ 420000000000000)

0x47978a69f410d0f61850c92acdb0d4c464d70937 是geth節點的coinbase地址。

問題是您的命令geth --testnet --unlock 0 attach ipc:.ethereum/testnet/geth.ipc實際上並沒有解鎖帳戶。此命令只是將控制台附加到正在執行的geth實例。

如果您geth --testnet --unlock 0 console改用了,您將被要求解鎖您的帳戶,並顯示以下消息:

Unlocking account 0 | Attempt 1/3
Passphrase: 
I0926 21:48:25.702979 cmd/geth/accountcmd.go:189] Unlocked account 3b44...a400

如果要使用該geth ... attach命令,請在執行事務之前執行以下命令:

personal.unlockAccount(eth.accounts[0], "{your password}");

這將解鎖您的帳戶,然後啟用交易付款。

引用自:https://ethereum.stackexchange.com/questions/8851