-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
240 lines (218 loc) · 6.78 KB
/
Copy pathmain.go
File metadata and controls
240 lines (218 loc) · 6.78 KB
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
236
237
238
239
240
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
filexp "github.com/aschmahmann/filexp/internal"
"github.com/aschmahmann/filexp/internal/state"
filaddr "github.com/filecoin-project/go-address"
filbuiltin "github.com/filecoin-project/go-state-types/builtin"
logging "github.com/ipfs/go-log/v2"
"github.com/mattn/go-isatty"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
)
var log = filexp.Logger
// 20 is considered good for WdPoST: https://github.com/filecoin-project/builtin-actors/blob/v13.0.0/runtime/src/runtime/policy.rs#L290-L293
// nevertheless bump to 30 as per https://filecoinproject.slack.com/archives/C02D73MHM63/p1718762303033709?thread_ts=1718693790.469889&cid=C02D73MHM63
var defaultRpcLookbackEpochs = uint(30)
const chainLoveURL = "https://api.chain.love/"
var stateFlags = []cli.Flag{
&cli.PathFlag{
Name: "car",
Usage: "Path to state snapshot CAR, tipset inferred from car root",
},
&cli.StringSliceFlag{
Name: "tipset-cids",
Usage: "Specific tipset CIDs to get directly over libp2p",
},
&cli.StringFlag{
Name: "rpc-endpoint",
Usage: "Filecoin RPC API endpoint to determine current tipset",
},
&cli.StringFlag{
Name: "rpc-fullnode",
Usage: "Filecoin Full Node RPC API, to use as source of current tipset and all IPLD blocks",
},
&cli.UintFlag{
Name: "lookback-epochs",
Usage: "How many epochs to look back when pulling state from the network",
Value: defaultRpcLookbackEpochs,
DefaultText: fmt.Sprintf("%d epochs / %s minutes",
defaultRpcLookbackEpochs,
strconv.FormatFloat(float64(defaultRpcLookbackEpochs*filbuiltin.EpochDurationSeconds)/60, 'f', -1, 64),
),
},
&cli.BoolFlag{
Name: "trust-chainlove",
Usage: "Equivalent to --rpc-endpoint=https://api.chain.love",
DisableDefaultText: true,
},
&cli.BoolFlag{
Name: "count-unique-cids",
Usage: "Keep track of all CIDs seen, can be memory intensive",
},
}
func main() {
logging.SetLogLevel("*", "INFO")
// the network stack is incredibly chatty: silence it all
for _, c := range []string{
"rpc",
"bitswap",
"dht",
"dht/RtRefreshManager",
"routing/http/contentrouter",
"net/identify",
"bs:sess",
"bitswap/bsnet",
"bitswap/session",
"bitswap_network",
"bitswap/network",
"bitswap-client",
"bitswap/client",
"bitswap/client/msgq",
"swarm2",
"connmgr",
"canonical-log",
} {
logging.SetLogLevel(c, "ERROR")
}
app := &cli.App{
Name: "filexp",
Usage: "explore filecoin state",
Commands: []*cli.Command{
{
Name: "get-balance",
Usage: "<actor>",
Description: "Get the balance for a given actor",
Flags: append([]cli.Flag{}, stateFlags...),
Action: func(cctx *cli.Context) error {
actorAddr, err := filaddr.NewFromString(cctx.Args().Get(0))
if err != nil {
return err
}
bg, ts, err := getAnchorPoint(cctx, cctx.Bool("count-unique-cids"))
if err != nil {
return err
}
defer bg.LogStats()
return state.GetBalance(cctx.Context, bg, ts, actorAddr)
},
},
{
Name: "fil-to-eth-address",
Usage: "<fX....>",
Description: "Converts an fX address to a 0x one if possible",
Flags: append([]cli.Flag{}, stateFlags...),
Action: filToEthAddr,
},
{
Name: "addresses",
Usage: "<address>",
Description: "Lists all the address types associated with an fX or 0x address. Note: will not back calculate fX addresses for f0 or masked ID 0x addresses",
Flags: append([]cli.Flag{}, stateFlags...),
Action: filAddrs,
},
{
Name: "msig-coins",
Usage: "<signer-address>",
Description: "Add up all of the coins controlled by multisigs with the given signer and signing threshold of 1",
Flags: append([]cli.Flag{}, stateFlags...),
Action: func(cctx *cli.Context) error {
signerAddr, err := filaddr.NewFromString(cctx.Args().Get(0))
if err != nil {
return err
}
bg, ts, err := getAnchorPoint(cctx, cctx.Bool("count-unique-cids"))
if err != nil {
return err
}
defer bg.LogStats()
return state.GetCoins(cctx.Context, bg, ts, signerAddr)
},
},
{
Name: "enumerate-actors",
Description: "List all actors",
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "count-only",
Usage: "will not emit the actor IDs, and just count them",
DisableDefaultText: true,
},
}, stateFlags...),
Action: func(cctx *cli.Context) error {
bg, ts, err := getAnchorPoint(cctx, cctx.Bool("count-unique-cids"))
if err != nil {
return err
}
defer bg.LogStats()
return state.GetActors(cctx.Context, bg, ts, cctx.Bool("count-only"))
},
},
{
Name: "dump-statemarketdeals",
Description: "Write legacy f05 state to STDOUT, semanitcally identical to FilRPC.StateMarketDeals()",
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "single-document",
Usage: "emit an inefficient single JSON object identical to FilRPC.StateMarketDeals.{Result}",
DisableDefaultText: true,
},
}, stateFlags...),
Action: func(cctx *cli.Context) error {
if isatty.IsTerminal(os.Stdout.Fd()) {
return xerrors.New("dumping to terminal is not supported - redirect the output to file or pipe")
}
bg, ts, err := getAnchorPoint(cctx, cctx.Bool("count-unique-cids"))
if err != nil {
return err
}
defer bg.LogStats()
return state.DumpStateF05(cctx.Context, bg, ts, os.Stdout, cctx.Bool("single-document"))
},
},
{
Name: "fevm-exec",
Description: "Execute a read-only FVM actor",
Usage: "<eth-addr> <eth-data>",
Flags: append(append([]cli.Flag{}, stateFlags...),
&cli.PathFlag{
Name: "output",
Usage: "The path for an output CAR containing the data loaded while computing the result (is not output if undefined)",
}),
Action: cmdFevmExec,
},
{
Name: "fevm-daemon",
Description: "Start a daemon that will respond to Ethereum JSON RPC calls. Note: passing an RPC endpoint enables real-time updates and lookback-epochs is not supported",
Usage: "[port]",
Flags: append([]cli.Flag{}, stateFlags...),
Action: cmdFevmDaemon,
},
},
}
topCtx, topCtxShutdown := context.WithCancel(context.Background())
// sighandler
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs,
syscall.SIGTERM,
syscall.SIGINT,
syscall.SIGHUP,
syscall.SIGPIPE,
)
// wait
s := <-sigs
log.Warnf("process received %s, cleaning up...", filexp.DecodeSigname(s))
topCtxShutdown()
}()
// end of sighandler
if err := app.RunContext(topCtx, os.Args); err != nil {
log.Fatalf("%+v", err)
os.Exit(1)
}
}