forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
7311: add peertask foundation code (hyperledger#7628)
* 7311: Add PeerTask system for use in future PRs Signed-off-by: Matilda Clerke <[email protected]>
- Loading branch information
1 parent
dfbfb96
commit 2169985
Showing
18 changed files
with
898 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
.../org/hyperledger/besu/ethereum/eth/manager/peertask/InvalidPeerTaskResponseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright contributors to Hyperledger Besu. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.ethereum.eth.manager.peertask; | ||
|
||
public class InvalidPeerTaskResponseException extends Exception { | ||
|
||
public InvalidPeerTaskResponseException() { | ||
super(); | ||
} | ||
|
||
public InvalidPeerTaskResponseException(final Throwable cause) { | ||
super(cause); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ain/java/org/hyperledger/besu/ethereum/eth/manager/peertask/NoAvailablePeerException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright contributors to Hyperledger Besu. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.ethereum.eth.manager.peertask; | ||
|
||
public class NoAvailablePeerException extends Exception {} |
42 changes: 42 additions & 0 deletions
42
...um/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerSelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright contributors to Hyperledger Besu. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.ethereum.eth.manager.peertask; | ||
|
||
import org.hyperledger.besu.ethereum.eth.manager.EthPeer; | ||
import org.hyperledger.besu.ethereum.p2p.peers.PeerId; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
|
||
/** Selects the EthPeers for the PeerTaskExecutor */ | ||
public interface PeerSelector { | ||
|
||
/** | ||
* Gets a peer matching the supplied filter | ||
* | ||
* @param filter a Predicate\<EthPeer\> matching desirable peers | ||
* @return a peer matching the supplied conditions | ||
*/ | ||
Optional<EthPeer> getPeer(final Predicate<EthPeer> filter); | ||
|
||
/** | ||
* Attempts to get the EthPeer identified by peerId | ||
* | ||
* @param peerId the peerId of the desired EthPeer | ||
* @return An Optional\<EthPeer\> containing the EthPeer identified by peerId if present in the | ||
* PeerSelector, or empty otherwise | ||
*/ | ||
Optional<EthPeer> getPeerByPeerId(PeerId peerId); | ||
} |
83 changes: 83 additions & 0 deletions
83
ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright contributors to Hyperledger Besu. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.ethereum.eth.manager.peertask; | ||
|
||
import org.hyperledger.besu.ethereum.eth.manager.EthPeer; | ||
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData; | ||
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.SubProtocol; | ||
|
||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Represents a task to be executed on an EthPeer by the PeerTaskExecutor | ||
* | ||
* @param <T> The type of the result of this PeerTask | ||
*/ | ||
public interface PeerTask<T> { | ||
/** | ||
* Returns the SubProtocol used for this PeerTask | ||
* | ||
* @return the SubProtocol used for this PeerTask | ||
*/ | ||
SubProtocol getSubProtocol(); | ||
|
||
/** | ||
* Gets the request data to send to the EthPeer | ||
* | ||
* @return the request data to send to the EthPeer | ||
*/ | ||
MessageData getRequestMessage(); | ||
|
||
/** | ||
* Parses the MessageData response from the EthPeer | ||
* | ||
* @param messageData the response MessageData to be parsed | ||
* @return a T built from the response MessageData | ||
* @throws InvalidPeerTaskResponseException if the response messageData is invalid | ||
*/ | ||
T parseResponse(MessageData messageData) throws InvalidPeerTaskResponseException; | ||
|
||
/** | ||
* Gets the number of times this task may be attempted against other peers | ||
* | ||
* @return the number of times this task may be attempted against other peers | ||
*/ | ||
default int getRetriesWithOtherPeer() { | ||
return 5; | ||
} | ||
|
||
/** | ||
* Gets the number of times this task may be attempted against the same peer | ||
* | ||
* @return the number of times this task may be attempted against the same peer | ||
*/ | ||
default int getRetriesWithSamePeer() { | ||
return 5; | ||
} | ||
|
||
/** | ||
* Gets a Predicate that checks if an EthPeer is suitable for this PeerTask | ||
* | ||
* @return a Predicate that checks if an EthPeer is suitable for this PeerTask | ||
*/ | ||
Predicate<EthPeer> getPeerRequirementFilter(); | ||
|
||
/** | ||
* Checks if the supplied result is considered a success | ||
* | ||
* @return true if the supplied result is considered a success | ||
*/ | ||
boolean isSuccess(T result); | ||
} |
Oops, something went wrong.