Skip to content

Commit 17686b7

Browse files
committed
problem: no credentials for grpc connection
solution: allow to build with an existing stub, or provide interceptors
1 parent d91de78 commit 17686b7

File tree

2 files changed

+101
-36
lines changed

2 files changed

+101
-36
lines changed

etherjar-rpc-emerald/src/main/java/io/emeraldpay/etherjar/rpc/emerald/EmeraldTransport.java

+56-24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.emeraldpay.api.proto.BlockchainOuterClass;
2323
import io.emeraldpay.api.proto.Common;
2424
import io.emeraldpay.api.Chain;
25+
import io.emeraldpay.api.proto.ReactorBlockchainGrpc;
2526
import io.grpc.*;
2627
import io.grpc.netty.NettyChannelBuilder;
2728
import io.emeraldpay.etherjar.rpc.*;
@@ -55,7 +56,6 @@
5556
*/
5657
public class EmeraldTransport implements RpcTransport<DefaultBatch.FutureBatchItem> {
5758

58-
private final Channel channel;
5959
private final BlockchainGrpc.BlockchainBlockingStub blockingStub;
6060

6161
private final ResponseJsonConverter responseJsonConverter = new ResponseJsonConverter();
@@ -67,17 +67,16 @@ public class EmeraldTransport implements RpcTransport<DefaultBatch.FutureBatchIt
6767
private final Common.ChainRef chainRef;
6868
private BlockchainOuterClass.Selector selector;
6969

70-
public EmeraldTransport(Channel channel,
70+
public EmeraldTransport(BlockchainGrpc.BlockchainBlockingStub stub,
7171
ObjectMapper objectMapper,
7272
JacksonRpcConverter rpcConverter,
7373
ExecutorService executorService,
7474
Common.ChainRef chainRef) {
75-
this.channel = channel;
7675
this.objectMapper = objectMapper;
7776
this.rpcConverter = rpcConverter;
7877
this.executorService = executorService;
7978
this.chainRef = chainRef;
80-
blockingStub = BlockchainGrpc.newBlockingStub(channel);
79+
blockingStub = stub;
8180
}
8281

8382
/**
@@ -97,7 +96,7 @@ public static EmeraldTransport.Builder newBuilder() {
9796
* @return new instance of EmeraldGrpcTransport configured for new chain
9897
*/
9998
public EmeraldTransport copyForChain(Chain chain) {
100-
return new EmeraldTransport(channel, objectMapper, rpcConverter, executorService, Common.ChainRef.forNumber(chain.getId()));
99+
return new EmeraldTransport(blockingStub, objectMapper, rpcConverter, executorService, Common.ChainRef.forNumber(chain.getId()));
101100
}
102101

103102
/**
@@ -135,7 +134,7 @@ public EmeraldTransport copyForChain(Chain chain) {
135134
* @return new instance of EmeraldGrpcTransport configured with new selector
136135
*/
137136
public EmeraldTransport copyWithSelector(BlockchainOuterClass.Selector selector) {
138-
EmeraldTransport copy = new EmeraldTransport(channel, objectMapper, rpcConverter, executorService, chainRef);
137+
EmeraldTransport copy = new EmeraldTransport(blockingStub, objectMapper, rpcConverter, executorService, chainRef);
139138
copy.selector = selector;
140139
return copy;
141140
}
@@ -237,7 +236,7 @@ public <JS, RES> JS read(ByteString bytes, DefaultBatch.FutureBatchItem<JS, RES>
237236

238237
@Override
239238
public void close() throws IOException {
240-
Channel channel = this.channel;
239+
Channel channel = this.blockingStub.getChannel();
241240
if (channel instanceof ManagedChannel) {
242241
((ManagedChannel) channel).shutdownNow();
243242
try {
@@ -258,6 +257,8 @@ public static class Builder {
258257

259258
private SslContextBuilder sslContextBuilder;
260259
private Channel channel;
260+
private BlockchainGrpc.BlockchainBlockingStub stub;
261+
private ClientInterceptor[] interceptors;
261262

262263
private ObjectMapper objectMapper;
263264
private JacksonRpcConverter rpcConverter;
@@ -278,6 +279,20 @@ public Builder connectUsing(Channel channel) {
278279
return this;
279280
}
280281

282+
/**
283+
* Setup with an existing stub. All other settings related to connection will be ignored
284+
*
285+
* @param stub existing stub
286+
* @return builder
287+
*/
288+
public Builder connectUsing(BlockchainGrpc.BlockchainBlockingStub stub) {
289+
this.stub = stub;
290+
this.channel = null;
291+
this.channelBuilder = null;
292+
this.sslContextBuilder = null;
293+
return this;
294+
}
295+
281296
/**
282297
* Apply a custom modification for the default NettyChannelBuilder
283298
*
@@ -455,32 +470,49 @@ public Builder chain(Chain chain) {
455470
return this;
456471
}
457472

473+
/**
474+
* Add interceptors to the client calls
475+
*
476+
* @param interceptors interceptors
477+
* @return builder
478+
*/
479+
public Builder interceptors(ClientInterceptor... interceptors) {
480+
this.interceptors = interceptors;
481+
return this;
482+
}
483+
458484
/**
459485
* Validates configuration and builds GrpcTransport
460486
*
461487
* @return configured grpc transport
462488
* @throws SSLException if problem with TLS certificates
463489
*/
464490
public EmeraldTransport build() throws SSLException {
465-
if (channel == null) {
466-
NettyChannelBuilder nettyBuilder = channelBuilder;
467-
if (sslContextBuilder != null) {
468-
nettyBuilder = nettyBuilder.useTransportSecurity()
469-
.sslContext(sslContextBuilder.build());
470-
}
471-
if (useLoadBalancing) {
472-
String policy = "round_robin";
473-
if (LoadBalancerRegistry.getDefaultRegistry().getProvider(policy) != null) {
474-
nettyBuilder = nettyBuilder.defaultLoadBalancingPolicy(policy);
491+
if (stub == null) {
492+
if (channel == null) {
493+
NettyChannelBuilder nettyBuilder = channelBuilder;
494+
if (sslContextBuilder != null) {
495+
nettyBuilder = nettyBuilder.useTransportSecurity()
496+
.sslContext(sslContextBuilder.build());
497+
}
498+
if (useLoadBalancing) {
499+
String policy = "round_robin";
500+
if (LoadBalancerRegistry.getDefaultRegistry().getProvider(policy) != null) {
501+
nettyBuilder = nettyBuilder.defaultLoadBalancingPolicy(policy);
502+
}
475503
}
504+
ManagedChannelBuilder<?> finalBuilder;
505+
if (this.channelUpdate != null) {
506+
finalBuilder = this.channelUpdate.apply(nettyBuilder);
507+
} else {
508+
finalBuilder = nettyBuilder;
509+
}
510+
channel = finalBuilder.build();
476511
}
477-
ManagedChannelBuilder<?> finalBuilder;
478-
if (this.channelUpdate != null) {
479-
finalBuilder = this.channelUpdate.apply(nettyBuilder);
480-
} else {
481-
finalBuilder = nettyBuilder;
512+
stub = BlockchainGrpc.newBlockingStub(channel);
513+
if (interceptors != null) {
514+
stub = stub.withInterceptors(interceptors);
482515
}
483-
channel = finalBuilder.build();
484516
}
485517
if (executorService == null) {
486518
threadsCount(2);
@@ -499,7 +531,7 @@ public EmeraldTransport build() throws SSLException {
499531
chain = Chain.UNSPECIFIED;
500532
}
501533
Common.ChainRef chainRef = Common.ChainRef.forNumber(chain.getId());
502-
return new EmeraldTransport(channel, objectMapper, rpcConverter, executorService, chainRef);
534+
return new EmeraldTransport(stub, objectMapper, rpcConverter, executorService, chainRef);
503535
}
504536
}
505537
}

etherjar-rpc-emerald/src/main/java/io/emeraldpay/etherjar/rpc/emerald/ReactorEmeraldClient.java

+45-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.emeraldpay.api.proto.ReactorBlockchainGrpc;
2424
import io.emeraldpay.api.Chain;
2525
import io.grpc.Channel;
26+
import io.grpc.ClientInterceptor;
2627
import io.grpc.Status;
2728
import io.grpc.StatusRuntimeException;
2829
import io.grpc.netty.GrpcSslContexts;
@@ -46,7 +47,6 @@
4647

4748
public class ReactorEmeraldClient extends AbstractReactorRpcClient implements ReactorRpcClient {
4849

49-
private final Channel channel;
5050
private final ReactorBlockchainGrpc.ReactorBlockchainStub stub;
5151

5252
private final ObjectMapper objectMapper;
@@ -56,9 +56,8 @@ public class ReactorEmeraldClient extends AbstractReactorRpcClient implements Re
5656

5757
ResponseJsonConverter responseJsonConverter = new ResponseJsonConverter();
5858

59-
public ReactorEmeraldClient(Channel channel, ObjectMapper objectMapper, JacksonRpcConverter rpcConverter, Common.ChainRef chainRef) {
60-
this.channel = channel;
61-
this.stub = ReactorBlockchainGrpc.newReactorStub(channel);
59+
public ReactorEmeraldClient(ReactorBlockchainGrpc.ReactorBlockchainStub stub, ObjectMapper objectMapper, JacksonRpcConverter rpcConverter, Common.ChainRef chainRef) {
60+
this.stub = stub;
6261
this.objectMapper = objectMapper;
6362
this.rpcConverter = rpcConverter;
6463
this.chainRef = chainRef;
@@ -76,7 +75,7 @@ public static Builder newBuilder() {
7675
* @return new instance of ReactorEmeraldClient configured for new chain
7776
*/
7877
public ReactorEmeraldClient copyForChain(Chain chain) {
79-
return new ReactorEmeraldClient(channel, objectMapper, rpcConverter, Common.ChainRef.forNumber(chain.getId()));
78+
return new ReactorEmeraldClient(stub, objectMapper, rpcConverter, Common.ChainRef.forNumber(chain.getId()));
8079
}
8180

8281
/**
@@ -114,7 +113,7 @@ public ReactorEmeraldClient copyForChain(Chain chain) {
114113
* @return new instance of ReactorEmeraldClient configured with new selector
115114
*/
116115
public ReactorEmeraldClient copyWithSelector(BlockchainOuterClass.Selector selector) {
117-
ReactorEmeraldClient copy = new ReactorEmeraldClient(channel, objectMapper, rpcConverter, chainRef);
116+
ReactorEmeraldClient copy = new ReactorEmeraldClient(stub, objectMapper, rpcConverter, chainRef);
118117
copy.selector = selector;
119118
return copy;
120119
}
@@ -212,6 +211,9 @@ public static class Builder {
212211
private SslContextBuilder sslContextBuilder;
213212
private Channel channel;
214213

214+
private ReactorBlockchainGrpc.ReactorBlockchainStub stub;
215+
private ClientInterceptor[] interceptors;
216+
215217
private ObjectMapper objectMapper;
216218
private JacksonRpcConverter rpcConverter;
217219

@@ -230,6 +232,20 @@ public Builder connectUsing(Channel channel) {
230232
return this;
231233
}
232234

235+
/**
236+
* Setup with an existing stub. All other settings related to connection will be ignored
237+
*
238+
* @param stub existing stub
239+
* @return builder
240+
*/
241+
public Builder connectUsing(ReactorBlockchainGrpc.ReactorBlockchainStub stub) {
242+
this.stub = stub;
243+
this.channel = null;
244+
this.channelBuilder = null;
245+
this.sslContextBuilder = null;
246+
return this;
247+
}
248+
233249
/**
234250
* Setup for address formatted as host:port
235251
*
@@ -368,6 +384,17 @@ public Builder executor(Executor executor) {
368384
return this;
369385
}
370386

387+
/**
388+
* Add interceptors to the client calls
389+
*
390+
* @param interceptors interceptors
391+
* @return builder
392+
*/
393+
public Builder interceptors(ClientInterceptor... interceptors) {
394+
this.interceptors = interceptors;
395+
return this;
396+
}
397+
371398
/**
372399
*
373400
* @param chain chain
@@ -385,12 +412,18 @@ public Builder chain(Chain chain) {
385412
* @throws SSLException if problem with TLS certificates
386413
*/
387414
public ReactorEmeraldClient build() throws SSLException {
388-
if (channel == null) {
389-
if (sslContextBuilder != null) {
390-
channelBuilder.useTransportSecurity()
391-
.sslContext(sslContextBuilder.build());
415+
if (stub == null) {
416+
if (channel == null) {
417+
if (sslContextBuilder != null) {
418+
channelBuilder.useTransportSecurity()
419+
.sslContext(sslContextBuilder.build());
420+
}
421+
channel = channelBuilder.build();
422+
}
423+
stub = ReactorBlockchainGrpc.newReactorStub(channel);
424+
if (interceptors != null) {
425+
stub = stub.withInterceptors(interceptors);
392426
}
393-
channel = channelBuilder.build();
394427
}
395428
if (objectMapper == null) {
396429
objectMapper = new ObjectMapper();
@@ -402,7 +435,7 @@ public ReactorEmeraldClient build() throws SSLException {
402435
chain = Chain.UNSPECIFIED;
403436
}
404437
Common.ChainRef chainRef = Common.ChainRef.forNumber(chain.getId());
405-
return new ReactorEmeraldClient(channel, objectMapper, rpcConverter, chainRef);
438+
return new ReactorEmeraldClient(stub, objectMapper, rpcConverter, chainRef);
406439
}
407440
}
408441

0 commit comments

Comments
 (0)