Java源码示例:org.web3j.protocol.core.methods.response.Web3ClientVersion
示例1
@Test(expected = ExecutionException.class)
public void testCancelRequestAfterTimeout() throws Exception {
when(executorService.schedule(
any(Runnable.class),
eq(WebSocketService.REQUEST_TIMEOUT),
eq(TimeUnit.SECONDS)))
.then(invocation -> {
Runnable runnable = invocation.getArgumentAt(0, Runnable.class);
runnable.run();
return null;
});
CompletableFuture<Web3ClientVersion> reply = service.sendAsync(
request,
Web3ClientVersion.class);
assertTrue(reply.isDone());
reply.get();
}
示例2
@Test
public void testSyncRequest() throws Exception {
CountDownLatch requestSent = new CountDownLatch(1);
// Wait for a request to be sent
doAnswer(invocation -> {
requestSent.countDown();
return null;
}).when(webSocketClient).send(anyString());
// Send reply asynchronously
runAsync(() -> {
try {
requestSent.await(2, TimeUnit.SECONDS);
sendGethVersionReply();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Web3ClientVersion reply = service.send(request, Web3ClientVersion.class);
assertEquals(reply.getWeb3ClientVersion(), "geth-version");
}
示例3
public TransactionHandler(int networkId)
{
String nodeURL = EthRPCNodes.getNodeURLByNetworkId(networkId);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(20, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
HttpService service = new HttpService(nodeURL, builder.build(), false);
mWeb3 = Web3j.build(service);
try
{
Web3ClientVersion web3ClientVersion = mWeb3.web3ClientVersion().sendAsync().get();
System.out.println(web3ClientVersion.getWeb3ClientVersion());
}
catch (Exception e)
{
e.printStackTrace();
}
}
示例4
public TransactionHandler(int networkId)
{
String nodeURL = EthereumNetworkBase.getNetworkByChain(networkId).rpcServerUrl;
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(20, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
HttpService service = new HttpService(nodeURL, builder.build(), false);
mWeb3 = Web3j.build(service);
try
{
Web3ClientVersion web3ClientVersion = mWeb3.web3ClientVersion().sendAsync().get();
System.out.println(web3ClientVersion.getWeb3ClientVersion());
}
catch (Exception e)
{
e.printStackTrace();
}
}
示例5
@Test
public void testCancelRequestAfterTimeout() {
when(executorService.schedule(
any(Runnable.class),
eq(WebSocketService.REQUEST_TIMEOUT),
eq(TimeUnit.SECONDS)))
.then(
invocation -> {
Runnable runnable = invocation.getArgument(0, Runnable.class);
runnable.run();
return null;
});
CompletableFuture<Web3ClientVersion> reply =
service.sendAsync(request, Web3ClientVersion.class);
assertTrue(reply.isDone());
assertThrows(ExecutionException.class, () -> reply.get());
}
示例6
private Web3j getEthereumClient() {
String clientAddress = console.readLine(
"Please confirm address of running Ethereum client you wish to send "
+ "the transfer request to [" + HttpService.DEFAULT_URL + "]: ")
.trim();
Web3j web3j;
if (clientAddress.equals("")) {
web3j = Web3j.build(new HttpService());
} else {
web3j = Web3j.build(new HttpService(clientAddress));
}
try {
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
if (web3ClientVersion.hasError()) {
exitError("Unable to process response from client: "
+ web3ClientVersion.getError());
} else {
console.printf("Connected successfully to client: %s%n",
web3ClientVersion.getWeb3ClientVersion());
return web3j;
}
} catch (InterruptedException | ExecutionException e) {
exitError("Problem encountered verifying client: " + e.getMessage());
}
throw new RuntimeException("Application exit failure");
}
示例7
@Override
public Request<?, Web3ClientVersion> web3ClientVersion() {
return new Request<>(
"web3_clientVersion",
Collections.<String>emptyList(),
web3jService,
Web3ClientVersion.class);
}
示例8
@Test
public void testNoLongerWaitingForResponseAfterReply() throws Exception {
service.sendAsync(request, Web3ClientVersion.class);
sendGethVersionReply();
assertFalse(service.isWaitingForReply(1));
}
示例9
@Test
public void testSendWebSocketRequest() throws Exception {
service.sendAsync(request, Web3ClientVersion.class);
verify(webSocketClient).send(
"{\"jsonrpc\":\"2.0\",\"method\":\"web3_clientVersion\",\"params\":[],\"id\":1}");
}
示例10
@Test
public void testIgnoreInvalidReplies() throws Exception {
thrown.expect(IOException.class);
thrown.expectMessage("Failed to parse incoming WebSocket message");
service.sendAsync(request, Web3ClientVersion.class);
service.onWebSocketMessage("{");
}
示例11
@Test
public void testThrowExceptionIfIdHasInvalidType() throws Exception {
thrown.expect(IOException.class);
thrown.expectMessage("'id' expected to be long, but it is: 'true'");
service.sendAsync(request, Web3ClientVersion.class);
service.onWebSocketMessage("{\"id\":true}");
}
示例12
@Test
public void testThrowExceptionIfIdIsMissing() throws Exception {
thrown.expect(IOException.class);
thrown.expectMessage("Unknown message type");
service.sendAsync(request, Web3ClientVersion.class);
service.onWebSocketMessage("{}");
}
示例13
@Test
public void testThrowExceptionIfUnexpectedIdIsReceived() throws Exception {
thrown.expect(IOException.class);
thrown.expectMessage("Received reply for unexpected request id: 12345");
service.sendAsync(request, Web3ClientVersion.class);
service.onWebSocketMessage(
"{\"jsonrpc\":\"2.0\",\"id\":12345,\"result\":\"geth-version\"}");
}
示例14
@Test
public void testReceiveReply() throws Exception {
CompletableFuture<Web3ClientVersion> reply = service.sendAsync(
request,
Web3ClientVersion.class);
sendGethVersionReply();
assertTrue(reply.isDone());
assertEquals("geth-version", reply.get().getWeb3ClientVersion());
}
示例15
@Test
public void testReceiveError() throws Exception {
CompletableFuture<Web3ClientVersion> reply = service.sendAsync(
request,
Web3ClientVersion.class);
sendErrorReply();
assertTrue(reply.isDone());
Web3ClientVersion version = reply.get();
assertTrue(version.hasError());
assertEquals(
new Response.Error(-1, "Error message"),
version.getError());
}
示例16
@Test
public void testCloseRequestWhenConnectionIsClosed() throws Exception {
thrown.expect(ExecutionException.class);
CompletableFuture<Web3ClientVersion> reply = service.sendAsync(
request,
Web3ClientVersion.class);
service.onWebSocketClose();
assertTrue(reply.isDone());
reply.get();
}
示例17
@Test
public void testSlowResponse() throws Exception {
String response = "{\"jsonrpc\":\"2.0\",\"id\":1,"
+ "\"result\":\"Geth/v1.5.4-stable-b70acf3c/darwin/go1.7.3\"}\n";
unixDomainSocket = new UnixDomainSocket(reader, writer, response.length());
final LinkedList<String> segments = new LinkedList<>();
// 1st part of response
segments.add(response.substring(0, 50));
// rest of response
segments.add(response.substring(50));
doAnswer(invocation -> {
String segment = segments.poll();
if (segment == null) {
return 0;
} else {
Object[] args = invocation.getArguments();
((CharBuffer) args[0]).append(segment);
return segment.length();
}
}).when(reader).read(any(CharBuffer.class));
IpcService ipcService = new IpcService() {
@Override
protected IOFacade getIO() {
return unixDomainSocket;
}
};
ipcService.send(new Request(), Web3ClientVersion.class);
}
示例18
@Test
public void testSend() throws IOException {
when(ioFacade.read()).thenReturn(
"{\"jsonrpc\":\"2.0\",\"id\":1,"
+ "\"result\":\"Geth/v1.5.4-stable-b70acf3c/darwin/go1.7.3\"}\n");
ipcService.send(new Request(), Web3ClientVersion.class);
verify(ioFacade).write("{\"jsonrpc\":\"2.0\",\"method\":null,\"params\":null,\"id\":0}");
}
示例19
@Test
public void testWeb3ClientVersion() throws Exception {
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
System.out.println("Ethereum client version: " + clientVersion);
assertFalse(clientVersion.isEmpty());
}
示例20
private Web3j getEthereumClient() {
String clientAddress = console.readLine(
"Please confirm address of running Ethereum client you wish to send "
+ "the transfer request to [" + HttpService.DEFAULT_URL + "]: ")
.trim();
Web3j web3j;
if (clientAddress.equals("")) {
web3j = Web3j.build(new HttpService());
} else if (clientAddress.contains("infura.io")) {
web3j = Web3j.build(new InfuraHttpService(clientAddress));
} else {
web3j = Web3j.build(new HttpService(clientAddress));
}
try {
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
if (web3ClientVersion.hasError()) {
exitError("Unable to process response from client: "
+ web3ClientVersion.getError());
} else {
console.printf("Connected successfully to client: %s%n",
web3ClientVersion.getWeb3ClientVersion());
return web3j;
}
} catch (InterruptedException | ExecutionException e) {
exitError("Problem encountered verifying client: " + e.getMessage());
}
throw new RuntimeException("Application exit failure");
}
示例21
@Override
public Request<?, Web3ClientVersion> web3ClientVersion() {
return new Request<>(
"web3_clientVersion",
Collections.<String>emptyList(),
web3jService,
Web3ClientVersion.class);
}
示例22
@Test
public void testWeb3ClientVersion() {
buildResponse(
"{\n"
+ " \"id\":67,\n"
+ " \"jsonrpc\":\"2.0\",\n"
+ " \"result\": \"Mist/v0.9.3/darwin/go1.4.1\"\n"
+ "}"
);
Web3ClientVersion web3ClientVersion = deserialiseResponse(Web3ClientVersion.class);
assertThat(web3ClientVersion.getWeb3ClientVersion(), is("Mist/v0.9.3/darwin/go1.4.1"));
}
示例23
@Test
public void testSlowResponse() throws Exception {
String response = "{\"jsonrpc\":\"2.0\",\"id\":1,"
+ "\"result\":\"Geth/v1.5.4-stable-b70acf3c/darwin/go1.7.3\"}\n";
unixDomainSocket = new UnixDomainSocket(reader, writer, response.length());
final LinkedList<String> segments = new LinkedList<>();
// 1st part of response
segments.add(response.substring(0, 50));
// rest of response
segments.add(response.substring(50));
doAnswer(invocation -> {
String segment = segments.poll();
if (segment == null) {
return 0;
} else {
Object[] args = invocation.getArguments();
((CharBuffer) args[0]).append(segment);
return segment.length();
}
}).when(reader).read(any(CharBuffer.class));
IpcService ipcService = new IpcService() {
@Override
protected IOFacade getIO() {
return unixDomainSocket;
}
};
ipcService.send(new Request(), Web3ClientVersion.class);
}
示例24
@Test
public void testSend() throws IOException {
when(ioFacade.read()).thenReturn(
"{\"jsonrpc\":\"2.0\",\"id\":1,"
+ "\"result\":\"Geth/v1.5.4-stable-b70acf3c/darwin/go1.7.3\"}\n");
ipcService.send(new Request(), Web3ClientVersion.class);
verify(ioFacade).write("{\"jsonrpc\":\"2.0\",\"method\":null,\"params\":null,\"id\":0}");
}
示例25
@Override
protected String doInBackground(Void... params) {
Web3ClientVersion web3ClientVersion = null;
try {
web3ClientVersion = web3jConnection.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例26
@Override
protected String doInBackground(Void... params) {
Web3ClientVersion web3ClientVersion = null;
try {
web3ClientVersion = web3jConnection.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例27
@Override
protected String doInBackground(Void... params) {
Web3ClientVersion web3ClientVersion = null;
try {
web3ClientVersion = web3jConnection.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例28
public void run() throws Exception {
// show client details
Web3ClientVersion client = web3j
.web3ClientVersion()
.sendAsync()
.get();
System.out.println("Connected to " + client.getWeb3ClientVersion() + "\n");
}
示例29
public static String getClientVersion(Web3j web3j) throws InterruptedException, ExecutionException {
Web3ClientVersion client = web3j
.web3ClientVersion()
.sendAsync()
.get();
return client.getWeb3ClientVersion();
}
示例30
@Test
public void testWeb3ClientVersion() throws Exception {
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
System.out.println("Ethereum client version: " + clientVersion);
assertFalse(clientVersion.isEmpty());
}