91 implement challenge store (#95)

* ignite scaffold type challenge initiator challenger challangee result description --module dao
* add height to challenge
* implement challenge store
* rename challenge result to success

---------

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-09-27 14:00:51 +02:00 committed by GitHub
parent 6718f289a9
commit d978e9da56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 648 additions and 9 deletions

View File

@ -315,7 +315,7 @@ func New(
machinemoduletypes.StoreKey, machinemoduletypes.TAIndexKey, machinemoduletypes.IssuerPlanetmintIndexKey, machinemoduletypes.IssuerLiquidIndexKey,
machinemoduletypes.TrustAnchorKey, machinemoduletypes.AddressIndexKey,
assetmoduletypes.StoreKey,
daomoduletypes.StoreKey,
daomoduletypes.StoreKey, daomoduletypes.ChallengeKey,
// this line is used by starport scaffolding # stargate/app/storeKey
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
@ -566,6 +566,7 @@ func New(
appCodec,
keys[daomoduletypes.StoreKey],
keys[daomoduletypes.MemStoreKey],
keys[daomoduletypes.ChallengeKey],
app.GetSubspace(daomoduletypes.ModuleName),
app.BankKeeper,

View File

@ -0,0 +1,14 @@
syntax = "proto3";
package planetmintgo.dao;
option go_package = "github.com/planetmint/planetmint-go/x/dao/types";
message Challenge {
string initiator = 1;
string challenger = 2;
string challangee = 3;
uint64 height = 4;
bool success = 5;
string description = 6;
}

View File

@ -20,11 +20,14 @@ import (
func DaoKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
challengeStoreKey := storetypes.NewMemoryStoreKey(types.ChallengeKey)
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
stateStore.MountStoreWithDB(challengeStoreKey, storetypes.StoreTypeIAVL, db)
require.NoError(t, stateStore.LoadLatestVersion())
registry := codectypes.NewInterfaceRegistry()
@ -40,6 +43,7 @@ func DaoKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
cdc,
storeKey,
memStoreKey,
challengeStoreKey,
paramsSubspace,
nil,
nil,

30
x/dao/keeper/challenge.go Normal file
View File

@ -0,0 +1,30 @@
package keeper
import (
"math/big"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/planetmint/planetmint-go/x/dao/types"
)
func (k Keeper) StoreChallenge(ctx sdk.Context, challenge types.Challenge) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ChallengeKey))
appendValue := k.cdc.MustMarshal(&challenge)
store.Set(getChallengeBytes(challenge.Height), appendValue)
}
func (k Keeper) GetChallenge(ctx sdk.Context, height uint64) (val types.Challenge, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ChallengeKey))
challenge := store.Get(getChallengeBytes(height))
if challenge == nil {
return val, false
}
k.cdc.MustUnmarshal(challenge, &val)
return val, true
}
func getChallengeBytes(height uint64) []byte {
// Adding 1 because 0 will be interpreted as nil, which is an invalid key
return big.NewInt(int64(height + 1)).Bytes()
}

View File

@ -0,0 +1,37 @@
package keeper_test
import (
"fmt"
"testing"
keepertest "github.com/planetmint/planetmint-go/testutil/keeper"
"github.com/stretchr/testify/assert"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/planetmint/planetmint-go/x/dao/keeper"
"github.com/planetmint/planetmint-go/x/dao/types"
)
func createNChallenge(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Challenge {
items := make([]types.Challenge, n)
for i := range items {
items[i].Height = uint64(i)
items[i].Initiator = fmt.Sprintf("initiator%v", i)
items[i].Challenger = fmt.Sprintf("challanger%v", i)
items[i].Challangee = fmt.Sprintf("challangee%v", i)
items[i].Success = true
items[i].Description = fmt.Sprintf("expected %v got %v", i, i)
keeper.StoreChallenge(ctx, items[i])
}
return items
}
func TestGetChallenge(t *testing.T) {
keeper, ctx := keepertest.DaoKeeper(t)
items := createNChallenge(keeper, ctx, 10)
for _, item := range items {
challenge, found := keeper.GetChallenge(ctx, item.Height)
assert.True(t, found)
assert.Equal(t, item, challenge)
}
}

View File

@ -18,10 +18,11 @@ import (
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
challengeKey storetypes.StoreKey
paramstore paramtypes.Subspace
bankKeeper types.BankKeeper
accountKeeper types.AccountKeeper
@ -32,6 +33,7 @@ func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
challengeKey storetypes.StoreKey,
ps paramtypes.Subspace,
bankKeeper types.BankKeeper,
@ -43,10 +45,11 @@ func NewKeeper(
}
return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
challengeKey: challengeKey,
paramstore: ps,
bankKeeper: bankKeeper,
accountKeeper: accountKeeper,

548
x/dao/types/challenge.pb.go Normal file
View File

@ -0,0 +1,548 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: planetmintgo/dao/challenge.proto
package types
import (
fmt "fmt"
proto "github.com/cosmos/gogoproto/proto"
io "io"
math "math"
math_bits "math/bits"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
type Challenge struct {
Initiator string `protobuf:"bytes,1,opt,name=initiator,proto3" json:"initiator,omitempty"`
Challenger string `protobuf:"bytes,2,opt,name=challenger,proto3" json:"challenger,omitempty"`
Challangee string `protobuf:"bytes,3,opt,name=challangee,proto3" json:"challangee,omitempty"`
Height uint64 `protobuf:"varint,4,opt,name=height,proto3" json:"height,omitempty"`
Success bool `protobuf:"varint,5,opt,name=success,proto3" json:"success,omitempty"`
Description string `protobuf:"bytes,6,opt,name=description,proto3" json:"description,omitempty"`
}
func (m *Challenge) Reset() { *m = Challenge{} }
func (m *Challenge) String() string { return proto.CompactTextString(m) }
func (*Challenge) ProtoMessage() {}
func (*Challenge) Descriptor() ([]byte, []int) {
return fileDescriptor_04a15978d0195742, []int{0}
}
func (m *Challenge) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *Challenge) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_Challenge.Marshal(b, m, deterministic)
} else {
b = b[:cap(b)]
n, err := m.MarshalToSizedBuffer(b)
if err != nil {
return nil, err
}
return b[:n], nil
}
}
func (m *Challenge) XXX_Merge(src proto.Message) {
xxx_messageInfo_Challenge.Merge(m, src)
}
func (m *Challenge) XXX_Size() int {
return m.Size()
}
func (m *Challenge) XXX_DiscardUnknown() {
xxx_messageInfo_Challenge.DiscardUnknown(m)
}
var xxx_messageInfo_Challenge proto.InternalMessageInfo
func (m *Challenge) GetInitiator() string {
if m != nil {
return m.Initiator
}
return ""
}
func (m *Challenge) GetChallenger() string {
if m != nil {
return m.Challenger
}
return ""
}
func (m *Challenge) GetChallangee() string {
if m != nil {
return m.Challangee
}
return ""
}
func (m *Challenge) GetHeight() uint64 {
if m != nil {
return m.Height
}
return 0
}
func (m *Challenge) GetSuccess() bool {
if m != nil {
return m.Success
}
return false
}
func (m *Challenge) GetDescription() string {
if m != nil {
return m.Description
}
return ""
}
func init() {
proto.RegisterType((*Challenge)(nil), "planetmintgo.dao.Challenge")
}
func init() { proto.RegisterFile("planetmintgo/dao/challenge.proto", fileDescriptor_04a15978d0195742) }
var fileDescriptor_04a15978d0195742 = []byte{
// 238 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x90, 0x31, 0x4e, 0xc4, 0x30,
0x14, 0x44, 0x63, 0x58, 0x02, 0x31, 0x0d, 0x72, 0x81, 0x5c, 0x20, 0xcb, 0xa2, 0x4a, 0x43, 0x5c,
0x70, 0x03, 0xa8, 0x68, 0x53, 0xd2, 0x79, 0x1d, 0xcb, 0xb1, 0x94, 0xf5, 0x8f, 0xe2, 0xbf, 0x12,
0xdc, 0x82, 0x0b, 0xd1, 0x53, 0x6e, 0x49, 0x89, 0x92, 0x8b, 0x20, 0x59, 0xbb, 0x1b, 0x77, 0x7f,
0xde, 0x8c, 0x46, 0xfa, 0x43, 0xe5, 0x38, 0xe8, 0x60, 0x71, 0xe7, 0x03, 0x3a, 0x50, 0x9d, 0x06,
0x65, 0x7a, 0x3d, 0x0c, 0x36, 0x38, 0xdb, 0x8c, 0x13, 0x20, 0xb0, 0xbb, 0x3c, 0xd1, 0x74, 0x1a,
0x1e, 0xbf, 0x09, 0xad, 0x5e, 0x4f, 0x29, 0xf6, 0x40, 0x2b, 0x1f, 0x3c, 0x7a, 0x8d, 0x30, 0x71,
0x22, 0x49, 0x5d, 0xb5, 0x2b, 0x60, 0x82, 0xd2, 0x73, 0xe1, 0xc4, 0x2f, 0x92, 0x9d, 0x91, 0xb3,
0xaf, 0x83, 0xb3, 0x96, 0x5f, 0x66, 0x7e, 0x22, 0xec, 0x9e, 0x96, 0xbd, 0xf5, 0xae, 0x47, 0xbe,
0x91, 0xa4, 0xde, 0xb4, 0x47, 0xc5, 0x38, 0xbd, 0x8e, 0x7b, 0x63, 0x6c, 0x8c, 0xfc, 0x4a, 0x92,
0xfa, 0xa6, 0x3d, 0x49, 0x26, 0xe9, 0x6d, 0x67, 0xa3, 0x99, 0xfc, 0x88, 0x1e, 0x02, 0x2f, 0x53,
0x65, 0x8e, 0x5e, 0xde, 0x7e, 0x66, 0x41, 0x0e, 0xb3, 0x20, 0x7f, 0xb3, 0x20, 0x5f, 0x8b, 0x28,
0x0e, 0x8b, 0x28, 0x7e, 0x17, 0x51, 0xbc, 0x2b, 0xe7, 0xb1, 0xdf, 0x6f, 0x1b, 0x03, 0x3b, 0xb5,
0xbe, 0x9d, 0x9d, 0x4f, 0x0e, 0xd4, 0x47, 0x9a, 0x09, 0x3f, 0x47, 0x1b, 0xb7, 0x65, 0xda, 0xe8,
0xf9, 0x3f, 0x00, 0x00, 0xff, 0xff, 0x46, 0x8a, 0xdb, 0xcf, 0x47, 0x01, 0x00, 0x00,
}
func (m *Challenge) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBuffer(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *Challenge) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *Challenge) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
if len(m.Description) > 0 {
i -= len(m.Description)
copy(dAtA[i:], m.Description)
i = encodeVarintChallenge(dAtA, i, uint64(len(m.Description)))
i--
dAtA[i] = 0x32
}
if m.Success {
i--
if m.Success {
dAtA[i] = 1
} else {
dAtA[i] = 0
}
i--
dAtA[i] = 0x28
}
if m.Height != 0 {
i = encodeVarintChallenge(dAtA, i, uint64(m.Height))
i--
dAtA[i] = 0x20
}
if len(m.Challangee) > 0 {
i -= len(m.Challangee)
copy(dAtA[i:], m.Challangee)
i = encodeVarintChallenge(dAtA, i, uint64(len(m.Challangee)))
i--
dAtA[i] = 0x1a
}
if len(m.Challenger) > 0 {
i -= len(m.Challenger)
copy(dAtA[i:], m.Challenger)
i = encodeVarintChallenge(dAtA, i, uint64(len(m.Challenger)))
i--
dAtA[i] = 0x12
}
if len(m.Initiator) > 0 {
i -= len(m.Initiator)
copy(dAtA[i:], m.Initiator)
i = encodeVarintChallenge(dAtA, i, uint64(len(m.Initiator)))
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func encodeVarintChallenge(dAtA []byte, offset int, v uint64) int {
offset -= sovChallenge(v)
base := offset
for v >= 1<<7 {
dAtA[offset] = uint8(v&0x7f | 0x80)
v >>= 7
offset++
}
dAtA[offset] = uint8(v)
return base
}
func (m *Challenge) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = len(m.Initiator)
if l > 0 {
n += 1 + l + sovChallenge(uint64(l))
}
l = len(m.Challenger)
if l > 0 {
n += 1 + l + sovChallenge(uint64(l))
}
l = len(m.Challangee)
if l > 0 {
n += 1 + l + sovChallenge(uint64(l))
}
if m.Height != 0 {
n += 1 + sovChallenge(uint64(m.Height))
}
if m.Success {
n += 2
}
l = len(m.Description)
if l > 0 {
n += 1 + l + sovChallenge(uint64(l))
}
return n
}
func sovChallenge(x uint64) (n int) {
return (math_bits.Len64(x|1) + 6) / 7
}
func sozChallenge(x uint64) (n int) {
return sovChallenge(uint64((x << 1) ^ uint64((int64(x) >> 63))))
}
func (m *Challenge) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowChallenge
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: Challenge: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: Challenge: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Initiator", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowChallenge
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthChallenge
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthChallenge
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Initiator = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 2:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Challenger", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowChallenge
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthChallenge
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthChallenge
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Challenger = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 3:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Challangee", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowChallenge
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthChallenge
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthChallenge
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Challangee = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 4:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field Height", wireType)
}
m.Height = 0
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowChallenge
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
m.Height |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
case 5:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field Success", wireType)
}
var v int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowChallenge
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
v |= int(b&0x7F) << shift
if b < 0x80 {
break
}
}
m.Success = bool(v != 0)
case 6:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Description", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowChallenge
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthChallenge
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthChallenge
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Description = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipChallenge(dAtA[iNdEx:])
if err != nil {
return err
}
if (skippy < 0) || (iNdEx+skippy) < 0 {
return ErrInvalidLengthChallenge
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func skipChallenge(dAtA []byte) (n int, err error) {
l := len(dAtA)
iNdEx := 0
depth := 0
for iNdEx < l {
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowChallenge
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
wireType := int(wire & 0x7)
switch wireType {
case 0:
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowChallenge
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
iNdEx++
if dAtA[iNdEx-1] < 0x80 {
break
}
}
case 1:
iNdEx += 8
case 2:
var length int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowChallenge
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
length |= (int(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
if length < 0 {
return 0, ErrInvalidLengthChallenge
}
iNdEx += length
case 3:
depth++
case 4:
if depth == 0 {
return 0, ErrUnexpectedEndOfGroupChallenge
}
depth--
case 5:
iNdEx += 4
default:
return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
}
if iNdEx < 0 {
return 0, ErrInvalidLengthChallenge
}
if depth == 0 {
return iNdEx, nil
}
}
return 0, io.ErrUnexpectedEOF
}
var (
ErrInvalidLengthChallenge = fmt.Errorf("proto: negative length found during unmarshaling")
ErrIntOverflowChallenge = fmt.Errorf("proto: integer overflow")
ErrUnexpectedEndOfGroupChallenge = fmt.Errorf("proto: unexpected end of group")
)

View File

@ -12,6 +12,8 @@ const (
// MemStoreKey defines the in-memory store key
MemStoreKey = "mem_dao"
ChallengeKey = "Dao/Challenge"
)
func KeyPrefix(p string) []byte {