Skip to content

Commit

Permalink
created vote and value struct for keys
Browse files Browse the repository at this point in the history
  • Loading branch information
scottburch committed Mar 23, 2021
1 parent 61ba098 commit 36f3828
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 48 deletions.
22 changes: 17 additions & 5 deletions x/oracle/keeper/keeper_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

type ValueKey struct {
Batch string
SourceName string
}

func (vk ValueKey) Bytes() []byte {
return []byte(fmt.Sprintf("%s>%s", vk.Batch, vk.SourceName))
}


func (k Keeper) RegisterValueUpdatedListener(listener types.ValueUpdateListener) {
valueUpdateListeners = append(valueUpdateListeners, listener)
}
Expand Down Expand Up @@ -39,7 +49,11 @@ func (k Keeper) UpdateSourceValue(ctx sdk.Context, batch string, sourceName stri
weight = source.Weight
}

key := MakeSourceValueKey(batch, sourceName)
key := ValueKey{
Batch: batch,
SourceName: sourceName,
}.Bytes()

sourceValue := types.SourceValue{
SourceName: sourceName,
Batch: batch,
Expand All @@ -49,16 +63,14 @@ func (k Keeper) UpdateSourceValue(ctx sdk.Context, batch string, sourceName stri
Count: int64(len(votes)),
Weight: weight,
}
store.Set([]byte(key), k.cdc.MustMarshalBinaryBare(sourceValue))
store.Set(key, k.cdc.MustMarshalBinaryBare(sourceValue))

for _, listener := range valueUpdateListeners {
listener(ctx, sourceValue)
}
}

func MakeSourceValueKey(batch string, sourceName string) string {
return fmt.Sprintf("%s>%s", batch, sourceName)
}



func calculateAverageFromVotes(votes []types.Vote) sdk.Dec {
Expand Down
29 changes: 22 additions & 7 deletions x/oracle/keeper/keeper_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,34 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)


type VoteKey struct {
Batch string
SourceName string
Valcons string
}

func voteToVoteKey(vote types.Vote) VoteKey {
return VoteKey{
Batch: vote.Batch,
SourceName: vote.SourceName,
Valcons: vote.Valcons,
}
}

func (vk VoteKey) Bytes() []byte {
return []byte(fmt.Sprintf("%s>%s>%s", vk.Batch, vk.SourceName, vk.Valcons))
}

func (k Keeper) GetVoteStore(ctx sdk.Context) sdk.KVStore {
return ctx.KVStore(k.voteStoreKey)
}

func CreateVoteKey(vote types.Vote) string {
return fmt.Sprintf("%s>%s>%s", vote.Batch, vote.SourceName, vote.Valcons)
}

func (k Keeper) StoreVote(ctx sdk.Context, vote types.Vote) string {
key := CreateVoteKey(vote)
func (k Keeper) StoreVote(ctx sdk.Context, vote types.Vote) {
key := voteToVoteKey(vote).Bytes()
store := k.GetVoteStore(ctx)
store.Set([]byte(key), k.cdc.MustMarshalBinaryBare(vote))
return key
store.Set(key, k.cdc.MustMarshalBinaryBare(vote))
}

func (k Keeper) DeleteVotes(ctx sdk.Context, prefix string) int {
Expand Down
36 changes: 0 additions & 36 deletions x/oracle/keeper/keeper_vote_test.go

This file was deleted.

0 comments on commit 36f3828

Please sign in to comment.