Testrpc
節點失去與 testrpc 的連接
按照本教程編寫投票合約並在 testrpc 上部署,我創建了以下 js 程式碼
voting.js
並通過以下方式執行它:> node > require('./voting.js');
在某些地方列印
web3.isConnected()
後,似乎在需要 solc 包後連接鬆動。知道為什麼嗎?怎麼解決?投票.js:
Web3 = require('web3'); web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); console.log(web3.eth.accounts); console.log(web3.isConnected()); fs = require('fs'); console.log(web3.isConnected()); code = fs.readFileSync('Voting.sol').toString(); console.log(web3.isConnected()); solc = require('solc'); console.log(web3.isConnected()); compiledCode = solc.compile(code); console.log(web3.isConnected()); abiDefinition = JSON.parse(compiledCode.contracts['Voting'].interface); byteCode = compiledCode.contracts['Voting'].bytecode; console.log(web3.isConnected()); VotingContract = web3.eth.contract(abiDefinition); console.log(web3.isConnected()); deployedContract = VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000}); console.log(deployedContract.address); contractInstance = VotingContract.at(deployedContract.address); contractInstance.totalVotesFor.call('Rama');
輸出:
> node > require('./voting.js'); [ '0xabefa5873fe5c980a2b7ffe84a6726386a1c6c3a', '0xe4bf2c9df073808c187066adac463f2b2a4a3847', '0x49d61cad7ad214f968a4d5b402e936aa3eef1ef5', '0xbb3ae1453c1c843f28952611c3948691376003bc', '0xb57b5c0f2ec7869c2371831c11a4d1f8c54086bb', '0xb14fad57e7ab17d378499e8aa0f2bbefa9f70344', '0xc911f8c0f1d2e2e4a08d0c5eaf3390d54cb6ce93', '0x6ee5769268c665f0c9541ecd0216041af111dced', '0x79a88a34d78151d9d79b997b51f14ea4211faacd', '0x8a287c6e4407ed53723cc9cde6741125efdbc8ba' ] true true true false false false false Error: CONNECTION ERROR: Couldn't connect to node http://localhost:8545. at Object.InvalidConnection (/home/bush/hello_world_voting/node_modules/web3/lib/web3/errors.js:31:16) at HttpProvider.send (/home/bush/hello_world_voting/node_modules/web3/lib/web3/httpprovider.js:83:22) at RequestManager.send (/home/bush/hello_world_voting/node_modules/web3/lib/web3/requestmanager.js:58:32) at Eth.get [as accounts] (/home/bush/hello_world_voting/node_modules/web3/lib/web3/property.js:107:62) at Object.<anonymous> (/home/bush/hello_world_voting/voting.js:24:93) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) >
我可以確認要求“solc”會導致與 TestRPC 的連接斷開。我沒有調查原因,但一種解決方法是通過在啟動控制台會話之前編譯契約並將其寫入文件來避免在目前節點控制台會話中要求“solc”。
compile.js
在命令行中執行:在您的節點控制台中~$ node compile.js
要求之前。voting.js
文件內容:
編譯.js
solc = require('solc'); fs = require('fs'); code = fs.readFileSync('Voting.sol').toString(); compiledCode = solc.compile(code); fs.writeFileSync("./Voting.compiled", JSON.stringify(compiledCode), 'utf-8');
投票.js
Web3 = require('web3'); web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); console.log(web3.eth.accounts); compiledCode = JSON.parse(fs.readFileSync("./Voting.compiled").toString()); abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface); VotingContract = web3.eth.contract(abiDefinition); byteCode = compiledCode.contracts[':Voting'].bytecode; deployedContract = VotingContract.new(['Rama', 'Nick', 'Jose'], { data: byteCode, from: web3.eth.accounts[0], gas: 4700000, }); setTimeout( function(){ console.log(deployedContract.address); contractInstance = VotingContract.at(deployedContract.address); console.log(contractInstance.address); console.log(contractInstance.totalVotesFor.call('Rama')); }, 3000);
注意:你必須等待
VotingContract.new
返回才能設置contractInstance
,否則它將是未定義的。因此,我將它包裝在setTimeout
等待三秒鐘的時間,在將契約送出到您的 TestRPC 伺服器後有足夠的時間VotingContract
返回。這通常不是處理非同步函式的最佳實踐,但以最少的重寫和附加包滿足了展示的要求。