cosmos-sdk/types/tags.go

116 lines
2.2 KiB
Go
Raw Normal View History

package types
import (
"fmt"
"strings"
cmn "github.com/tendermint/tendermint/libs/common"
)
2018-04-26 07:18:01 -07:00
// Type synonym for convenience
type Tag = cmn.KVPair
2018-04-26 07:18:01 -07:00
// Type synonym for convenience
2018-05-09 15:47:28 -07:00
type Tags cmn.KVPairs
// New empty tags
func EmptyTags() Tags {
return make(Tags, 0)
}
2018-05-09 15:47:28 -07:00
// Append a single tag
func (t Tags) AppendTag(k string, v string) Tags {
2018-05-09 15:47:28 -07:00
return append(t, MakeTag(k, v))
}
// Append two lists of tags
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
2018-06-26 19:00:12 -07:00
func (t Tags) AppendTags(tags Tags) Tags {
return append(t, tags...)
2018-05-09 15:47:28 -07:00
}
2018-05-23 13:25:56 -07:00
// Turn tags into KVPair list
func (t Tags) ToKVPairs() []cmn.KVPair {
return []cmn.KVPair(t)
}
2018-05-09 15:47:28 -07:00
// New variadic tags, must be k string, v []byte repeating
func NewTags(tags ...interface{}) Tags {
var ret Tags
if len(tags)%2 != 0 {
panic("must specify key-value pairs as varargs")
}
i := 0
for {
if i == len(tags) {
break
}
ret = append(ret, Tag{Key: toBytes(tags[i]), Value: toBytes(tags[i+1])})
2018-05-09 15:47:28 -07:00
i += 2
}
return ret
}
func toBytes(i interface{}) []byte {
switch x := i.(type) {
case []uint8:
return x
case string:
return []byte(x)
default:
panic(i)
}
}
// Make a tag from a key and a value
func MakeTag(k string, v string) Tag {
return Tag{Key: []byte(k), Value: []byte(v)}
}
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
2018-06-26 19:00:12 -07:00
//__________________________________________________
// common tags
var (
TagAction = "action"
TagSrcValidator = "source-validator"
TagDstValidator = "destination-validator"
TagDelegator = "delegator"
)
// A KVPair where the Key and Value are both strings, rather than []byte
type StringTag struct {
Key string `json:"key"`
Value string `json:"value,omitempty"`
}
func (st StringTag) String() string {
return fmt.Sprintf("%s = %s", st.Key, st.Value)
}
// A slice of StringTag
type StringTags []StringTag
func (st StringTags) String() string {
var sb strings.Builder
for _, t := range st {
sb.WriteString(fmt.Sprintf(" - %s\n", t.String()))
}
return sb.String()
}
// Conversion function from a []byte tag to a string tag
func TagToStringTag(tag Tag) StringTag {
return StringTag{
Key: string(tag.Key),
Value: string(tag.Value),
}
}
// Conversion function from Tags to a StringTags
func TagsToStringTags(tags Tags) StringTags {
var stringTags StringTags
for _, tag := range tags {
stringTags = append(stringTags, TagToStringTag(tag))
}
return stringTags
}