目录
环境
想法
- 由于挖矿很费性能,对于手上的垃圾服务器来说,要挂nginx和tomcat还要挖矿实在是难为它了。
- 因此想把挖矿的任务分给另一台服务器。
- 目标效果即为:
- 副节点连上主节点,两者的数据同步。
- 主节点关闭挖矿,节省性能,由副节点挖矿。
开搞
让副节点连上主节点
- 注意:
- 不要在副节点上直接打开geth执行
- $ geth attach http://your_ip:8545
- 这样相当于远程连接控制主节点了,就像我们在windows上用ssh远程连接linux去控制geth挖矿的开启和关闭一样,链上仍然只有主节点一个,只不过有主节点本机和另一台机可以去控制主节点。
- 不要在副节点上直接打开geth执行
- 副节点需要和主节点用一样的创世块文件
- 这里就用和之前的文章(以太坊私链搭建)所用的创世块一致
- genesis.json
{
"config":{
"chainId":7,
"homesteadBlock":0,
"eip150Block":0,
"eip155Block":0,
"eip158Block":0,
"byzantiumBlock":0,
"constantinopleBlock":0,
"petersburgBlock":0,
"istanbulBlock":0,
"ethhash":{}
},
"alloc":{},
"coinbase":"0x0000000000000000000000000000000000000000",
"difficulty":"0x2",
"extraData":"",
"gasLimit":"0xffffffff",
"nonce":"0x0000000000000042",
"mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp":"0x00"
}
- 还要准备启动脚本,这里也保持和主节点一致
- rungeth.sh
geth --datadir ./data --networkid 7 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "admin,debug,web3,eth,txpool,personal,ethash,miner,net" --http.corsdomain "*" --snapshot --mine --miner.threads 1 --allow-insecure-unlock console 2> 1.log
- 在副节点所在机新建一个用户
- $ geth --datadir ./data account new
- 然后在副节点所在机初始化创世块
- $ sudo geth --datadir ./data init ./genesis.json
- 启动geth
- $ sudo ./rungeth.sh
- 如果是在一台机子上搞两个节点,则要修改他们的运行端口,使之不要重复,不然其中一个会启动不起来。
- 至此,副节点就部署完成了,接下来要在主节点中添加副节点。
- 首先查看一下副节点的enode
- > admin.nodeInfo.enode
- 首先查看一下副节点的enode
![](https://blog.coolight.cool/wp-content/uploads/2022/02/image.png)
- -
- 在主节点添加副节点的enode
- >admin.addPeer("encode")
- 在主节点添加副节点的enode
![](https://blog.coolight.cool/wp-content/uploads/2022/02/image-1.png)
- 查看是否添加成功
- 方法1:
- 直接对比两者的块高度看看是否相等,如果块比较多,可能需要等待一下同步。
- > eth.blockNumber
- 方法2:
- net.peerCount
- admin.peers
- 方法1:
- 让副节点代替主节点挖矿
- 把主节点挖矿停掉,在主节点所在机执行:
- > miner.stop()
- 让副节点挖矿,在副节点所在机执行:
- > miner.start()
- 把主节点挖矿停掉,在主节点所在机执行:
修改java程序连接副节点
- 上一篇的文章:java使用web3j调用以太坊私链上的智能合约
- 上文中讲到,java程序连接单节点(主节点)的私链去调用智能合约。
- 注意:
- 如果不修改的话,此时主节点虽然开着geth,但关闭了挖矿,只能和副节点进行数据同步,而连接主节点发送的请求都是不能执行的。
- 而且主节点的geth不能关闭,否则副节点也会出问题。
- 不要在开着geth的时候直接关闭ssh窗口,否则会导致链的数据丢失,要在geth里执行> exit 退出。
- 复制副节点的用户私钥文件到java程序执行的机子上,
![](https://blog.coolight.cool/wp-content/uploads/2022/02/image-2.png)
- 修改java程序使用这个私钥文件,并把用户地址也改成这个私钥文件的用户。
- 修改java程序连接副节点所在的服务器ip地址。
- 到这里就可以了,如此就实现了让副节点代替主节点挖矿的任务了。
存在的问题
- 主节点的账号是不能在连接副节点后使用的,也就是说,两个节点实际上只有链上的数据是互通的,账号是不互通的。因此java程序连接副节点时需要修改。
- 此时主节点只能同步数据,不能挖矿,连接到主节点后发送的交易并不会由副节点代替挖矿完成。
I like it when people come together and share opinions. Great website, keep it up!
Hello there! This post couldn’t be written much better! Going through this article reminds me of my previous roommate! He continually kept talking about this. I will forward this information to him. Pretty sure he will have a good read. Many thanks for sharing!
This blog was… how do you say it? Relevant!! Finally I’ve found something which helped me. Thank you.
I absolutely love your website.. Very nice colors & theme. Did you develop this website yourself? Please reply back as I’m looking to create my very own website and would love to find out where you got this from or what the theme is called. Appreciate it!
Hi there! This post couldn’t be written much better! Looking at this post reminds me of my previous roommate! He always kept talking about this. I’ll forward this post to him. Pretty sure he will have a very good read. I appreciate you for sharing!
I quite like reading a post that can make people think. Also, thank you for allowing for me to comment.
I want to to thank you for this fantastic read!! I certainly loved every little bit of it. I have you book-marked to check out new stuff you post…
This is a topic that is near to my heart… Many thanks! Where can I find the contact details for questions?
Next time I read a blog, Hopefully it does not disappoint me just as much as this particular one. I mean, Yes, it was my choice to read through, nonetheless I genuinely believed you’d have something helpful to talk about. All I hear is a bunch of moaning about something that you could possibly fix if you weren’t too busy looking for attention.
I used to be able to find good information from your content.
I enjoy reading an article that will make men and women think. Also, thanks for permitting me to comment.
An intriguing discussion is worth comment. I think that you need to write more on this subject matter, it may not be a taboo matter but typically people do not talk about these topics. To the next! Many thanks!
I could not refrain from commenting. Perfectly written.
Everything is very open with a very clear explanation of the issues. It was really informative. Your website is very helpful. Thanks for sharing!
Upgrade your child’s wardrobe with the latest from janie and jack.
Discover unique styles at R13 Clothing.
Explore unique fashion pieces at Z SUPPLY Clothing.
I was pretty pleased to find this website. I wanted to thank you for your time for this fantastic read!! I definitely savored every little bit of it and I have you saved as a favorite to see new things in your website.
There’s certainly a lot to learn about this topic. I love all the points you’ve made.
I like it when individuals come together and share thoughts. Great blog, stick with it.
Spot on with this write-up, I seriously believe this website needs much more attention. I’ll probably be returning to see more, thanks for the info.
I need to to thank you for this excellent read!! I certainly loved every bit of it. I have you book-marked to look at new stuff you post…
This is a topic that is near to my heart… Cheers! Exactly where can I find the contact details for questions?
I love reading an article that will make people think. Also, thank you for allowing for me to comment.
Your style is very unique compared to other people I have read stuff from. Thanks for posting when you’ve got the opportunity, Guess I will just book mark this page.
Next time I read a blog, I hope that it won’t disappoint me just as much as this one. After all, I know it was my choice to read through, but I actually believed you would probably have something interesting to talk about. All I hear is a bunch of complaining about something that you could possibly fix if you were not too busy searching for attention.
Right here is the right web site for anybody who wants to understand this topic. You know a whole lot its almost tough to argue with you (not that I actually would want to…HaHa). You definitely put a new spin on a topic that has been discussed for ages. Wonderful stuff, just great.
Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
paxlovid buy: Paxlovid over the counter – п»їpaxlovid
Way cool! Some very valid points! I appreciate you penning this write-up and the rest of the site is also really good.
Having read this I believed it was rather enlightening. I appreciate you finding the time and effort to put this article together. I once again find myself spending a lot of time both reading and commenting. But so what, it was still worthwhile.
Upgrade your wardrobe and home with the latest in clothing and homeware from Clothing & Homeware.
Next time I read a blog, I hope that it does not fail me as much as this particular one. I mean, I know it was my choice to read, nonetheless I truly believed you would have something helpful to talk about. All I hear is a bunch of complaining about something that you could fix if you weren’t too busy looking for attention.
where buy cheap clomid without rx: where to buy cheap clomid no prescription – clomid cheap
ciprofloxacin generic: buy cipro without rx – antibiotics cipro
I was more than happy to discover this page. I want to to thank you for your time just for this wonderful read!! I definitely appreciated every bit of it and I have you saved as a favorite to look at new information on your site.
Hi, I do believe this is a great blog. I stumbledupon it 😉 I may come back yet again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to help other people.
http://paxloviddelivery.pro/# paxlovid covid
paxlovid pill: п»їpaxlovid – Paxlovid buy online
You’ve made some good points there. I looked on the net for more info about the issue and found most people will go along with your views on this website.
You are so interesting! I don’t believe I have read something like that before. So nice to discover someone with unique thoughts on this issue. Really.. many thanks for starting this up. This web site is something that is needed on the internet, someone with a bit of originality.
paxlovid generic: п»їpaxlovid – paxlovid for sale
Get more for less on Marc Jacobs at the marc jacobs factory outlet online.
http://paxloviddelivery.pro/# paxlovid india
I blog often and I genuinely appreciate your information. This article has truly peaked my interest. I will bookmark your website and keep checking for new information about once per week. I subscribed to your RSS feed too.