mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 23:35:19 +00:00
add get set methods
This commit is contained in:
parent
f2e4956d88
commit
a145846ed2
@ -2,6 +2,7 @@ package keeper
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
//govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
//govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
||||||
@ -70,25 +71,53 @@ func (k Keeper) AddVote(ctx sdk.Context, msg types.MsgVote) sdk.Error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------
|
|
||||||
|
|
||||||
func (k Keeper) GetCommittee(ctx sdk.Context, committeeID uint64) (types.Committee, bool) {
|
func (k Keeper) GetCommittee(ctx sdk.Context, committeeID uint64) (types.Committee, bool) {
|
||||||
return types.Committee{}, false
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix)
|
||||||
|
bz := store.Get(types.GetKeyFromID(committeeID))
|
||||||
|
if bz == nil {
|
||||||
|
return types.Committee{}, false
|
||||||
|
}
|
||||||
|
var committee types.Committee
|
||||||
|
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &committee)
|
||||||
|
return committee, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k Keeper) SetCommittee(ctx sdk.Context, committee types.Committee) {
|
func (k Keeper) SetCommittee(ctx sdk.Context, committee types.Committee) {
|
||||||
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix)
|
||||||
}
|
bz := k.cdc.MustMarshalBinaryLengthPrefixed(committee)
|
||||||
|
store.Set(types.GetKeyFromID(committee.ID), bz)
|
||||||
func (k Keeper) GetVote(ctx sdk.Context, voteID uint64) (types.Vote, bool) {
|
|
||||||
return types.Vote{}, false
|
|
||||||
}
|
|
||||||
func (k Keeper) SetVote(ctx sdk.Context, vote types.Vote) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (types.Proposal, bool) {
|
func (k Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (types.Proposal, bool) {
|
||||||
return types.Proposal{}, false
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix)
|
||||||
|
bz := store.Get(types.GetKeyFromID(proposalID))
|
||||||
|
if bz == nil {
|
||||||
|
return types.Proposal{}, false
|
||||||
|
}
|
||||||
|
var proposal types.Proposal
|
||||||
|
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposal)
|
||||||
|
return proposal, true
|
||||||
}
|
}
|
||||||
func (k Keeper) SetProposal(ctx sdk.Context, proposal types.Proposal) {
|
|
||||||
|
|
||||||
|
func (k Keeper) SetProposal(ctx sdk.Context, proposal types.Proposal) {
|
||||||
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix)
|
||||||
|
bz := k.cdc.MustMarshalBinaryLengthPrefixed(proposal)
|
||||||
|
store.Set(types.GetKeyFromID(proposal.ID), bz)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Keeper) GetVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) (types.Vote, bool) {
|
||||||
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix)
|
||||||
|
bz := store.Get(types.GetVoteKey(proposalID, voter))
|
||||||
|
if bz == nil {
|
||||||
|
return types.Vote{}, false
|
||||||
|
}
|
||||||
|
var vote types.Vote
|
||||||
|
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &vote)
|
||||||
|
return vote, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Keeper) SetVote(ctx sdk.Context, vote types.Vote) {
|
||||||
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix)
|
||||||
|
bz := k.cdc.MustMarshalBinaryLengthPrefixed(vote)
|
||||||
|
store.Set(types.GetVoteKey(vote.ProposalID, vote.Voter), bz)
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,12 @@ package keeper_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/x/committee/types"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
abci "github.com/tendermint/tendermint/abci/types"
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
abci "github.com/tendermint/tendermint/abci/types"
|
||||||
|
|
||||||
"github.com/kava-labs/kava/app"
|
"github.com/kava-labs/kava/app"
|
||||||
"github.com/kava-labs/kava/x/committee/keeper"
|
"github.com/kava-labs/kava/x/committee/keeper"
|
||||||
@ -17,18 +19,66 @@ type KeeperTestSuite struct {
|
|||||||
keeper keeper.Keeper
|
keeper keeper.Keeper
|
||||||
app app.TestApp
|
app app.TestApp
|
||||||
ctx sdk.Context
|
ctx sdk.Context
|
||||||
|
|
||||||
|
addresses []sdk.AccAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *KeeperTestSuite) SetupTest() {
|
func (suite *KeeperTestSuite) SetupTest() {
|
||||||
|
|
||||||
suite.app = app.NewTestApp()
|
suite.app = app.NewTestApp()
|
||||||
suite.keeper = suite.app.GetCommitteeKeeper()
|
suite.keeper = suite.app.GetCommitteeKeeper()
|
||||||
suite.ctx = suite.app.NewContext(true, abci.Header{})
|
suite.ctx = suite.app.NewContext(true, abci.Header{})
|
||||||
|
_, suite.addresses = app.GeneratePrivKeyAddressPairs(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *KeeperTestSuite) TestGetSetCommittee() {
|
func (suite *KeeperTestSuite) TestGetSetCommittee() {
|
||||||
|
// test setup
|
||||||
|
com := types.Committee{
|
||||||
|
ID: 12,
|
||||||
|
// TODO other fields
|
||||||
|
}
|
||||||
|
|
||||||
|
// write and read from store
|
||||||
|
suite.keeper.SetCommittee(suite.ctx, com)
|
||||||
|
readCommittee, found := suite.keeper.GetCommittee(suite.ctx, com.ID)
|
||||||
|
|
||||||
|
// check before and after match
|
||||||
|
suite.True(found)
|
||||||
|
suite.Equal(com, readCommittee)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (suite *KeeperTestSuite) TestGetSetProposal() {
|
||||||
|
// test setup
|
||||||
|
prop := types.Proposal{
|
||||||
|
ID: 12,
|
||||||
|
// TODO other fields
|
||||||
|
}
|
||||||
|
|
||||||
|
// write and read from store
|
||||||
|
suite.keeper.SetProposal(suite.ctx, prop)
|
||||||
|
readProposal, found := suite.keeper.GetProposal(suite.ctx, prop.ID)
|
||||||
|
|
||||||
|
// check before and after match
|
||||||
|
suite.True(found)
|
||||||
|
suite.Equal(prop, readProposal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *KeeperTestSuite) TestGetSetVote() {
|
||||||
|
// test setup
|
||||||
|
vote := types.Vote{
|
||||||
|
ProposalID: 12,
|
||||||
|
Voter: suite.addresses[0],
|
||||||
|
// TODO other fields
|
||||||
|
}
|
||||||
|
|
||||||
|
// write and read from store
|
||||||
|
suite.keeper.SetVote(suite.ctx, vote)
|
||||||
|
readVote, found := suite.keeper.GetVote(suite.ctx, vote.ProposalID, vote.Voter)
|
||||||
|
|
||||||
|
// check before and after match
|
||||||
|
suite.True(found)
|
||||||
|
suite.Equal(vote, readVote)
|
||||||
|
}
|
||||||
|
|
||||||
func TestKeeperTestSuite(t *testing.T) {
|
func TestKeeperTestSuite(t *testing.T) {
|
||||||
suite.Run(t, new(KeeperTestSuite))
|
suite.Run(t, new(KeeperTestSuite))
|
||||||
|
@ -1,11 +1,18 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// ModuleName The name that will be used throughout the module
|
// ModuleName The name that will be used throughout the module
|
||||||
ModuleName = "committee"
|
ModuleName = "committee"
|
||||||
|
|
||||||
// StoreKey Top level store key where all module items will be stored
|
// StoreKey Top level store key where all module items will be stored
|
||||||
StoreKey = ModuleName
|
StoreKey = ModuleName
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// RouterKey Top level router key
|
// RouterKey Top level router key
|
||||||
RouterKey = ModuleName
|
RouterKey = ModuleName
|
||||||
@ -15,5 +22,41 @@ const (
|
|||||||
|
|
||||||
// DefaultParamspace default name for parameter store
|
// DefaultParamspace default name for parameter store
|
||||||
DefaultParamspace = ModuleName
|
DefaultParamspace = ModuleName
|
||||||
*/
|
*/
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Key prefixes
|
||||||
|
var (
|
||||||
|
CommitteeKeyPrefix = []byte{0x00} // prefix for keys that store committees
|
||||||
|
ProposalKeyPrefix = []byte{0x01} // prefix for keys that store proposals
|
||||||
|
VoteKeyPrefix = []byte{0x02} // prefix for keys that store votes
|
||||||
|
//AuctionByTimeKeyPrefix = []byte{0x01} // prefix for keys that are part of the auctionsByTime index
|
||||||
|
|
||||||
|
//NextAuctionIDKey = []byte{0x02} // key for the next auction id
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetKeyFromID returns the bytes to use as a key for a uint64 id
|
||||||
|
func GetKeyFromID(id uint64) []byte {
|
||||||
|
return uint64ToBytes(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetVoteKey(proposalID uint64, voter sdk.AccAddress) []byte {
|
||||||
|
return append(GetKeyFromID(proposalID), voter.Bytes()...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// // GetAuctionByTimeKey returns the key for iterating auctions by time
|
||||||
|
// func GetAuctionByTimeKey(endTime time.Time, auctionID uint64) []byte {
|
||||||
|
// return append(sdk.FormatTimeBytes(endTime), Uint64ToBytes(auctionID)...)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Uint64ToBytes converts a uint64 into fixed length bytes for use in store keys.
|
||||||
|
func uint64ToBytes(id uint64) []byte {
|
||||||
|
bz := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bz, uint64(id))
|
||||||
|
return bz
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uint64FromBytes converts some fixed length bytes back into a uint64.
|
||||||
|
func uint64FromBytes(bz []byte) uint64 {
|
||||||
|
return binary.BigEndian.Uint64(bz)
|
||||||
|
}
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
// A Committee is a collection of addresses that are allowed to vote and enact any governance proposal that passes their permissions.
|
// A Committee is a collection of addresses that are allowed to vote and enact any governance proposal that passes their permissions.
|
||||||
type Committee struct {
|
type Committee struct {
|
||||||
|
ID uint64 // TODO or a name?
|
||||||
Members []sdk.AccAddress
|
Members []sdk.AccAddress
|
||||||
Permissions []Permission
|
Permissions []Permission
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user