|
| 1 | +// x/scavenge/keeper/msg_server_reveal_solution.go |
| 2 | + |
1 | 3 | package keeper |
2 | 4 |
|
3 | 5 | import ( |
4 | 6 | "context" |
| 7 | + "crypto/sha256" |
| 8 | + "encoding/hex" |
5 | 9 |
|
6 | 10 | sdk "github.com/cosmos/cosmos-sdk/types" |
| 11 | + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" |
| 12 | + "github.com/tendermint/tendermint/crypto" |
| 13 | + |
7 | 14 | "scavenge/x/scavenge/types" |
8 | 15 | ) |
9 | 16 |
|
10 | 17 | func (k msgServer) RevealSolution(goCtx context.Context, msg *types.MsgRevealSolution) (*types.MsgRevealSolutionResponse, error) { |
11 | 18 | ctx := sdk.UnwrapSDKContext(goCtx) |
12 | 19 |
|
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 | + } |
15 | 86 |
|
| 87 | + // save the udpated scavenge to the store |
| 88 | + k.SetScavenge(ctx, scavenge) |
16 | 89 | return &types.MsgRevealSolutionResponse{}, nil |
17 | 90 | } |
0 commit comments