Merge branch 'dev/change_dec_marshaljson' of github.com:cosmos/cosmos-sdk into dev/change_dec_marshaljson
This commit is contained in:
+29
-1
@@ -2,6 +2,7 @@ package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
cmn "github.com/tendermint/tendermint/libs/common"
|
||||
@@ -286,7 +287,34 @@ func (err *sdkError) QueryResult() abci.ResponseQuery {
|
||||
}
|
||||
}
|
||||
|
||||
// nolint
|
||||
//----------------------------------------
|
||||
// REST error utilities
|
||||
|
||||
// appends a message to the head of the given error
|
||||
func AppendMsgToErr(msg string, err string) string {
|
||||
msgIdx := strings.Index(err, "message\":\"")
|
||||
if msgIdx != -1 {
|
||||
errMsg := err[msgIdx+len("message\":\"") : len(err)-2]
|
||||
errMsg = fmt.Sprintf("%s; %s", msg, errMsg)
|
||||
return fmt.Sprintf("%s%s%s",
|
||||
err[:msgIdx+len("message\":\"")],
|
||||
errMsg,
|
||||
err[len(err)-2:],
|
||||
)
|
||||
}
|
||||
return fmt.Sprintf("%s; %s", msg, err)
|
||||
}
|
||||
|
||||
// returns the index of the message in the ABCI Log
|
||||
func mustGetMsgIndex(abciLog string) int {
|
||||
msgIdx := strings.Index(abciLog, "message\":\"")
|
||||
if msgIdx == -1 {
|
||||
panic(fmt.Sprintf("invalid error format: %s", abciLog))
|
||||
}
|
||||
return msgIdx + len("message\":\"")
|
||||
}
|
||||
|
||||
// parses the error into an object-like struct for exporting
|
||||
type humanReadableError struct {
|
||||
Codespace CodespaceType `json:"codespace"`
|
||||
Code CodeType `json:"code"`
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -65,3 +66,28 @@ func TestErrFn(t *testing.T) {
|
||||
|
||||
require.Equal(t, ABCICodeOK, ToABCICode(CodespaceRoot, CodeOK))
|
||||
}
|
||||
|
||||
func TestAppendMsgToErr(t *testing.T) {
|
||||
for i, errFn := range errFns {
|
||||
err := errFn("")
|
||||
errMsg := err.Stacktrace().Error()
|
||||
abciLog := err.ABCILog()
|
||||
|
||||
// plain msg error
|
||||
msg := AppendMsgToErr("something unexpected happened", errMsg)
|
||||
require.Equal(t, fmt.Sprintf("something unexpected happened; %s",
|
||||
errMsg),
|
||||
msg,
|
||||
fmt.Sprintf("Should have formatted the error message of ABCI Log. tc #%d", i))
|
||||
|
||||
// ABCI Log msg error
|
||||
msg = AppendMsgToErr("something unexpected happened", abciLog)
|
||||
msgIdx := mustGetMsgIndex(abciLog)
|
||||
require.Equal(t, fmt.Sprintf("%s%s; %s}",
|
||||
abciLog[:msgIdx],
|
||||
"something unexpected happened",
|
||||
abciLog[msgIdx:len(abciLog)-1]),
|
||||
msg,
|
||||
fmt.Sprintf("Should have formatted the error message of ABCI Log. tc #%d", i))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,254 +0,0 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// Linear defines a primitive mapper type
|
||||
type Linear struct {
|
||||
cdc *codec.Codec
|
||||
store sdk.KVStore
|
||||
keys *LinearKeys
|
||||
}
|
||||
|
||||
// LinearKeys defines keysions for the key bytes
|
||||
type LinearKeys struct {
|
||||
LengthKey []byte
|
||||
ElemKey []byte
|
||||
TopKey []byte
|
||||
}
|
||||
|
||||
// Should never be modified
|
||||
var cachedDefaultLinearKeys = DefaultLinearKeys()
|
||||
|
||||
// DefaultLinearKeys returns the default setting of LinearOption
|
||||
func DefaultLinearKeys() *LinearKeys {
|
||||
keys := LinearKeys{
|
||||
LengthKey: []byte{0x00},
|
||||
ElemKey: []byte{0x01},
|
||||
TopKey: []byte{0x02},
|
||||
}
|
||||
return &keys
|
||||
}
|
||||
|
||||
// NewLinear constructs new Linear
|
||||
func NewLinear(cdc *codec.Codec, store sdk.KVStore, keys *LinearKeys) Linear {
|
||||
if keys == nil {
|
||||
keys = cachedDefaultLinearKeys
|
||||
}
|
||||
if keys.LengthKey == nil || keys.ElemKey == nil || keys.TopKey == nil {
|
||||
panic("Invalid LinearKeys")
|
||||
}
|
||||
return Linear{
|
||||
cdc: cdc,
|
||||
store: store,
|
||||
keys: keys,
|
||||
}
|
||||
}
|
||||
|
||||
// List is a Linear interface that provides list-like functions
|
||||
// It panics when the element type cannot be (un/)marshalled by the codec
|
||||
type List interface {
|
||||
|
||||
// Len() returns the length of the list
|
||||
// The length is only increased by Push() and not decreased
|
||||
// List dosen't check if an index is in bounds
|
||||
// The user should check Len() before doing any actions
|
||||
Len() uint64
|
||||
|
||||
// Get() returns the element by its index
|
||||
Get(uint64, interface{}) error
|
||||
|
||||
// Set() stores the element to the given position
|
||||
// Setting element out of range will break length counting
|
||||
// Use Push() instead of Set() to append a new element
|
||||
Set(uint64, interface{})
|
||||
|
||||
// Delete() deletes the element in the given position
|
||||
// Other elements' indices are preserved after deletion
|
||||
// Panics when the index is out of range
|
||||
Delete(uint64)
|
||||
|
||||
// Push() inserts the element to the end of the list
|
||||
// It will increase the length when it is called
|
||||
Push(interface{})
|
||||
|
||||
// Iterate*() is used to iterate over all existing elements in the list
|
||||
// Return true in the continuation to break
|
||||
// The second element of the continuation will indicate the position of the element
|
||||
// Using it with Get() will return the same one with the provided element
|
||||
|
||||
// CONTRACT: No writes may happen within a domain while iterating over it.
|
||||
Iterate(interface{}, func(uint64) bool)
|
||||
}
|
||||
|
||||
// NewList constructs new List
|
||||
func NewList(cdc *codec.Codec, store sdk.KVStore, keys *LinearKeys) List {
|
||||
return NewLinear(cdc, store, keys)
|
||||
}
|
||||
|
||||
// Key for the length of the list
|
||||
func (m Linear) LengthKey() []byte {
|
||||
return m.keys.LengthKey
|
||||
}
|
||||
|
||||
// Key for the elements of the list
|
||||
func (m Linear) ElemKey(index uint64) []byte {
|
||||
return append(m.keys.ElemKey, []byte(fmt.Sprintf("%020d", index))...)
|
||||
}
|
||||
|
||||
// Len implements List
|
||||
func (m Linear) Len() (res uint64) {
|
||||
bz := m.store.Get(m.LengthKey())
|
||||
if bz == nil {
|
||||
return 0
|
||||
}
|
||||
m.cdc.MustUnmarshalBinary(bz, &res)
|
||||
return
|
||||
}
|
||||
|
||||
// Get implements List
|
||||
func (m Linear) Get(index uint64, ptr interface{}) error {
|
||||
bz := m.store.Get(m.ElemKey(index))
|
||||
return m.cdc.UnmarshalBinary(bz, ptr)
|
||||
}
|
||||
|
||||
// Set implements List
|
||||
func (m Linear) Set(index uint64, value interface{}) {
|
||||
bz := m.cdc.MustMarshalBinary(value)
|
||||
m.store.Set(m.ElemKey(index), bz)
|
||||
}
|
||||
|
||||
// Delete implements List
|
||||
func (m Linear) Delete(index uint64) {
|
||||
m.store.Delete(m.ElemKey(index))
|
||||
}
|
||||
|
||||
// Push implements List
|
||||
func (m Linear) Push(value interface{}) {
|
||||
length := m.Len()
|
||||
m.Set(length, value)
|
||||
m.store.Set(m.LengthKey(), m.cdc.MustMarshalBinary(length+1))
|
||||
}
|
||||
|
||||
// IterateRead implements List
|
||||
func (m Linear) Iterate(ptr interface{}, fn func(uint64) bool) {
|
||||
iter := sdk.KVStorePrefixIterator(m.store, []byte{0x01})
|
||||
for ; iter.Valid(); iter.Next() {
|
||||
v := iter.Value()
|
||||
m.cdc.MustUnmarshalBinary(v, ptr)
|
||||
k := iter.Key()
|
||||
s := string(k[len(k)-20:])
|
||||
index, err := strconv.ParseUint(s, 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if fn(index) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
iter.Close()
|
||||
}
|
||||
|
||||
// Queue is a Linear interface that provides queue-like functions
|
||||
// It panics when the element type cannot be (un/)marshalled by the codec
|
||||
type Queue interface {
|
||||
// Push() inserts the elements to the rear of the queue
|
||||
Push(interface{})
|
||||
|
||||
// Popping/Peeking on an empty queue will cause panic
|
||||
// The user should check IsEmpty() before doing any actions
|
||||
|
||||
// Peek() returns the element at the front of the queue without removing it
|
||||
Peek(interface{}) error
|
||||
|
||||
// Pop() returns the element at the front of the queue and removes it
|
||||
Pop()
|
||||
|
||||
// IsEmpty() checks if the queue is empty
|
||||
IsEmpty() bool
|
||||
|
||||
// Flush() removes elements it processed
|
||||
// Return true in the continuation to break
|
||||
// The interface{} is unmarshalled before the continuation is called
|
||||
// Starts from the top(head) of the queue
|
||||
// CONTRACT: Pop() or Push() should not be performed while flushing
|
||||
Flush(interface{}, func() bool)
|
||||
}
|
||||
|
||||
// NewQueue constructs new Queue
|
||||
func NewQueue(cdc *codec.Codec, store sdk.KVStore, keys *LinearKeys) Queue {
|
||||
return NewLinear(cdc, store, keys)
|
||||
}
|
||||
|
||||
// Key for the top element position in the queue
|
||||
func (m Linear) TopKey() []byte {
|
||||
return m.keys.TopKey
|
||||
}
|
||||
|
||||
func (m Linear) getTop() (res uint64) {
|
||||
bz := m.store.Get(m.TopKey())
|
||||
if bz == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
m.cdc.MustUnmarshalBinary(bz, &res)
|
||||
return
|
||||
}
|
||||
|
||||
func (m Linear) setTop(top uint64) {
|
||||
bz := m.cdc.MustMarshalBinary(top)
|
||||
m.store.Set(m.TopKey(), bz)
|
||||
}
|
||||
|
||||
// Peek implements Queue
|
||||
func (m Linear) Peek(ptr interface{}) error {
|
||||
top := m.getTop()
|
||||
return m.Get(top, ptr)
|
||||
}
|
||||
|
||||
// Pop implements Queue
|
||||
func (m Linear) Pop() {
|
||||
top := m.getTop()
|
||||
m.Delete(top)
|
||||
m.setTop(top + 1)
|
||||
}
|
||||
|
||||
// IsEmpty implements Queue
|
||||
func (m Linear) IsEmpty() bool {
|
||||
top := m.getTop()
|
||||
length := m.Len()
|
||||
return top >= length
|
||||
}
|
||||
|
||||
// Flush implements Queue
|
||||
func (m Linear) Flush(ptr interface{}, fn func() bool) {
|
||||
top := m.getTop()
|
||||
length := m.Len()
|
||||
|
||||
var i uint64
|
||||
for i = top; i < length; i++ {
|
||||
err := m.Get(i, ptr)
|
||||
if err != nil {
|
||||
// TODO: Handle with #870
|
||||
panic(err)
|
||||
}
|
||||
m.Delete(i)
|
||||
if fn() {
|
||||
break
|
||||
}
|
||||
}
|
||||
m.setTop(i)
|
||||
}
|
||||
|
||||
func subspace(prefix []byte) (start, end []byte) {
|
||||
end = make([]byte, len(prefix))
|
||||
copy(end, prefix)
|
||||
end[len(end)-1]++
|
||||
return prefix, end
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
dbm "github.com/tendermint/tendermint/libs/db"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/store"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
type S struct {
|
||||
I uint64
|
||||
B bool
|
||||
}
|
||||
|
||||
func defaultComponents(key sdk.StoreKey) (sdk.Context, *codec.Codec) {
|
||||
db := dbm.NewMemDB()
|
||||
cms := store.NewCommitMultiStore(db)
|
||||
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
|
||||
cms.LoadLatestVersion()
|
||||
ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger())
|
||||
cdc := codec.New()
|
||||
return ctx, cdc
|
||||
}
|
||||
|
||||
func TestNewLinear(t *testing.T) {
|
||||
cdc := codec.New()
|
||||
require.NotPanics(t, func() { NewLinear(cdc, nil, nil) })
|
||||
require.NotPanics(t, func() { NewLinear(cdc, nil, DefaultLinearKeys()) })
|
||||
require.NotPanics(t, func() { NewLinear(cdc, nil, &LinearKeys{[]byte{0xAA}, []byte{0xBB}, []byte{0xCC}}) })
|
||||
|
||||
require.Panics(t, func() { NewLinear(cdc, nil, &LinearKeys{nil, nil, nil}) })
|
||||
require.Panics(t, func() { NewLinear(cdc, nil, &LinearKeys{[]byte{0xAA}, nil, nil}) })
|
||||
require.Panics(t, func() { NewLinear(cdc, nil, &LinearKeys{nil, []byte{0xBB}, nil}) })
|
||||
require.Panics(t, func() { NewLinear(cdc, nil, &LinearKeys{nil, nil, []byte{0xCC}}) })
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
key := sdk.NewKVStoreKey("test")
|
||||
ctx, cdc := defaultComponents(key)
|
||||
store := ctx.KVStore(key)
|
||||
lm := NewList(cdc, store, nil)
|
||||
|
||||
val := S{1, true}
|
||||
var res S
|
||||
|
||||
lm.Push(val)
|
||||
require.Equal(t, uint64(1), lm.Len())
|
||||
lm.Get(uint64(0), &res)
|
||||
require.Equal(t, val, res)
|
||||
|
||||
val = S{2, false}
|
||||
lm.Set(uint64(0), val)
|
||||
lm.Get(uint64(0), &res)
|
||||
require.Equal(t, val, res)
|
||||
|
||||
val = S{100, false}
|
||||
lm.Push(val)
|
||||
require.Equal(t, uint64(2), lm.Len())
|
||||
lm.Get(uint64(1), &res)
|
||||
require.Equal(t, val, res)
|
||||
|
||||
lm.Delete(uint64(1))
|
||||
require.Equal(t, uint64(2), lm.Len())
|
||||
|
||||
lm.Iterate(&res, func(index uint64) (brk bool) {
|
||||
var temp S
|
||||
lm.Get(index, &temp)
|
||||
require.Equal(t, temp, res)
|
||||
|
||||
require.True(t, index != 1)
|
||||
return
|
||||
})
|
||||
|
||||
lm.Iterate(&res, func(index uint64) (brk bool) {
|
||||
lm.Set(index, S{res.I + 1, !res.B})
|
||||
return
|
||||
})
|
||||
|
||||
lm.Get(uint64(0), &res)
|
||||
require.Equal(t, S{3, true}, res)
|
||||
}
|
||||
|
||||
func TestQueue(t *testing.T) {
|
||||
key := sdk.NewKVStoreKey("test")
|
||||
ctx, cdc := defaultComponents(key)
|
||||
store := ctx.KVStore(key)
|
||||
|
||||
qm := NewQueue(cdc, store, nil)
|
||||
|
||||
val := S{1, true}
|
||||
var res S
|
||||
|
||||
qm.Push(val)
|
||||
qm.Peek(&res)
|
||||
require.Equal(t, val, res)
|
||||
|
||||
qm.Pop()
|
||||
empty := qm.IsEmpty()
|
||||
|
||||
require.True(t, empty)
|
||||
require.NotNil(t, qm.Peek(&res))
|
||||
|
||||
qm.Push(S{1, true})
|
||||
qm.Push(S{2, true})
|
||||
qm.Push(S{3, true})
|
||||
qm.Flush(&res, func() (brk bool) {
|
||||
if res.I == 3 {
|
||||
brk = true
|
||||
}
|
||||
return
|
||||
})
|
||||
|
||||
require.False(t, qm.IsEmpty())
|
||||
|
||||
qm.Pop()
|
||||
require.True(t, qm.IsEmpty())
|
||||
}
|
||||
|
||||
func TestOptions(t *testing.T) {
|
||||
key := sdk.NewKVStoreKey("test")
|
||||
ctx, cdc := defaultComponents(key)
|
||||
store := ctx.KVStore(key)
|
||||
|
||||
keys := &LinearKeys{
|
||||
LengthKey: []byte{0xDE, 0xAD},
|
||||
ElemKey: []byte{0xBE, 0xEF},
|
||||
TopKey: []byte{0x12, 0x34},
|
||||
}
|
||||
linear := NewLinear(cdc, store, keys)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
linear.Push(i)
|
||||
}
|
||||
|
||||
var len uint64
|
||||
var top uint64
|
||||
var expected int
|
||||
var actual int
|
||||
|
||||
// Checking keys.LengthKey
|
||||
err := cdc.UnmarshalBinary(store.Get(keys.LengthKey), &len)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, len, linear.Len())
|
||||
|
||||
// Checking keys.ElemKey
|
||||
for i := 0; i < 10; i++ {
|
||||
linear.Get(uint64(i), &expected)
|
||||
bz := store.Get(append(keys.ElemKey, []byte(fmt.Sprintf("%020d", i))...))
|
||||
err = cdc.UnmarshalBinary(bz, &actual)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
linear.Pop()
|
||||
|
||||
err = cdc.UnmarshalBinary(store.Get(keys.TopKey), &top)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, top, linear.getTop())
|
||||
|
||||
}
|
||||
@@ -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,24 @@ func MustSortJSON(toSortJSON []byte) []byte {
|
||||
return js
|
||||
}
|
||||
|
||||
// Slight modification of the RFC3339Nano but it right pads all zeros and drops the time zone info
|
||||
const SortableTimeFormat = "2006-01-02T15:04:05.000000000"
|
||||
|
||||
// Formats a time.Time into a []byte that can be sorted
|
||||
func FormatTimeBytes(t time.Time) []byte {
|
||||
return []byte(t.UTC().Round(0).Format(SortableTimeFormat))
|
||||
}
|
||||
|
||||
// Parses a []byte encoded using FormatTimeKey back into a time.Time
|
||||
func ParseTimeBytes(bz []byte) (time.Time, error) {
|
||||
str := string(bz)
|
||||
t, err := time.Parse(SortableTimeFormat, str)
|
||||
if err != nil {
|
||||
return t, err
|
||||
}
|
||||
return t.UTC().Round(0), nil
|
||||
}
|
||||
|
||||
// DefaultChainID returns the chain ID from the genesis file if present. An
|
||||
// error is returned if the file cannot be read or parsed.
|
||||
//
|
||||
|
||||
@@ -2,6 +2,7 @@ package types
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -43,3 +44,23 @@ func TestSortJSON(t *testing.T) {
|
||||
require.Equal(t, string(got), tc.want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeFormatAndParse(t *testing.T) {
|
||||
cases := []struct {
|
||||
RFC3339NanoStr string
|
||||
SDKSortableTimeStr string
|
||||
Equal bool
|
||||
}{
|
||||
{"2009-11-10T23:00:00Z", "2009-11-10T23:00:00.000000000", true},
|
||||
{"2011-01-10T23:10:05.758230235Z", "2011-01-10T23:10:05.758230235", true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
timeFromRFC, err := time.Parse(time.RFC3339Nano, tc.RFC3339NanoStr)
|
||||
require.Nil(t, err)
|
||||
timeFromSDKFormat, err := time.Parse(SortableTimeFormat, tc.SDKSortableTimeStr)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.True(t, timeFromRFC.Equal(timeFromSDKFormat))
|
||||
require.Equal(t, timeFromRFC.Format(SortableTimeFormat), tc.SDKSortableTimeStr)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user