This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
182 lines (167 loc) · 3.81 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
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
// Mempool examples
//
package main
import (
"context"
// "encoding/hex"
// "encoding/json"
"flag"
"fmt"
"io"
"os"
"blockwatch.cc/tzgo/rpc"
"blockwatch.cc/tzgo/tezos"
"github.com/echa/log"
)
var (
flags = flag.NewFlagSet("mempool", flag.ContinueOnError)
verbose bool
node string
ttl int64
)
func init() {
flags.Usage = func() {}
flags.BoolVar(&verbose, "v", false, "be verbose")
flags.StringVar(&node, "node", "https://rpc.tzstats.com", "Tezos node URL")
flags.Int64Var(&ttl, "ttl", 120, "Operation TTL")
}
func main() {
if err := flags.Parse(os.Args[1:]); err != nil {
if err == flag.ErrHelp {
fmt.Println("Usage: mempool [args] <cmd> [sub-args]")
fmt.Println("\nCommands")
fmt.Printf(" stream [<filter>] stream new ops entering the mempool, optional filter\n")
fmt.Printf(" wait <ophash> wait for operation to be visible in mempool\n")
fmt.Printf(" info print info about mempool\n")
fmt.Println("\nArguments")
flags.PrintDefaults()
os.Exit(0)
}
fmt.Println("Error:", err)
return
}
if err := run(); err != nil {
fmt.Println("Error:", err)
}
}
func run() error {
n := flags.NArg()
if n < 1 {
return fmt.Errorf("Command required")
}
switch {
case verbose:
log.SetLevel(log.LevelDebug)
default:
log.SetLevel(log.LevelWarn)
}
rpc.UseLogger(log.Log)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := rpc.NewClient(node, nil)
if err != nil {
return err
}
switch cmd := flags.Arg(0); cmd {
case "info":
return info(ctx, c)
case "stream":
return stream(ctx, c, flags.Arg(1))
case "wait":
if n != 2 {
return fmt.Errorf("Missing ophash")
}
return wait(ctx, c, flags.Arg(1))
default:
return fmt.Errorf("Unknown command %q", cmd)
}
}
func info(ctx context.Context, c *rpc.Client) error {
mem, err := c.GetMempool(ctx)
if err != nil {
return err
}
cnt := make([]map[tezos.OpType]int, 5)
for i, v := range [][]*rpc.Operation{
mem.Applied,
mem.Refused,
mem.BranchRefused,
mem.BranchDelayed,
mem.Unprocessed,
} {
if cnt[i] == nil {
cnt[i] = make(map[tezos.OpType]int)
}
m := cnt[i]
for _, op := range v {
for _, o := range op.Contents {
k := o.Kind()
if c, ok := m[k]; ok {
m[k] = c + 1
} else {
m[k] = 1
}
}
}
}
fmt.Println("Applied:", len(mem.Applied))
for n, c := range cnt[0] {
fmt.Printf(" %s (%d)\n", n, c)
}
fmt.Println("Refused:", len(mem.Refused))
for n, c := range cnt[1] {
fmt.Printf(" %s (%d)\n", n, c)
}
fmt.Println("Branch refused:", len(mem.BranchRefused))
for n, c := range cnt[2] {
fmt.Printf(" %s (%d)\n", n, c)
}
fmt.Println("Branch delayed:", len(mem.BranchDelayed))
for n, c := range cnt[3] {
fmt.Printf(" %s (%d)\n", n, c)
}
fmt.Println("Unprocessed:", len(mem.Unprocessed))
for n, c := range cnt[4] {
fmt.Printf(" %s (%d)\n", n, c)
}
return nil
}
func stream(ctx context.Context, c *rpc.Client, flt string) error {
var mon *rpc.MempoolMonitor
for {
if mon == nil {
fmt.Println("Connecting monitor")
mon = rpc.NewMempoolMonitor()
if err := c.MonitorMempool(ctx, mon); err != nil {
mon.Close()
return err
}
}
ops, err := mon.Recv(ctx)
if err != nil {
if err != io.EOF {
fmt.Println("Monitor failed")
return err
}
head, err := c.GetTipHeader(ctx)
if err == nil {
fmt.Println("Monitor closed on new block", head.Level, head.Hash)
} else {
fmt.Println("Monitor closed:", err)
}
mon = nil
continue
}
for _, op := range ops {
fmt.Println(op.Hash, op.Contents[0].Kind())
// calculate cost but discard result to catch errors
_ = op.TotalCosts()
}
}
}
func wait(ctx context.Context, c *rpc.Client, hash string) error {
fmt.Println("Not implemented yet")
return nil
}