From e419396bd199632b2478b7d83ccc936c4bfbb5d5 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Wed, 17 Oct 2018 02:09:19 -0400 Subject: [PATCH] fixed time key marshal (#2516) Do not use Amino Binary for key sorting. --- types/utils.go | 13 +++++++++++++ x/stake/keeper/key.go | 17 ++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/types/utils.go b/types/utils.go index b196acb230..fa2999a183 100644 --- a/types/utils.go +++ b/types/utils.go @@ -2,6 +2,8 @@ package types import ( "encoding/json" + "time" + tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" tmtypes "github.com/tendermint/tendermint/types" ) @@ -34,6 +36,17 @@ func MustSortJSON(toSortJSON []byte) []byte { return js } +// Formats a time.Time into a []byte that can be sorted +func FormatTimeBytes(t time.Time) []byte { + return []byte(t.UTC().Round(0).Format("2006-01-02T15:04:05.000000000")) +} + +// Parses a []byte encoded using FormatTimeKey back into a time.Time +func ParseTimeBytes(bz []byte) (time.Time, error) { + str := string(bz) + return time.Parse("2006-01-02T15:04:05.000000000", str) +} + // DefaultChainID returns the chain ID from the genesis file if present. An // error is returned if the file cannot be read or parsed. // diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index aef685cb9b..6e6ff54ff0 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -34,6 +34,17 @@ var ( const maxDigitsForAccount = 12 // ~220,000,000 atoms created at launch +// Formats a time.Time into a []byte that can be sorted +func FormatTimeKey(t time.Time) []byte { + return []byte(t.UTC().Round(0).Format("2006-01-02T15:04:05.000000000")) +} + +// Parses a []byte encoded using FormatTimeKey back into a time.Time +func ParseTimeKey(bz []byte) (time.Time, error) { + str := string(bz) + return time.Parse("2006-01-02T15:04:05.000000000", str) +} + // gets the key for the validator with address // VALUE: stake/types.Validator func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { @@ -96,7 +107,7 @@ func getValidatorPowerRank(validator types.Validator) []byte { // gets the prefix for all unbonding delegations from a delegator func GetValidatorQueueTimeKey(timestamp time.Time) []byte { - bz := types.MsgCdc.MustMarshalBinary(timestamp) + bz := sdk.FormatTimeBytes(timestamp) return append(ValidatorQueueKey, bz...) } @@ -154,7 +165,7 @@ func GetUBDsByValIndexKey(valAddr sdk.ValAddress) []byte { // gets the prefix for all unbonding delegations from a delegator func GetUnbondingDelegationTimeKey(timestamp time.Time) []byte { - bz := types.MsgCdc.MustMarshalBinary(timestamp) + bz := sdk.FormatTimeBytes(timestamp) return append(UnbondingQueueKey, bz...) } @@ -228,7 +239,7 @@ func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte { // gets the prefix for all unbonding delegations from a delegator func GetRedelegationTimeKey(timestamp time.Time) []byte { - bz, _ := timestamp.MarshalBinary() + bz := sdk.FormatTimeBytes(timestamp) return append(RedelegationQueueKey, bz...) }