Skip to content

Commit a978009

Browse files
author
nashqueue
committed
added keeper reveal solution
1 parent c08f663 commit a978009

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed
Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,90 @@
1+
// x/scavenge/keeper/msg_server_reveal_solution.go
2+
13
package keeper
24

35
import (
46
"context"
7+
"crypto/sha256"
8+
"encoding/hex"
59

610
sdk "github.com/cosmos/cosmos-sdk/types"
11+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
12+
"github.com/tendermint/tendermint/crypto"
13+
714
"scavenge/x/scavenge/types"
815
)
916

1017
func (k msgServer) RevealSolution(goCtx context.Context, msg *types.MsgRevealSolution) (*types.MsgRevealSolutionResponse, error) {
1118
ctx := sdk.UnwrapSDKContext(goCtx)
1219

13-
// TODO: Handling the message
14-
_ = ctx
20+
// concatenate a solution and a scavenger address and convert it to bytes
21+
var solutionScavengerBytes = []byte(msg.Solution + msg.Creator)
22+
23+
// find the hash of solution and address
24+
var solutionScavengerHash = sha256.Sum256(solutionScavengerBytes)
25+
26+
// convert the hash to a string
27+
var solutionScavengerHashString = hex.EncodeToString(solutionScavengerHash[:])
28+
29+
// try getting a commit using the the hash of solution and address
30+
_, isFound := k.GetCommit(ctx, solutionScavengerHashString)
31+
32+
// return an error if a commit doesn't exist
33+
if !isFound {
34+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Commit with that hash doesn't exists")
35+
}
36+
37+
// find a hash of the solution
38+
var solutionHash = sha256.Sum256([]byte(msg.Solution))
39+
40+
// encode the solution hash to string
41+
var solutionHashString = hex.EncodeToString(solutionHash[:])
42+
var scavenge types.Scavenge
43+
44+
// get a scavenge from the stre using the solution hash
45+
scavenge, isFound = k.GetScavenge(ctx, solutionHashString)
46+
47+
// return an error if the solution doesn't exist
48+
if !isFound {
49+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Scavenge with that solution hash doesn't exists")
50+
}
51+
52+
// check that the scavenger property contains a valid address
53+
_, err := sdk.AccAddressFromBech32(scavenge.Scavenger)
54+
55+
// return an error if a scavenge has already been solved
56+
if err == nil {
57+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Scavenge has already been solved")
58+
}
59+
60+
// save the scavebger address to the scavenge
61+
scavenge.Scavenger = msg.Creator
62+
63+
// save the correct solution to the scavenge
64+
scavenge.Solution = msg.Solution
65+
66+
// get address of the module account
67+
moduleAcct := sdk.AccAddress(crypto.AddressHash([]byte(types.ModuleName)))
68+
69+
// convert scavenger address from string to sdk.AccAddress
70+
scavenger, err := sdk.AccAddressFromBech32(scavenge.Scavenger)
71+
if err != nil {
72+
panic(err)
73+
}
74+
75+
// parse tokens from a string to sdk.Coins
76+
reward, err := sdk.ParseCoinsNormalized(scavenge.Reward)
77+
if err != nil {
78+
panic(err)
79+
}
80+
81+
// send tokens from a module account to the scavenger
82+
sdkError := k.bankKeeper.SendCoins(ctx, moduleAcct, scavenger, reward)
83+
if sdkError != nil {
84+
return nil, sdkError
85+
}
1586

87+
// save the udpated scavenge to the store
88+
k.SetScavenge(ctx, scavenge)
1689
return &types.MsgRevealSolutionResponse{}, nil
1790
}

0 commit comments

Comments
 (0)