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

What is the address book structure of Bytom's P2P network

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

Share

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

This article mainly explains "what is the structure of Bytom's P2P network address book". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what the structure of Bytom's P2P network address book is".

Addrbook introduction

Addrbook is used to store the nearest peer node address in a P2P network under MacOS, and the default address book path is stored in ~ / Library/Bytom/addrbook.json

Address book format

* * ~ / Library/Bytom/addrbook.json * *

{"Key": "359be6d08bc0c6e21c84bbb2", "Addrs": [{"Addr": {"IP": "122.224.11.144", "Port": 46657}, "Src": {"IP": "198.74.61.131" "Port": 46657}, "Attempts": 0, "LastAttempt": "2018-05-04T12:58:23.894057702+08:00", "LastSuccess": "0001-01-01T00:00:00Z", "BucketType": 1, "Buckets": 10]} address type

There are two kinds of addresses stored in addrbook: * * p2p/addrbook.go * *

Const (bucketTypeNew = 0x01 / / identifies the new address, unreliable address (not successfully connected). Stored in only one bucket bucketTypeOld = 0x02 / / identifies the old address, the reliable address (which has been successfully connected). Can be stored in multiple bucket, up to maxNewBucketsPerAddress)

Note: the type change of an address is not introduced in this article, but will be discussed in later articles.

Address book related structure

address book

Type AddrBook struct {cmn.BaseService mtx sync.Mutex filePath string / / address book path routabilityStrict bool / / is routable. Default is true rand * rand.Rand key string / / address book identity. Index ourAddrs map [string] * NetAddress / / used to calculate addrNew and addrOld to store local network addresses, used to exclude the use of addrLookup map [string] * knownAddress / / to store new and old address sets when adding P2P addresses Used to query addrNew [] map [string] * knownAddress / / store new addresses addrOld [] map [string] * knownAddress / / store old addresses wg sync.WaitGroup nOld int / / number of old addresses nNew int / / number of new addresses}

Known address

Type knownAddress struct {Addr * NetAddress / / addr Src * NetAddress of known peer / / Source addr Attempts int32 of known peer addr / / number of retries of connection peer LastAttempt time.Time / / time of last attempt to connect LastSuccess time.Time / / time of last attempt to connect BucketType byte / / address Type (indicates reliable or unreliable address) Buckets [] int / / buckets to which the current addr belongs}

The routabilityStrict parameter indicates whether the ip stored in the address book is routable. Routable is divided according to RFC, specific reference: RFC standard

Initialize the address book / / NewAddrBook creates a new address book.// Use Start to begin processing asynchronous address updates.func NewAddrBook (filePath string, routabilityStrict bool) * AddrBook {am: = & AddrBook {rand: rand.New (rand.NewSource (time.Now (). UnixNano (), ourAddrs: make (map [string] * NetAddress) AddrLookup: make (map [string] * knownAddress), filePath: filePath, routabilityStrict: routabilityStrict,} am.init () am.BaseService = * cmn.NewBaseService (nil, "AddrBook", am) return am} / / When modifying this Don't forget to update loadFromFile () func (a * AddrBook) init () {/ / address book unique ID a.key = crypto.CRandHex (24) / / 24Accord 2 * 8 = 96 bits / / New addr buckets The default size is a.addrNew = make ([] map [string] * knownAddress, newBucketCount) for I: = range a.addrNew {a.addrNew [I] = make (map [string] * knownAddress)} / / Old addr buckets The default size is 64 a.addrOld = make ([] map [string] * knownAddress, oldBucketCount) for I: = range a.addrOld {a.addrOld [I] = make (map [string] * knownAddress)}} bytomd loads the local address book at startup

When loadFromFile starts with bytomd, it first loads the local address book

/ / OnStart implements Service.func (a * AddrBook) OnStart () error {a.BaseService.OnStart () a.loadFromFile (a.filePath) a.wg.Add (1) go a.saveRoutine () return nil} / / Returns false if file does not exist.// cmn.Panics if file is corrupt.func (a * AddrBook) loadFromFile (filePath string) bool {/ / If doesn't exist, do nothing. / / if the local address book does not exist, return _, err: = os.Stat (filePath) if os.IsNotExist (err) {return false} / / load address book json content / / Load addrBookJSON {} r Err: = os.Open (filePath) if err! = nil {cmn.PanicCrisis (cmn.Fmt ("Error opening file% s:% v", filePath) Err)} defer r.Close () aJSON: = & addrBookJSON {} dec: = json.NewDecoder (r) err = dec.Decode (aJSON) if err! = nil {cmn.PanicCrisis (cmn.Fmt ("Error reading file% s:% v", filePath, err))} / / fill addrNew, addrOld, etc. / / Restore all the fields... / / Restore the key a.key = aJSON.Key / / Restore .addrNew & .addrOld for _, ka: = range aJSON.Addrs {for _, bucketIndex: = range ka.Buckets {bucket: = a.getBucket (ka.BucketType BucketIndex) bucket [ka.Addr.String ()] = ka} a.addrLookup [ka.Addr.String ()] = ka if ka.BucketType = = bucketTypeNew {a.nNewswire +} else {a.nOldstories + }} return true} update the address book regularly

Bytomd updates the local address book regularly, every 2 minutes by default

Func (a * AddrBook) saveRoutine () {dumpAddressTicker: = time.NewTicker (dumpAddressInterval) out: for {select {case

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