Skip to content

Commit

Permalink
Change the datahash
Browse files Browse the repository at this point in the history
to use a hex encoded protobuf instead of sorted json
to be more cross language friendly.
  • Loading branch information
zmanian committed Aug 26, 2021
1 parent 6fd250f commit e66b5c5
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions x/allocation/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package keeper
import (
"bytes"
"context"
"encoding/json"
"encoding/hex"
"fmt"
"strings"

Expand Down Expand Up @@ -168,18 +168,19 @@ func (k Keeper) AllocationCommit(c context.Context, msg *types.MsgAllocationComm
return nil, sdkerrors.Wrap(types.ErrNoPrecommit, val.String())
}

// parse data to json in order to compute the vote hash and sort
jsonBz, err := json.Marshal(commit.Cellar)
// marshal the protobuf message to computing the hash
databytes, err := commit.Cellar.Marshal()

if err != nil {
return nil, sdkerrors.Wrap(
sdkerrors.ErrJSONMarshal, "failed to marshal json pool allocations",
)
}

jsonBz = sdk.MustSortJSON(jsonBz)
hexbytes := hex.EncodeToString(databytes)

// calculate the vote hash on the server
commitHash := types.DataHash(commit.Salt, string(jsonBz), val)
commitHash := types.DataHash(commit.Salt, hexbytes, val)

// compare to precommit hash
if !bytes.Equal(commitHash, precommit.Hash) {
Expand Down

0 comments on commit e66b5c5

Please sign in to comment.