当前位置: 首页 > news >正文

go ethereum之p2p

简介

p2p是以太坊节点网络层的骨架,夹在底层 TCP/UDP 和应用协议(eth、snap、les)之间,管三件事:节点发现(谁在线)、连接管理(怎么连上、怎么断)、消息多路复用(连上了跑什么协议)

重要结构

主要有

  • Server
  • Peer:表示一个连接

Server结构定义为

typeServerstruct{// Config fields may not be modified while the server is running.Config// Hooks for testing. These are useful because we can inhibit// the whole protocol stack.newTransportfunc(net.Conn,*ecdsa.PublicKey)transport newPeerHookfunc(*Peer)listenFuncfunc(network,addrstring)(net.Listener,error)lock sync.Mutex// protects runningrunningboollistener net.Listener ourHandshake*protoHandshake loopWG sync.WaitGroup// loop, listenLooppeerFeed event.Feed log log.Logger nodedb*enode.DB localnode*enode.LocalNode discv4*discover.UDPv4 discv5*discover.UDPv5 discmix*enode.FairMix dialsched*dialScheduler// This is read by the NAT port mapping loop.portMappingRegisterchan*portMapping// Channels into the run loop.quitchanstruct{}addtrustedchan*enode.Node removetrustedchan*enode.Node peerOpchanpeerOpFunc peerOpDonechanstruct{}delpeerchanpeerDrop checkpointPostHandshakechan*conn checkpointAddPeerchan*conn// State of run loop and listenLoop.inboundHistory expHeap}

Config:p2p服务端配置
listener:监听器,接受连接
discv4,discv5,discmix:节点发现
dialsched:向外连接
peerOp:对接操作
localnode:结点的ENR

Peer结构定义为

typePeerstruct{rw*conn runningmap[string]*protoRW log log.Logger created mclock.AbsTime wg sync.WaitGroup protoErrchanerrorclosedchanstruct{}pingRecvchanstruct{}discchanDiscReason// events receives message send / receive events if setevents*event.Feed testPipe*MsgPipeRW// for testing}

启动

通过Start启动p2p,主要作以下事项

  • setupLocalNode:创建dev2p handshake和localnode
  • setupPortMapping:开启端口映射循环,根据NAT配置执行不同的协程
  • setupListening:开启监听协程listenLoop处理外部连接
  • setupDiscovery:开启节点发现,根据配置开启发现服务discv4,discv5
  • setupDialScheduler:开启拨号调度器dialScheduler,开启两个协程readNodesloop
  • srv.run():处理通道事件,如新连接等
http://www.jsqmd.com/news/1099487/

相关文章:

  • 59.可跨平台投产!标准 ST 语言 PLC 电机状态机控制完整源码
  • Keepalived简介与工作原理Keepalived是一个基于VRRP协议(虚拟路由冗余协议) 实现的高可用解
  • 买卖股票的最佳时机
  • 23 万 Star 的秘密知识库,运维和安全人员的工具箱
  • 3个步骤让Windows 11重获新生:Win11Debloat系统优化完全指南
  • 一文搞懂巴别鸟版本管理:从历史回溯到冲突解决的完整攻略
  • 基于STM32单片机老人防丢智能拐杖 盲人导航定位跌倒检测设计系统3(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • 别只会堆功能!后端业务代码重构实战,告别祖传烂代码
  • App Store Connect CLI 1.5.4 官方版下载(夸克网盘+百度网盘,SHA256校验)
  • Mi-Create:小米穿戴设备表盘设计的完整技术指南与架构解析
  • 做虚拟资料,哪些内容绝对不能碰?一份版权风险排查清单
  • 大模型核心技术与企业级应用实战指南-附录
  • HTML和CSS基本知识
  • 内蒙古教培机构 AI 招生体系 GEO + 招生官网 + 知识短视频全案测评
  • 企业级数字人直播解决方案:多账号矩阵 + 全域同步推流
  • 如何用LinkSwift彻底告别网盘下载烦恼:九大平台一站式解决方案指南
  • 【云原生与DevOps】05-GitOps工作流:ArgoCD+GitHub Actions完整方案
  • 批量图片翻译视频字幕智能抠图一站式解决跨境电商难题
  • MoE模型参数量与激活机制的技术辨析
  • 免费开源:用pk3DS打造你的专属宝可梦3DS游戏世界
  • 双向链表,反转链表
  • 2026年家电原生智能品牌实用推荐
  • 61.吃透 PLC 状态机编程!带超时停机 + 料仓满报警 + 传感器防抖完整实战
  • PHP本地开发环境一键部署与API集成实践指南
  • MySQL索引深潜:从B+树到查询优化器的艺术
  • Spring Boot 3.0.5 + Vue 3 实战:手把手教你搞定WebSocket消息推送(含完整前后端代码)
  • 浏览器中的专业SVG编辑器:如何用SVG-Edit解决矢量图形编辑难题
  • 基于stm32单片机的智能空气净化器设计家居成品PM2.5甲醛检测定制3(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • 基于STM32单片机智能窗帘设计 智能晾衣架控制 定时开关光照 雨滴3(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • 2026年值得关注的AI外呼厂商盘点:从云厂商到垂直方案,怎么选更合适?