{"id":22192030,"url":"https://github.com/cryptape/ethereum-bootstrap","last_synced_at":"2025-04-05T23:10:20.663Z","repository":{"id":66465074,"uuid":"51901406","full_name":"cryptape/ethereum-bootstrap","owner":"cryptape","description":"Starter kit to ethereum private chain.","archived":false,"fork":false,"pushed_at":"2019-02-21T09:51:01.000Z","size":652,"stargazers_count":381,"open_issues_count":0,"forks_count":138,"subscribers_count":65,"default_branch":"master","last_synced_at":"2025-03-29T22:08:08.328Z","etag":null,"topics":["ethereum"],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cryptape.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-17T06:50:37.000Z","updated_at":"2025-01-03T21:19:35.000Z","dependencies_parsed_at":"2023-04-30T01:18:39.933Z","dependency_job_id":null,"html_url":"https://github.com/cryptape/ethereum-bootstrap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptape%2Fethereum-bootstrap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptape%2Fethereum-bootstrap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptape%2Fethereum-bootstrap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptape%2Fethereum-bootstrap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cryptape","download_url":"https://codeload.github.com/cryptape/ethereum-bootstrap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411235,"owners_count":20934653,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ethereum"],"created_at":"2024-12-02T12:18:47.028Z","updated_at":"2025-04-05T23:10:20.643Z","avatar_url":"https://github.com/cryptape.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ethereum Bootstrap\n\n通过本文所述方法和项目中的脚本，我们可以快速的搭建好自己的私链进行开发测试。\n\n仓库中包含的工具有：\n\n* 一个测试账户导入脚本，在首次部署时将五个测试账户私钥导入以太坊节点。\n* 一个genesis.json配置文件，为对应的五个测试账户提供初始资金（以太币），方便开发测试。\n* 一个快速启动私有链节点并进入交互模式的脚本。\n* 一个合约样例：`contracts/Token.sol`。这是一个使用合约语言[Solidity](http://solidity.readthedocs.org/en/latest/)编写的智能合约。Token合约的功能是发行一种token（可以理解为货币，积分等等），只有合约的创建者有发行权，token的拥有者有使用权，并且可以自由转账。\n\n**测试账户私钥是放在Github上的公开数据，千万不要用于正式环境中或者公有链上。如果在测试环境之外的地方使用这些私钥，你的资金将会被窃取！**\n\n## 准备\n\n1. 在本地安装好[go-ethereum](https://github.com/ethereum/go-ethereum)和[solc](http://solidity.readthedocs.org/en/latest/), 可以执行`geth`和`solc`命令。如果操作系统是ubuntu, 安装官方的ethereum安装包即可。\n2. 将本仓库通过`git clone`命令下载到本地。\n3. 安装[expect](http://expect.sourceforge.net/)，工具脚本用它来自动化一些过程。例如在ubuntu上: `sudo apt-get install expect`\n\n## 启动geth\n\n1. 进入本仓库目录: `cd ethereum-bootstrap`\n2. 导入测试账户私钥: `./bin/import_keys.sh`\n3. 初始化blockchain: `./bin/private_blockchain_init.sh`\n   输出的结果类似如下, $(DIR))代表你的下载路径：\n   ```\n    INFO [09-25|11:07:26] Allocated cache and file handles         database=$(DIR)/ethereum-bootstrap/data/geth/chaindata cache=16 handles=16\n\tINFO [09-25|11:07:26] Successfully wrote genesis state         database=chaindata                                      hash=194258…f0efa3\n\tINFO [09-25|11:07:26] Allocated cache and file handles         database=$(DIR)/ethereum-bootstrap/data/geth/lightchaindata cache=16 handles=16\n\tINFO [09-25|11:07:26] Successfully wrote genesis state         database=lightchaindata                                      hash=194258…f0efa3\n   ```\n4. 为解决之后操作账户锁定问题，修改本目录下bin/private_blockchain.sh文件，在$geth后添加--unlock 0 --password value.\n   其中0表示解锁第0个账户，value为包含你第2步设置的密码的文件的地址路径，需要你建立。这样下面就不需要再解锁账户的操作。\n5. 启动私有链节点: `./bin/private_blockchain.sh`. 启动成功后可以看到类似如下输出:\n  ![private-started.png](screenshots/private-started.png)\n6. 此时以太坊交互式控制台已经启动，我们可以开始测试和开发了。\n\n注意：工具脚本假设你的geth安装在默认位置, 可以直接通过`geth`执行。如果`geth`命令安装在非标准的位置，可以设置`GETH`环境变量指定geth可执行文件的路径。例如:\n\n`GETH=/some/weird/dir/geth ./bin/import_keys.sh`\n\n## 通过挖矿来为account发放ether\n查看账号余额：\n```\n\u003e web3.eth.getBalance(web3.eth.accounts[0])\n2e+30\n```\n可以通过挖矿的方式给第一个账号发行ether：\n```\n\u003e miner.start(1)\nI0822 17:17:43.496826 miner/miner.go:119] Starting mining operation (CPU=1 TOT=3)\nI0822 17:17:43.497379 miner/worker.go:573] commit new work on block 30 with 0 txs \u0026 1 uncles. Took 527.407µs\n```\n需要调用miner.stop来停止挖矿：\n```\n\u003e miner.stop()\ntrue\n\u003e web3.eth.getBalance(web3.eth.accounts[0])\n2.000000000105e+30\n```\n使用账号前先解锁：\n```\n\u003e personal.unlockAccount(web3.eth.accounts[0])\n```\n\n## 使用以太坊控制台编译和部署智能合约\n\n在`contracts`目录下有一个智能合约样例文件`Token.sol`, 通过Solidity语言实现了基本的代币功能, 合约持有者可以发行代币, 使用者可以互相转账.\n\n我们可以使用以太坊控制台来编译部署这个合约．以太坊控制台是最基本的工具，使用会比较繁琐．社区也提供了其他更加方便的部署工具，此处不做讨论．\n\n第一步，我们先编译合约代码．新建一个ssh session, 切换到geth用户环境`su - geth`, 然后输入：``echo \"var tokenCompiled=`solc --optimize --combined-json abi,bin,interface contracts/Token.sol`\" \u003e token.js``.(此步为编译合约代码，并将生成的内容放入当前路径下的token.js文件)\n\n切换到以太坊控制台，把js文件加载进来:\n\n```javascript\nloadScript(\"token.js\")\n```\n\n通过`tokenCompiled.contracts[\"contracts/Token.sol:Token\"].bin`可以看到编译好的二进制代码，通过`tokenCompiled.contracts[\"contracts/Token.sol:Token\"].abi`可以看到合约的[ABI](https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI)．\n\n以变量的形式取出合约的二进制代码和ABI。\n```javascript\nvar tokenContractsBin = \"0x\" + tokenCompiled.contracts[\"contracts/Token.sol:Token\"].bin;\nvar tokenContractsAbi = tokenCompiled.contracts[\"contracts/Token.sol:Token\"].abi;\n```\n\n接下来我们要把编译好的合约部署到网络上去．\n\n首先我们用ABI来创建一个javascript环境中的合约对象：\n\n```javascript\nvar contract = web3.eth.contract(JSON.parse(tokenContractsAbi));\n```\n\n我们通过合约对象来部署合约：\n\n```javascript\nvar initializer = {from: web3.eth.accounts[0], data: tokenContractsBin, gas: 300000};\n\nvar token = contract.new(initializer);\n```\n\n`contract.new`方法的第一个参数设置了这个新合约的创建者地址`from`, 这个新合约的代码`data`, 和用于创建新合约的费用`gas`．`gas`是一个估计值，只要比所需要的gas多就可以，合约创建完成后剩下的gas会退还给合约创建者．\n\n`contract.new`方法的第二个参数设置了一个回调函数，可以告诉我们部署是否成功．\n\n`contract.new`执行时会提示输入钱包密码．执行成功后，我们的合约Token就已经广播到网络上了．此时只要等待矿工把我们的合约打包保存到以太坊区块链上，部署就完成了．\n\n在公有链上，矿工打包平均需要15秒，在私有链上，我们需要自己来做这件事情．首先开启挖矿：\n\n```javascript\nminer.start(1)\n```\n\n此时需要等待一段时间，以太坊节点会生成挖矿必须的数据，这些数据都会放到内存里面．在数据生成好之后，挖矿就会开始，稍后就能在控制台输出中看到类似：\n\n```\n:hammer:mined potential block\n```\n\n的信息，这说明挖到了一个块，合约已经部署到以太坊网络上了！此时我们可以把挖矿关闭：\n\n```javascript\nminer.stop()\n```\n\n接下来我们就可以调用合约了．先通过`token.address`获得合约部署到的地址, 以后新建合约对象时可以使用．这里我们直接使用原来的contract对象：\n\n```\n// 本地钱包的第一个地址所持有的token数量\n\u003e web3.eth.getBalance(web3.eth.accounts[0])\n0\n\n// 发行100个token给本地钱包的第一个地址\n\u003e token.issue.sendTransaction(web3.eth.accounts[0], 100, {from: web3.eth.accounts[0]});\nI1221 11:48:30.512296   11155 xeth.go:1055] Tx(0xc0712460a826bfea67d58a30f584e4bebdbb6138e7e6bc1dbd6880d2fce3a8ef) to: 0x37dc85ae239ec39556ae7cc35a129698152afe3c\n\"0xc0712460a826bfea67d58a30f584e4bebdbb6138e7e6bc1dbd6880d2fce3a8ef\"\n\n// 发行token是一个transaction, 因此需要挖矿使之生效\n\u003e miner.start(1)\n:hammer:mined potential block\n\u003e miner.stop()\n\n// 再次查询本地钱包第一个地址的token数量\n\u003e token.getBalance(web3.eth.accounts[0])\n100\n\n// 从第一个地址转30个token给本地钱包的第二个地址\n\u003e token.transfer.sendTransaction(web3.eth.accounts[1], 30, {from: web3.eth.accounts[0]})\nI1221 11:53:31.852541   11155 xeth.go:1055] Tx(0x1d209cef921dea5592d8604ac0da680348987b131235943e372f8df35fd43d1b) to: 0x37dc85ae239ec39556ae7cc35a129698152afe3c\n\"0x1d209cef921dea5592d8604ac0da680348987b131235943e372f8df35fd43d1b\"\n\u003e miner.start(1)\n:hammer:mined potential block\n\u003e miner.stop()\n\u003e token.getBalance(web3.eth.accounts[0])\n70\n\u003e token.getBalance(web3.eth.accounts[1])\n30\n```\n\n## 其他\n\n私有链的所有数据都会放在仓库根目录下的`data`目录中，删除这个目录可以清除所有数据，重新启动新环境。\n[solidity_compiler_helper](https://github.com/rakeshbs/solidity_compiler_helper)，可以使用这个小工具来部署，更方便。\n\n\n获取关于以太坊的更多信息请访问[EthFans](http://ethfans.org).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcryptape%2Fethereum-bootstrap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcryptape%2Fethereum-bootstrap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcryptape%2Fethereum-bootstrap/lists"}