|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "os/signal" |
| 7 | + "strings" |
| 8 | + "syscall" |
| 9 | + |
| 10 | + "github.com/urfave/cli/v2" |
| 11 | + "golang.org/x/xerrors" |
| 12 | + |
| 13 | + "github.com/filecoin-project/sentinel-visor/chain" |
| 14 | + "github.com/filecoin-project/sentinel-visor/vector" |
| 15 | +) |
| 16 | + |
| 17 | +var Vector = &cli.Command{ |
| 18 | + Name: "vector", |
| 19 | + Usage: "Vector tooling for Visor.", |
| 20 | + Subcommands: []*cli.Command{ |
| 21 | + BuildVector, |
| 22 | + ExecuteVector, |
| 23 | + }, |
| 24 | +} |
| 25 | + |
| 26 | +var BuildVector = &cli.Command{ |
| 27 | + Name: "build", |
| 28 | + Usage: "Create a vector.", |
| 29 | + Flags: []cli.Flag{ |
| 30 | + &cli.Int64Flag{ |
| 31 | + Name: "from", |
| 32 | + Usage: "Limit actor and message processing to tipsets at or above `HEIGHT`", |
| 33 | + EnvVars: []string{"VISOR_HEIGHT_FROM"}, |
| 34 | + }, |
| 35 | + &cli.Int64Flag{ |
| 36 | + Name: "to", |
| 37 | + Usage: "Limit actor and message processing to tipsets at or below `HEIGHT`", |
| 38 | + Value: estimateCurrentEpoch(), |
| 39 | + DefaultText: "current epoch", |
| 40 | + EnvVars: []string{"VISOR_HEIGHT_TO"}, |
| 41 | + }, |
| 42 | + &cli.StringFlag{ |
| 43 | + Name: "tasks", |
| 44 | + Usage: "Comma separated list of tasks to build. Each task is reported separately in the database.", |
| 45 | + Value: strings.Join([]string{chain.BlocksTask}, ","), |
| 46 | + EnvVars: []string{"VISOR_VECTOR_TASKS"}, |
| 47 | + }, |
| 48 | + &cli.StringFlag{ |
| 49 | + Name: "actor-address", |
| 50 | + Usage: "Address of an actor.", |
| 51 | + }, |
| 52 | + &cli.StringFlag{ |
| 53 | + Name: "vector-file", |
| 54 | + Usage: "Path of vector file.", |
| 55 | + Required: true, |
| 56 | + }, |
| 57 | + &cli.StringFlag{ |
| 58 | + Name: "vector-desc", |
| 59 | + Usage: "Short description of the test vector.", |
| 60 | + Required: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + Action: build, |
| 64 | +} |
| 65 | + |
| 66 | +func build(cctx *cli.Context) error { |
| 67 | + // Set up a context that is canceled when the command is interrupted |
| 68 | + ctx, cancel := context.WithCancel(cctx.Context) |
| 69 | + |
| 70 | + // Set up a signal handler to cancel the context |
| 71 | + go func() { |
| 72 | + interrupt := make(chan os.Signal, 1) |
| 73 | + signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT) |
| 74 | + select { |
| 75 | + case <-interrupt: |
| 76 | + cancel() |
| 77 | + case <-ctx.Done(): |
| 78 | + } |
| 79 | + }() |
| 80 | + |
| 81 | + if err := setupLogging(cctx); err != nil { |
| 82 | + return xerrors.Errorf("setup logging: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + builder, err := vector.NewBuilder(cctx) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + schema, err := builder.Build(ctx) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + return schema.Persist(cctx.String("vector-file")) |
| 96 | +} |
| 97 | + |
| 98 | +var ExecuteVector = &cli.Command{ |
| 99 | + Name: "execute", |
| 100 | + Usage: "execute a test vector", |
| 101 | + Flags: []cli.Flag{ |
| 102 | + &cli.StringFlag{ |
| 103 | + Name: "vector-file", |
| 104 | + Usage: "Path to vector file.", |
| 105 | + Required: true, |
| 106 | + }, |
| 107 | + }, |
| 108 | + Action: execute, |
| 109 | +} |
| 110 | + |
| 111 | +func execute(cctx *cli.Context) error { |
| 112 | + // Set up a context that is canceled when the command is interrupted |
| 113 | + ctx, cancel := context.WithCancel(cctx.Context) |
| 114 | + |
| 115 | + // Set up a signal handler to cancel the context |
| 116 | + go func() { |
| 117 | + interrupt := make(chan os.Signal, 1) |
| 118 | + signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT) |
| 119 | + select { |
| 120 | + case <-interrupt: |
| 121 | + cancel() |
| 122 | + case <-ctx.Done(): |
| 123 | + } |
| 124 | + }() |
| 125 | + |
| 126 | + if err := setupLogging(cctx); err != nil { |
| 127 | + return xerrors.Errorf("setup logging: %w", err) |
| 128 | + } |
| 129 | + runner, err := vector.NewRunner(ctx, cctx.String("vector-file"), 0) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + err = runner.Run(ctx) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + return runner.Validate(ctx) |
| 140 | +} |
0 commit comments