-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
194 lines (176 loc) · 5.43 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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/go-state-types/abi"
lotusapi "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/ipfs/go-cid"
bstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/urfave/cli/v2"
)
var stateFlags = []cli.Flag{
&cli.PathFlag{
Name: "car",
Usage: "path to state CAR",
},
&cli.StringSliceFlag{
Name: "tipset-cids",
Usage: "tipset CIDs for use when downloading state over the network",
},
&cli.BoolFlag{
Name: "trust-chainlove",
Usage: "ask chain.love for the state from roughly 2 hours ago",
},
}
func main() {
app := &cli.App{
Name: "filexp",
Usage: "explore filecoin state",
Commands: []*cli.Command{
{
Name: "msig-coins",
Usage: "<signer-key>",
Description: "Add up all of the coins controlled by multisigs with the given signer and signing threshold of 1. Either pass a state CAR, tipset CIDs, or trust chain.love for a recent time",
Flags: stateFlags,
Action: func(ctx *cli.Context) error {
args := ctx.Args()
signerKey := args.Get(0)
signerAddr, err := address.NewFromString(signerKey)
if err != nil {
return err
}
bs, tsk, err := getState(ctx)
if err != nil {
return err
}
return getCoins(ctx.Context, bs, tsk, signerAddr)
},
},
{
Name: "enumerate-actors",
Description: "List all actors. Either pass a state CAR, tipset CIDs, or trust chain.love for a recent time",
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "count-only",
Value: false,
DefaultText: "will not emit the actor IDs, and just count them",
},
}, stateFlags...),
Action: func(ctx *cli.Context) error {
bs, tsk, err := getState(ctx)
if err != nil {
return err
}
return getActors(ctx.Context, bs, tsk, ctx.Bool("count-only"))
},
},
{
Name: "get-balance",
Description: "Get the balance for a given actor. Either pass a state CAR, tipset CIDs, or trust chain.love for a recent time",
Flags: append([]cli.Flag{}, stateFlags...),
Action: func(ctx *cli.Context) error {
args := ctx.Args()
actorAddrString := args.Get(0)
actorAddr, err := address.NewFromString(actorAddrString)
if err != nil {
return err
}
bs, tsk, err := getState(ctx)
if err != nil {
return err
}
return getBalance(ctx.Context, bs, tsk, actorAddr)
},
},
{
Name: "fevm-exec",
Description: "Execute a read-only FVM actor. Either pass a state CAR, tipset CIDs, or trust chain.love for a recent time",
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. Either pass a state CAR, tipset CIDs, or trust chain.love for a recent time",
Usage: "[port]",
Flags: append([]cli.Flag{}, stateFlags...),
Action: cmdFevmDaemon,
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func getState(ctx *cli.Context) (bstore.Blockstore, types.TipSetKey, error) {
carSet := ctx.IsSet("car")
tsSet := ctx.IsSet("tipset-cids")
clSet := ctx.IsSet("trust-chainlove")
if carSet {
carLocation := ctx.String("car")
if tsSet || clSet {
return nil, types.EmptyTSK, fmt.Errorf("choose only one of CAR, tipset-cids, or trust-chainlove")
}
bs, tsk, err := getStateFromCar(ctx.Context, carLocation)
if err != nil {
return nil, types.EmptyTSK, err
}
return bs, tsk, nil
}
var tsk types.TipSetKey
if tsSet {
if clSet {
return nil, types.EmptyTSK, fmt.Errorf("choose only one of CAR, tipset-cids, or trust-chainlove")
}
cidStrs := ctx.StringSlice("tipset-cids")
var cids []cid.Cid
for _, s := range cidStrs {
c, err := cid.Decode(s)
if err != nil {
return nil, types.EmptyTSK, err
}
cids = append(cids, c)
}
tsk = types.NewTipSetKey(cids...)
}
if clSet {
var height int64
var err error
height, tsk, err = getStableChainloveTSK(ctx.Context)
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("using chainheight %d, with reported tipset %s\n", height, tsk)
}
bs, err := getStateDynamicallyLoadedFromBitswap(ctx.Context)
if err != nil {
return nil, types.EmptyTSK, err
}
return bs, tsk, err
}
func getStableChainloveTSK(ctx context.Context) (int64, types.TipSetKey, error) {
addr := "api.chain.love"
var api lotusapi.FullNodeStruct
closer, err := jsonrpc.NewMergeClient(ctx, "ws://"+addr+"/rpc/v0", "Filecoin", []interface{}{&api.Internal, &api.CommonStruct.Internal}, nil)
if err != nil {
return 0, types.EmptyTSK, fmt.Errorf("connecting with lotus API failed: %w", err)
}
defer closer()
chainHeightTwoHoursAgo := (time.Now().Add(-2*time.Hour).Unix() - 1598306400) / 30
tipset, err := api.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(chainHeightTwoHoursAgo), types.TipSetKey{})
if err != nil {
return 0, types.EmptyTSK, fmt.Errorf("could not get chain tipset from height %d: %w", chainHeightTwoHoursAgo, err)
}
return chainHeightTwoHoursAgo, tipset.Key(), nil
}