快速开始
安装
go get github.com/yu-org/yu
开发一个链上的 Execution 和 Query
import (
"github.com/yu-org/yu/apps/pow"
. "github.com/yu-org/yu/core/types"
"github.com/yu-org/yu/chain_env"
"github.com/yu-org/yu/common"
"github.com/yu-org/yu/context"
"github.com/yu-org/yu/startup"
"github.com/yu-org/yu/tripod"
)
type Example struct {
*tripod.DefaultTripod
}
// 此处定制开发一个 Execution
func (e *Example) Exec(ctx *context.Context, _ *CompactBlock) error {
caller := ctx.Caller
// 将数据存入链上状态中。
e.State.Set(e, caller.Bytes(), []byte("yu"))
// 向链外发射一个event
return ctx.EmitEvent("execute success")
}
// 此处定制开发一个 Query
func (e *Example) Qry(ctx *context.Context, env *chain_env.ChainEnv, _ common.Hash) (interface{}, error) {
caller := ctx.Caller
value, err := e.State.Get(e, caller.Bytes())
return string(value), err
}
在main函数中添加 tripods
func NewExample() *Example {
df := tripod.NewDefaultTripod("example")
e := &Example{df}
// 此处需要手动将自定义的 Execution 注册到 tripod 中,
// 这里的 10 表示设置该操作所需要消耗的 lei 的数量 (lei 和 gas 同义)
e.SetExec(e.Exec, 10)
// 此处需要手动将自定义的 Query 注册到 tripod 中
e.SetQueries(e.Qry)
return e
}
func main() {
startup.StartUp(pow.NewPow(1024), NewExample())
}
运行
go build -o yu-example
// -dc=true 表示使用默认配置,无需另行配置文件。
./yu-example -dc=true
至此,一条区块链便启动起来了。 后续可以加入更多区块链节点来构建区块链网络。