スパコンDGXでTruffleセットアップ
スマートコントラクトのデプロイから実行まで /Solidity Smart Contracts in Truffle
LIST OF LINKS------
Truffle Site: http://truffleframework.com/
List of Official Tutorials: http://truffleframework.com/tutorials/
Basic: https://qiita.com/toshikase/items/d313b7c28bf16b940b12
http://truffleframework.com/tutorials/debugging-a-smart-contract
Tutorial: SimpleStorage.sol
newest: http://truffleframework.com/tutorials/debugging-a-smart-contract
https://medium.com/@gus_tavo_guim/using-truffle-to-create-and-deploy-smart-contracts-95d65df626a2
Tutorial: MetaCoin.sol http://truffleframework.com/docs/getting_started/contracts#reading-amp-writing-data
Code: calculator https://ethereum.stackexchange.com/questions/15704/how-to-get-return-value-of-function-for-solidity-contract
Tutorial: PoExistance https://ethereum.stackexchange.com/questions/25131/deploying-smart-contract-on-testrpc-with-truffle
Tutorial: SimpleStorage.sol------
1: set files: sol, migration
2: in Terminal, following:
truffle compile
truffle migrate
truffle console
3: in console:
truffle(development)> SimpleStorage.deployed().then(function(instance){return instance.get.call();}).then(function(value){return value.toNumber()});
CHECK: result is 0.
truffle(development)> SimpleStorage.deployed().then(function(instance){return instance.set(4);});
undefined
truffle(development)> SimpleStorage.deployed().then(function(instance){return instance.get.call();}).then(function(value){return value.toNumber()});
4
Truffle migrateをしても "Error: -- has not been deployed"となる場合:
状況:
(1) truffleでのcompileとmigrateは成功
➜ truffle_minernode1 truffle migrate
Using network 'development'.
Network up to date.
(2) truffle consoleでは、ネットワークにdeployされていないと言われる
truffle(development)> SimpleStorage.deployed()
Error: SimpleStorage has not been deployed to detected network (network/artifact mismatch)
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:299260:17
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
truffle(development)> SimpleStorag
ReferenceError: SimpleStorag is not defined
at evalmachine.<anonymous>:1:1
...
解決策: buildファイルの中の全てのJSONファイルを削除
再度 compile & migrateすると成功。
Applescript/Terminal 複数タブからコマンドを打つ
複数タブからコマンドを打ちたいとき:
Applescriptを用いる。
参考:
Writing external scripts | Truffle Suite
結果:
tell application "Terminal"
-- Activate it.
activate
set targetWindow to 0
-- Get a window that's not busy.
-- If there are no open windows, open one.
if (count of windows) is greater than 0 then
repeat with currentWindow in windows
if currentWindow is not busy then
set targetWindow to currentWindow
end if
end repeat
else
set targetWindow to window 1
end if
-- List Commands
set firstCommand to "cd /Users/username/blockchain_projects/truffle_minernode1"
set secondCommand to "cd /Users/username/blockchain_projects/truffle_node1"
set truffleCommand to "truffle exec /Users/username/blockchain_projects/truffle_minernode1/txcommand.js"
-- Tab 1.
do script firstCommand in targetWindow
do script truffleCommand in targetWindow
-- Tab 2.
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
do script secondCommand in targetWindow
do script truffleCommand in targetWindow
-- Tab 3
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
do script secondCommand in targetWindow
do script truffleCommand in targetWindow
end tell
---------
truffle スクリプトのJS file:
module.exports = function(callback) {
web3.eth.sendTransaction({from:"0xcd2a3d9f938..26", to:"0x5199dd5c...85", value: 10})
}
applescript "Expected end of line but found identifier"
AppleScript: "Expected end of line but found identifier"
https://www.iterm2.com/documentation-scripting.html
MacのScript Editorでのエラー:
tell application "iTerm 2"
create window with default profile command "echo "Hello World""
end tell
解決:コマンドにエラーがあった場合に出るエラーのよう。
コード:
tell application "iTerm 2"
create window with default profile command "echo Hello World"
end tell
RSK: error: peers do not connect in a private net
2017-12-10-23:00:45.0077 WARN [net] Can't decrypt AuthInitiateMessage from /192.168.0.4:49892. Most likely the remote peer used wrong public key (NodeID) to encrypt message.
2017-12-10-23:00:53.0861 TRACE [sync] Node list obtained from discovery: empty
2017-12-10-23:00:56.0863 TRACE [sync] Node list obtained from discovery: c2e177b3
2017-12-10-23:00:56.0863 TRACE [sync] Peer c2e177b3: initiate connection
2017-12-10-23:00:56.0863 INFO [facade] Connecting to: 192.168.0.4:30307
2017-12-10-23:00:56.0867 INFO [net] Open outbound connection, channel: [id: 0x23cd0200]
2017-12-10-23:00:56.0873 ERROR [net] Handshake failed: 192.168.0.4/192.168.0.4:30307(null)
2017-12-10-23:00:56.0873 DEBUG [net] Peer c2e177b3: notifies about disconnect
2017-12-10-23:00:56.0874 DEBUG [net] Peer removed from active peers: c2e177b3 | 192.168.0.4/192.168.0.4:30307
RSK: multiple nodes setup in private network + send transactions from different nodes
Thanks to ajlopez · GitHub from RSKJ gitter, 2017/12/10.
-----------
2 ways: use genconf, a tool to set up automatically and to set up manually.
GENCONF: Automatic setup tool:
RskUtils/genconf at master · ajlopez/RskUtils · GitHub
Git clone & go to genconf directory to install node and type commands.
*... to get the config to work, users need to modify generated config files as follows;
1: put coinbase.secret OUTSIDE of a { }
2: set solc path
-----------
Manual guide:
[To set up multiple nodes in a privatenet & send transactions from different nodes]
Use same Jar file and different config file and truffle projects.
Before you go ahead: Check: that you can run 1 node in private net
STEPS:
0: Set genesis block of the private network
Use file rsk-dev.json as below:
*rsk-dev.json is in the published jar as a resource
1: Set config for a private network
a: blockchain.config.name = "regtest" for the first line of the config file
b: enable wallet
In this way, there is an special account cd2a3d9f938e13cd947ec05abc7fe734df8dd826 with initial funds (maybe is the first OR the second account in account list)
The account with funds is only available for "regtest", use case private network
That account is called the "cow" account.
2: copy your local config file, and ALTER the following key/values
a: rpc port (from 4444 to 4445 to 4446, etc)
b: peer listen.port (from 30305 to 30306 to 30307, etc)
c: peer.discovery enable SHOULD be false
d:set database different for each config (from {dir = "/Users/name/rskjdir/database"} to "{dir = "/Users/name/rskjdir/databases/node1"}" , etc)
e: you must have generated the node id and private key FOR EACH NODE. the private key should be put at peer privateKey
e(1): To check node information / nodeinfo
$ java -Drsk.conf.file=node1.conf -cp rskj-core-0.2.5-GINGER-all.jar co.rsk.GenNodeKeyId
{
"privateKey": "2492e05b0feeal...",
"publicKey": "04c2e1...",
"address": "0eusirg.....",
"nodeId": "c2e177b...."
}
from: https://github.com/rsksmart/rskj/wiki/How-to-initialize-RSK-node-configuration-file-settings
f: see the peer active section
*port = peer listen.port of the other nodes you wish to connect to
Set the variables so that the first node config points to the OTHER TWO nodes
The second node config points to the FIRST and the THIRD ones, etc....
e(1): To know your ip on Mac:
https://www.wikihow.com/Find-Your-IP-Address-on-a-Mac
3: check: ONE or more nodes should be miner
This is because if don't have a miner NO TRANSACTION WILL BE PROCESSED in the private net
To set a miner:
put true into miner client.enabled AND miner.server.enabled
* If you have MORE THAN ONE miner, use a different coinbase.secret for each one (the above config is valid for bamboo release)
------------------
<CHECK: set up multiple nodes in a private network, connected as peers>
1: start RSK network with edited conf files on different terminals
eg:
$ java -Drsk.conf.file=node1.conf -cp rskj-core-0.2.5-GINGER-all.jar co.rsk.Start
and
$ java -Drsk.conf.file=minernode1.conf -cp rskj-core-0.2.5-GINGER-all.jar co.rsk.Start
2: see log for something like this:
2017-12-10-21:50:18.0238 TRACE [sync] Peer c2e177b3: initiate connection
*c2e177b3 = first letters of nodeId of the added peer
---------------------
4: to send a transaction from a specific node of choice
You can use web3, truffle, or your own javascript script, pointint to http://localhost:4444 or http://localhost:4445 and so on
To use truffle, alter the truffle.js file. Change the port from 8545 to 4444, etc
As below:
*... port is the rpc port
*..."from" account is set to be the "cow" account, from step 1.
The address is : '0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826'
Only 1 "cow" account exists in 1 "regtest" network. So each truffle project's truffle.js should have the same cow account in config.
5: check that you have the acconts set up
a: start truffle console
truffle(development)> web3.eth.accounts
[ '0xf4fe95909b7f3bd0f96f93811c29e14e6f97aff5',
'0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826' ] <= "cow" account
truffle(development)> web3.eth.getBalance(web3.eth.accounts[0])
BigNumber { s: 1, e: 0, c: [ 0 ] }
truffle(development)> web3.eth.getBalance(web3.eth.accounts[1])
BigNumber { s: 1, e: 21, c: [ 20000000 ] }
Truffle compile: "Invalid input source specified." の解決法
問題:
➜ truffle-webpack git:(addmarketplacesolc) truffle compile --verbose
Compiling ./contracts/marketplace.sol...
Compiling ./contracts/networkmanager.sol...
Compiling networkmanager.sol...
Invalid input source specified.
Compiliation failed. See above.
解決策:
Worked: import "./networkmanager.sol";
Did NOT work: import "networkmanager.sol";
解決後:
➜ truffle-webpack git:(addmarketplacesolc) truffle compile -dev
Compiling ./contracts/marketplace.sol...
Compiling ./contracts/networkmanager.sol...
Writing artifacts to ./build/contracts
参考:
truffle compatibility? · Issue #79 · ConsenSys/singulardtv-contracts · GitHub
(過去のsolc versionでは、文法が真逆だった):