From 09f1560c74950d0aa0a918fc923768fa95831851 Mon Sep 17 00:00:00 2001 From: simlecode <69969590+simlecode@users.noreply.github.com> Date: Sat, 12 Oct 2024 14:24:59 +0800 Subject: [PATCH] feat: generate deal info from message --- cmd/droplet-client/direct-deal.go | 84 +++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/cmd/droplet-client/direct-deal.go b/cmd/droplet-client/direct-deal.go index 63ef23b1..769d8596 100644 --- a/cmd/droplet-client/direct-deal.go +++ b/cmd/droplet-client/direct-deal.go @@ -65,6 +65,7 @@ var directDealCommands = &cli.Command{ Usage: "direct deal tools", Subcommands: []*cli.Command{ directDealAllocate, + genDealInfoFromMessage, }, } @@ -594,3 +595,86 @@ func autoImportDealToDroplet(cliCtx *cli.Context, allocations map[types.Allocati return mapi.ImportDirectDeal(ctx, ¶ms) } + +var genDealInfoFromMessage = &cli.Command{ + Name: "generate-deal-info-from-message", + Usage: "generate deal info from message", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "msg", + Usage: "message cid", + Required: true, + }, + &cli.StringFlag{ + Name: "manifest", + Usage: "Path to the manifest file", + Required: true, + }, + &cli.StringFlag{ + Name: "output", + Usage: "Output deal information to a file.", + Required: true, + }, + &cli.BoolFlag{ + Name: "piece-size-padded", + Hidden: true, + }, + }, + Action: func(cliCtx *cli.Context) error { + fapi, fcloser, err := cli2.NewFullNode(cliCtx, cli2.OldClientRepoPath) + if err != nil { + return err + } + defer fcloser() + + msgCid, err := cid.Decode(cliCtx.String("msg")) + if err != nil { + return err + } + + pieceInfos, _, err := pieceInfosFromFile(cliCtx) + if err != nil { + return err + } + + ctx := cliCtx.Context + ml, err := fapi.StateSearchMsg(ctx, types.EmptyTSK, msgCid, -1, true) + if err != nil { + return err + } + + if ml.Receipt.ExitCode != 0 { + return fmt.Errorf("message execution failed with exit code %d", ml.Receipt.ExitCode) + } + + tr := &types.TransferReturn{} + err = tr.UnmarshalCBOR(bytes.NewReader(ml.Receipt.Return)) + if err != nil { + return err + } + + ar := &types.AllocationsResponse{} + err = ar.UnmarshalCBOR(bytes.NewReader(tr.RecipientData)) + if err != nil { + return err + } + fmt.Println("allocations: ", len(ar.NewAllocations)) + + msg, err := fapi.ChainGetMessage(ctx, msgCid) + if err != nil { + return err + } + + allocations := make(map[types.AllocationId]types.Allocation) + for _, allocationID := range ar.NewAllocations { + a, err := fapi.StateGetAllocation(context.Background(), msg.From, allocationID, types.EmptyTSK) + if err != nil { + return err + } + allocations[allocationID] = *a + + } + + return writeAllocationsToFile(cliCtx.String("output"), allocations, pieceInfos) + }, +}