Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

[NC-1337] Support responding to GetNodeData requests. #587

Merged
merged 5 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package tech.pegasys.pantheon.consensus.ibftlegacy.protocol;

import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.db.WorldStateArchive;
import tech.pegasys.pantheon.ethereum.eth.EthProtocol;
import tech.pegasys.pantheon.ethereum.eth.manager.EthProtocolManager;
import tech.pegasys.pantheon.ethereum.p2p.api.Message;
Expand All @@ -27,11 +28,12 @@ public class Istanbul64ProtocolManager extends EthProtocolManager {

public Istanbul64ProtocolManager(
final Blockchain blockchain,
final WorldStateArchive worldStateArchive,
final int networkId,
final boolean fastSyncEnabled,
final int syncWorkers,
final int txWorkers) {
super(blockchain, networkId, fastSyncEnabled, syncWorkers, txWorkers);
super(blockchain, worldStateArchive, networkId, fastSyncEnabled, syncWorkers, txWorkers);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import tech.pegasys.pantheon.ethereum.trie.MerklePatriciaTrie;
import tech.pegasys.pantheon.ethereum.worldstate.DefaultMutableWorldState;
import tech.pegasys.pantheon.ethereum.worldstate.WorldStateStorage;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.util.Optional;

public class WorldStateArchive {
private final WorldStateStorage storage;
Expand All @@ -42,4 +45,8 @@ public WorldState get() {
public MutableWorldState getMutable() {
return getMutable(EMPTY_ROOT_HASH);
}

public Optional<BytesValue> getNodeData(final Hash hash) {
return storage.getNodeData(hash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public Optional<BytesValue> getAccountStorageTrieNode(final Bytes32 nodeHash) {
return keyValueStorage.get(nodeHash);
}

@Override
public Optional<BytesValue> getNodeData(final Hash hash) {
return keyValueStorage.get(hash);
}

@Override
public Updater updater() {
return new Updater(keyValueStorage.startTransaction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface WorldStateStorage {

Optional<BytesValue> getAccountStorageTrieNode(Bytes32 nodeHash);

Optional<BytesValue> getNodeData(Hash hash);

Updater updater();

interface Updater {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tech.pegasys.pantheon.ethereum.chain.MinedBlockObserver;
import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.db.WorldStateArchive;
import tech.pegasys.pantheon.ethereum.eth.EthProtocol;
import tech.pegasys.pantheon.ethereum.eth.messages.EthPV62;
import tech.pegasys.pantheon.ethereum.eth.messages.NewBlockMessage;
Expand Down Expand Up @@ -65,6 +66,7 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver {

EthProtocolManager(
final Blockchain blockchain,
final WorldStateArchive worldStateArchive,
final int networkId,
final boolean fastSyncEnabled,
final int requestLimit,
Expand All @@ -83,18 +85,20 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver {
ethContext = new EthContext(getSupportedProtocol(), ethPeers, ethMessages, scheduler);

// Set up request handlers
new EthServer(blockchain, ethMessages, requestLimit);
new EthServer(blockchain, worldStateArchive, ethMessages, requestLimit);
}

EthProtocolManager(
final Blockchain blockchain,
final WorldStateArchive worldStateArchive,
final int networkId,
final boolean fastSyncEnabled,
final int syncWorkers,
final int txWorkers,
final int requestLimit) {
this(
blockchain,
worldStateArchive,
networkId,
fastSyncEnabled,
requestLimit,
Expand All @@ -103,11 +107,19 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver {

public EthProtocolManager(
final Blockchain blockchain,
final WorldStateArchive worldStateArchive,
final int networkId,
final boolean fastSyncEnabled,
final int syncWorkers,
final int txWorkers) {
this(blockchain, networkId, fastSyncEnabled, syncWorkers, txWorkers, DEFAULT_REQUEST_LIMIT);
this(
blockchain,
worldStateArchive,
networkId,
fastSyncEnabled,
syncWorkers,
txWorkers,
DEFAULT_REQUEST_LIMIT);
}

public EthContext ethContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.core.TransactionReceipt;
import tech.pegasys.pantheon.ethereum.db.WorldStateArchive;
import tech.pegasys.pantheon.ethereum.eth.messages.BlockBodiesMessage;
import tech.pegasys.pantheon.ethereum.eth.messages.BlockHeadersMessage;
import tech.pegasys.pantheon.ethereum.eth.messages.EthPV62;
Expand Down Expand Up @@ -47,11 +48,17 @@ class EthServer {
private static final Logger LOG = LogManager.getLogger();

private final Blockchain blockchain;
private final WorldStateArchive worldStateArchive;
private final EthMessages ethMessages;
private final int requestLimit;

EthServer(final Blockchain blockchain, final EthMessages ethMessages, final int requestLimit) {
EthServer(
final Blockchain blockchain,
final WorldStateArchive worldStateArchive,
final EthMessages ethMessages,
final int requestLimit) {
this.blockchain = blockchain;
this.worldStateArchive = worldStateArchive;
this.ethMessages = ethMessages;
this.requestLimit = requestLimit;
this.setupListeners();
Expand Down Expand Up @@ -106,7 +113,8 @@ private void handleGetReceipts(final EthMessage message) {
private void handleGetNodeData(final EthMessage message) {
LOG.trace("Responding to GET_NODE_DATA request");
try {
final MessageData response = constructGetNodeDataResponse(message.getData(), requestLimit);
final MessageData response =
constructGetNodeDataResponse(worldStateArchive, message.getData(), requestLimit);
message.getPeer().send(response);
} catch (final RLPException e) {
message.getPeer().disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
Expand Down Expand Up @@ -195,7 +203,9 @@ static MessageData constructGetReceiptsResponse(
}

static MessageData constructGetNodeDataResponse(
final MessageData message, final int requestLimit) {
final WorldStateArchive worldStateArchive,
final MessageData message,
final int requestLimit) {
final GetNodeDataMessage getNodeDataMessage = GetNodeDataMessage.readFrom(message);
final Iterable<Hash> hashes = getNodeDataMessage.hashes();

Expand All @@ -206,7 +216,8 @@ static MessageData constructGetNodeDataResponse(
break;
}
count++;
// TODO: Lookup node data and add it to the list

worldStateArchive.getNodeData(hash).ifPresent(nodeData::add);
}
return NodeDataMessage.create(nodeData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public final class NodeDataMessage extends AbstractMessageData {

Expand Down Expand Up @@ -53,10 +53,10 @@ public int getCode() {
return EthPV63.NODE_DATA;
}

public Iterable<BytesValue> nodeData() {
public List<BytesValue> nodeData() {
final RLPInput input = new BytesValueRLPInput(data, false);
input.enterList();
final Collection<BytesValue> nodeData = new ArrayList<>();
final List<BytesValue> nodeData = new ArrayList<>();
while (!input.isEndOfCurrentList()) {
nodeData.add(input.readBytesValue());
}
Expand Down
Loading