|
| 1 | +package chains |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path" |
| 9 | + |
| 10 | + authv1betav1 "cosmossdk.io/api/cosmos/auth/v1beta1" |
| 11 | + autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" |
| 12 | + reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1" |
| 13 | + "google.golang.org/grpc" |
| 14 | + "google.golang.org/grpc/credentials" |
| 15 | + "google.golang.org/grpc/credentials/insecure" |
| 16 | + "google.golang.org/protobuf/proto" |
| 17 | + "google.golang.org/protobuf/reflect/protodesc" |
| 18 | + "google.golang.org/protobuf/reflect/protoregistry" |
| 19 | + "google.golang.org/protobuf/types/descriptorpb" |
| 20 | +) |
| 21 | + |
| 22 | +type Conn struct { |
| 23 | + chainName string |
| 24 | + config *ChainConfig |
| 25 | + configDir string |
| 26 | + client *grpc.ClientConn |
| 27 | + |
| 28 | + ProtoFiles *protoregistry.Files |
| 29 | + ModuleOptions map[string]*autocliv1.ModuleOptions |
| 30 | +} |
| 31 | + |
| 32 | +func NewConn(chainName string, cfg *ChainConfig) (*Conn, error) { |
| 33 | + configDir, err := ConfigDir() |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + |
| 38 | + return &Conn{ |
| 39 | + chainName: chainName, |
| 40 | + config: cfg, |
| 41 | + configDir: configDir, |
| 42 | + }, nil |
| 43 | +} |
| 44 | + |
| 45 | +// fdsCacheFilename returns the filename for the cached file descriptor set. |
| 46 | +func (c *Conn) fdsCacheFilename() string { |
| 47 | + return path.Join(c.configDir, fmt.Sprintf("%s.fds", c.chainName)) |
| 48 | +} |
| 49 | + |
| 50 | +// appOptsCacheFilename returns the filename for the app options cache file. |
| 51 | +func (c *Conn) appOptsCacheFilename() string { |
| 52 | + return path.Join(c.configDir, fmt.Sprintf("%s.autocli", c.chainName)) |
| 53 | +} |
| 54 | + |
| 55 | +func (c *Conn) Load(ctx context.Context) error { |
| 56 | + var err error |
| 57 | + fdSet := &descriptorpb.FileDescriptorSet{} |
| 58 | + fdsFilename := c.fdsCacheFilename() |
| 59 | + |
| 60 | + if _, err := os.Stat(fdsFilename); os.IsNotExist(err) { |
| 61 | + client, err := c.Connect() |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + reflectionClient := reflectionv1.NewReflectionServiceClient(client) |
| 67 | + fdRes, err := reflectionClient.FileDescriptors(ctx, &reflectionv1.FileDescriptorsRequest{}) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("error getting file descriptors: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + fdSet = &descriptorpb.FileDescriptorSet{File: fdRes.Files} |
| 73 | + bz, err := proto.Marshal(fdSet) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + if err = os.WriteFile(fdsFilename, bz, 0o600); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + } else { |
| 82 | + bz, err := os.ReadFile(fdsFilename) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + if err = proto.Unmarshal(bz, fdSet); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + c.ProtoFiles, err = protodesc.FileOptions{AllowUnresolvable: true}.NewFiles(fdSet) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("error building protoregistry.Files: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + appOptsFilename := c.appOptsCacheFilename() |
| 98 | + if _, err := os.Stat(appOptsFilename); os.IsNotExist(err) { |
| 99 | + client, err := c.Connect() |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + autocliQueryClient := autocliv1.NewQueryClient(client) |
| 105 | + appOptsRes, err := autocliQueryClient.AppOptions(ctx, &autocliv1.AppOptionsRequest{}) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("error getting autocli config: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + bz, err := proto.Marshal(appOptsRes) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + if err := os.WriteFile(appOptsFilename, bz, 0o600); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + c.ModuleOptions = appOptsRes.ModuleOptions |
| 120 | + } else { |
| 121 | + bz, err := os.ReadFile(appOptsFilename) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + var appOptsRes autocliv1.AppOptionsResponse |
| 127 | + if err := proto.Unmarshal(bz, &appOptsRes); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + c.ModuleOptions = appOptsRes.ModuleOptions |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func (c *Conn) Connect() (*grpc.ClientConn, error) { |
| 138 | + if c.client != nil { |
| 139 | + return c.client, nil |
| 140 | + } |
| 141 | + |
| 142 | + var err error |
| 143 | + creds := credentials.NewTLS(&tls.Config{ |
| 144 | + MinVersion: tls.VersionTLS12, |
| 145 | + }) |
| 146 | + |
| 147 | + c.client, err = grpc.NewClient(c.config.GRPCEndpoint, grpc.WithTransportCredentials(creds)) |
| 148 | + if err != nil { |
| 149 | + return nil, fmt.Errorf("failed to connect to gRPC server: %w", err) |
| 150 | + } |
| 151 | + |
| 152 | + // try connection by querying an endpoint |
| 153 | + // fallback to insecure if it doesn't work |
| 154 | + authClient := authv1betav1.NewQueryClient(c.client) |
| 155 | + if _, err = authClient.Params(context.Background(), &authv1betav1.QueryParamsRequest{}); err != nil { |
| 156 | + creds = insecure.NewCredentials() |
| 157 | + c.client, err = grpc.NewClient(c.config.GRPCEndpoint, grpc.WithTransportCredentials(creds)) |
| 158 | + if err != nil { |
| 159 | + return nil, fmt.Errorf("failed to connect to gRPC server: %w", err) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return c.client, nil |
| 164 | +} |
0 commit comments