use of org.hyperledger.besu.ethereum.chain.ChainHead in project besu by hyperledger.
the class AdminNodeInfo method response.
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final Map<String, Object> response = new HashMap<>();
final Map<String, Integer> ports = new HashMap<>();
if (!peerNetwork.isP2pEnabled()) {
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.P2P_DISABLED);
}
final Optional<EnodeURL> maybeEnode = peerNetwork.getLocalEnode();
if (maybeEnode.isEmpty()) {
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.P2P_NETWORK_NOT_RUNNING);
}
final EnodeURL enode = maybeEnode.get();
response.put("enode", enode.toString());
response.put("ip", enode.getIpAsString());
final Bytes nodeId = enode.getNodeId();
final String ip = getIp(enode);
final int listeningPort = getListeningPort(enode);
final int discoveryPort = getDiscoveryPort(enode);
response.put("enode", getNodeAsString(enode, ip, listeningPort, discoveryPort));
response.put("ip", ip);
if (enode.isListening()) {
response.put("listenAddr", String.format("%s:%d", ip, listeningPort));
}
response.put("id", nodeId.toUnprefixedHexString());
response.put("name", clientVersion);
if (enode.isRunningDiscovery()) {
ports.put("discovery", discoveryPort);
}
if (enode.isListening()) {
ports.put("listener", listeningPort);
}
response.put("ports", ports);
final ChainHead chainHead = blockchainQueries.getBlockchain().getChainHead();
response.put("protocols", ImmutableMap.of("eth", ImmutableMap.of("config", genesisConfigOptions.asMap(), "difficulty", chainHead.getTotalDifficulty().toBigInteger(), "genesis", blockchainQueries.getBlockHashByNumber(0).get().toString(), "head", chainHead.getHash().toString(), "network", networkId)));
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), response);
}
use of org.hyperledger.besu.ethereum.chain.ChainHead in project besu by hyperledger.
the class SyncState method checkInSync.
private synchronized void checkInSync() {
final ChainHead localChain = getLocalChainHead();
final Optional<ChainHeadEstimate> syncTargetChain = getSyncTargetChainHead();
final Optional<ChainHeadEstimate> bestPeerChain = getBestPeerChainHead();
// Remove listener when we've found a peer.
newPeerListenerId.ifPresent(listenerId -> {
ethPeers.unsubscribeConnect(listenerId);
newPeerListenerId = Optional.empty();
});
inSyncTrackers.values().forEach((syncTracker) -> syncTracker.checkState(localChain, syncTargetChain, bestPeerChain));
}
use of org.hyperledger.besu.ethereum.chain.ChainHead in project besu by hyperledger.
the class ChainStateTest method chainIsBetterThan_chainStateIsLighterAndShorter.
@Test
public void chainIsBetterThan_chainStateIsLighterAndShorter() {
final ChainState chainState = new ChainState();
updateChainState(chainState, Difficulty.of(50), 50);
final ChainHead chainHead = new ChainHead(Hash.ZERO, Difficulty.of(100), 100);
assertThat(chainState.chainIsBetterThan(chainHead)).isFalse();
}
use of org.hyperledger.besu.ethereum.chain.ChainHead in project besu by hyperledger.
the class ChainStateTest method chainIsBetterThan_chainStateIsHeavierAndShorter.
@Test
public void chainIsBetterThan_chainStateIsHeavierAndShorter() {
final ChainState chainState = new ChainState();
updateChainState(chainState, Difficulty.of(100), 50);
final ChainHead chainHead = new ChainHead(Hash.ZERO, Difficulty.of(50), 100);
assertThat(chainState.chainIsBetterThan(chainHead)).isTrue();
}
use of org.hyperledger.besu.ethereum.chain.ChainHead in project besu by hyperledger.
the class JsonRpcHttpServiceTestBase method initServerAndClient.
public static void initServerAndClient() throws Exception {
peerDiscoveryMock = mock(P2PNetwork.class);
ethPeersMock = mock(EthPeers.class);
blockchain = mock(Blockchain.class);
blockchainQueries = mock(BlockchainQueries.class);
chainHead = mock(ChainHead.class);
synchronizer = mock(Synchronizer.class);
final Set<Capability> supportedCapabilities = new HashSet<>();
supportedCapabilities.add(EthProtocol.ETH62);
supportedCapabilities.add(EthProtocol.ETH63);
rpcMethods = spy(new JsonRpcMethodsFactory().methods(CLIENT_VERSION, CHAIN_ID, new StubGenesisConfigOptions(), peerDiscoveryMock, blockchainQueries, synchronizer, MainnetProtocolSchedule.fromConfig(new StubGenesisConfigOptions().constantinopleBlock(0).chainId(CHAIN_ID), EvmConfiguration.DEFAULT), mock(ProtocolContext.class), mock(FilterManager.class), mock(TransactionPool.class), mock(PoWMiningCoordinator.class), new NoOpMetricsSystem(), supportedCapabilities, Optional.of(mock(AccountLocalConfigPermissioningController.class)), Optional.of(mock(NodeLocalConfigPermissioningController.class)), JSON_RPC_APIS, mock(PrivacyParameters.class), mock(JsonRpcConfiguration.class), mock(WebSocketConfiguration.class), mock(MetricsConfiguration.class), natService, new HashMap<>(), folder.getRoot().toPath(), ethPeersMock));
service = createJsonRpcHttpService(createLimitedJsonRpcConfig());
service.start().join();
// Build an OkHttp client.
client = new OkHttpClient();
baseUrl = service.url();
}
Aggregations