kvstore

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

README

App开发步骤

  1. 自定义Tx

type KvstoreTx struct {
	Key   []byte
	Value []byte
	Bytes []byte
}

  1. 创建Mapper封装Tx操作

const KvKVStoreName = "kvmapper"

func NewKvMapper() *KvMapper {
	var txMapper = KvMapper{}
	txMapper.BaseMapper = mapper.NewBaseMapper(nil, KvKVStoreName)
	return &txMapper
}

func (mapper *KvMapper) Copy() mapper.IMapper {
	cpyMapper := &KvMapper{}
	cpyMapper.BaseMapper = mapper.BaseMapper.Copy()
	return cpyMapper
}

var _ mapper.IMapper = (*KvMapper)(nil)

func (mapper *KvMapper) SaveKV(key string, value string) {
	mapper.Set([]byte(key), value)
}

func (mapper *KvMapper) GetKey(key string) (v string) {
	mapper.Get([]byte(key), &v)
	return
}

  1. 自定义Tx实现txs.ITx接口

func (kv *KvstoreTx) ValidateData(ctx context.Context) bool {
	if len(kv.Key) < 0 {
		return false
	}
	return true
}

func (kv *KvstoreTx) Exec(ctx context.Context) (result types.Result, crossTxQcps *txs.TxQcp) {
	//获取注册的mapper:
	kvMapper := ctx.Mapper(KvMapperName).(*KvMapper)
	kvMapper.SaveKV(key, string(kv.Value))
	return
}

func (kv *KvstoreTx) GetSigner() []types.Address {
	return nil
}

func (kv *KvstoreTx) CalcGas() types.BigInt {
	return types.ZeroInt()
}

func (kv *KvstoreTx) GetGasPayer() types.Address {
	return types.Address{}
}

func (kv *KvstoreTx) GetSignData() []byte {
	return nil
}

  1. 创建BaseApp,注册cdc编码,注册Mapper

db, err := dbm.NewGoLevelDB("kvstore", filepath.Join("", "data"))

var baseapp = baseabci.NewBaseApp("kvstore", logger, db, func(cdc *go_amino.Codec){
  //将自定义的Tx注册到cdc编码中
  cdc.RegisterConcrete(&KvstoreTx{}, "kvstore/main/kvstoretx", nil)
})


//注册自定义的Mapper
	var kvMapper = kvstore.NewKvMapper(store.NewKVStoreKey("kv"))
	baseapp.RegisterMapper(kvMapper)

//加载kvstore
baseapp.LoadLatestVersion()

  1. 启动app

	// Start the ABCI server
  srv, err := server.NewServer("0.0.0.0:26658", "socket", baseapp)

  srv.Start()

启动

kvstore示例仅实现abci app,没有集成server包。参考basecoin集成server

  1. 启动tendermint

tendermint init
tendermint node
  1. 启动kvstore服务端
cd example/kvstore/cmd/kvstored
go build
./kvstored

客户端执行查询及发送命令

cd example/kvstore/cmd/kvstorecli
go build
  1. 查询key值:
./kvstorecli -k abc

  1. 设置key值:

./kvstorecli -m set -k abc -v 111

Documentation

Index

Constants

View Source
const KVMapperName = "kv"

Variables

This section is empty.

Functions

func MakeKVStoreCodec added in v0.0.2

func MakeKVStoreCodec() *go_amino.Codec

func RegisterCodec added in v0.0.2

func RegisterCodec(cdc *go_amino.Codec)

Types

type KvMapper

type KvMapper struct {
	*mapper.BaseMapper
}

func NewKvMapper

func NewKvMapper() *KvMapper

func (*KvMapper) Copy

func (mapper *KvMapper) Copy() mapper.IMapper

func (*KvMapper) GetKey

func (mapper *KvMapper) GetKey(key string) (v string)

func (*KvMapper) SaveKV

func (mapper *KvMapper) SaveKV(key string, value string)

type KvstoreTx

type KvstoreTx struct {
	Key   []byte
	Value []byte
	Bytes []byte
}

func NewKvstoreTx

func NewKvstoreTx(key []byte, value []byte) KvstoreTx

func (KvstoreTx) CalcGas

func (kv KvstoreTx) CalcGas() types.BigInt

func (KvstoreTx) Exec

func (kv KvstoreTx) Exec(ctx context.Context) (result types.Result, crossTxQcps *txs.TxQcp)

func (KvstoreTx) GetGasPayer

func (kv KvstoreTx) GetGasPayer() types.Address

func (KvstoreTx) GetSignData

func (kv KvstoreTx) GetSignData() []byte

func (KvstoreTx) GetSigner

func (kv KvstoreTx) GetSigner() []types.Address

func (KvstoreTx) ValidateData

func (kv KvstoreTx) ValidateData(ctx context.Context) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL