mirror of https://github.com/hyperledger/besu
PAN-2723: Testing permissioning with static nodes behaviour (#1764)
* PAN-2723: Created Miner API transactions and conditions * PAN-2723: Added static nodes options to permissioned node builder * PAN-2723: Implemented permissioned node with static-nodes AT * PAN-2723: Renaming test * Refactoring test to use the waitForBlockHeight method Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>pull/2/head
parent
ef0f9793a9
commit
ab52ba3fe1
@ -0,0 +1,36 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.condition.miner; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static tech.pegasys.pantheon.tests.acceptance.dsl.WaitUtils.waitFor; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthMiningTransaction; |
||||
|
||||
public class MiningStatusCondition implements Condition { |
||||
|
||||
private final EthMiningTransaction transaction; |
||||
private final boolean expectedMiningStatus; |
||||
|
||||
public MiningStatusCondition(final EthMiningTransaction transaction, final boolean miningStatus) { |
||||
this.transaction = transaction; |
||||
this.expectedMiningStatus = miningStatus; |
||||
} |
||||
|
||||
@Override |
||||
public void verify(final Node node) { |
||||
waitFor(10, () -> assertThat(node.execute(transaction)).isEqualTo(expectedMiningStatus)); |
||||
} |
||||
} |
@ -0,0 +1,38 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.NodeRequests; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import org.web3j.protocol.core.methods.response.EthMining; |
||||
|
||||
public class EthMiningTransaction implements Transaction<Boolean> { |
||||
|
||||
EthMiningTransaction() {} |
||||
|
||||
@Override |
||||
public Boolean execute(final NodeRequests node) { |
||||
try { |
||||
EthMining response = node.eth().ethMining().send(); |
||||
assertThat(response).isNotNull(); |
||||
return response.isMining(); |
||||
} catch (final IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,41 @@ |
||||
/* |
||||
* Copyright 2019 ConsenSys AG. |
||||
* |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.miner; |
||||
|
||||
import org.web3j.protocol.Web3jService; |
||||
import org.web3j.protocol.core.Request; |
||||
|
||||
public class MinerRequestFactory { |
||||
|
||||
private final Web3jService web3jService; |
||||
|
||||
public MinerRequestFactory(final Web3jService web3jService) { |
||||
this.web3jService = web3jService; |
||||
} |
||||
|
||||
Request<?, org.web3j.protocol.core.methods.response.VoidResponse> minerStart() { |
||||
return new Request<>( |
||||
"miner_start", |
||||
null, |
||||
web3jService, |
||||
org.web3j.protocol.core.methods.response.VoidResponse.class); |
||||
} |
||||
|
||||
Request<?, org.web3j.protocol.core.methods.response.VoidResponse> minerStop() { |
||||
return new Request<>( |
||||
"miner_stop", |
||||
null, |
||||
web3jService, |
||||
org.web3j.protocol.core.methods.response.VoidResponse.class); |
||||
} |
||||
} |
@ -0,0 +1,29 @@ |
||||
/* |
||||
* Copyright 2019 ConsenSys AG. |
||||
* |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.miner; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.NodeRequests; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; |
||||
|
||||
public class MinerStartTransaction implements Transaction<Void> { |
||||
|
||||
@Override |
||||
public Void execute(final NodeRequests node) { |
||||
try { |
||||
node.miner().minerStart().send(); |
||||
} catch (Exception e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
return null; |
||||
} |
||||
} |
@ -0,0 +1,29 @@ |
||||
/* |
||||
* Copyright 2019 ConsenSys AG. |
||||
* |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.miner; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.NodeRequests; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; |
||||
|
||||
public class MinerStopTransaction implements Transaction<Void> { |
||||
|
||||
@Override |
||||
public Void execute(final NodeRequests node) { |
||||
try { |
||||
node.miner().minerStop().send(); |
||||
} catch (Exception e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
return null; |
||||
} |
||||
} |
@ -0,0 +1,24 @@ |
||||
/* |
||||
* Copyright 2019 ConsenSys AG. |
||||
* |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.miner; |
||||
|
||||
public class MinerTransactions { |
||||
|
||||
public MinerStartTransaction minerStart() { |
||||
return new MinerStartTransaction(); |
||||
} |
||||
|
||||
public MinerStopTransaction minerStop() { |
||||
return new MinerStopTransaction(); |
||||
} |
||||
} |
@ -0,0 +1,81 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.permissioning; |
||||
|
||||
import static java.util.stream.Collectors.toList; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.RunnableNode; |
||||
|
||||
import java.net.URI; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
public class NodesSmartContractPermissioningStaticNodesAcceptanceTest |
||||
extends NodeSmartContractPermissioningAcceptanceTestBase { |
||||
|
||||
private Node miner; |
||||
private Node permissionedNode; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
miner = miner("miner"); |
||||
permissionedCluster.start(miner); |
||||
} |
||||
|
||||
@Test |
||||
public void onlyTrustStaticNodesWhileOutOfSync() { |
||||
// wait for some blocks so the permissioned node has some syncing to do
|
||||
waitForBlockHeight(miner, 50); |
||||
stopMining(miner); |
||||
|
||||
// start permissioned node with miner node in the static nodes list
|
||||
permissionedNode = permissionedNodeWithStaticNodes(Arrays.asList(miner)); |
||||
permissionedCluster.addNode(permissionedNode); |
||||
|
||||
// as soon as we start the node should connect to static nodes
|
||||
permissionedNode.verify(net.awaitPeerCount(1)); |
||||
waitForBlockHeight(permissionedNode, 50); |
||||
|
||||
// after syncing up with the network the node won't trust static nodes anymore
|
||||
permissionedNode.verify(net.awaitPeerCount(0)); |
||||
} |
||||
|
||||
private void stopMining(final Node node) { |
||||
node.execute(minerTransactions.minerStop()); |
||||
node.verify(eth.miningStatus(false)); |
||||
} |
||||
|
||||
private Node permissionedNodeWithStaticNodes(final List<Node> staticNodes) { |
||||
return permissionedNodeBuilder |
||||
.name("node-with-static-nodes") |
||||
.genesisFile(GENESIS_FILE) |
||||
.nodesContractEnabled(CONTRACT_ADDRESS) |
||||
.staticNodes(mapNodesToEnodeURLs(staticNodes)) |
||||
.disableMining() |
||||
.build(); |
||||
} |
||||
|
||||
@NotNull |
||||
private List<String> mapNodesToEnodeURLs(final List<Node> staticNodes) { |
||||
return staticNodes.stream() |
||||
.map(node -> (RunnableNode) node) |
||||
.map(RunnableNode::enodeUrl) |
||||
.map(URI::toASCIIString) |
||||
.collect(toList()); |
||||
} |
||||
} |
Loading…
Reference in new issue