cosmos-sdk/tx/textual/valuerenderer/duration.go
Jim Larson d9f3eb027d
feat: structured screens for SIGN_MODE_TEXTUAL (#13434)
## Description

Refs: #11970

Changes target of `SIGN_MODE_TEXTUAL` rendering to be a structured datatype
instead of lines of ASCII text. This avoids the complexities of in-band, signaling and
allows more capable signing devices not to be hindered by the limitations of those less
capable.

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [X] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [X] added `!` to the type prefix if API or client breaking change
- [X] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [X] provided a link to the relevant issue or specification
- [X] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [X] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [X] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [X] updated the relevant documentation or specification
- [X] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed
NOTE: changelog intentionally omitted - we'll add an entry when
#11970 is complete.

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-10-15 08:04:36 +00:00

187 lines
5.2 KiB
Go

package valuerenderer
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"google.golang.org/protobuf/reflect/protoreflect"
dpb "google.golang.org/protobuf/types/known/durationpb"
)
type durationValueRenderer struct{}
// NewDurationValueRenderer returns a ValueRenderer for protocol buffer Duration messages.
// It renders durations by grouping seconds into units of days (86400s), hours (3600s),
// and minutes(60s), plus the total seconds elapsed. E.g. a duration of 1483530s is
// formatted as "17 days, 4 hours, 5 minutes, 30 seconds".
// Note that the days are always 24 hours regardless of daylight savings changes.
func NewDurationValueRenderer() ValueRenderer {
return durationValueRenderer{}
}
const (
min_sec = 60
hour_sec = 60 * min_sec
day_sec = 24 * hour_sec
)
type factors struct {
days, hours, minutes, seconds int64
}
func factorSeconds(x int64) factors {
var f factors
f.days = x / day_sec
x -= f.days * day_sec
f.hours = x / hour_sec
x -= f.hours * hour_sec
f.minutes = x / min_sec
x -= f.minutes * min_sec
f.seconds = x
return f
}
func maybePlural(s string, plural bool) string {
if plural {
return s + "s"
}
return s
}
func formatSeconds(seconds int64, nanos int32) string {
var s string
if nanos == 0 {
s = fmt.Sprintf("%d", seconds)
} else {
frac := fmt.Sprintf("%09d", nanos)
frac = strings.TrimRight(frac, "0")
s = fmt.Sprintf("%d.%s", seconds, frac)
}
return s
}
// Format implements the ValueRenderer interface.
func (dr durationValueRenderer) Format(_ context.Context, v protoreflect.Value) ([]Screen, error) {
// Reify the reflected message as a proto Duration
msg := v.Message().Interface()
duration, ok := msg.(*dpb.Duration)
if !ok {
return nil, fmt.Errorf("expected Duration, got %T", msg)
}
// Bypass use of time.Duration, as the range is more limited than that of dpb.Duration.
// (Too bad the companies that produced both technologies didn't coordinate better!)
if err := duration.CheckValid(); err != nil {
return nil, err
}
negative := false
if duration.Seconds < 0 || duration.Nanos < 0 {
negative = true
// copy to avoid side-effecting our input
d := *duration
duration = &d
duration.Seconds *= -1
duration.Nanos *= -1
}
factors := factorSeconds(duration.Seconds)
components := []string{}
if factors.days > 0 {
components = append(components, fmt.Sprintf("%d %s", factors.days, maybePlural("day", factors.days != 1)))
}
if factors.hours > 0 || (len(components) > 0 && (factors.minutes > 0 || factors.seconds > 0 || duration.Nanos > 0)) {
components = append(components, fmt.Sprintf("%d %s", factors.hours, maybePlural("hour", factors.hours != 1)))
}
if factors.minutes > 0 || (len(components) > 0 && (factors.seconds > 0 || duration.Nanos > 0)) {
components = append(components, fmt.Sprintf("%d %s", factors.minutes, maybePlural("minute", factors.minutes != 1)))
}
if factors.seconds > 0 || duration.Nanos > 0 {
components = append(components, formatSeconds(factors.seconds, duration.Nanos)+" "+maybePlural("second", factors.seconds != 1 || duration.Nanos > 0))
}
s := strings.Join(components, ", ")
if s == "" {
s = "0 seconds"
}
if negative {
s = "-" + s
}
return []Screen{{Text: s}}, nil
}
var durRegexp = regexp.MustCompile(`^(-)?(?:([0-9]+) days?)?(?:, )?(?:([0-9]+) hours?)?(?:, )?(?:([0-9]+) minutes?)?(?:, )?(?:([0-9]+)(?:\.([0-9]+))? seconds?)?$`)
// Parse implements the ValueRenderer interface.
func (dr durationValueRenderer) Parse(_ context.Context, screens []Screen) (protoreflect.Value, error) {
if len(screens) != 1 {
return protoreflect.Value{}, fmt.Errorf("expected single screen: %v", screens)
}
parts := durRegexp.FindStringSubmatch(screens[0].Text)
if parts == nil {
return protoreflect.Value{}, fmt.Errorf("bad duration format: %s", screens[0].Text)
}
negative := parts[1] != ""
var days, hours, minutes, seconds, nanos int64
var err error
if parts[2] != "" {
days, err = strconv.ParseInt(parts[2], 10, 64)
if err != nil {
return protoreflect.Value{}, fmt.Errorf(`bad number "%s": %w`, parts[2], err)
}
}
if parts[3] != "" {
hours, err = strconv.ParseInt(parts[3], 10, 64)
if err != nil {
return protoreflect.Value{}, fmt.Errorf(`bad number "%s": %w`, parts[3], err)
}
}
if parts[4] != "" {
minutes, err = strconv.ParseInt(parts[4], 10, 64)
if err != nil {
return protoreflect.Value{}, fmt.Errorf(`bad number "%s": %w`, parts[4], err)
}
}
if parts[5] != "" {
seconds, err = strconv.ParseInt(parts[5], 10, 64)
if err != nil {
return protoreflect.Value{}, fmt.Errorf(`bad number "%s": %w`, parts[5], err)
}
if parts[6] != "" {
if len(parts[6]) > 9 {
return protoreflect.Value{}, fmt.Errorf(`too many nanos "%s"`, parts[6])
}
addZeros := 9 - len(parts[6])
text := parts[6] + strings.Repeat("0", addZeros)
nanos, err = strconv.ParseInt(text, 10, 32)
if err != nil {
return protoreflect.Value{}, fmt.Errorf(`bad number "%s": %w`, text, err)
}
}
}
dur := &dpb.Duration{}
dur.Seconds = days*day_sec + hours*hour_sec + minutes*min_sec + seconds
// #nosec G701
// Since there are 9 digits or fewer, this conversion is safe.
dur.Nanos = int32(nanos)
if negative {
dur.Seconds *= -1
dur.Nanos *= -1
}
msg := dur.ProtoReflect()
return protoreflect.ValueOfMessage(msg), nil
}