use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.
the class DeFramerTest method decode_shouldHandleRemoteSocketAddressIsNull.
@Test
public void decode_shouldHandleRemoteSocketAddressIsNull() {
final Peer peer = createRemotePeer();
final PeerInfo remotePeerInfo = new PeerInfo(p2pVersion, clientId, capabilities, 0, peer.getId());
HelloMessage helloMessage = HelloMessage.create(remotePeerInfo);
ByteBuf data = Unpooled.wrappedBuffer(helloMessage.getData().toArray());
when(framer.deframe(any())).thenReturn(new RawMessage(helloMessage.getCode(), helloMessage.getData())).thenReturn(null);
when(ctx.channel().remoteAddress()).thenReturn(null);
ChannelFuture future = NettyMocks.channelFuture(true);
when(ctx.writeAndFlush(any())).thenReturn(future);
List<Object> out = new ArrayList<>();
deFramer.decode(ctx, data, out);
assertThat(connectFuture).isDone();
assertThat(connectFuture).isCompletedExceptionally();
assertThatThrownBy(connectFuture::get).hasCauseInstanceOf(PeerChannelClosedException.class);
verify(ctx).close();
assertThat(out).isEmpty();
}
use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.
the class DeFramerTest method decode_handlesUnexpectedPeerId.
@Test
public void decode_handlesUnexpectedPeerId() {
ChannelFuture future = NettyMocks.channelFuture(false);
when(channel.closeFuture()).thenReturn(future);
final Peer peer = createRemotePeer();
final Bytes mismatchedId = Peer.randomId();
final PeerInfo remotePeerInfo = new PeerInfo(p2pVersion, clientId, capabilities, peer.getEnodeURL().getListeningPortOrZero(), mismatchedId);
final DeFramer deFramer = createDeFramer(peer);
HelloMessage helloMessage = HelloMessage.create(remotePeerInfo);
ByteBuf data = Unpooled.wrappedBuffer(helloMessage.getData().toArray());
when(framer.deframe(eq(data))).thenReturn(new RawMessage(helloMessage.getCode(), helloMessage.getData())).thenReturn(null);
List<Object> out = new ArrayList<>();
deFramer.decode(ctx, data, out);
assertThat(connectFuture).isDone();
assertThat(connectFuture).isCompletedExceptionally();
assertThatThrownBy(connectFuture::get).hasCauseInstanceOf(UnexpectedPeerConnectionException.class).hasMessageContaining("Expected id " + peer.getId().toString());
assertThat(out).isEmpty();
// Next phase of pipeline should be setup
verify(pipeline, times(1)).addLast(any());
}
use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.
the class PeerPermissionsDenylistTest method remove_peer.
@Test
public void remove_peer() {
PeerPermissionsDenylist denylist = PeerPermissionsDenylist.create();
Peer peer = createPeer();
denylist.add(peer);
final AtomicInteger callbackCount = new AtomicInteger(0);
denylist.subscribeUpdate((restricted, affectedPeers) -> {
callbackCount.incrementAndGet();
assertThat(restricted).isFalse();
assertThat(affectedPeers).contains(Collections.singletonList(peer));
});
assertThat(callbackCount).hasValue(0);
denylist.remove(peer);
assertThat(callbackCount).hasValue(1);
}
use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.
the class PeerPermissionsDenylistTest method subscribeUpdate.
@Test
public void subscribeUpdate() {
PeerPermissionsDenylist denylist = PeerPermissionsDenylist.create();
final AtomicInteger callbackCount = new AtomicInteger(0);
final AtomicInteger restrictedCallbackCount = new AtomicInteger(0);
Peer peer = createPeer();
denylist.subscribeUpdate((permissionsRestricted, affectedPeers) -> {
callbackCount.incrementAndGet();
if (permissionsRestricted) {
restrictedCallbackCount.incrementAndGet();
}
});
checkPermissions(denylist, peer, true);
assertThat(callbackCount).hasValue(0);
assertThat(restrictedCallbackCount).hasValue(0);
denylist.add(peer);
assertThat(callbackCount).hasValue(1);
assertThat(restrictedCallbackCount).hasValue(1);
denylist.add(peer);
assertThat(callbackCount).hasValue(1);
assertThat(restrictedCallbackCount).hasValue(1);
denylist.remove(peer);
assertThat(callbackCount).hasValue(2);
assertThat(restrictedCallbackCount).hasValue(1);
denylist.remove(peer);
assertThat(callbackCount).hasValue(2);
assertThat(restrictedCallbackCount).hasValue(1);
denylist.add(peer);
assertThat(callbackCount).hasValue(3);
assertThat(restrictedCallbackCount).hasValue(2);
}
use of org.hyperledger.besu.ethereum.p2p.peers.Peer in project besu by hyperledger.
the class PeerPermissionsDenylistTest method createWithLimitedCapacity.
@Test
public void createWithLimitedCapacity() {
final PeerPermissionsDenylist denylist = PeerPermissionsDenylist.create(2);
Peer peerA = createPeer();
Peer peerB = createPeer();
Peer peerC = createPeer();
// All peers are initially permitted
checkPermissions(denylist, peerA, true);
checkPermissions(denylist, peerB, true);
checkPermissions(denylist, peerC, true);
// Add peerA
denylist.add(peerA);
checkPermissions(denylist, peerA, false);
checkPermissions(denylist, peerB, true);
checkPermissions(denylist, peerC, true);
// Add peerB
denylist.add(peerB);
checkPermissions(denylist, peerA, false);
checkPermissions(denylist, peerB, false);
checkPermissions(denylist, peerC, true);
// Add peerC
// Limit is exceeded and peerA should drop off of the list and be allowed
denylist.add(peerC);
checkPermissions(denylist, peerA, true);
checkPermissions(denylist, peerB, false);
checkPermissions(denylist, peerC, false);
}
Aggregations