golang构建Polkadot/Substrate兼容运行时的框架插件gosemble的使用

Golang构建Polkadot/Substrate兼容运行时的框架插件Gosemble的使用

概述

Gosemble是一个用Go语言实现的与Polkadot/Substrate兼容的运行时框架。它允许开发者使用Go语言构建区块链运行时模块,同时保持与Substrate生态系统的兼容性。

Go Report Card codecov

⚠️ 警告: Gosemble目前处于预生产阶段,代码尚未经过审计。使用时需自行承担风险。

快速开始

前提条件

  • Git
  • Go 1.21
  • Docker
  • Rust (用于构建Substrate节点)

克隆仓库

git clone https://github.com/LimeChain/gosemble.git
cd gosemble

拉取所有必要的git子模块

git submodule update --init --recursive

构建

要构建运行时,执行:

make build-docker-release

启动本地网络

构建完运行时后,使用Substrate主机启动本地网络:

make start-network

运行测试

构建完运行时后,执行测试:

make test-unit
make test-integration

示例代码

以下是一个简单的Gosemble模块示例:

package example

import (
	"github.com/LimeChain/gosemble/primitives/types"
)

// ExampleModule 结构体实现了一个简单的模块
type ExampleModule struct{}

// NewExampleModule 创建新的ExampleModule实例
func NewExampleModule() *ExampleModule {
	return &ExampleModule{}
}

// Name 返回模块名称
func (m *ExampleModule) Name() string {
	return "ExampleModule"
}

// OnInitialize 在区块初始化时调用
func (m *ExampleModule) OnInitialize(n *types.BlockNumber) types.Weight {
	// 初始化逻辑
	return 0
}

// OnFinalize 在区块最终化时调用
func (m *ExampleModule) OnFinalize(n *types.BlockNumber) {
	// 最终化逻辑
}

// ExampleCall 是一个可调用的模块函数
func (m *ExampleModule) ExampleCall(param string) types.Call {
	return types.NewCall(
		"ExampleModule",
		"example_call",
		param,
	)
}

集成到运行时

要将上述模块集成到运行时中:

package runtime

import (
	"github.com/LimeChain/gosemble/frame"
	"github.com/LimeChain/gosemble/primitives/types"
	"github.com/your-org/example-module/example"
)

// NewRuntime 创建新的运行时实例
func NewRuntime() *frame.Runtime {
	runtime := frame.NewRuntime()

	// 添加系统模块
	runtime.RegisterModule(frame.NewSystemModule())

	// 添加示例模块
	exampleModule := example.NewExampleModule()
	runtime.RegisterModule(exampleModule)

	// 配置运行时元数据
	runtime.Metadata = types.NewMetadataV14(
		// 元数据配置...
	)

	return runtime
}

注意事项

  1. Gosemble仍在开发中,API可能会发生变化
  2. 生产环境使用前请确保充分测试
  3. 建议定期检查项目更新和文档变更

通过Gosemble,开发者可以利用Go语言的生态系统和工具链来构建Substrate兼容的区块链运行时,同时保持与Polkadot生态系统的互操作性。


更多关于golang构建Polkadot/Substrate兼容运行时的框架插件gosemble的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang构建Polkadot/Substrate兼容运行时的框架插件gosemble的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


使用Gosemble构建Polkadot/Substrate兼容运行时

Gosemble是一个用Go语言实现的Substrate兼容框架,允许开发者用Go构建与Polkadot生态系统兼容的区块链运行时。下面我将介绍Gosemble的基本使用方法和示例代码。

Gosemble概述

Gosemble旨在提供:

  • 与Substrate兼容的运行时环境
  • 用Go语言编写区块链逻辑
  • 可与其他Substrate链互操作

基本结构

一个典型的Gosemble运行时包含以下组件:

package main

import (
	"github.com/ChainSafe/gosemble"
	"github.com/ChainSafe/gosemble/primitives/types"
)

// 定义你的pallet
type MyPallet struct {
	// 你的pallet状态
}

// 实现必要的接口
func (p *MyPallet) Name() string {
	return "MyPallet"
}

func (p *MyPallet) Version() uint32 {
	return 1
}

func main() {
	// 初始化运行时
	runtime := gosemble.NewRuntime()

	// 注册你的pallet
	runtime.RegisterPallet(&MyPallet{})

	// 启动运行时
	if err := runtime.Start(); err != nil {
		panic(err)
	}
}

实现自定义功能

1. 定义存储项

import (
	"github.com/ChainSafe/gosemble/frame/support/storage"
)

type MyPallet struct {
	myValue storage.StorageValue[uint64]
}

func NewMyPallet() *MyPallet {
	return &MyPallet{
		myValue: storage.NewStorageValue[uint64]("MyPallet", "MyValue"),
	}
}

2. 实现可调用函数

import (
	"github.com/ChainSafe/gosemble/primitives/types"
)

func (p *MyPallet) Callables() []types.Callable {
	return []types.Callable{
		{
			Name: "set_value",
			Call: p.setValue,
			Args: []types.Arg{
				{Name: "value", Type: types.U64},
			},
		},
		{
			Name: "get_value",
			Call: p.getValue,
		},
	}
}

func (p *MyPallet) setValue(args []interface{}) (interface{}, error) {
	value := args[0].(uint64)
	p.myValue.Put(value)
	return nil, nil
}

func (p *MyPallet) getValue(args []interface{}) (interface{}, error) {
	return p.myValue.Get(), nil
}

3. 配置Genesis数据

func (p *MyPallet) Genesis() interface{} {
	return struct {
		InitialValue uint64 `json:"initialValue"`
	}{}
}

func (p *MyPallet) InitializeGenesis(genesis interface{}) error {
	config := genesis.(struct {
		InitialValue uint64 `json:"initialValue"`
	})
	p.myValue.Put(config.InitialValue)
	return nil
}

集成到运行时

func main() {
	runtime := gosemble.NewRuntime(
		gosemble.WithPallets(
			&system.Pallet{},
			&balances.Pallet{},
			NewMyPallet(),
		),
	)

	if err := runtime.Start(); err != nil {
		panic(err)
	}
}

构建和运行

  1. 安装依赖:
go get github.com/ChainSafe/gosemble
  1. 构建你的运行时:
go build -o my-runtime
  1. 运行节点:
./my-runtime --chain=local --validator

与Substrate节点交互

你可以使用Polkadot.js API或其他Substrate兼容工具与你的Gosemble运行时交互:

const { ApiPromise, WsProvider } = require('@polkadot/api');

async function main() {
  const provider = new WsProvider('ws://localhost:9944');
  const api = await ApiPromise.create({ provider });
  
  // 调用自定义pallet的方法
  const value = await api.query.myPallet.getValue();
  console.log('Current value:', value.toHuman());
}

main().catch(console.error);

注意事项

  1. Gosemble仍在开发中,API可能会有变化
  2. 性能可能不如原生Rust实现
  3. 确保遵循Substrate的运行时规范
  4. 测试与Polkadot.js和其他Substrate工具的兼容性

Gosemble为Go开发者提供了进入Polkadot生态系统的途径,使得可以利用Go语言丰富的库生态系统和开发工具链来构建区块链运行时。

回到顶部