Block (block) data synchronization for Ethereum source scenario analysis

Source: Internet
Author: User
Tags hash
Block data synchronization is divided into passive synchronization and active synchronous passive synchronization refers to the local node receives some messages from other nodes, and then requests the chunk information. Like Newblockhashesmsg.
Active synchronization refers to the node actively requesting chunk data from other nodes, such as the syning at the start of Geth, as well as the runtime timing and synchronization of neighboring nodes. Passive SynchronizationPassive synchronization is done by fetcher, and passive mode is divided into two received full block broadcast messages (NEWBLOCKMSG) received Blockhash broadcast messages (NEWBLOCKHASHESMSG)
newblockhashesmsg Passive ModeBecause the NEWBLOCKHASHESMSG passive synchronous mode and the active synchronous mode will request other nodes and receive headers and body, it is necessary to have the relevant logic to distinguish between the data requested in the passive synchronous mode or the data requested in the active mode. This distinction is achieved through filterheaders and filterbodies. Newblockhashesmsg the logic in this mode is very complex, let's take a look at the process in this mode the source receives the external node newblockhashesmsg message, and then sends a announce to Fetcher
Func (PM *protocolmanager) handlemsg {    case msg. Code = = newblockhashesmsg:          var announces newblockhashesdata           If Err: = Msg. Decode (& Announces); Err! = Nil {             return Errresp (Errdecode, "%v:% V ", MSG, err)         }         // Mark the hashes as present at the remote node          for _, Block: = Range anno unces {            p. MarkBlock (block. Hash)         }         // Schedule all the unknown hashes for retrieval          unknown: = Make (Newblockh Ashesdata, 0, Len (announces))          for _, Block: = Range Announces {              if! Pm.blockchain. Hasblock (block. Hash, block. Number) {                 Unknown = append (unknown, block)             }          }          for _, Block: = Range Unknown {            pm.fetcher. Notify (p.id, block. Hash, block. Number, time. Now (), P.requestoneheader, p.requestbodies)         }}
Func (f * fetcher) Notify (peer string, hash common. Hash, number UInt64, time time. Time, Headerfetcher HEADERREQUESTERFN, Bodyfetcher Bodyrequesterfn) Error {block: = & announce{Hash : Hash, Number:number, Time:time, Origin:peer, Fetchheader:headerfetcher, fetc Hbodies:bodyfetcher,} Select {//Send notification case f.notify <-Block:return Nil case <-F . Quit:return errterminated}}
Fetcher received announce will place it into announced[] array func (f * fetcher) loop () {        case  Notificatio N: = <-f.notify:             //All are well, schedule the announce if block ' s not Yet downloading              if _, OK: = f.fetching[ Notification.hash]; OK {                 break              }               if _, OK: = f.completing [Notification.hash]; OK {                 break              }              f.announces[notification.origin] = Count &NBSP;&NBSP;&NBSP;&NBSP;&NBsp;       f.announced[notification.hash] = append (f.announced[ Notification.hash], notification)              if F.announcechangehook! = Nil && len (f.announced[notification.hash]) = = 1 {                 f. Announcechangehook (Notification.hash, True)              }               If Len (f.announced) = = 1 {                 f.reschedulefetch (Fetchtimer)            }}
Fetchtimer work Scene f.announced Len=1, the above will call F.reschedulefetch, then will go into FETCHTIMER.C branch f.announced len > 1, then it itself will constantly call themselves, Knowing that all announce have been processed Fetchtimer will place announce in f.fetching and call Fetchheader request other nodes to get header data
Func (f * fetcher) loop () {case <-FETCHTIMER.C://@ least one block ' s timer ran out, check for NE eding Retrieval Request: = Make (Map [string][]common. Hash)
For Hash, announces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.