安装geth
使用apt源安装方式 (ubuntu 22)
smali1sudo apt-get install software-properties-common2sudo add-apt-repository -y ppa:ethereum/ethereum3sudo apt-get update4sudo apt-get install ethereum5//升级apt源6sudo apt-get upgrade7//查看geth是否安装成功和version8geth version
创建账号密码(联盟链)
因为需要将账号(地址)存放在指定目录所以不使用gethconsole的方式创建账号密码。
Bash1mkdir ddnNode2cd ddnNode3mkdir node14mkdir node25mkdir node3
创建完三个节点目录后使用geth --datadir <目录> account new geth --datadir ./node1/data account new geth --datadir ./node2/data account new geth --datadir ./node3/data account new
每次运行账号后会返回私钥和存储位置,妥善保存,其中0x后的是地址
创建创世区块文件
创建完账号密码后需要初始化区块链,使用puppeth来创建创世区块文件,自己编辑也是可以的,但是会出现很多奇奇怪怪的bug。
livecodeserver1//使用apt-get获取puppeth2sudo apt-get install puppeth3//启动puppeth4puppeth5//返回6+-----------------------------------------------------------+7| Welcome to puppeth, your Ethereum private network manager |8| |9| This tool lets you create a new Ethereum network down to |10| the genesis block, bootnodes, miners and ethstats servers |11| without the hassle that it would normally entail. |12| |13| Puppeth uses SSH to dial in to remote servers, and builds |14| its network components out of Docker containers using the |15| docker-compose toolset. |16+-----------------------------------------------------------+17Please specify a network name to administer (no spaces, hyphens or capital letters please) 1819//填写你的区块链链名20luxchain2122//返回23Sweet, you can set this via --network=luxchain next time!
输入完私链名称后, 出现选择菜单. 选择2: 配置新创世块
pgsql1What would you like to do? (default = stats)2 1. Show network stats3 2. Configure new genesis4 3. Track new remote server5 4. Deploy network components
出现子菜单, 选择私链的共识机制(Consensus Protocols).
- proof-of-work(PoW): 工作量证明, 通过算力证明来达成共识
- proof-of-authority(PoA): 权威证明, 通过预先设定的权威节点来负责达成共识
这里选择PoA的认证方式, 和目前以太坊主链保持一致
fsharp1Which consensus engine to use? (default = clique)2 1. Ethash - proof-of-work3 2. Clique - proof-of-authority
选择2 poa共识算法
选择PoA后, 会提示选择出块的间隔时间. 由于是用于内部测试的私链, 可以将出块时间设置较少, 这里配置5秒.
mipsasm1How many seconds should blocks take? (default = 15)2 35
设置那个账号作为权威来生成块, 这里输入account1账号作为权威账号
Shell1Which accounts are allowed to seal? (mandatory at least one)2> 0x5a92A5Da4F5AAa216A192eaF5ea28bf0940CcAC83> 0x
选择是否需要初始化就给指定账号ether, 这里可以输入之前建立账号的地址.直接输入地址,最好将创建的所有账号输入。
Shell1Which accounts should be pre-funded? (advisable at least one)2> 0x5a92A5Da4F5AAa216A192eaF5ea28bf0940CcAC83> 0x45443Ba7C01e9B40ae455d1eCb5FaDe1E8955a894> 0xD6Fb0c6c67F2a962e696DDD0eCd434fE2B240907
是否需要添加预编译账号时给预编译账号1wei的余额,推荐选择yes,不选yes部署时可能会有奇奇怪怪的bug。
Shell1Should the precompile-addresses (0x1 .. 0xff) be pre-funded with 1 wei? (advisable yes)2yes
输入私链ID, 直接输入回车,已默认随机数作为私链ID,这里就随便填个数字
Shell1Specify your chain/network ID if you want an explicit one (default = random)2>4396
创世块中可以输入个人个性化信息, 这里可以随便输入字符.
Shell1Anything fun to embed into the genesis block? (max 32 bytes)2>This is bayunche's private chain.
回到主菜单, 重新选择2
Shell1What would you like to do? (default = stats)2 1. Show network stats3 2. Manage existing genesis4 3. Track new remote server5 4. Deploy network components6>
选择导出创世块配置文件
Shell1 1. Modify existing fork rules2 2. Export genesis configuration3>2
在提示创世块配置文件位置时, 直接输入回车, 默认当前目录生成创世块配置文件
Shell1Which file to save the genesis into? (default = luxchain.json)2>
创世块配置文件生成后, 会回到主菜单, 直接按Ctrl+z退出puppeth.
初始化区块链
创建完创世块配置文件后,就需要初始化区块链了,创建多节点的要求就是基于同个创世区块
Shell1geth --datadir ./node1/data init ./luxitem.json2geth --datadir ./node2/data init ./luxitem.json3geth --datadir ./node3/data init ./luxitem.json
初始化区块链后就可以启动节点了
Bash1geth --datadir ./node1/data --port 2001 --http --http.port 8548 --allow-insecure-unlock --rpc.enabledeprecatedpersonal console2 3geth --datadir ./node2/data --port 2002 --http --http.port 8549 --allow-insecure-unlock --rpc.enabledeprecatedpersonal console4 5geth --datadir ./node3/data --port 2002 --http --http.port 8549 --allow-insecure-unlock --rpc.enabledeprecatedpersonal console
启动成功后,我们需要将node1 和node2、3进行链接,geth会扫描但是不会自动链接节点。(以太坊主链则通过在代码中预设了启动节点, 可以启动发现其他节点)
登录node1, 获取节点地址
(如果在启动时没打console启动geth console,则需要再开一个bash以连接到console) 链接方式:
Bash1geth attach ipc:node1/data/geth.ipc
Bash1 2admin.nodeInfo.enode3>enode://a1d457bad1a14bd0e8ff33fdc379224430602770b3a51eb012f82613935c5fb212cbee23f56749632c1a98a44821f9f87a806f8ab318bfe5fb03b4af4865f669@127.0.0.1:2001
或者在log的p2p networking中可以获取到ennode
在node2和node3的console中添加node1的ennode
Bash1>admin.addPeer("enode://a1d457bad1a14bd0e8ff33fdc379224430602770b3a51eb012f82613935c5fb212cbee23f56749632c1a98a44821f9f87a806f8ab318bfe5fb03b4af4865f669@127.0.0.1:2001")2 3>true
在node1中可以查看是否成功添加节点
Bash1>net.peerCount2 3>2
或者log中会出现peercount从0变为2
Bash1 2INFO [12-14|16:55:47.141] Looking for peers peercount=2 tried=105 static=0
设置账户以备部署
我们在前面使用了node1的账户作为验证账户,为防止出现错误,也使用node1来设置账户。
首先获取账户的余额以确认是否设置成功。
Bash1>eth.getBalance(eth.accounts[0])2>9.04625697166532776746648320380374280103671755200316906558262375061821325312e+74
然后解锁账户以备挖矿更新链(设置为0是永久解锁)
Bash1>personal.unlockAccount(eth.accounts[0],"123456", 0) 2>true3>personal.listWallets4>[{5 accounts: [{6 address: "0x5a92a5da4f5aaa216a192eaf5ea28bf0940ccac8",7 url: "keystore:///root/ddnwork/ddnNode/node1/data/keystore/UTC--2023-12-14T07-32-52.094500002Z--5a92a5da4f5aaa216a192eaf5ea28bf0940ccac8"8 }],9 status: "unLocked",10 url: "keystore:///root/ddnwork/ddnNode/node1/data/keystore/UTC--2023-12-14T07-32-52.094500002Z--5a92a5da4f5aaa216a192eaf5ea28bf0940ccac8"11}]
设置矿工账户(可以获得eth)
Bash1>miner.setEtherbase("0x5a92a5da4f5aaa216a192eaf5ea28bf0940ccac8")2>true
然后启动挖矿以实时更新链
Bash1>miner.start()2>null
部署合约
这里我使用了我自己编写的truffle项目,在truffle-config中找到network。
dts1networks: {2 development: {3 host: "127.0.0.1", // Ganache 或本地节点的地址4 port: 7545, // Ganache 或本地节点的 RPC 端口5 network_id: "5777", // 任意网络 ID6 },7 advanced: {8 host: "127.0.0.1",9 port: 8545,10 network_id: "*", // Any network (default: none)11 }12 },
修改port为部署的http端口
然后使用yarn进行部署操作
Bash1yarn truffle migrate --network product
部署的时候会先对你的合约进行编译,编译成abi后再进行部署。
Bash1Compiling your contracts...2===========================3> Everything is up to date, there is nothing to compile.4 5 6Starting migrations...7======================8> Network name: 'product'9> Network id: 439610> Block gas limit: 4877543 (0x4a6ce7)11 12 131_LuxuryItemTracking_migration.js14=================================15 16 Deploying 'Migrations'17 ----------------------18 > transaction hash: 0x3ac893fda6e51c6fcfd1f2d3393312251f905842ebabe0569fa6c03a4c4e14ea19 > Blocks: 0 Seconds: 020 > contract address: 0x4A17Af1b0E3a024a94de2E1cfe6F1A73286a852D21 > block number: 3922 > block timestamp: 170254415323 > account: 0x5a92A5Da4F5AAa216A192eaF5ea28bf0940CcAC824 > balance: 904625697166532776746648320380374280103671755200316906558.26237506182132531225 > gas used: 318427 (0x4dbdb)26 > gas price: 1 gwei27 > value sent: 0 ETH28 > total cost: 0.000318427 ETH29 30 31 Deploying 'LuxuryItemTracking'32 ------------------------------33 > transaction hash: 0x6577c1310af48da5019267fc80b7f22c3da5e3350456a80ad629f71491bb545834 > Blocks: 1 Seconds: 435 > contract address: 0xc9D08B8421Fc514733ACBFABFB6b33a3e9fb9e3836 > block number: 4037 > block timestamp: 170254415838 > account: 0x5a92A5Da4F5AAa216A192eaF5ea28bf0940CcAC839 > balance: 904625697166532776746648320380374280103671755200316906558.26237506182132531240 > gas used: 4159271 (0x3f7727)41 > gas price: 1 gwei42 > value sent: 0 ETH43 > total cost: 0.004159271 ETH44 45 > Saving migration to chain.46 > Saving artifacts47 -------------------------------------48 > Total cost: 0.004477698 ETH49 50Summary51=======52> Total deployments: 253> Final cost: 0.004477698 ETH54 55 56Done in 14.51s.