|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + ouroboros "github.com/cloudstruct/go-ouroboros-network" |
| 7 | + "github.com/cloudstruct/go-ouroboros-network/protocol/localstatequery" |
| 8 | + "os" |
| 9 | +) |
| 10 | + |
| 11 | +type queryFlags struct { |
| 12 | + flagset *flag.FlagSet |
| 13 | +} |
| 14 | + |
| 15 | +func newQueryFlags() *queryFlags { |
| 16 | + f := &queryFlags{ |
| 17 | + flagset: flag.NewFlagSet("query", flag.ExitOnError), |
| 18 | + } |
| 19 | + return f |
| 20 | +} |
| 21 | + |
| 22 | +func buildLocalStateQueryConfig() localstatequery.Config { |
| 23 | + return localstatequery.Config{} |
| 24 | +} |
| 25 | + |
| 26 | +func testQuery(f *globalFlags) { |
| 27 | + queryFlags := newQueryFlags() |
| 28 | + err := queryFlags.flagset.Parse(f.flagset.Args()[1:]) |
| 29 | + if err != nil { |
| 30 | + fmt.Printf("failed to parse subcommand args: %s\n", err) |
| 31 | + os.Exit(1) |
| 32 | + } |
| 33 | + if len(queryFlags.flagset.Args()) < 1 { |
| 34 | + fmt.Printf("ERROR: you must specify a query\n") |
| 35 | + os.Exit(1) |
| 36 | + } |
| 37 | + |
| 38 | + conn := createClientConnection(f) |
| 39 | + errorChan := make(chan error) |
| 40 | + go func() { |
| 41 | + for { |
| 42 | + err := <-errorChan |
| 43 | + fmt.Printf("ERROR: %s\n", err) |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | + }() |
| 47 | + o, err := ouroboros.New( |
| 48 | + ouroboros.WithConnection(conn), |
| 49 | + ouroboros.WithNetworkMagic(uint32(f.networkMagic)), |
| 50 | + ouroboros.WithErrorChan(errorChan), |
| 51 | + ouroboros.WithNodeToNode(f.ntnProto), |
| 52 | + ouroboros.WithKeepAlive(true), |
| 53 | + ouroboros.WithLocalStateQueryConfig(buildLocalStateQueryConfig()), |
| 54 | + ) |
| 55 | + if err != nil { |
| 56 | + fmt.Printf("ERROR: %s\n", err) |
| 57 | + os.Exit(1) |
| 58 | + } |
| 59 | + o.LocalStateQuery.Client.Start() |
| 60 | + |
| 61 | + switch queryFlags.flagset.Args()[0] { |
| 62 | + case "current-era": |
| 63 | + era, err := o.LocalStateQuery.Client.GetCurrentEra() |
| 64 | + if err != nil { |
| 65 | + fmt.Printf("ERROR: failure querying current era: %s\n", err) |
| 66 | + os.Exit(1) |
| 67 | + } |
| 68 | + fmt.Printf("current-era: %d\n", era) |
| 69 | + case "tip": |
| 70 | + era, err := o.LocalStateQuery.Client.GetCurrentEra() |
| 71 | + if err != nil { |
| 72 | + fmt.Printf("ERROR: failure querying current era: %s\n", err) |
| 73 | + os.Exit(1) |
| 74 | + } |
| 75 | + epochNo, err := o.LocalStateQuery.Client.GetEpochNo() |
| 76 | + if err != nil { |
| 77 | + fmt.Printf("ERROR: failure querying current epoch: %s\n", err) |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | + blockNo, err := o.LocalStateQuery.Client.GetChainBlockNo() |
| 81 | + if err != nil { |
| 82 | + fmt.Printf("ERROR: failure querying current chain block number: %s\n", err) |
| 83 | + os.Exit(1) |
| 84 | + } |
| 85 | + point, err := o.LocalStateQuery.Client.GetChainPoint() |
| 86 | + if err != nil { |
| 87 | + fmt.Printf("ERROR: failure querying current chain point: %s\n", err) |
| 88 | + os.Exit(1) |
| 89 | + } |
| 90 | + fmt.Printf("tip: era = %d, epoch = %d, blockNo = %d, slot = %d, hash = %x\n", era, epochNo, blockNo, point.Slot, point.Hash) |
| 91 | + case "system-start": |
| 92 | + systemStart, err := o.LocalStateQuery.Client.GetSystemStart() |
| 93 | + if err != nil { |
| 94 | + fmt.Printf("ERROR: failure querying system start: %s\n", err) |
| 95 | + os.Exit(1) |
| 96 | + } |
| 97 | + fmt.Printf("system-start: year = %d, day = %d, picoseconds = %d\n", systemStart.Year, systemStart.Day, systemStart.Picoseconds) |
| 98 | + case "era-history": |
| 99 | + eraHistory, err := o.LocalStateQuery.Client.GetEraHistory() |
| 100 | + if err != nil { |
| 101 | + fmt.Printf("ERROR: failure querying era history: %s\n", err) |
| 102 | + os.Exit(1) |
| 103 | + } |
| 104 | + fmt.Printf("era-history: %#v", *eraHistory) |
| 105 | + default: |
| 106 | + fmt.Printf("ERROR: unknown query: %s\n", queryFlags.flagset.Args()[0]) |
| 107 | + os.Exit(1) |
| 108 | + } |
| 109 | +} |
0 commit comments