Replies: 2 comments 2 replies
-
You can see here how to get an object from another key. Basically you store as many references as you need with other keys. In this case we store the validator address using the consAddr as a key, so you you the value you get as a key to another cosmos-sdk/x/staking/keeper/validator.go Lines 45 to 57 in 864ce72 |
Beta Was this translation helpful? Give feedback.
-
@facundomedica Thank you so much for the answer, I'm going forward with your guidance to see how it goes. I just have a question, please: I see this code: So, how can I find these values that marks with Thank you so much for your help |
Beta Was this translation helpful? Give feedback.
-
Fetching for other properties of an object fails. Using SDK objects would fetch by their key, meaning after setting up a key for an object, then create a Get function with passing the key to it , that object can be fetch (Get) by its key.
The requirement is to fetch objects by their other properties as well, not just by its key.
I will explain this bug by giving an example:
We have an object with the name of a client. Client object has these set of properties:
message Client { string pubKey = 1; string address = 2; string score = 4; string rewardMultiplier = 5; string type = 9; }
For storing this client object in the store by the SDK, we need to use :
func (k Keeper) SetClient(ctx sdk.Context, client types.Client) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClientKeyPrefix1)) b := k.cdc.MustMarshal(&client) store.Set(types.ClientKey( client.PubKey, ), b) }
Due to we set the public key of the Client object as a key, we can fetch Client object by public key (PubKey) later by this code:
func (k Keeper) GetClient(ctx sdk.Context, pubKey string) (val types.Client, found bool) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClientKeyPrefix)) b := store.Get(types.ClientKey( pubKey, )) if b == nil { return val, false } k.cdc.MustUnmarshal(b, &val) return val, true }
But we expect to fetch a Client object by its other properties as well, Like Client.Type. For example we need to have this GetClientByType, GetClientByAddress and etc as well.
Right now if we don't set up a key for a client we can not fetch it by its other properties and there is no possibility to set up more than key for an object. Means could fetch an object just with its key using SDK.
Is there away to fetch object from its other properties as well? I mean fetch an object by its key and other properties?
Beta Was this translation helpful? Give feedback.
All reactions