/
main.go
235 lines (204 loc) · 6.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (C) 2019 David N. Cohen
// This file is part of github.com/dncohen/rcl
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Command RCL-data
//
// The rcl-data command retrieves historical data from
// data.ripple.com/v2/... and displays information about accounts on
// the Ripple Consensus Ledger.
//
// Each subcommand has a -help flag that explains it in more detail. For instance
//
// rcl-data show -help
//
// explains the purpose and usage of the show subcommand.
//
// There is a set of global flags such as -config to specify the
// configuration directory, where rcl-data expects to find one or more
// *.cfg files. These global flags apply to all subcommands.
//
// Each subcommand has its own set of flags, which if used must appear
// after the subcommand name.
//
// For a list of available subcommands and global flags, run
//
// rcl-data -help
//
package main
import (
"flag"
"fmt"
"log"
"strings"
"github.com/go-ini/ini"
"github.com/pkg/errors"
"github.com/rubblelabs/ripple/data"
"src.d10.dev/command"
"src.d10.dev/command/config"
)
// Use `go get src.d10.dev/dumbdown` to fetch dumbdown tool.
//go:generate sh -c "go doc | dumbdown > README.md"
func main() {
command.RegisterCommand(command.Command{
Application: "rcl",
Description: "Inspect historical Ripple Consensus Ledger activity.",
})
_, err := command.Config()
if errors.Cause(err) == config.ConfigNotFound {
// not a problem, we'll use defaults
// TODO(dnc): if -config specified explicitly on command line, make this a fatal error
command.Info(err)
err = nil
}
command.CheckUsage(err)
// this command requires an operation
if len(flag.CommandLine.Args()) < 1 {
command.CheckUsage(errors.New("command requires an operation"))
}
// default prefix for subcommand
log.SetPrefix(fmt.Sprintf("rcl-data %s: ", flag.CommandLine.Args()[0]))
err = command.CurrentOperation().Operate()
command.CheckUsage(err)
command.Exit()
}
// AccountTag is a "fully qualified" account identifier, meaning an
// address and destination (or source) tag.
type AccountTag struct {
Account data.Account
Tag uint32 // not *uint32, because we want to use AccountTag as map key
}
func NewAccountTag(acct data.Account, tag *uint32) AccountTag {
this := AccountTag{Account: acct}
if tag != nil {
this.Tag = *tag
}
return this
}
func (this *AccountTag) String() string {
if this == nil {
log.Panic("nil AccountTag passed to AccountTag.String()")
}
if this.Tag == 0 { // treat 0 as no tag
return this.Account.String()
} else {
return fmt.Sprintf("%s.%d", this.Account, this.Tag)
}
}
var accountByNickname map[string]AccountTag
var accountConfig map[AccountTag]*ini.Section
func initializeNicknames() error {
// once
if accountByNickname != nil {
return nil
}
accountByNickname = make(map[string]AccountTag)
accountConfig = make(map[AccountTag]*ini.Section)
cfg, err := command.Config()
if err != nil {
return err
}
for _, section := range cfg.Sections() {
if section.HasKey("address") {
nickname := section.Name()
address := section.Key("address").Value()
var tag *uint32
if section.HasKey("tag") {
t, err := section.Key("tag").Uint()
if err != nil {
return fmt.Errorf("failed to parse RCL configuration %q: %w", section.Name(), err)
}
tmp := uint32(t)
tag = &tmp
}
account, err := data.NewAccountFromAddress(address)
if err != nil {
return err
}
at := NewAccountTag(*account, tag)
//log.Printf("account nickname %q: %v", nickname, at) // troubleshooting
accountByNickname[nickname] = at
accountConfig[at] = section
if at.Tag != 0 {
// use nickname even when tag is not used
noTag := NewAccountTag(*account, nil)
_, ok := accountConfig[noTag]
if !ok {
accountConfig[noTag] = section
}
}
}
}
return nil
}
// avoid scientific notation, not understood by ledger-cli
func formatValue(v data.Value) string {
// unfortunately rubblelabs does not export data.Value:isScientific,
// so we must manipulate strings
str := v.String()
if strings.Index(str, "e") != -1 {
// simply using fmt.Sprintf("%f", v.Float()) produces, for example "0.000000"
rat := v.Rat()
// debug
command.V(1).Infof("formatValue: converting scientific notation from %q to %q", v.String(), rat.FloatString(16))
str = fmt.Sprintf("%s", rat.FloatString(16)) // TODO(dnc): proper decimal precision?
}
return str
}
// returns account nickname if known; otherwise, address string
func formatAccount(account data.Account, tag *uint32) string {
nick, _ := accountDetail(account, tag)
return nick
}
func accountDetail(account data.Account, tag *uint32) (nick, ledger string) {
at := NewAccountTag(account, tag)
cfg, ok := accountConfig[at]
if !ok && tag != nil {
// fallback to nickname without tag
at.Tag = 0
cfg, ok = accountConfig[at]
}
if !ok {
//log.Printf("no account nickname for %v in %v", at, accountConfig) // troubleshooting
// no nickname for this account
return account.String(), ""
}
nick = cfg.Name()
if cfg.HasKey("ledger") {
ledger = cfg.Key("ledger").Value()
}
return
}
// Helper for operations that expect a list of accounts. We want to
// accept (and display) accounts by local nickname, as well as normal
// ripple address.
func parseAccountArg(arg []string) ([]AccountTag, error) {
err := initializeNicknames()
if err != nil {
return nil, err
}
var account []AccountTag
for _, a := range arg {
acct, ok := accountByNickname[a]
if !ok {
tmp, err := data.NewAccountFromAddress(a)
if err != nil {
return account, fmt.Errorf("bad address (%q): %w", a, err)
}
acct = NewAccountTag(*tmp, nil)
}
account = append(account, acct)
}
return account, err
}