Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect other nodes after bytom starts

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to connect other nodes after bytom starts". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Where to connect to other nodes after the original startup

At first I always wondered about this problem: blockchain is a distributed network, so after a node is started, how does it know where to find other nodes to join the network?

After seeing the code, I realized that I had hard-coded some seed addresses in the code, so that when I started, I could first join the network through the seed address. Although the whole network is distributed, it still needs some centralization at the beginning.

Precoded content

For the configuration file config.toml, the contents of the configuration file are hardcoded than in the original code:

Config/toml.go#L22-L45

Var defaultConfigTmpl = `# This is a TOML config file.# For more information See https://github.com/toml-lang/tomlfast_sync = "leveldb" api_addr = "0.0.0.0pur9888" `var mainNetConfigTmpl = `chain_id = "mainnet" [P2P] laddr = "tcp://0.0.0.0:46657" seeds = "45.79.213.28Vera 46657198.74.61.131WR 46657212.111.41.245WR 46657Magi 47.100.214.15446657TM 47.100.109.19946657MAL 47.100.105.165: 46657 "`var testNetConfigTmpl = `chain_id =" testnet "[P2P] laddr =" tcp://0.0.0.0:46656 "seeds =" 47.96.42.1 Vera 46656172.104.224.219 Freera 46656 "`var soloNetConfigTmpl = `chain_id =" solonet "[P2P] laddr =" tcp://0.0.0.0:46658 "seeds ="`"

It can be seen that the preset seeds are different for different chain_id.

Of course, if we know the address of some nodes ourselves, we can also manually modify the file and add it after initializing the generation of config.toml.

Start syncManager

So, does Beehara use these seed addresses and connect them in the code? The point is that the code for the connection is in SyncManager, so we need to find out where to start syncManager.

First, when we start with bytomd node, the following function will be called:

Cmd/bytomd/commands/run_node.go#L41

Func runNode (cmd * cobra.Command, args [] string) error {/ / Create & start node n: = node.NewNode (config) if _, err: = n.Start (); err! = nil {/ /...} / /.}

N.Start is called here, where the Start method comes from the cmn.BaseService embedded in Node:

Node/node.go#L39

Type Node struct {cmn.BaseService / /...}

So n.Start corresponds to the following method:

Vendor/github.com/tendermint/tmlibs/common/service.go#L97

Func (bs * BaseService) Start () (bool, error) {/ /. Err: = bs.impl.OnStart () / /...}

Here, since bs.impl corresponds to Node, we will continue to call Node.OnStart ():

Node/node.go#L169

Func (n * Node) OnStart () error {/ /... N.syncManager.Start () / /.}

As you can see, we finally got to the place where syncManager.Start () was called.

Processing in syncManager

Then there is some processing within syncManager.

It mainly needs to connect the previously connected and saved nodes in the local AddressBook.json in addition to getting seed nodes from config.toml, so that even if the preset seed nodes fail, it is still possible to connect to the network (partially addressing the previously mentioned concerns about centralization).

SyncManager.Start () corresponds to:

Netsync/handle.go#L141

Func (sm * SyncManager) Start () {go sm.netStart () / /...}

Where sm.netStart () corresponds to:

Netsync/handle.go#L121

Func (sm * SyncManager) netStart () error {/ /... / / If seeds exist, add them to the address book and dial out if sm.config.P2P.Seeds! = "{/ / dial out seeds: = strings.Split (sm.config.P2P.Seeds,", ") if err: = sm.DialSeeds (seeds); err! = nil {return err}} / /.}

The sm.config.P2P.Seeds corresponds to the seeds in config.toml. How the two correspond to each other will be explained in detail in later articles.

Then, connect the seed through sm.DialSeeds (seeds), and the corresponding code for this method is located in:

Netsync/handle.go#L229

Func (sm * SyncManager) DialSeeds (seeds [] string) error {return sm.sw.DialSeeds (sm.addrBook, seeds)}

Actually, sm.sw.DialSeeds is called, and sm.sw refers to Switch. At this point, you can see that something called addrBook is involved, which saves the address of the node that has been successfully connected before, and we won't discuss it much here.

Switch.DialSeeds corresponds to:

P2p/switch.go#L311

Func (sw * Switch) DialSeeds (addrBook * AddrBook, seeds [] string) error {/ /. Perm: = rand.Perm (len (netAddrs)) for I: = 0; I < len (perm) / 2; iTunes + {j: = perm [I] sw.dialSeed} / /.}

Random numbers are introduced here to disrupt the order in which connections are initiated, so that each seed has a fair chance to connect.

Sw.dialSeed (netAdres [j]) corresponds to:

P2p/switch.go#L342

Func (sw * Switch) dialSeed (addr * NetAddress) {peer, err: = sw.DialPeerWithAddress (addr, false) / /.}

Sw.DialPeerWithAddress (addr, false) corresponds to:

P2p/switch.go#L351

Func (sw * Switch) DialPeerWithAddress (addr * NetAddress, persistent bool) (* Peer, error) {/ / Log.WithField ("address", addr) .Info ("Dialing peer") peer, err: = newOutboundPeerWithConfig (addr, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError, sw.nodePrivKey, sw.peerConfig) / /.}

If the persistent parameter is true, it indicates that the peer is important, and in some cases, if you disconnect, you will try to reconnect. If the persistent is false, there will be no such treatment.

NewOutboundPeerWithConfig corresponds to:

P2p/peer.go#L69

Func newOutboundPeerWithConfig (addr * NetAddress, reactorsByCh map [byte] Reactor, chDescs [] * ChannelDescriptor, onPeerError func (* Peer, interface {}), ourNodePrivKey crypto.PrivKeyEd25519, config * PeerConfig) (* Peer, error) {conn, err: = dial (addr, config) / /.}

Continue dial and add timeout:

P2p/peer.go#L284

Func dial (addr * NetAddress, config * PeerConfig) (net.Conn, error) {conn, err: = addr.DialTimeout (config.DialTimeout * time.Second) if err! = nil {return nil, err} return conn, nil}

Addr.DialTimeout corresponds to:

P2p/netaddress.go#L141

Func (na * NetAddress) DialTimeout (timeout time.Duration) (net.Conn, error) {conn, err: = net.DialTimeout ("tcp", na.String (), timeout) if err! = nil {return nil, err} return conn, nil} "how to connect other nodes after bytom starts" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report