Cover photo

以太坊交互

以太坊客户端库

私有化区块链运行:

  • 下载go-ethereum工程并编译生成geth文件,加入PATH目录

  • 创建geth运行目录(bin config data keys)

  • 在config目录下创建genesis.json文件: { "config": { "chainId": 12345, "homesteadBlock": 0, "eip150Block": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0, "constantinopleBlock": 0, "petersburgBlock": 0, "istanbulBlock": 0, "berlinBlock": 0, "londonBlock": 0 }, "alloc": { "0x2eb172812C4E511Aa28b0263E97284Ca01723B98": { "balance": "100000000000000000000" } }, "coinbase": "0x0000000000000000000000000000000000000000", "difficulty": "0x20000", "extraData": "", "gasLimit": "0x2fefd8", "nonce": "0x0000000000000042", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp": "0x00" }

  • 执行命令初始化创世区块: ./geth --datadir ../data/ init ../config/genesis.json

  • 创建coinbase账户 ./ethkey generate --lightkdf --json ../data/keystore/key.json

  • 执行命令启动后台程序: nohup ./geth --datadir ../data/ -http --http.api "eth,web3,miner,admin,personal,net" --http.corsdomain "*" --nodiscover --networkid 12345 --http.addr 127.0.0.1 --http.port 18130 --port 18131 --allow-insecure-unlock --mine --miner.threads 1 --miner.etherbase 0x2eb172812C4E511Aa28b0263E97284Ca01723B98 --miner.gasprice 1000000000 --txpool.pricelimit 1000000000 &

  • 如果程序被阻塞则执行命令:bg

  • 如果要做命令行的交互则执行命令: ./geth --datadir ../data/ attach

  • 挖矿的交互命令:

miner.stop() miner.start()

  • 查询的交互命令(需要callback的函数可以使用console.log函数)

eth.getTransaction("0x3f6e7368ab0148610bd8924f93f16789f589c370b77ba20843f9b1409a22054f") eth.getTransactionReceipt("0x3f6e7368ab0148610bd8924f93f16789f589c370b77ba20843f9b1409a22054f") eth.getPendingTransactions(console.log) txpool.contentFrom("0xee66651b6560e8edd8b09005efbdf4ad003f98b4")

  • 可添加metamask的network来实现钱包的交互