diff --git a/sdk-service/src/main/java/org/fisco/bcos/sdk/service/GroupServiceImpl.java b/sdk-service/src/main/java/org/fisco/bcos/sdk/service/GroupServiceImpl.java index d866033b8..4422e8a40 100644 --- a/sdk-service/src/main/java/org/fisco/bcos/sdk/service/GroupServiceImpl.java +++ b/sdk-service/src/main/java/org/fisco/bcos/sdk/service/GroupServiceImpl.java @@ -168,11 +168,21 @@ public BigInteger getLatestBlockNumber() { @Override public String getNodeWithTheLatestBlockNumber() { - // the case that the sdk is allowed to access all the connected node, select the first - // connected node to send the request - if (nodeWithLatestBlockNumber.size() > 0) { - int random = (int) (Math.random() * (nodeWithLatestBlockNumber.size())); - return nodeWithLatestBlockNumber.get(random); + try { + // the case that the sdk is allowed to access all the connected node, select the first + // connected node to send the request + if (nodeWithLatestBlockNumber.size() > 0) { + // Note: when the nodeWithLatestBlockNumber modified, and the random value + // calculated after the modification, and the nodeWithLatestBlockNumber.get is + // called after the modification, this function will throw + // ArrayIndexOutOfBoundsException + int random = (int) (Math.random() * (nodeWithLatestBlockNumber.size())); + return nodeWithLatestBlockNumber.get(random); + } + } catch (Exception e) { + logger.error( + "getNodeWithTheLatestBlockNumber for {}, select the node to send message randomly", + e.getMessage()); } // select the first element if (!groupNodeSet.isEmpty()) {