|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/ipfs/go-cid" |
| 12 | + "github.com/urfave/cli/v2" |
| 13 | + "golang.org/x/xerrors" |
| 14 | + |
| 15 | + "github.com/filecoin-project/go-address" |
| 16 | + "github.com/filecoin-project/go-state-types/abi" |
| 17 | + |
| 18 | + curioproof "github.com/filecoin-project/curio/lib/proof" |
| 19 | + "github.com/filecoin-project/curio/lib/proofsvc" |
| 20 | + "github.com/filecoin-project/curio/lib/proofsvc/common" |
| 21 | + |
| 22 | + "github.com/filecoin-project/lotus/chain/types" |
| 23 | + cliutil "github.com/filecoin-project/lotus/cli/util" |
| 24 | +) |
| 25 | + |
| 26 | +type commit2In struct { |
| 27 | + SectorNum int64 `json:"SectorNum"` |
| 28 | + Phase1Out []byte `json:"Phase1Out"` |
| 29 | + SectorSize uint64 `json:"SectorSize"` |
| 30 | +} |
| 31 | + |
| 32 | +var proofsvcClientCmd = &cli.Command{ |
| 33 | + Name: "proofsvc-client", |
| 34 | + Usage: "Interact with the remote proof service", |
| 35 | + Subcommands: []*cli.Command{ |
| 36 | + proofsvcCreateVoucherCmd, |
| 37 | + proofsvcSubmitCmd, |
| 38 | + proofsvcStatusCmd, |
| 39 | + }, |
| 40 | +} |
| 41 | + |
| 42 | +var proofsvcCreateVoucherCmd = &cli.Command{ |
| 43 | + Name: "create-voucher", |
| 44 | + Usage: "Create a client voucher", |
| 45 | + Flags: []cli.Flag{ |
| 46 | + &cli.StringFlag{Name: "client", Required: true}, |
| 47 | + &cli.StringFlag{Name: "amount", Required: true}, |
| 48 | + }, |
| 49 | + Action: func(cctx *cli.Context) error { |
| 50 | + clientStr := cctx.String("client") |
| 51 | + amountStr := cctx.String("amount") |
| 52 | + |
| 53 | + full, closer, err := cliutil.GetFullNodeAPIV1(cctx) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + defer closer() |
| 58 | + |
| 59 | + svc := common.NewService(full) |
| 60 | + |
| 61 | + clientAddr, err := address.NewFromString(clientStr) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + cumulativeAmount, err := types.ParseFIL(amountStr) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + idAddr, err := full.StateLookupID(cctx.Context, clientAddr, types.EmptyTSK) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + clientID, err := address.IDFromAddress(idAddr) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + state, err := svc.GetClientState(cctx.Context, clientID) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + nonce := state.LastNonce + 1 |
| 85 | + cumulativeAmount = types.FIL(types.BigAdd(types.BigInt(state.VoucherRedeemed), types.BigInt(cumulativeAmount))) |
| 86 | + |
| 87 | + voucher, err := svc.CreateClientVoucher(cctx.Context, clientID, cumulativeAmount.Int, nonce) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + sig, err := full.WalletSign(cctx.Context, clientAddr, voucher) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + output := struct { |
| 98 | + Voucher string `json:"voucher"` |
| 99 | + Signature string `json:"signature"` |
| 100 | + Nonce uint64 `json:"nonce"` |
| 101 | + }{ |
| 102 | + Voucher: hex.EncodeToString(voucher), |
| 103 | + Signature: hex.EncodeToString(sig.Data), |
| 104 | + Nonce: nonce, |
| 105 | + } |
| 106 | + |
| 107 | + enc, err := json.MarshalIndent(output, "", " ") |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + fmt.Print(string(enc)) |
| 112 | + return nil |
| 113 | + }, |
| 114 | +} |
| 115 | + |
| 116 | +var proofsvcSubmitCmd = &cli.Command{ |
| 117 | + Name: "submit", |
| 118 | + Usage: "Submit a proof request", |
| 119 | + Flags: []cli.Flag{ |
| 120 | + &cli.StringFlag{Name: "c1", Usage: "path to lotus-bench c1 json", Required: true}, |
| 121 | + &cli.StringFlag{Name: "miner", Usage: "miner address", Required: true}, |
| 122 | + &cli.Uint64Flag{Name: "client-id", Required: true}, |
| 123 | + &cli.Uint64Flag{Name: "nonce", Required: true}, |
| 124 | + &cli.StringFlag{Name: "amount", Required: true}, |
| 125 | + &cli.StringFlag{Name: "sig", Required: true}, |
| 126 | + }, |
| 127 | + Action: func(cctx *cli.Context) error { |
| 128 | + inb, err := os.ReadFile(cctx.String("c1")) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + var c2 commit2In |
| 133 | + if err := json.Unmarshal(inb, &c2); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + minerAddr, err := address.NewFromString(cctx.String("miner")) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + idAddr, err := address.IDFromAddress(minerAddr) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + c1raw, err := curioproof.DecodeCommit1OutRaw(bytes.NewReader(c2.Phase1Out)) |
| 147 | + if err != nil { |
| 148 | + return xerrors.Errorf("decode c1: %w", err) |
| 149 | + } |
| 150 | + |
| 151 | + pd := common.ProofData{ |
| 152 | + SectorID: &abi.SectorID{Miner: abi.ActorID(idAddr), Number: abi.SectorNumber(c2.SectorNum)}, |
| 153 | + PoRep: &c1raw, |
| 154 | + } |
| 155 | + |
| 156 | + pbytes, err := json.Marshal(pd) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + |
| 161 | + ctx := context.Background() |
| 162 | + cid, err := proofsvc.UploadProofData(ctx, pbytes) |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + amt, err := types.ParseFIL(cctx.String("amount")) |
| 168 | + if err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + sigBytes, err := hex.DecodeString(cctx.String("sig")) |
| 172 | + if err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + |
| 176 | + pr := common.ProofRequest{ |
| 177 | + Data: cid, |
| 178 | + PaymentClientID: int64(cctx.Uint64("client-id")), |
| 179 | + PaymentNonce: int64(cctx.Uint64("nonce")), |
| 180 | + PaymentCumulativeAmount: abi.NewTokenAmount(amt.Int64()), |
| 181 | + PaymentSignature: sigBytes, |
| 182 | + } |
| 183 | + |
| 184 | + _, err = proofsvc.RequestProof(pr) |
| 185 | + if err != nil { |
| 186 | + return err |
| 187 | + } |
| 188 | + |
| 189 | + fmt.Println("request-id:", cid.String()) |
| 190 | + return nil |
| 191 | + }, |
| 192 | +} |
| 193 | + |
| 194 | +var proofsvcStatusCmd = &cli.Command{ |
| 195 | + Name: "status", |
| 196 | + Usage: "Check proof status", |
| 197 | + Flags: []cli.Flag{ |
| 198 | + &cli.StringFlag{Name: "id", Required: true}, |
| 199 | + }, |
| 200 | + Action: func(cctx *cli.Context) error { |
| 201 | + rcid, err := cid.Parse(cctx.String("id")) |
| 202 | + if err != nil { |
| 203 | + return err |
| 204 | + } |
| 205 | + resp, err := proofsvc.GetProofStatus(rcid) |
| 206 | + if err != nil { |
| 207 | + return err |
| 208 | + } |
| 209 | + b, err := json.MarshalIndent(resp, "", " ") |
| 210 | + if err != nil { |
| 211 | + return err |
| 212 | + } |
| 213 | + fmt.Print(string(b)) |
| 214 | + return nil |
| 215 | + }, |
| 216 | +} |
0 commit comments