以太坊代码分析2---区块数据网络同步

时间:2022-04-08 23:25:33

1.P2P层协议成功后加peer

p2p/server.go

srv.runPeer(p)

2. 解析以太坊协议,同步区块链数据

eth/handler.go

handle(p *peer)

handleMsg(p *peer)

3. 以太坊协议定义

/eth/protocol.go

// Constants to match up protocol versions and messages
const (
eth62 = 62
eth63 = 63
)


// eth protocol message codes
const (
// Protocol messages belonging to eth/62
StatusMsg          = 0x00
NewBlockHashesMsg  = 0x01
TxMsg              = 0x02
GetBlockHeadersMsg = 0x03
BlockHeadersMsg    = 0x04
GetBlockBodiesMsg  = 0x05
BlockBodiesMsg     = 0x06
NewBlockMsg        = 0x07


// Protocol messages belonging to eth/63
GetNodeDataMsg = 0x0d
NodeDataMsg    = 0x0e
GetReceiptsMsg = 0x0f
ReceiptsMsg    = 0x10

)


4.  区块同步

  发送header

      p.SendBlockHeaders(headers)

  接受header

      pm.downloader.DeliverHeaders(p.id, headers)

  发送body

      p.SendBlockBodiesRLP(bodies)

   接受body

      pm.downloader.DeliverBodies(p.id, trasactions, uncles)


5. full chain synchronisation.

eth/downloader/downloader.go

6.. 应用层向网络发送数据:

send:
eth/peer.go
SendTransactions()


p2p/message.go
Send()