kantaroso / dapps-app-sample

0 stars 0 forks source link

環境構築 #1

Closed kantaroso closed 2 years ago

kantaroso commented 2 years ago

やりたいこと

資料

●概要 https://www.softbank.jp/biz/blog/business/articles/201804/blockchain-basic/

●基本 https://book.ethereum-jp.net/ https://zenn.dev/hid3/articles/5533d50b1d3de8 https://github.com/ethereum/go-ethereum#operating-a-private-network

https://mo-gu-mo-gu.com/geth-private-network-metamask-integration/

●フレームワーク https://qiita.com/wataruxun/items/e5ba1f2c75e032921c2c

kantaroso commented 2 years ago

geth

初期設定

## vi /geth/private_net/genesis.json
{
  "config": {
    "chainId": 2525,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "berlinBlock": 0,
    "londonBlock": 0
  },
  "alloc": {},
  "coinbase": "0x0000000000000000000000000000000000000000",
  "difficulty": "0x20000",
  "extraData": "",
  "gasLimit": "0x2fefd8",
  "nonce": "0x0000000000000042",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp": "0x00"
}

初期化

geth --datadir /geth/private_net init /geth/private_net/genesis.json

geth起動

--http.addr 0.0.0.0他のコンテナやホストからRPCにアクセスする場合は、忘れないでください。デフォルトでgethは、ローカルインターフェイスにバインドし、RPCエンドポイントに外部からアクセスすることはできません。

https://hub.docker.com/r/ethereum/client-go

geth --networkid 2525 --nodiscover --datadir /geth/private_net \
--http  --http.addr 0.0.0.0 --http.port 8545  \
--http.api "eth,net,web3,personal" --miner.gaslimit "20000000" \
--http.vhosts "*" --allow-insecure-unlock console 2> /geth/private_net/error.log

コマンドメモ

# アカウント作成
> personal.newAccount("kantaro1")
"0x3a23c2452c0683458db4abfaefbe3c930cb17c37"
> personal.newAccount("kantaro2")
"0x942cb993ef7b40b723f08b688f414837aa4d1201"

# アカウント一覧
> eth.accounts
["0x3a23c2452c0683458db4abfaefbe3c930cb17c37", "0x942cb993ef7b40b723f08b688f414837aa4d1201"]

# プライマリアカウント確認(採掘報酬が入るアカウント)
> eth.coinbase
'0x3a23c2452c0683458db4abfaefbe3c930cb17c37'

# マイニング開始
> miner.start()
null

# マイニング確認
> eth.blockNumber
59

# いーさ確認
> eth.getBalance(eth.accounts[0])
118000000000000000000

# マイニング終了
> miner.stop()
null

metamaskとの連携

metamaskのjsonでのアカウントインポートは壊れてそう https://github.com/MetaMask/metamask-extension/issues/10113

以下の参考に秘密鍵作成のスクリプト作成 https://qiita.com/bc_yuuuuuki/items/f93f6c7e9530f08dfd92

tools/get_private_key.js
kantaroso commented 2 years ago

ganache

パッケージインストールして使うだけ https://trufflesuite.com/ganache/

kantaroso commented 2 years ago

truffle

以下参考

https://trufflesuite.com/docs/truffle/quickstart/

初期化

truffle init

サンプル作成

コントラクト作成

https://book.ethereum-jp.net/

当時よりversion上がってて少し修正が必要

https://www.servernote.net/article.cgi?id=solidity-solc-error-different-version

実行までの流れ

https://trufflesuite.com/docs/truffle/quickstart/

コンパイル

truffle compile

デプロイ

truffle migrate

動作確認

truffle console

コマンドメモ

# アカウント一覧
truffle(development)> web3.eth.getAccounts()
[
  '0xc316Ca53958e467D203151b4655a7BE7663064a9',
  '0x8Ad4292b4Feb90D35B8Fc03C9250B6e9377912D7',
  '0x788534E5797541292a872C358faDf7950BFF7343',
  '0x35895Ef8d02634986Dee1a117158a546ecC1cEE7',
  '0xEF18E9E7024aCdd62619d1FCC3b9f82eC23cf71A',
  '0x5c863774Cf79fC1d6591DDa276591a804DeC27D7',
  '0xace72803B81873248d1B6C8895dDbfc8878C1727',
  '0x26F2d86C9462DA81130BAA411C0F82e13Eae91A5',
  '0x8Ec48c397169000Efd4C53dCd03C76399608bBCB',
  '0x979cC107cC24bE57f3159BDeF5b781906E13f7c3'
]

# コントラクト実行
truffle(development)> let instance = await SingleNumRegister.deployed()
undefined
truffle(development)> instance.get()
BN { negative: 0, words: [ 0, <1 empty item> ], length: 1, red: null }
truffle(development)> instance.set(6)
{
  tx: '0xd133074e8113c31269cfebdd97b12f6c52ca2da353e53b644ed293e9c481730f',
  receipt: {
    transactionHash: '0xd133074e8113c31269cfebdd97b12f6c52ca2da353e53b644ed293e9c481730f',
    transactionIndex: 0,
    blockHash: '0x53c67b804123055cef0bb348c78f10a4cc1ace4caa19b75bd1e00789039ffab4',
    blockNumber: 5,
    from: '0xc316ca53958e467d203151b4655a7be7663064a9',
    to: '0xc6eff47778f0c134d73b2f864b2a011a672b08e0',
    gasUsed: 41602,
    cumulativeGasUsed: 41602,
    contractAddress: null,
    logs: [],
    status: true,
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    rawLogs: []
  },
  logs: []
}
truffle(development)> instance.get()
BN { negative: 0, words: [ 6, <1 empty item> ], length: 1, red: null }

# コンソール終了
truffle(development)> .exit
/var/app # 
kantaroso commented 2 years ago

gethへのデプロイメモ

gethにtruffleで接続できない

別のdockerコンテナな構成だったので下記で行けた

https://www.webcyou.com/?p=11107

レンタルサーバーを借りて、ブロックチェーンプライベートサーバーを建てて利用していると、クライアント(ウェブ)側で「Invalid host specified」のエラー。 --http.vhostsの設定が必要でした。 ちゃんと設定を行ったのが良いのですが、今回は「*」(アスタリスク)指定で全許可。

コントラクトがデプロイできない

https://www.edureka.co/community/35983/migrations-returned-authentication-needed-password-unlock

/var/app # truffle migrate
This version of µWS is not compatible with your Node.js build:

Error: node-loader:
Error: Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /usr/local/lib/node_modules/truffle/node_modules/ganache/dist/node/tiTj4M0Y.node)
Falling back to a NodeJS implementation; performance may be degraded.

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.

Starting migrations...
======================
> Network name:    'development'
> Network id:      2525
> Block gas limit: 3301861 (0x3261e5)

1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
 *** Deployment Failed ***

"Migrations" -- Returned error: authentication needed: password or unlock -- Reason given: Custom error (could not decode)..

Exiting: Review successful transactions manually by checking the transaction hashes above on Etherscan.

Error:  *** Deployment Failed ***

"Migrations" -- Returned error: authentication needed: password or unlock -- Reason given: Custom error (could not decode)..

スクリーンショット 2022-08-27 22 50 51

> eth.coinbase
"0x3a23c2452c0683458db4abfaefbe3c930cb17c37"
> personal.unlockAccount("0x3a23c2452c0683458db4abfaefbe3c930cb17c37")
Unlock account 0x3a23c2452c0683458db4abfaefbe3c930cb17c37
Passphrase: 
GoError: Error: account unlock with HTTP access is forbidden at web3.js:6365:9(45)
    at github.com/ethereum/go-ethereum/internal/jsre.MakeCallback.func1 (native)
    at <eval>:1:1(3)

https://qiita.com/katsuomi/items/8b6413156189c44b8516

オプション: --allow-insecure-unlock を付けましょう。 参照:https://github.com/ethereum/go-ethereum/issues/19507

> personal.unlockAccount("0x3a23c2452c0683458db4abfaefbe3c930cb17c37")
Unlock account 0x3a23c2452c0683458db4abfaefbe3c930cb17c37
Passphrase: 
true

処理がタイムアウト

1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0x403f63c0caacb8ba8f161d5def98c3100bebb73720583783f88a3a0455d1dd0b
   ✖ Blocks: 0            Seconds: 974
 *** Deployment Failed ***

"Migrations" -- Transaction was not mined within 750 seconds, please make sure your transaction was properly sent. Be aware that it might still be mined! -- Reason given: Custom error (could not decode)..

geth側でマイニングしておく必要あり

kantaroso commented 2 years ago

できた。 基本truffleで良さそう

truffle(development)> web3.eth.getAccounts()
[
  '0x3a23c2452C0683458Db4ABfAefbE3c930Cb17c37',
  '0x942Cb993ef7B40B723f08B688F414837aA4d1201'
]
truffle(development)> let instance = await SingleNumRegister.deployed()
undefined
truffle(development)> instance.get()
BN { negative: 0, words: [ 0, <1 empty item> ], length: 1, red: null }
truffle(development)> instance.set(19)
{
  tx: '0x327c386af0666eef22d5fbee0f1104cdf522f8aa07292b8a42f5c6804fcdea4a',
  receipt: {
    blockHash: '0x1e5f27a2e4197b1b588f2c279ade03214a8d28480725c4804d7741058579ca7c',
    blockNumber: 171,
    contractAddress: null,
    cumulativeGasUsed: 43702,
    effectiveGasPrice: 2500000007,
    from: '0x3a23c2452c0683458db4abfaefbe3c930cb17c37',
    gasUsed: 43702,
    logs: [],
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    status: true,
    to: '0xdf5d460dedc0f52d495478f10a6d75594bdcb383',
    transactionHash: '0x327c386af0666eef22d5fbee0f1104cdf522f8aa07292b8a42f5c6804fcdea4a',
    transactionIndex: 0,
    type: '0x2',
    rawLogs: []
  },
  logs: []
}
truffle(development)> instance.get()
BN { negative: 0, words: [ 19, <1 empty item> ], length: 1, red: null }