@@ -8,16 +8,17 @@ import (
88 "fmt"
99
1010 "google.golang.org/grpc"
11+ "google.golang.org/grpc/metadata"
1112
1213 "github.com/ava-labs/avalanchego/ids"
1314)
1415
15- // NewChainClient returns grpc.ClientConn that prefixes method calls with the
16- // provided chainID prefix
16+ // NewChainClient returns a grpc.ClientConn that sets the chain-id header for
17+ // all requests
1718func NewChainClient (uri string , chainID ids.ID , opts ... grpc.DialOption ) (* grpc.ClientConn , error ) {
1819 dialOpts := []grpc.DialOption {
19- grpc .WithUnaryInterceptor (PrefixChainIDUnaryClientInterceptor (chainID )),
20- grpc .WithStreamInterceptor (PrefixChainIDStreamClientInterceptor (chainID )),
20+ grpc .WithUnaryInterceptor (SetChainIDHeaderUnaryClientInterceptor (chainID )),
21+ grpc .WithStreamInterceptor (SetChainIDHeaderStreamClientInterceptor (chainID )),
2122 }
2223
2324 dialOpts = append (dialOpts , opts ... )
@@ -30,9 +31,9 @@ func NewChainClient(uri string, chainID ids.ID, opts ...grpc.DialOption) (*grpc.
3031 return conn , nil
3132}
3233
33- // PrefixChainIDUnaryClientInterceptor prefixes unary grpc calls with the
34- // provided chainID prefix
35- func PrefixChainIDUnaryClientInterceptor (chainID ids.ID ) grpc.UnaryClientInterceptor {
34+ // SetChainIDHeaderUnaryClientInterceptor sets the chain-id header for unary
35+ // requests
36+ func SetChainIDHeaderUnaryClientInterceptor (chainID ids.ID ) grpc.UnaryClientInterceptor {
3637 return func (
3738 ctx context.Context ,
3839 method string ,
@@ -42,13 +43,14 @@ func PrefixChainIDUnaryClientInterceptor(chainID ids.ID) grpc.UnaryClientInterce
4243 invoker grpc.UnaryInvoker ,
4344 opts ... grpc.CallOption ,
4445 ) error {
45- return invoker (ctx , prefix (chainID , method ), req , reply , cc , opts ... )
46+ ctx = newContextWithChainIDHeader (ctx , chainID )
47+ return invoker (ctx , method , req , reply , cc , opts ... )
4648 }
4749}
4850
49- // PrefixChainIDStreamClientInterceptor prefixes streaming grpc calls with the
50- // provided chainID prefix
51- func PrefixChainIDStreamClientInterceptor (chainID ids.ID ) grpc.StreamClientInterceptor {
51+ // SetChainIDHeaderStreamClientInterceptor sets the chain-id header for
52+ // streaming requests
53+ func SetChainIDHeaderStreamClientInterceptor (chainID ids.ID ) grpc.StreamClientInterceptor {
5254 return func (
5355 ctx context.Context ,
5456 desc * grpc.StreamDesc ,
@@ -57,11 +59,13 @@ func PrefixChainIDStreamClientInterceptor(chainID ids.ID) grpc.StreamClientInter
5759 streamer grpc.Streamer ,
5860 opts ... grpc.CallOption ,
5961 ) (grpc.ClientStream , error ) {
60- return streamer (ctx , desc , cc , prefix (chainID , method ), opts ... )
62+ ctx = newContextWithChainIDHeader (ctx , chainID )
63+ return streamer (ctx , desc , cc , method , opts ... )
6164 }
6265}
6366
64- // http/2 :path takes the form of /ChainID/Service/Method
65- func prefix (chainID ids.ID , method string ) string {
66- return "/" + chainID .String () + method
67+ // newContextWithChainHeader sets the chain-id header which the server uses
68+ // to route the client grpc request
69+ func newContextWithChainIDHeader (ctx context.Context , chainID ids.ID ) context.Context {
70+ return metadata .AppendToOutgoingContext (ctx , "chain-id" , chainID .String ())
6771}
0 commit comments