-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
188 lines (165 loc) · 4.8 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
package main
import (
"context"
"fmt"
"log"
"os"
cli "github.com/urfave/cli/v2"
)
// TODO: Adopt a logging library
func main() {
var natsURL, walletURL, daemonURL string
var maxExtraAncestors, ignoreBelowHeight int
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "nats-url",
Aliases: []string{"nats", "n"},
Value: "http://localhost:4222",
Usage: "URL to the NATS Streaming Server",
Destination: &natsURL,
},
&cli.IntFlag{
Name: "ignore-below-height",
Aliases: []string{"i"},
Value: 0,
Usage: "Ignores Blocks and Transactions with height lower than this value",
Destination: &ignoreBelowHeight,
},
},
Commands: []*cli.Command{
{
Name: "ping",
Usage: "Pings the NATS server, to verify that connection is configured properly",
Action: func(c *cli.Context) error {
evPublisher := NewNatsPublishingClient(natsURL)
if !evPublisher.IsConnected() {
return fmt.Errorf("failed to ping NATS at %s", natsURL)
}
return nil
},
},
{
Name: "transaction",
Aliases: []string{"tx"},
Usage: "Gather extra context about a Monero Tx and publish it through NATS",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "monero-wallet-rpc-url",
Aliases: []string{"wallet", "w"},
Value: "http://localhost:38083",
Usage: "URL to the RPC server of the Monero Wallet",
Destination: &walletURL,
},
},
Action: func(c *cli.Context) error {
txid := c.Args().First()
if txid == "" {
return fmt.Errorf("tx command requires a txid argument")
}
rpcClient := NewRPCClient(walletURL)
evPublisher := NewNatsPublishingClient(natsURL)
return ProcessTxid(txid, ignoreBelowHeight, rpcClient, evPublisher)
},
},
{
Name: "block",
Aliases: []string{"blk"},
Usage: "Gather extra context about a Monero Block and publish it through NATS",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "monero-daemon-rpc-url",
Aliases: []string{"daemon", "d"},
Value: "http://localhost:38081",
Usage: "URL to the RPC server of the Monero Daemon",
Destination: &daemonURL,
},
&cli.IntFlag{
Name: "max-extra-ancestor-blocks",
Aliases: []string{"extra-ancestors", "ea"},
Value: 0,
Usage: "Max number of extra ancestor blocks to include with each published block",
Destination: &maxExtraAncestors,
},
},
Action: func(c *cli.Context) error {
blockHash := c.Args().First()
if blockHash == "" {
return fmt.Errorf("block command requires a blockHash argument")
}
rpcClient := NewRPCClient(daemonURL)
evPublisher := NewNatsPublishingClient(natsURL)
return ProcessBlockHash(blockHash, maxExtraAncestors, ignoreBelowHeight, rpcClient, evPublisher)
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
type TxGetter interface {
GetTransferByTxid(context.Context, string) ([]RpcTx, error)
}
type TxEventPublisher interface {
PushTxEvent(Tx) error
}
// ProcessTxid fetches extra context about the Monero Transaction from
// Monero Wallet RPC. Then publishes a NATS event about the Transaction.
func ProcessTxid(txid string, ignoreBelowheight int, rc TxGetter, nc TxEventPublisher) error {
ctx := context.Background()
transfers, err := rc.GetTransferByTxid(ctx, txid)
if err != nil {
return err
}
tx, err := RpcTxToTx(transfers)
if err != nil {
return err
}
if tx.Height < ignoreBelowheight {
// The Tx is below ignoring height. It won't be
// published to NATS
return nil
}
return nc.PushTxEvent(*tx)
}
type BlockGetter interface {
GetBlockByHash(context.Context, string) (*RpcBlock, error)
GetBlockHeadersRange(context.Context, int, int) ([]RpcBlockHeader, error)
}
type BlockEventPublisher interface {
PushBlockEvent(Block) error
}
func ProcessBlockHash(blockHash string, maxExtraAncestors, ignoreBelowHeight int, bg BlockGetter, nc BlockEventPublisher) error {
ctx := context.Background()
rpcBlock, err := bg.GetBlockByHash(ctx, blockHash)
if err != nil {
return err
}
blk := RpcBlockToBlock(*rpcBlock)
if blk.Height < ignoreBelowHeight {
// Block is below ignoring height. Its ancestors won't be fetched
// and it won't be published to NATS
return nil
}
if blk.Height == 0 {
// Blocks is Genesis Block. It has no ancestors
return nc.PushBlockEvent(blk)
}
end := blk.Height - 1
start := blk.Height - maxExtraAncestors
if start < 0 {
start = 0
}
blocks, err := bg.GetBlockHeadersRange(ctx, start, end)
if err != nil {
return err
}
prevHashes := []string{}
for _, b := range blocks {
prevHashes = append(prevHashes, b.Hash)
}
blk.PrevHashes = prevHashes
return nc.PushBlockEvent(blk)
}