| timezone | UTC+8 |
|---|
GitHub ID: JoyWQ
Telegram: @JoyWQQ
在一家传统物联网公司做数据预警平台的后端研发工作 主要是流式系统 工作中会用到一些数据挖掘或者自然语言学习的小模型 最近也在全力以赴的学习Web3相关的技术与知识和参加业余的黑客松 希望能通过此次活动更大幅度地提升自己 并结识到优秀、有意思的新朋友
第二阶段黑客松的规划及准备
- X402协议逻辑链路
1. 服务发现与定价阶段
- AI代理通过标准HTTP请求访问目标服务
- 服务端检查请求合法性,确定服务定价
- 返回HTTP 402状态码,包含PaymentRequirements结构体
2. 支付构造与验证阶段
- 代理解析PaymentRequirements,选择支付方案(scheme、网络、资产、金额)
- 构造PaymentPayload,包含支付签名和交易数据
- 通过X-PAYMENT头发送支付载荷
3. 结算执行阶段
- 服务端调用facilitator服务的/verify接口验证支付
- 验证成功后,通过/settle接口执行链上结算
- 资产从代理钱包转移到服务方钱包
4. 服务交付阶段
- 支付验证成功后,服务端执行实际业务逻辑
- 返回HTTP 200状态码,业务结果在响应体中
- 通过X-PAYMENT-RESPONSE头返回支付确认信息
关键特性 :整个流程在单个HTTP请求-响应周期内完成,实现"调用即支付"的原子操作
- AP2协议逻辑链路
1. 授权契约建立阶段
- 用户为AI代理创建授权契约(Mandate),定义支付权限边界
- 契约包含:最大支付金额、有效期限、允许的服务类型
- 契约通过数字签名确保不可篡改
2. 可验证凭证生成阶段
- 代理在每次交易前生成可验证凭证(Verifiable Credential)
- 凭证包含:交易详情、时间戳、代理身份证明
- 凭证通过密码学证明确保交易的可审计性
3. 交易执行与审计阶段
- 代理在支付时同时提交授权契约和可验证凭证
- 服务方验证凭证的有效性和契约的合规性
- 交易记录进入审计系统,确保商业合规
4. 争议解决与追溯阶段
- 系统支持交易争议的快速解决机制
- 通过完整的审计轨迹追溯交易全过程
- 提供法律和技术双重保障
- X402与AP2可能的协作逻辑链路
用户授权 → AP2契约建立 → 代理发现服务 → X402支付构造 → AP2凭证验证 → X402支付执行 → 服务交付 → 审计记录
- 完成erc-8004-journal项目的集成测试
结果日志
================================================= test session starts =================================================
platform win32 -- Python 3.11.13, pytest-8.4.2, pluggy-1.6.0 -- D:\conda\envs\a2a-agent\python.exe
cachedir: .pytest_cache
rootdir: d:\workspace\CurrentPractice\erc-8004-journal\tests
configfile: pytest.ini
plugins: asyncio-1.2.0
asyncio: mode=Mode.AUTO, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
collected 1 item
tests\integration\erc8004_integration_test.py::TestFullERC8004Workflow::test_full_erc8004_workflow 🔍 描述符管理器: 目录路径 d:\workspace\CurrentPractice\erc-8004-journal\src\descriptors
🔍 目录存在: True
🔍 发现 3 个JSON文件: ['provider-agent.json', 'requester-agent.json', 'validator-agent.json']
🚀 初始化测试代理
✅ 已加载私钥,账户: 0xd3066C388978d0C96372b6e85594D84cC4c37030
✅ provider代理已加载
✅ 已加载私钥,账户: 0x8e63f840A7872aB60c5DC90e3aC2f8bf48A6Ed93
✅ requester代理已加载
✅ 已加载私钥,账户: 0x0217daD53ACd16c503De310Cb530bb2d5B678D77
✅ validator代理已加载
🚀 provider代理已启动
🚀 requester代理已启动
🚀 validator代理已启动
🚀 启动代理: provider
📍 端口: 8001
🌐 端点: http://localhost:8001
🔗 区块链: 已连接
📋 能力: 3 个
🚀 启动代理: requester
📍 端口: 8002
🌐 端点: http://localhost:8002
🔗 区块链: 已连接
📋 能力: 3 个
🚀 启动代理: validator
📍 端口: 8003
🌐 端点: http://localhost:8003
🔗 区块链: 已连接
📋 能力: 3 个
✅ 代理服务已在端口 8001 启动
✅ 代理服务已在端口 8003 启动
✅ 代理服务已在端口 8002 启动
✅ provider代理服务健康检查通过
✅ requester代理服务健康检查通过
✅ validator代理服务健康检查通过
🎯 开始ERC-8004全流程测试
============================================================
1️⃣ 阶段1: 链上注册
----------------------------------------
📝 注册provider代理到区块链...
🔗 开始注册代理: provider
🔗 开始注册代理,账户: 0xd3066C388978d0C96372b6e85594D84cC4c37030
📄 Token URI: {"agent_id":"provider","capabilities":["service://ai/image-generation","service://data/processing","...
📊 当前账户代理数量: 104
⛽ 预估Gas: 651531
🔐 签名交易...
✅ 交易已发送,哈希: 8e5087eb6c122e39896dd5b1e2b566487cc19c14f02399d80693d6b6787f3fc1
⏳ 等待交易确认...
✅ 交易确认! 区块: 9478930, 状态: 1
📊 当前区块号: 9478930
🔍 查询注册事件: from_block=9478930, to_block=9478930
✅ 查询到 1 个注册事件
事件 1: agentId=330, owner=0xd3066C388978d0C96372b6e85594D84cC4c37030, tokenURI={"agent_id":"provider","capabilities":["service://ai/image-generation","service://data/processing","service://ai/text-analysis"],"description":"AI\u670d\u52a1\u63d0\u4f9b\u65b9\u4ee3\u7406\uff0c\u63d0\u4f9b\u591a\u79cdAI\u80fd\u529b","endpoints":{"service://ai/image-generation":"http://localhost:8002/call","service://ai/text-analysis":"http://localhost:8002/call","service://data/processing":"http://localhost:8002/call"},"identity":{"address":"0x456C3F5E678901234567890ABCDEF01234567890AB","public_key":"0x04def0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef01","verification_method":"secp256k1"},"service_endpoint":"http://localhost:8001","timestamp":1761294844,"version":"1.0"}
🎉 注册成功! Agent ID: 330
✅ 交易已提交: 8e5087eb6c122e39896dd5b1e2b566487cc19c14f02399d80693d6b6787f3fc1
✅ provider代理注册成功: (True, '8e5087eb6c122e39896dd5b1e2b566487cc19c14f02399d80693d6b6787f3fc1')
📝 注册requester代理到区块链...
🔗 开始注册代理: requester
🔗 开始注册代理,账户: 0x8e63f840A7872aB60c5DC90e3aC2f8bf48A6Ed93
📄 Token URI: {"agent_id":"requester","capabilities":["a2a://workflow/orchestrate","a2a://discovery/query","a2a://...
📊 当前账户代理数量: 65
⛽ 预估Gas: 651445
🔐 签名交易...
✅ 交易已发送,哈希: 00d4de4872096918a43fe4afd83ed87d2141691c65d6b8546e665d383220a13b
⏳ 等待交易确认...
✅ 交易确认! 区块: 9478935, 状态: 1
📊 当前区块号: 9478935
🔍 查询注册事件: from_block=9478935, to_block=9478935
✅ 查询到 1 个注册事件
事件 1: agentId=331, owner=0x8e63f840A7872aB60c5DC90e3aC2f8bf48A6Ed93, tokenURI={"agent_id":"requester","capabilities":["a2a://workflow/orchestrate","a2a://discovery/query","a2a://payment/initiate"],"description":"AI\u670d\u52a1\u9700\u6c42\u65b9\u4ee3\u7406\uff0c\u8d1f\u8d23\u53d1\u73b0\u548c\u534f\u8c03\u670d\u52a1","endpoints":{"a2a://discovery/query":"http://localhost:8001/call","a2a://payment/initiate":"http://localhost:8001/call","a2a://workflow/orchestrate":"http://localhost:8001/call"},"identity":{"address":"0x893A5B67B2d0e8A5C71c5c8f4B5e2F8C3a1b0c2d","public_key":"0x04a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef012345","verification_method":"secp256k1"},"service_endpoint":"http://localhost:8002","timestamp":1761294872,"version":"1.0"}
🎉 注册成功! Agent ID: 331
✅ 交易已提交: 00d4de4872096918a43fe4afd83ed87d2141691c65d6b8546e665d383220a13b
✅ requester代理注册成功: (True, '00d4de4872096918a43fe4afd83ed87d2141691c65d6b8546e665d383220a13b')
📝 注册validator代理到区块链...
🔗 开始注册代理: validator
🔗 开始注册代理,账户: 0x0217daD53ACd16c503De310Cb530bb2d5B678D77
📄 Token URI: {"agent_id":"validator","capabilities":["reputation://score/calculation","validation://compliance/ch...
📊 当前账户代理数量: 64
⛽ 预估Gas: 674937
🔐 签名交易...
✅ 交易已发送,哈希: a562a6073b0e7ce544059091874038cd614778498175e243bf6ce092823a1b92
⏳ 等待交易确认...
✅ 交易确认! 区块: 9478937, 状态: 1
📊 当前区块号: 9478937
🔍 查询注册事件: from_block=9478937, to_block=9478937
✅ 查询到 1 个注册事件
事件 1: agentId=332, owner=0x0217daD53ACd16c503De310Cb530bb2d5B678D77, tokenURI={"agent_id":"validator","capabilities":["reputation://score/calculation","validation://compliance/check","validation://quality/assurance"],"description":"\u9a8c\u8bc1\u548c\u58f0\u8a89\u4ee3\u7406\uff0c\u786e\u4fdd\u670d\u52a1\u8d28\u91cf\u548c\u5408\u89c4\u6027","endpoints":{"reputation://score/calculation":"http://localhost:8003/call","validation://compliance/check":"http://localhost:8003/call","validation://quality/assurance":"http://localhost:8003/call"},"identity":{"address":"0x78901234567890ABCDEF01234567890ABCDEF0123","public_key":"0x0456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789","verification_method":"secp256k1"},"service_endpoint":"http://localhost:8003","timestamp":1761294929,"version":"1.0"}
🎉 注册成功! Agent ID: 332
✅ 交易已提交: a562a6073b0e7ce544059091874038cd614778498175e243bf6ce092823a1b92
✅ validator代理注册成功: (True, 'a562a6073b0e7ce544059091874038cd614778498175e243bf6ce092823a1b92')
2️⃣ 阶段2: 服务发现与握手
🔄 从链上事件刷新代理缓存...
🔍 开始查询注册事件...
📊 首次查询: from_block=9477937, to_block=9478937 (限制查询范围)
📊 当前区块号: 9478937
🔍 查询注册事件: from_block=9477937, to_block=9478937
✅ 查询到 30 个注册事件
📋 获取到 30 个注册事件
解析事件 1/30...
⚠️ 解析Token URI失败: 无法解析Token URI: ipfs://bafkreibecca2m7txyqcrwssij6ccnbv7p26eiqmzf6chq2dunx46ic2wme...: line 1 column 1 (char 0)
Token URI内容: ipfs://bafkreibecca2m7txyqcrwssij6ccnbv7p26eiqmzf6chq2dunx46ic2wme...
✅ 成功解析代理: token_id=303, endpoint=
解析事件 2/30...
✅ 成功解析代理: token_id=304, endpoint=http://localhost:8001
解析事件 3/30...
✅ 成功解析代理: token_id=305, endpoint=http://localhost:8002
解析事件 4/30...
✅ 成功解析代理: token_id=306, endpoint=http://localhost:8003
解析事件 5/30...
✅ 成功解析代理: token_id=307, endpoint=http://localhost:8001
解析事件 6/30...
✅ 成功解析代理: token_id=308, endpoint=http://localhost:8002
解析事件 7/30...
✅ 成功解析代理: token_id=309, endpoint=http://localhost:8001
解析事件 8/30...
✅ 成功解析代理: token_id=310, endpoint=http://localhost:8003
解析事件 9/30...
✅ 成功解析代理: token_id=311, endpoint=http://localhost:8002
解析事件 10/30...
✅ 成功解析代理: token_id=312, endpoint=http://localhost:8003
解析事件 11/30...
✅ 成功解析代理: token_id=313, endpoint=http://localhost:8001
解析事件 12/30...
✅ 成功解析代理: token_id=314, endpoint=http://localhost:8002
解析事件 13/30...
✅ 成功解析代理: token_id=315, endpoint=http://localhost:8003
解析事件 14/30...
✅ 成功解析代理: token_id=316, endpoint=http://localhost:8001
解析事件 15/30...
✅ 成功解析代理: token_id=317, endpoint=http://localhost:8002
解析事件 16/30...
✅ 成功解析代理: token_id=318, endpoint=http://localhost:8003
解析事件 17/30...
✅ 成功解析代理: token_id=319, endpoint=http://localhost:8001
解析事件 18/30...
✅ 成功解析代理: token_id=320, endpoint=http://localhost:8002
解析事件 19/30...
✅ 成功解析代理: token_id=321, endpoint=http://localhost:8001
解析事件 20/30...
✅ 成功解析代理: token_id=322, endpoint=http://localhost:8002
解析事件 21/30...
✅ 成功解析代理: token_id=323, endpoint=http://localhost:8003
解析事件 22/30...
✅ 成功解析代理: token_id=324, endpoint=http://localhost:8001
解析事件 23/30...
✅ 成功解析代理: token_id=325, endpoint=http://localhost:8002
解析事件 24/30...
✅ 成功解析代理: token_id=326, endpoint=http://localhost:8003
解析事件 25/30...
✅ 成功解析代理: token_id=327, endpoint=http://localhost:8001
解析事件 26/30...
✅ 成功解析代理: token_id=328, endpoint=http://localhost:8002
解析事件 27/30...
✅ 成功解析代理: token_id=329, endpoint=http://localhost:8003
解析事件 28/30...
✅ 成功解析代理: token_id=330, endpoint=http://localhost:8001
解析事件 29/30...
✅ 成功解析代理: token_id=331, endpoint=http://localhost:8002
解析事件 30/30...
✅ 成功解析代理: token_id=332, endpoint=http://localhost:8003
📊 总共解析到 30 个代理
✅ 缓存已更新,共 30 个代理
🔍 发现 10 个符合条件的代理
📊 声誉阈值=0.0: 发现 10 个代理
1. token_id=304, 声誉=0.500, 能力匹配=True
2. token_id=307, 声誉=0.500, 能力匹配=True
3. token_id=309, 声誉=0.500, 能力匹配=True
4. token_id=313, 声誉=0.500, 能力匹配=True
5. token_id=316, 声誉=0.500, 能力匹配=True
6. token_id=319, 声誉=0.500, 能力匹配=True
7. token_id=321, 声誉=0.500, 能力匹配=True
8. token_id=324, 声誉=0.500, 能力匹配=True
9. token_id=327, 声誉=0.500, 能力匹配=True
10. token_id=330, 声誉=0.500, 能力匹配=True
🔍 发现 10 个符合条件的代理
📊 声誉阈值=0.5: 发现 10 个代理
1. token_id=304, 声誉=0.500, 能力匹配=True
2. token_id=307, 声誉=0.500, 能力匹配=True
3. token_id=309, 声誉=0.500, 能力匹配=True
4. token_id=313, 声誉=0.500, 能力匹配=True
5. token_id=316, 声誉=0.500, 能力匹配=True
6. token_id=319, 声誉=0.500, 能力匹配=True
7. token_id=321, 声誉=0.500, 能力匹配=True
8. token_id=324, 声誉=0.500, 能力匹配=True
9. token_id=327, 声誉=0.500, 能力匹配=True
10. token_id=330, 声誉=0.500, 能力匹配=True
🔍 发现 0 个符合条件的代理
📊 声誉阈值=0.8: 发现 0 个代理
📊 声誉分布:
范围: 0.500 - 0.500
平均: 0.500
优秀(≥0.8): 0
良好(0.6-0.8): 0
一般(0.4-0.6): 10
较差(<0.4): 0
🎯 最终选择 10 个代理
🔍 收到握手请求: {'initiator': 'requester', 'initiator_endpoint': 'http://localhost:8002', 'target_agent': 'provider', 'target_endpoint': 'http://localhost:8001'}
🔗 将添加连接: requester -> http://localhost:8002
✅ 连接已添加,会话ID: f885d96c4707bc54
🤝 provider ↔ requester
📤 握手响应: {'status': 'success', 'session_id': 'f885d96c4707bc54', 'responder': 'provider', 'responder_endpoint': 'http://localhost:8001'}
✅ requester → provider 握手成功
🔍 收到握手请求: {'initiator': 'requester', 'initiator_endpoint': 'http://localhost:8002', 'target_agent': 'validator', 'target_endpoint': 'http://localhost:8003'}
🔗 将添加连接: requester -> http://localhost:8002
✅ 连接已添加,会话ID: b53a990b8f7efc07
🤝 validator ↔ requester
📤 握手响应: {'status': 'success', 'session_id': 'b53a990b8f7efc07', 'responder': 'validator', 'responder_endpoint': 'http://localhost:8003'}
✅ requester → validator 握手成功
3️⃣ 阶段3: 服务执行与验证
[Reputation] Recorded capability_call event from requester to provider (success: True, quality: 1.0)
🎨 服务调用成功
[Reputation] Recorded capability_call event from requester to validator (success: True, quality: 1.0)
🔍 验证完成
4️⃣ 阶段4: 声誉反馈
[Reputation] Recorded handshake event from requester to provider (success: True, quality: 1.0)
📝 记录交互事件: requester → provider (handshake)
[Reputation] Recorded handshake event from validator to provider (success: True, quality: 1.0)
📝 记录交互事件: validator → provider (handshake)
[Reputation] Recorded service_call event from requester to provider (success: True, quality: 1.0)
📝 记录交互事件: requester → provider (service_call)
📝 记录交互事件: requester → validator (validation_request)
✅ 交互事件记录完成
📊 链下声誉分数:
provider:
综合分数: 0.731
技术信任: 1.000
财务可靠: 0.500
社会共识: 0.560
活跃程度: 0.774
交互次数: 4
validator:
综合分数: 0.500
技术信任: 0.500
财务可靠: 0.500
社会共识: 0.500
活跃程度: 0.500
交互次数: 1
✅ 为 1 生成反馈授权数据成功(包含签名)
✅ 获取反馈授权成功
🎯 提交声誉反馈: 代理1, 评分90
⚠️ Gas估算失败,使用默认值: ('execution reverted: Invalid signature', '0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000011496e76616c6964207369676e6174757265000000000000000000000000000000')
🔐 签名交易...
✅ 交易已发送,哈希: 9c12018132440769801473971c962401df5cd663f285bfc20f16528e352b935f
⏳ 等待交易确认...
✅ 交易确认! 区块: 9478975, 状态: 0
📊 声誉反馈提交成功
🔗 provider声誉融合结果:
链上声誉: 无数据
链下声誉: 0.500
融合声誉: 0.500
🔗 validator声誉融合结果:
链上声誉: 无数据
链下声誉: 0.500
融合声誉: 0.500
5️⃣ 阶段5: 系统状态验证
🔗 provider 活跃连接: 1
- requester -> http://localhost:8002 (会话: 15b86619cd5c81f5)
🔗 requester 活跃连接: 2
- provider -> http://localhost:8001 (会话: 1efc9c96a76eb03b)
- validator -> http://localhost:8003 (会话: bff6de8d28664d46)
🔗 validator 活跃连接: 1
- requester -> http://localhost:8002 (会话: e651bbddecb3d39a)
📊 provider 尚无声誉数据
📊 validator 尚无声誉数据
✅ 所有系统检查完成
============================================================
✅ 测试完成
- erc-8004-journal项目说明
# erc-8004-journal
对接ERC-8004标准,简单实现A2A协议,支持代理注册、基于声誉和能力的服务发现、链上链下融合的声誉计算等基本功能
## 🌟 项目概述
erc-8004-journal项目旨在模拟一个去中心化的AI代理生态系统,其中:
- **智能代理** 可以在区块链上注册和发现
- **声誉系统** 基于链上链下数据融合计算
- **信任机制** 通过交互历史和验证反馈建立
- **服务市场** 支持AI服务的去中心化交易
## 🏗️ 架构设计
### 核心组件
```
src/
├── core/ # 核心业务逻辑
│ ├── agent/ # 代理管理
│ │ ├── agent_service.py # 代理服务主类
│ │ └── managers/ # 管理器模块
│ │ ├── connection_manager.py # 连接管理
│ │ ├── blockchain_manager.py # 区块链交互
│ │ └── http_request_handler.py # HTTP API处理
│ ├── domain/ # 领域模型
│ │ └── primitives.py # 基础数据类型
│ └── reputation/ # 声誉系统
│ ├── engine.py # 声誉计算引擎
│ └── entities.py # 声誉实体
├── infrastructure/ # 基础设施
│ ├── blockchain/ # 区块链集成
│ │ └── erc8004_client.py # ERC-8004客户端
│ └── context/ # 上下文管理
└── descriptors/ # 代理描述符
├── provider-agent.json # 服务提供方配置
├── requester-agent.json # 服务需求方配置
└── validator-agent.json # 验证方配置
```
### 技术栈
- **区块链**: Ethereum (Sepolia测试网), Web3.py
- **网络通信**: aiohttp (异步HTTP)
- **测试框架**: pytest, pytest-asyncio
- **开发语言**: Python 3.11+
## 🚀 快速开始
### 环境要求
```bash
# Python 3.11或更高版本
python --version
# 安装依赖
pip install -r requirements.txt
```
### 配置说明
1. **区块链配置**: 设置Sepolia测试网RPC端点
2. **代理配置**: 编辑`src/descriptors/`下的JSON配置文件
3. **端口配置**: 默认端口8001-8003用于代理服务
### 运行示例
```bash
# 运行集成测试
pytest tests/integration/erc8004_integration_test.py -v
# 运行单元测试
pytest tests/unit/ -v
# 启动单个代理
python src/main.py --config src/descriptors/provider-agent.json
```
## 🔧 核心功能
### 1. 代理注册与发现
- **链上注册**: 代理在区块链上注册身份和能力
- **服务发现**: 基于能力和声誉的服务发现机制
- **端点管理**: 动态端点注册和健康检查
### 2. 连接管理
- **握手协议**: 安全的双向连接建立
- **会话管理**: 连接状态和活动跟踪
- **连接清理**: 自动清理不活跃连接
### 3. 声誉系统
- **多维度评分**: 技术信任、财务可靠、社会共识、活跃程度
- **链上链下融合**: 结合区块链数据和本地交互历史
- **时间衰减**: 近期交互权重更高
### 4. 服务执行
- **能力调用**: 基于URI的能力调用机制
- **验证流程**: 第三方验证服务集成
- **反馈机制**: 声誉反馈和评分系统
### 工作流阶段说明
1. **启动阶段**: 代理初始化,加载配置和描述符
2. **注册阶段**: 在区块链上完成身份和能力注册
3. **发现阶段**: 通过服务发现机制寻找其他代理
4. **连接阶段**: 建立安全的双向通信连接
5. **执行阶段**: 服务调用和能力执行
6. **验证阶段**: 第三方验证和结果确认
7. **反馈阶段**: 声誉评分和链上更新
8. **维护阶段**: 连接状态监控和资源管理
## 🧪 测试体系
### 集成测试
```bash
# 完整工作流测试
pytest tests/integration/erc8004_integration_test.py::TestFullERC8004Workflow::test_full_erc8004_workflow
# 连接测试
pytest tests/integration/simple_connection_test.py
# 握手调试测试
pytest tests/integration/debug_handshake_test.py
```
### 单元测试
- **代理管理**: 连接、区块链、HTTP处理器的单元测试
- **声誉计算**: 声誉引擎和实体测试
- **基础设施**: 区块链客户端和上下文管理测试
## 📊 系统特性
- **去中心化声誉系统**: 结合区块链和本地交互的混合声誉模型
- **智能合约集成**: 完整的链上身份和声誉管理
- **模块化架构**: 高度可扩展的组件设计
- **异步通信**: 基于aiohttp的高性能异步通信框架
## 📊 项目状态
### 已完成功能
✅ **代理注册**
- 链上身份注册
- 能力描述符管理
- 端点动态配置
✅ **连接管理**
- 双向握手协议
- 会话状态跟踪
- 连接健康检查
✅ **声誉计算**
- 多维度评分算法
- 时间衰减机制
- 链上链下数据融合
✅ **测试覆盖**
- 完整集成测试流程
- 单元测试覆盖核心组件
- 调试工具和诊断功能
- 代码地址
https://gitee.com/joy_wq/erc-8004-journal.git
-
了解AP2协议与X402协议,站在A2A协议的角度思考如何集成
-
AP2侧重点:授权,通过加密签名的"意图授权"和"购物车授权"等构建不可篡改的交易链
-
X402侧重点:支付通道,将支付请求直接嵌入HTTP响应中,如402状态码
-
排查代理工作流程中的问题
-
进一步完成链下声誉计算的逻辑,采用传统算法进行链上链下的数据整合
-
进一步调试与测试链上合约交互的问题
-
构建第1个集成测试样例,即单个agent完成全流程操作
-
封装与测试链上合约交互的工具类并通过单元测试
-
构建以http作为通信方式的代理逻辑,对代理的行为进行抽象和拆分,比如代理访问链上合约、代理与代理交互、代理执行任务
-
通过单元测试加深对A2A协议的理解
-
将之前的js代码等效地迁移为python
-
增加A2A的JSON描述文件
-
增加A2A交互的握手逻辑
-
增加简单的链下声誉计算逻辑
-
测试并进行部分的问题修复
- 学习A2A协议
它作为代理之间协作的信息传递规范,一共包含了代理描述、协作提议、任务接收、结果交付、反馈评价5种消息格式
-
完成代码挑战一,构建js代码以访问链上的合约
-
在此基础上增加A2A交互的简单模拟
以下是模拟用的代码
a2aNetwork.js
import { IntelligentAgent } from './intelligentAgent.js';
export class A2ANetwork {
constructor() {
this.agents = new Map();
this.taskRegistry = new Map();
}
// 注册代理到网络
registerAgent(agent) {
this.agents.set(agent.name, agent);
console.log(`🌐 A2A网络注册: ${agent.name}`);
// 更新所有代理的已知代理列表
this.updateAgentDiscovery();
}
// 更新代理发现
updateAgentDiscovery() {
const agentList = Array.from(this.agents.values());
this.agents.forEach(agent => {
agent.discoverAgents(agentList);
});
}
// 发起协作任务
async initiateCollaboration(fromAgentName, toAgentName, taskType, taskData) {
const fromAgent = this.agents.get(fromAgentName);
if (!fromAgent) {
throw new Error(`代理未找到: ${fromAgentName}`);
}
console.log(`\n🔄 A2A网络: ${fromAgentName} → ${toAgentName} 协作开始`);
try {
const result = await fromAgent.proposeCollaboration(toAgentName, taskType, taskData);
console.log(`✅ 协作完成: ${fromAgentName} + ${toAgentName}`);
console.log(`📊 任务结果:`, result.results);
// 记录任务
const taskId = `task_${Date.now()}`;
this.taskRegistry.set(taskId, {
from: fromAgentName,
to: toAgentName,
task_type: taskType,
result: result,
timestamp: new Date().toISOString()
});
return result;
} catch (error) {
console.log(`❌ 协作失败: ${error.message}`);
throw error;
}
}
// 网络状态报告
getNetworkStatus() {
const status = {
total_agents: this.agents.size,
agent_list: Array.from(this.agents.keys()),
total_tasks: this.taskRegistry.size,
recent_tasks: Array.from(this.taskRegistry.entries()).slice(-5)
};
console.log('\n📈 A2A网络状态报告:');
console.log(` 代理数量: ${status.total_agents}`);
console.log(` 代理列表: ${status.agent_list.join(', ')}`);
console.log(` 任务总数: ${status.total_tasks}`);
return status;
}
// 获取代理详细状态
getAgentDetails(agentName) {
const agent = this.agents.get(agentName);
if (agent) {
return agent.getStatusReport();
}
return null;
}
}
a2aProtocol.js
import { ethers } from 'ethers';
import { CONTRACTS, RPC_URL } from './config.js';
export class A2AProtocol {
// 代理描述文件标准
static createAgentDescriptor(agentId, capabilities, endpoints) {
return {
version: "a2a-1.0",
agent_id: agentId,
identity: {
erc8004_id: agentId,
creation_date: new Date().toISOString(),
verification_status: "pending"
},
capabilities: capabilities,
communication: {
endpoints: endpoints,
supported_protocols: ["http", "websocket"],
message_format: "json"
},
reputation: {
on_chain_score: 0,
interaction_count: 0,
success_rate: 0
}
};
}
// 协作提议消息格式
static createCollaborationProposal(fromAgent, toAgent, task, compensation) {
return {
message_type: "collaboration_proposal",
proposal_id: `prop_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
timestamp: new Date().toISOString(),
from_agent: fromAgent,
to_agent: toAgent,
task: task,
compensation: compensation,
expiration: new Date(Date.now() + 30 * 60 * 1000).toISOString() // 30分钟过期
};
}
// 任务接受消息
static createTaskAcceptance(proposalId, acceptor, commitments) {
return {
message_type: "task_acceptance",
proposal_id: proposalId,
acceptor: acceptor,
acceptance_time: new Date().toISOString(),
commitments: commitments
};
}
// 结果交付消息
static createResultDelivery(taskId, results, qualityMetrics) {
return {
message_type: "result_delivery",
task_id: taskId,
delivery_time: new Date().toISOString(),
results: results,
quality_metrics: qualityMetrics
};
}
// 反馈评价消息
static createFeedback(taskId, fromAgent, toAgent, ratings, comments) {
return {
message_type: "feedback",
task_id: taskId,
from_agent: fromAgent,
to_agent: toAgent,
ratings: ratings,
comments: comments,
timestamp: new Date().toISOString()
};
}
}
config.js
import dotenv from 'dotenv';
// 确保先加载环境变量
dotenv.config();
export const CONTRACTS = {
SEPOLIA: {
IDENTITY: "0x8004a6090Cd10A7288092483047B097295Fb8847",
REPUTATION: "0x8004B8FD1A363aa02fDC07635C0c5F94f6Af5B7E",
VALIDATION: "0x8004CB39f29c09145F24Ad9dDe2A108C1A2cdfC5"
}
};
// 直接从环境变量读取,确保有默认值
export const RPC_URL = process.env.SEPOLIA_RPC_URL || "https://sepolia.rpc.thirdweb.com";
intelligentAgent.js
import { ethers } from 'ethers';
import { CONTRACTS, RPC_URL } from './config.js';
import { A2AProtocol } from './a2aProtocol.js';
export class IntelligentAgent {
constructor(name, privateKey, capabilities = []) {
this.name = name;
this.provider = new ethers.JsonRpcProvider(RPC_URL);
this.wallet = new ethers.Wallet(privateKey, this.provider);
// A2A 协议属性
this.capabilities = capabilities;
this.agentId = null;
this.descriptor = null;
this.messageQueue = [];
this.knownAgents = new Map();
this.collaborationHistory = [];
// 使用完整的 ABI
const IDENTITY_ABI = [
"function register() returns (uint256)",
"function register(string) returns (uint256)",
"function ownerOf(uint256) view returns (address)",
"function balanceOf(address) view returns (uint256)",
"function name() view returns (string)",
"function symbol() view returns (string)"
];
// 初始化合约
this.identity = new ethers.Contract(CONTRACTS.SEPOLIA.IDENTITY, IDENTITY_ABI, this.wallet);
this.reputation = new ethers.Contract(CONTRACTS.SEPOLIA.REPUTATION, [
"function getSummary(uint256,address[],bytes32,bytes32) view returns (uint64,uint8)"
], this.wallet);
}
// 注册代理并创建描述文件
async register() {
console.log(`🤖 ${this.name} 正在注册到A2A网络...`);
try {
// 先检查合约连接是否正常
const name = await this.identity.name();
console.log(` 连接合约: ${name}`);
// 获取当前代理数量
const currentBalance = await this.identity.balanceOf(this.wallet.address);
console.log(` 当前代理数量: ${currentBalance.toString()}`);
// 发送真实的注册交易
console.log(` 🚀 发送注册交易...`);
const tx = await this.identity['register()']();
console.log(` 交易哈希: ${tx.hash}`);
// 等待交易确认
console.log(` ⏳ 等待交易确认...`);
const receipt = await tx.wait();
console.log(` ✅ 交易确认! 区块: ${receipt.blockNumber}`);
// 获取新的代理数量
const newBalance = await this.identity.balanceOf(this.wallet.address);
console.log(` 新代理数量: ${newBalance.toString()}`);
// 设置代理ID为新余额-1
this.agentId = newBalance - 1n;
} catch (error) {
console.error(`❌ ${this.name} 注册失败:`, error.message);
console.log(`⚠️ ${this.name} 使用模拟ID继续测试`);
// 使用递增的模拟ID避免重复
this.agentId = Math.floor(Math.random() * 1000); // 随机ID避免冲突
}
// 创建 A2A 代理描述文件(无论成功与否都创建)
this.descriptor = A2AProtocol.createAgentDescriptor(
this.agentId.toString(),
this.capabilities,
[{ protocol: "internal", url: `agent://${this.name}` }]
);
console.log(`✅ ${this.name} A2A注册完成! ID: ${this.agentId}`);
console.log(`📋 能力: ${this.capabilities.join(', ')}`);
return this.agentId;
}
// 发现其他代理(简化版)
discoverAgents(agentList) {
agentList.forEach(agent => {
if (agent.name !== this.name) {
this.knownAgents.set(agent.name, agent);
console.log(`🔍 ${this.name} 发现代理: ${agent.name}`);
}
});
}
// 发送协作提议
async proposeCollaboration(toAgentName, taskType, taskData) {
const toAgent = this.knownAgents.get(toAgentName);
if (!toAgent) {
throw new Error(`未知代理: ${toAgentName}`);
}
const proposal = A2AProtocol.createCollaborationProposal(
this.name,
toAgentName,
{
type: taskType,
description: `由 ${this.name} 发起的 ${taskType} 任务`,
input_data: taskData,
expected_output: `${taskType}_result`,
deadline: new Date(Date.now() + 10 * 60 * 1000).toISOString() // 10分钟
},
{
type: "reputation_points",
amount: 5
}
);
console.log(`📨 ${this.name} → ${toAgentName}: 发送协作提议`);
// 发送提议到目标代理
const response = await toAgent.receiveCollaborationProposal(proposal);
return response;
}
// 接收协作提议
async receiveCollaborationProposal(proposal) {
console.log(`📩 ${this.name} 收到来自 ${proposal.from_agent} 的协作提议`);
// 检查是否有能力处理
const canHandle = this.capabilities.includes(proposal.task.type);
if (canHandle) {
console.log(`✅ ${this.name} 接受协作任务: ${proposal.task.type}`);
const acceptance = A2AProtocol.createTaskAcceptance(
proposal.proposal_id,
this.name,
{
completion_time: proposal.task.deadline,
quality_commitment: "high"
}
);
// 执行任务
const result = await this.executeTask(proposal.task.type, proposal.task.input_data);
// 返回结果
const delivery = A2AProtocol.createResultDelivery(
proposal.proposal_id,
result,
{
processing_time: "快速",
accuracy: "高",
resources_used: "正常"
}
);
this.collaborationHistory.push({
proposal: proposal,
acceptance: acceptance,
result: delivery,
timestamp: new Date().toISOString()
});
return delivery;
} else {
console.log(`❌ ${this.name} 无法处理此任务类型: ${proposal.task.type}`);
return { status: "rejected", reason: "能力不匹配" };
}
}
// 执行具体任务
async executeTask(taskType, inputData) {
console.log(`⚡ ${this.name} 执行任务: ${taskType}`);
await new Promise(resolve => setTimeout(resolve, 1000)); // 模拟处理时间
switch(taskType) {
case 'data_analysis':
return {
analysis_type: "statistical",
insights: ["趋势上升", "波动正常"],
recommendations: ["继续观察", "考虑调整策略"],
processed_by: this.name
};
case 'content_validation':
return {
validation_result: "通过",
issues_found: [],
confidence_score: 0.95,
validated_by: this.name
};
case 'prediction':
return {
prediction: "上涨趋势",
confidence: 0.87,
timeframe: "短期",
factors: ["市场情绪", "技术指标"],
predicted_by: this.name
};
default:
return { error: "未知任务类型", processed_by: this.name };
}
}
// 提供反馈
async provideFeedback(toAgentName, taskId, ratings, comments) {
const feedback = A2AProtocol.createFeedback(
taskId,
this.name,
toAgentName,
ratings,
comments
);
console.log(`⭐ ${this.name} 为 ${toAgentName} 提供反馈`);
return feedback;
}
// 获取代理状态报告
getStatusReport() {
return {
agent_name: this.name,
agent_id: this.agentId,
capabilities: this.capabilities,
known_agents: Array.from(this.knownAgents.keys()),
collaboration_count: this.collaborationHistory.length,
recent_activities: this.collaborationHistory.slice(-3)
};
}
}
a2aTest.js
import { IntelligentAgent } from './intelligentAgent.js';
import { A2ANetwork } from './a2aNetwork.js';
import dotenv from 'dotenv';
dotenv.config();
async function main() {
console.log('🚀 === 启动 A2A 代理网络测试 ===\n');
const privateKey = process.env.PRIVATE_KEY;
// 创建 A2A 网络
const a2aNetwork = new A2ANetwork();
// 创建具有不同能力的代理
const agentAlice = new IntelligentAgent(
'Alice',
privateKey,
['data_analysis', 'prediction']
);
const agentBob = new IntelligentAgent(
'Bob',
privateKey,
['content_validation', 'data_analysis']
);
const agentCharlie = new IntelligentAgent(
'Charlie',
privateKey,
['prediction', 'content_validation']
);
try {
// 阶段1: 代理注册
console.log('📋 阶段1: 代理注册到A2A网络');
console.log('='.repeat(50));
await agentAlice.register();
await agentBob.register();
await agentCharlie.register();
a2aNetwork.registerAgent(agentAlice);
a2aNetwork.registerAgent(agentBob);
a2aNetwork.registerAgent(agentCharlie);
// 阶段2: 代理发现
console.log('\n📋 阶段2: 代理相互发现');
console.log('='.repeat(50));
a2aNetwork.updateAgentDiscovery();
// 阶段3: 简单协作测试
console.log('\n📋 阶段3: 一对一协作测试');
console.log('='.repeat(50));
const sampleData = {
market_data: [10, 20, 15, 25, 30, 35, 40],
time_period: "7天",
indicators: ["移动平均", "RSI"]
};
// Alice 向 Bob 发起数据分析协作
await a2aNetwork.initiateCollaboration('Alice', 'Bob', 'data_analysis', sampleData);
// 阶段4: 复杂协作链
console.log('\n📋 阶段4: 多代理协作链');
console.log('='.repeat(50));
const complexData = {
content: "市场分析报告草案",
data_points: 150,
required_validation: true
};
// Bob 处理数据分析后,Charlie 进行验证
await a2aNetwork.initiateCollaboration('Bob', 'Charlie', 'content_validation', complexData);
// Alice 请求预测
const predictionData = {
historical_data: [100, 105, 98, 110, 115],
market_conditions: "波动市场"
};
await a2aNetwork.initiateCollaboration('Alice', 'Charlie', 'prediction', predictionData);
// 阶段5: 网络状态报告
console.log('\n📋 阶段5: A2A网络状态报告');
console.log('='.repeat(50));
const networkStatus = a2aNetwork.getNetworkStatus();
// 各代理状态
console.log('\n📊 各代理详细状态:');
for (const agentName of networkStatus.agent_list) {
const agentStatus = a2aNetwork.getAgentDetails(agentName);
console.log(`\n${agentName}:`);
console.log(` - 能力: ${agentStatus.capabilities.join(', ')}`);
console.log(` - 协作次数: ${agentStatus.collaboration_count}`);
console.log(` - 已知代理: ${agentStatus.known_agents.join(', ')}`);
}
console.log('\n🎉 === A2A协议测试完成 ===');
console.log('✅ 代理注册和描述文件创建');
console.log('✅ 代理发现机制工作正常');
console.log('✅ A2A消息协议实现');
console.log('✅ 多代理协作流程验证');
console.log('✅ 网络状态监控运行');
} catch (error) {
console.error('A2A测试失败:', error);
}
}
// 运行测试
main().catch(console.error);
运行结果如下:
npm run a2a-test
> erc8004-testnet-demo@1.0.0 a2a-test
> node src/a2aTest.js
[dotenv@17.2.3] injecting env (2) from .env -- tip: ⚙️ enable debug logging with { debug: true }
[dotenv@17.2.3] injecting env (0) from .env -- tip: 🔄 add secrets lifecycle management: https://dotenvx.com/ops
🚀 === 启动 A2A 代理网络测试 ===
📋 阶段1: 代理注册到A2A网络
==================================================
🤖 Alice 正在注册到A2A网络...
连接合约: AgentIdentity
当前代理数量: 8
🚀 发送注册交易...
交易哈希: 0x1e32733b1154919e0630406ec012ba9fd7a45ba5f8568088bc4e14261eac4edb
⏳ 等待交易确认...
✅ 交易确认! 区块: 9438270
新代理数量: 9
✅ Alice A2A注册完成! ID: 8
📋 能力: data_analysis, prediction
🤖 Bob 正在注册到A2A网络...
连接合约: AgentIdentity
当前代理数量: 9
🚀 发送注册交易...
交易哈希: 0xd43892314f33f25cb5c227ceed37ec8781c7a15d51eb9ffca52022a07c9de151
⏳ 等待交易确认...
✅ 交易确认! 区块: 9438271
新代理数量: 10
✅ Bob A2A注册完成! ID: 9
📋 能力: content_validation, data_analysis
🤖 Charlie 正在注册到A2A网络...
连接合约: AgentIdentity
当前代理数量: 10
🚀 发送注册交易...
交易哈希: 0x48b26a7b8619346c341cdf692c626da53ae8bfee915b9abd68f4927e2b7fd84b
⏳ 等待交易确认...
✅ 交易确认! 区块: 9438273
新代理数量: 11
✅ Charlie A2A注册完成! ID: 10
📋 能力: prediction, content_validation
🌐 A2A网络注册: Alice
🌐 A2A网络注册: Bob
🔍 Alice 发现代理: Bob
🔍 Bob 发现代理: Alice
🌐 A2A网络注册: Charlie
🔍 Alice 发现代理: Bob
🔍 Alice 发现代理: Charlie
🔍 Bob 发现代理: Alice
🔍 Bob 发现代理: Charlie
🔍 Charlie 发现代理: Alice
🔍 Charlie 发现代理: Bob
📋 阶段2: 代理相互发现
==================================================
🔍 Alice 发现代理: Bob
🔍 Alice 发现代理: Charlie
🔍 Bob 发现代理: Alice
🔍 Bob 发现代理: Charlie
🔍 Charlie 发现代理: Alice
🔍 Charlie 发现代理: Bob
📋 阶段3: 一对一协作测试
==================================================
🔄 A2A网络: Alice → Bob 协作开始
📨 Alice → Bob: 发送协作提议
📩 Bob 收到来自 Alice 的协作提议
✅ Bob 接受协作任务: data_analysis
⚡ Bob 执行任务: data_analysis
✅ 协作完成: Alice + Bob
📊 任务结果: {
analysis_type: 'statistical',
insights: [ '趋势上升', '波动正常' ],
recommendations: [ '继续观察', '考虑调整策略' ],
processed_by: 'Bob'
}
📋 阶段4: 多代理协作链
==================================================
🔄 A2A网络: Bob → Charlie 协作开始
📨 Bob → Charlie: 发送协作提议
📩 Charlie 收到来自 Bob 的协作提议
✅ Charlie 接受协作任务: content_validation
⚡ Charlie 执行任务: content_validation
✅ 协作完成: Bob + Charlie
📊 任务结果: {
validation_result: '通过',
issues_found: [],
confidence_score: 0.95,
validated_by: 'Charlie'
}
🔄 A2A网络: Alice → Charlie 协作开始
📨 Alice → Charlie: 发送协作提议
📩 Charlie 收到来自 Alice 的协作提议
✅ Charlie 接受协作任务: prediction
⚡ Charlie 执行任务: prediction
✅ 协作完成: Alice + Charlie
📊 任务结果: {
prediction: '上涨趋势',
confidence: 0.87,
timeframe: '短期',
factors: [ '市场情绪', '技术指标' ],
predicted_by: 'Charlie'
}
📋 阶段5: A2A网络状态报告
==================================================
📈 A2A网络状态报告:
代理数量: 3
代理列表: Alice, Bob, Charlie
任务总数: 3
📊 各代理详细状态:
Alice:
- 能力: data_analysis, prediction
- 协作次数: 0
- 已知代理: Bob, Charlie
Bob:
- 能力: content_validation, data_analysis
- 协作次数: 1
- 已知代理: Alice, Charlie
Charlie:
- 能力: prediction, content_validation
- 协作次数: 2
- 已知代理: Alice, Bob
🎉 === A2A协议测试完成 ===
✅ 代理注册和描述文件创建
✅ 代理发现机制工作正常
✅ A2A消息协议实现
✅ 多代理协作流程验证
✅ 网络状态监控运行
(ps. 几个代理并没有真正的并行执行,且如果有已经注册但离线不能提供服务的代理,代码里面不能识别)
原始数据
↓ [声誉分析层 - 量化信任]
声誉分数 + 元数据
↓ [TEE层 - 确保硬件层安全]
可信计算结果 + SGX证明
↓ [ZK层 - 保护隐私]
零知识证明 + 选择性披露
↓ [链上验证]
可验证的信任声明
步骤1.1:客户B发布需求
TaskRequirements {
min_reputation: 800,
tee_required: true,
zk_proof_required: true
}
步骤1.2:服务A提供基础证明
# A只需要提供ZK证明,声誉分数是预计算的
def generate_simple_qualification():
# 直接从声誉系统获取预计算的结果
precomputed_reputation = get_precomputed_reputation() # {score: 845, breaches: 0, ...}
# 仅使用ZK层生成证明
zk_proof = zk_circuit.prove(
statement="reputation_score > 800 AND data_breaches == 0",
witness=precomputed_reputation
)
return SimpleQualification(zk_proof, public_metadata)
步骤1.3:验证C快速验证
function quickVerify(SimpleQualification memory proof) public view returns (bool) {
return zkVerifier.verify(proof.zk_proof); // 快速验证
}
步骤2.1:数据在TEE中处理
# 这才是TEE的主要应用场景
def secure_data_processing(encrypted_data):
with tee_enclave():
# 解密和分析
raw_data = decrypt(encrypted_data)
analysis_result = ai_analysis(raw_data)
# 生成处理证明
processing_proof = generate_processing_attestation(
input_hash=hash(encrypted_data),
output_hash=hash(analysis_result),
code_integrity=AI_MODEL_HASH
)
return analysis_result, processing_proof
步骤3.1:结果交付
result_package = {
"insights": analysis_results,
"processing_proof": processing_proof, # TEE处理证明
"privacy_proof": generate_privacy_proof() # ZK隐私证明
}
步骤3.2:声誉系统完整更新(这才是三层架构的正确位置)
def complete_reputation_update(task_data, client_feedback, processing_proof):
# 第一层:声誉分析(收集所有数据)
performance_data = {
"task_complexity": task_data.complexity,
"processing_time": task_data.duration,
"resource_usage": task_data.resources,
"client_feedback": client_feedback,
"tee_attestation": processing_proof # 证明计算完整性
}
# 第二层:TEE安全计算新声誉分数
with tee_enclave():
# 在可信环境中重新计算声誉
new_reputation = reputation_algorithm.calculate(
current_score=845,
new_performance=performance_data
)
# 生成计算完整性证明
reputation_attestation = generate_sgx_attestation()
# 第三层:ZK生成新的资格证明
updated_zk_proof = zk_circuit.prove(
statement=f"reputation_score > {new_reputation} AND recent_performance_positive",
witness={
"new_score": new_reputation,
"performance_data": performance_data
}
)
return UpdatedReputation(new_reputation, reputation_attestation, updated_zk_proof)
-
阅读erc-8004官方标准及hashkey的文章
-
核心内容梳理:
-
智能体身份注册表:可使用erc721保证身份的唯一性
-
智能体声誉注册表:提供服务后授权评价并记录评价(链下分析评价数据,结果可回传至链上)
-
智能体验证注册表:申请并接收验证(链下验证,结果回传至链上)
-
-
创建新项目,参考IdentityRegistry.sol进行简单重写
-
拉取erc-8004-example代码
-
构建针对合约的简单测试样例
-
使用Foundry和Anvil进行简单的本地部署和测试