-
Notifications
You must be signed in to change notification settings - Fork 47
/
cmd_distribution.go
80 lines (71 loc) · 2.12 KB
/
cmd_distribution.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
package main
import (
"encoding/csv"
"flag"
"fmt"
"io"
"os"
"strconv"
"github.com/iov-one/weave"
bnsd "github.com/iov-one/weave/cmd/bnsd/app"
"github.com/iov-one/weave/x/distribution"
)
func cmdResetRevenue(input io.Reader, output io.Writer, args []string) error {
fl := flag.NewFlagSet("", flag.ExitOnError)
fl.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), `
Create a transaction for reseting a revenue stream with a new configuration.
`)
fl.PrintDefaults()
}
revenueFl := flHex(fl, "revenue", "", "A hex encoded ID of a revenue that is to be altered.")
destinationsFl := fl.String("destinations", "", "A path to a CSV file with destinations configuration. File should be a list of pairs (address, weight).")
fl.Parse(args)
destinations, err := readDestinations(*destinationsFl)
if err != nil {
return fmt.Errorf("cannot read %q destinations file: %s", *destinationsFl, err)
}
tx := &bnsd.Tx{
Sum: &bnsd.Tx_DistributionResetMsg{
DistributionResetMsg: &distribution.ResetMsg{
RevenueID: *revenueFl,
Destinations: destinations,
},
},
}
_, err = writeTx(output, tx)
return err
}
func readDestinations(csvpath string) ([]*distribution.Destination, error) {
fd, err := os.Open(csvpath)
if err != nil {
return nil, fmt.Errorf("cannot open file: %s", err)
}
defer fd.Close()
var destinations []*distribution.Destination
rd := csv.NewReader(fd)
for lineNo := 1; ; lineNo++ {
row, err := rd.Read()
if err != nil {
if err == io.EOF {
return destinations, nil
}
return destinations, err
}
if len(row) != 2 {
return destinations, fmt.Errorf("invalid line %d: expected 2 columns, got %d", lineNo, len(row))
}
address, err := weave.ParseAddress(row[0])
if err != nil {
return destinations, fmt.Errorf("invalid line %d: invalid address %q: %s", lineNo, row[0], err)
}
weight, err := strconv.ParseUint(row[1], 10, 32)
if err != nil {
return destinations, fmt.Errorf("invalid line %d: invalid weight (q-factor) %q: %s", lineNo, row[1], err)
}
destinations = append(destinations, &distribution.Destination{
Address: address,
Weight: int32(weight),
})
}
}