planetmint-go/x/dao/keeper/challenge.go
Jürgen Eckel 6a60b78b62
Added PoP Initialization and a Challenge Query (#207)
* added InitPoP message
* added getChallenge query to inspect challenges
* adjusted towards a unique block height identification unit: int64, not uint64
* added challenge param finished to identify challenges that weren't completed but will be part of re-issuance.

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2023-11-29 13:33:25 +01:00

31 lines
959 B
Go

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) LookupChallenge(ctx sdk.Context, height int64) (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 int64) []byte {
// Adding 1 because 0 will be interpreted as nil, which is an invalid key
return big.NewInt(height + 1).Bytes()
}