区块链研习社

Cosmos-- 三.教程 -- 4.Keeper

2019-02-26  本文已影响0人  糙米薏仁汤

cosmos主网即将上线,对文档做了大量更新。特地翻译了一下,方便小伙伴们阅览, 之后会持续更新

第三章教程:

  1. 开始
  2. 程序目标
  3. 开始编写你的程序
  4. Keeper
  5. Msg和Handler
  6. SetName
  7. BuyName
  8. Querier
  9. Codec文件
  10. Nameservice模块的CLI
  11. nameservice模块的REST接口
  12. 引入你的模块并完成程序
  13. Entrypoint
  14. 编译你的程序
  15. 编译并运行程序
  16. 运行REST路由

Keeper

Cosmos SDK模块的主要核心是名为Keeper的部分。它处理同存储的交互,引用其他的keeper进行跨模块的交互,并包含模块的大部分核心功能。

首先创建文件./x/nameservice/keeper.go来保存模块的keeper。在Cosmos SDK应用程序中,模块通常放在./x/文件夹中。

Keeper结构

开始制作你的SDK模块,请在./x/nameservice/keeper.go文件中定义nameservice.Keeper

package nameservice

import (
    "github.com/cosmos/cosmos-sdk/codec"
    "github.com/cosmos/cosmos-sdk/x/bank"

    sdk "github.com/cosmos/cosmos-sdk/types"
)

// Keeper maintains the link to data storage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
    coinKeeper bank.Keeper

    namesStoreKey  sdk.StoreKey // Unexposed key to access name store from sdk.Context
    ownersStoreKey sdk.StoreKey // Unexposed key to access owners store from sdk.Context
    pricesStoreKey sdk.StoreKey // Unexposed key to access prices store from sdk.Context

    cdc *codec.Codec // The wire codec for binary encoding/decoding.
}

关于上述代码的几点说明:

Getter和Setter

现在要添加通过Keeper来与存储交互的方法了。首先,添加一个函数来为指定域名设置解析字符串值:

// SetName - sets the value string that a name resolves to
func (k Keeper) SetName(ctx sdk.Context, name string, value string) {
    store := ctx.KVStore(k.namesStoreKey)
    store.Set([]byte(name), []byte(value))
}

在此方法中,首先使用Keeper中的namesStoreKey获取map[name]value的存储对象。

注意:这个函数使用sdk.Context。该对象持有访问像blockHeightchainID这样重要部分状态的函数。

接下来,你可以使用方法.Set([]byte,[]byte)向存储中插入<name, value>键值对。由于存储只接受[]byte,想要把string转化成[]byte再把它们作为参数传给Set方法。

接下来,添加一个函数来解析域名(即查找域名对应的解析值):

// ResolveName - returns the string that the name resolves to
func (k Keeper) ResolveName(ctx sdk.Context, name string) string {
    store := ctx.KVStore(k.namesStoreKey)
    bz := store.Get([]byte(name))
    return string(bz)
}

这里,与SetName方法一样,首先使用StoreKey访问存储。接下来,不使用使用.Get([] byte) []byte方法而不是Set方法。向函数传参,传递key值,要把name字符串转化成[]byte,并以[]byte的形式返回结果。将此转换成字符串再返回。

添加类似的函数来获取和设置域名所有者:

// HasOwner - returns whether or not the name already has an owner
func (k Keeper) HasOwner(ctx sdk.Context, name string) bool {
    store := ctx.KVStore(k.ownersStoreKey)
    bz := store.Get([]byte(name))
    return bz != nil
}

// GetOwner - get the current owner of a name
func (k Keeper) GetOwner(ctx sdk.Context, name string) sdk.AccAddress {
    store := ctx.KVStore(k.ownersStoreKey)
    bz := store.Get([]byte(name))
    return bz
}

// SetOwner - sets the current owner of a name
func (k Keeper) SetOwner(ctx sdk.Context, name string, owner sdk.AccAddress) {
    store := ctx.KVStore(k.ownersStoreKey)
    store.Set([]byte(name), owner)
}

注意在上述代码中:

最后需要在./x/nameservice/keeper.go文件中加上Keeper的构造函数:

// NewKeeper creates new instances of the nameservice Keeper
func NewKeeper(coinKeeper bank.Keeper, namesStoreKey sdk.StoreKey, ownersStoreKey sdk.StoreKey, priceStoreKey sdk.StoreKey, cdc *codec.Codec) Keeper {
    return Keeper{
        coinKeeper:     coinKeeper,
        namesStoreKey:  namesStoreKey,
        ownersStoreKey: ownersStoreKey,
        pricesStoreKey: priceStoreKey,
        cdc:            cdc,
    }
}
上一篇下一篇

猜你喜欢

热点阅读