Search in sources :

Example 1 with ChainHead

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);
}
Also used : HashMap(java.util.HashMap) BigInteger(java.math.BigInteger) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) ChainHead(org.hyperledger.besu.ethereum.chain.ChainHead) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 2 with ChainHead

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));
}
Also used : ChainHeadEstimate(org.hyperledger.besu.ethereum.eth.manager.ChainHeadEstimate) ChainHead(org.hyperledger.besu.ethereum.chain.ChainHead)

Example 3 with ChainHead

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();
}
Also used : ChainHead(org.hyperledger.besu.ethereum.chain.ChainHead) Test(org.junit.Test)

Example 4 with ChainHead

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();
}
Also used : ChainHead(org.hyperledger.besu.ethereum.chain.ChainHead) Test(org.junit.Test)

Example 5 with ChainHead

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();
}
Also used : Synchronizer(org.hyperledger.besu.ethereum.core.Synchronizer) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) OkHttpClient(okhttp3.OkHttpClient) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) WebSocketConfiguration(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) P2PNetwork(org.hyperledger.besu.ethereum.p2p.network.P2PNetwork) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) PrivacyParameters(org.hyperledger.besu.ethereum.core.PrivacyParameters) JsonRpcMethodsFactory(org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory) FilterManager(org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager) MetricsConfiguration(org.hyperledger.besu.metrics.prometheus.MetricsConfiguration) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) ChainHead(org.hyperledger.besu.ethereum.chain.ChainHead) StubGenesisConfigOptions(org.hyperledger.besu.config.StubGenesisConfigOptions) PoWMiningCoordinator(org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator) HashSet(java.util.HashSet)

Aggregations

ChainHead (org.hyperledger.besu.ethereum.chain.ChainHead)7 Test (org.junit.Test)4 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 OkHttpClient (okhttp3.OkHttpClient)1 Bytes (org.apache.tuweni.bytes.Bytes)1 StubGenesisConfigOptions (org.hyperledger.besu.config.StubGenesisConfigOptions)1 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)1 FilterManager (org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager)1 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)1 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)1 JsonRpcMethodsFactory (org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory)1 WebSocketConfiguration (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration)1 BlockchainQueries (org.hyperledger.besu.ethereum.api.query.BlockchainQueries)1 PoWMiningCoordinator (org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator)1 Blockchain (org.hyperledger.besu.ethereum.chain.Blockchain)1 PrivacyParameters (org.hyperledger.besu.ethereum.core.PrivacyParameters)1 Synchronizer (org.hyperledger.besu.ethereum.core.Synchronizer)1 ChainHeadEstimate (org.hyperledger.besu.ethereum.eth.manager.ChainHeadEstimate)1