fixed time key marshal (#2516)

Do not use Amino Binary for key sorting.
This commit is contained in:
Sunny Aggarwal 2018-10-17 02:09:19 -04:00 committed by Jae Kwon
parent 070bda3a25
commit e419396bd1
2 changed files with 27 additions and 3 deletions

View File

@ -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.
//

View File

@ -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...)
}