尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

DataHaven 的 Ethereum System Runtime API 深度指南:在 Substrate 运行时中查询 Ethereum 上的 Agent ID

DataHaven 的 Ethereum System Runtime API 深度指南:在 Substrate 运行时中查询 Ethereum 上的 Agent ID 区块链存储Web3【免费下载链接】datahavenAn EVM compatible Substrate chain, powered by StorageHub and secured by EigenLayer项目地址https://gitcode.com/gh_mirrors/da/datahaven点击查看免费下载本指南聚焦 DataHavenEVM 兼容的 Substrate 链由 StorageHub 驱动并由 EigenLayer 保障安全中 Snowbridge 系统运行时 API 的核心实现。我们将以 operator/pallets/system/runtime-api/README.md 为骨架深入解读ControlApi运行时 API 的声明、agent_id的底层计算逻辑、三条链mainnet/stagenet/testnet中的接入方式以及它在跨链桥体系中的实际用途。读完本文你将掌握如何查看、理解并复用以太坊侧 Agent 身份映射的完整调用链。一、这个 API 解决什么问题Agent ID 查询跨链桥场景中一条链如 Asset Hub、Bridge Hub需要在以太坊上拥有一个主权账户用于托管资产、接收消息。在 DataHaven 使用的 Snowbridge 体系中这个账户由部署在 Ethereum 上的Agent 合约承担。而ControlApi提供的agent_id函数就是完成给定一个 XCM Location → 得到唯一 Agent ID以太坊账户地址形式这一查询的运行时 API。原文档的表述非常精炼Provides an API for looking up an agent ID on Ethereum提供一个在以太坊上查找 agent ID 的 API本文将其展开为可实操、可追溯的完整技术脉络。二、API 的声明ControlApi 及其函数签名API 的声明位于 operator/pallets/system/runtime-api/src/lib.rs通过 Substrate 的sp_api::decl_runtime_apis!宏定义// SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023 Snowfork hellosnowfork.com #![cfg_attr(not(feature std), no_std)] use snowbridge_core::AgentId; use xcm::VersionedLocation; sp_api::decl_runtime_apis! { pub trait ControlApi { fn agent_id(location: VersionedLocation) - OptionAgentId; } }关键点拆解trait ControlApi运行时 API 的 trait 名称编译期由decl_runtime_apis!宏展开为节点端可调用的 RPC 方法绑定入参VersionedLocationXCM 的版本化 Location 类型用于跨版本兼容地描述一条链上的位置如全局共识、平行链 ID、账户等返回值OptionAgentId查询可能失败Location 无法转换因此返回Option而非直接返回AgentIdAgentId的实际类型在 operator/primitives/snowbridge/core/src/location.rs 中定义为pub type AgentId H256;即一个 32 字节的哈希值作为以太坊上 Agent 合约的唯一标识契约型映射。该 crate 的元信息operator/pallets/system/runtime-api/Cargo.toml显示包名为snowbridge-system-runtime-api版本 0.13.0依赖codecSCALE 编解码、snowbridge-core、sp-api与xcm并默认开启stdfeature。三、agent_id 的底层实现从 Location 到哈希agent_id的实现分两层均可在仓库中找到对应源码。3.1 运行时 API 的辅助函数位于 operator/pallets/system/src/api.rsuse snowbridge_core::AgentId; use xcm::{prelude::*, VersionedLocation}; use crate::{agent_id_of, Config}; pub fn agent_idRuntime(location: VersionedLocation) - OptionAgentId where Runtime: Config, { let location: Location location.try_into().ok()?; agent_id_of::Runtime(location).ok() }这里先将版本化的VersionedLocation解包为Location失败则返回None随后调用 pallet 内部的agent_id_of。3.2 pallet 内部的转换函数位于 operator/pallets/system/src/lib.rspub fn agent_id_ofT: Config(location: Location) - ResultH256, DispatchError { T::AgentIdOf::convert_location(location).ok_or(Error::T::LocationConversionFailed.into()) }它委托给Config中声明的关联类型AgentIdOf见同文件第 121-122 行/// Converts Location to AgentId type AgentIdOf: ConvertLocationAgentId;3.3 AgentIdOf 的默认实现HashedDescription在 operator/primitives/snowbridge/core/src/location.rs 中AgentIdOf被实现为HashedDescription类型/// Creates an AgentId from a Location. An AgentId is a unique mapping to an Agent contract on /// Ethereum which acts as the sovereign account for the Location. /// Resolves Polkadot locations (as seen by Ethereum) to unique AgentId identifiers. pub type AgentIdOf HashedDescription AgentId, ( DescribeHere, DescribeFamilyDescribeAllTerminal, DescribeGlobalPrefix(DescribeTerminus, DescribeFamilyDescribeTokenTerminal), ), ;其原理是将 XCMLocation按一组描述器DescribeHere、DescribeFamilyDescribeAllTerminal、DescribeGlobalPrefix...序列化后做哈希运算得到唯一的H256标识。从源码注释可以看出这个 AgentId 是对以太坊上作为该 Location 主权账户的 Agent 合约的唯一映射且按以太坊视角解析 Polkadot 位置。这正是跨链消息路由、资产管理中链→以太坊账户映射的标准做法。四、运行时接入三套网络的 impl_runtime_apisControlApi只是 trait 声明真正可被节点调用需要在运行时实现。DataHaven 的三套运行时mainnet、stagenet、testnet均已接入operator/runtime/mainnet/src/lib.rsoperator/runtime/stagenet/src/lib.rsoperator/runtime/testnet/src/lib.rs三者实现方式完全一致例如 stagenetimpl snowbridge_system_v2_runtime_api::ControlV2ApiBlock for Runtime { fn agent_id(location: VersionedLocation) - OptionAgentId { snowbridge_pallet_system_v2::api::agent_id::Runtime(location) } }注意运行时实现中使用的是ControlV2ApiSnowbridge v2 命名空间下的封装内部直接转调snowbridge_pallet_system_v2::api::agent_id::Runtime(location)即上文第三节的辅助函数。这种API 声明 crate → pallet 辅助函数 → 运行时 impl的三层结构是 Substrate 运行时 API 的标准组织方式方便节点端通过 RPCstate_call/ 专门的 API 调用在链下查询状态。五、agent_id 的实际使用场景注册 Agent 与消息路由agent_id并非孤立存在它在 pallet 的初始化和消息处理流程中承担关键角色。从 operator/pallets/system/src/lib.rs 可以看到Agent 注册存储第 220 行定义了Agents存储映射StorageMap_, Twox64Concat, AgentId, (), OptionQuery以AgentId为键记录已注册的 Agent注册流程第 459-488 行附近初始化时调用agent_id_of::T(asset_hub_location)计算 Asset Hub 的asset_hub_agent_id并插入Agents随后计算 Bridge Hub 的bridge_hub_agent_id agent_id_of::T(Location::here())并插入相关事件agent_id: asset_hub_agent_id等随之发出单元测试佐证operator/pallets/system/src/tests.rs 中通过make_agent_id(origin_location)生成agent_id并断言其匹配operator/pallets/system/src/mock.rs 中的make_agent_id正是通过Test as snowbridge_system::Config::AgentIdOf::convert_location(location)复算验证了哈希映射的确定性。六、Agent ID 计算的确定性验证仓库中的运行时测试直接印证了 Agent ID 计算的确定性与可复现性。以 operator/runtime/testnet/src/configs/mod.rs 中的test_rewards_agent_id_computation为例stagenet 的 operator/runtime/stagenet/src/configs/mod.rs 亦同let computed_agent_id H256(blake2_256(encoded)); let expected_agent_id H256(hex_literal::hex!(...)); assert_eq!(computed_agent_id, expected_agent_id);测试先按给定编码规则计算blake2_256哈希得到computed_agent_id再与硬编码的expected_agent_id比对。这说明 Agent ID 的生成路径在 XCM Location 编码与哈希算法上均有明确的预期值约束是跨链合约侧Ethereum 上的 Agent 合约地址推导与链侧必须严格对齐的契约点。七、总结与延伸阅读ControlApi::agent_id是 DataHaven / Snowbridge 体系中Location ↔ 以太坊 Agent 账户映射的查询入口其完整链路为VersionedLocation → Locationapi.rs 解包 → agent_id_of::Tpallet 内转换 → T::AgentIdOf::convert_locationHashedDescription 哈希 → AgentId H256以太坊 Agent 合约的唯一标识API 声明查看 operator/pallets/system/runtime-api/src/lib.rs辅助实现查看 operator/pallets/system/src/api.rs 与 operator/pallets/system/src/lib.rs类型定义查看 operator/primitives/snowbridge/core/src/location.rs运行时接入查看 operator/runtime/stagenet/src/lib.rsmainnet、testnet 相同测试验证查看 operator/pallets/system/src/tests.rs、operator/runtime/testnet/src/configs/mod.rs如果你想在 DataHaven 节点上查询某个 Location 对应的 Agent ID可以基于上述ControlApi生成对应的运行时 API 调用例如通过 Substrate 的 state_call 机制或在本地测试网中参照test_rewards_agent_id_computation的方式复算验证。这为理解 DataHaven 的跨链 Agent 账户体系、以及在 Ethereum 侧定位 Agent 合约提供了坚实的技术起点。赞分享区块链存储Web3【免费下载链接】datahavenAn EVM compatible Substrate chain, powered by StorageHub and secured by EigenLayer项目地址https://gitcode.com/gh_mirrors/da/datahaven点击查看免费下载相关推荐Substrate 中的 FRAME NFTs Runtime API 详解NftsApi 的声明、实现与链上查询Substrate 中的 FRAME NFTs Runtime API 详解NftsApi 的声明、实现与链上查询 导读 本文以 Substrate 仓库中区块链开发框架后端ChatOllama Agent Runtime 深度指南基于 Vercel AI SDK 的可移植 Agent 会话运行时ChatOllama Agent Runtime 深度指南基于 Vercel AI SDK 的可移植 Agent 会话运行时 导读 packages/agenGraalVM Native Image 运行时模块系统支持Runtime Module System深度解析GraalVM Native Image 运行时模块系统支持Runtime Module System深度解析 Native Image 在构建可执行镜像时编译器JIT编译语言运行时高性能计算内存管理上一篇Qix技术学习资源宝库开发者必备的终极学习指南 下一篇LeRobot 机器人框架实操SO-100 从录数据到跑通 ACT 策略创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表