Better errors for when permissioning contract is set up wrong (#1296)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Chris Mckay 6 years ago committed by GitHub
parent cfaa83747a
commit e187429fed
  1. 28
      ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/transaction/TransactionSimulator.java
  2. 18
      ethereum/permissioning/src/main/java/tech/pegasys/pantheon/ethereum/permissioning/SmartContractPermissioningController.java
  3. 37
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/SmartContractPermissioningControllerTest.java
  4. 64
      ethereum/permissioning/src/test/resources/SmartContractPermissioningControllerTest/corruptSmartPermissioning.json
  5. 28
      ethereum/permissioning/src/test/resources/SmartContractPermissioningControllerTest/noSmartPermissioning.json

@ -126,4 +126,32 @@ public class TransactionSimulator {
return Optional.of(new TransactionSimulatorResult(transaction, result));
}
public Optional<Boolean> doesAddressExist(final Address address, final Hash blockHeaderHash) {
final BlockHeader header = blockchain.getBlockHeader(blockHeaderHash).orElse(null);
return doesAddressExist(address, header);
}
public Optional<Boolean> doesAddressExist(final Address address, final long blockNumber) {
final BlockHeader header = blockchain.getBlockHeader(blockNumber).orElse(null);
return doesAddressExist(address, header);
}
public Optional<Boolean> doesAddressExistAtHead(final Address address) {
return doesAddressExist(address, blockchain.getChainHeadHeader());
}
public Optional<Boolean> doesAddressExist(final Address address, final BlockHeader header) {
if (header == null) {
return Optional.empty();
}
final MutableWorldState worldState =
worldStateArchive.getMutable(header.getStateRoot()).orElse(null);
if (worldState == null) {
return Optional.empty();
}
return Optional.of(worldState.get(address) != null);
}
}

@ -80,9 +80,27 @@ public class SmartContractPermissioningController implements NodePermissioningPr
final CallParameter callParams =
new CallParameter(null, contractAddress, -1, null, null, payload);
final Optional<Boolean> contractExists =
transactionSimulator.doesAddressExistAtHead(contractAddress);
if (contractExists.isPresent() && !contractExists.get()) {
throw new IllegalStateException("Permissioning contract does not exist");
}
final Optional<TransactionSimulatorResult> result =
transactionSimulator.processAtHead(callParams);
if (result.isPresent()) {
switch (result.get().getResult().getStatus()) {
case INVALID:
throw new IllegalStateException("Permissioning transaction found to be Invalid");
case FAILED:
throw new IllegalStateException("Permissioning transaction failed when processing");
default:
break;
}
}
return result.map(r -> checkTransactionResult(r.getOutput())).orElse(false);
}

@ -14,6 +14,7 @@ package tech.pegasys.pantheon.ethereum.permissioning;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain;
import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive;
@ -149,4 +150,40 @@ public class SmartContractPermissioningControllerTest {
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab63@[1:2:3:4:5:6:7:8]:30304")))
.isFalse();
}
@Test
public void testPermissioningContractMissing() throws IOException {
final SmartContractPermissioningController controller =
setupController(
"SmartContractPermissioningControllerTest/noSmartPermissioning.json",
"0x0000000000000000000000000000000000001234");
assertThatThrownBy(
() ->
controller.isPermitted(
EnodeURL.fromString(
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab61@[1:2:3:4:5:6:7:8]:30303"),
EnodeURL.fromString(
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab63@[1:2:3:4:5:6:7:8]:30304")))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Permissioning contract does not exist");
}
@Test
public void testPermissioningContractCorrupt() throws IOException {
final SmartContractPermissioningController controller =
setupController(
"SmartContractPermissioningControllerTest/corruptSmartPermissioning.json",
"0x0000000000000000000000000000000000001234");
assertThatThrownBy(
() ->
controller.isPermitted(
EnodeURL.fromString(
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab61@[1:2:3:4:5:6:7:8]:30303"),
EnodeURL.fromString(
"enode://1234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab63@[1:2:3:4:5:6:7:8]:30304")))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Permissioning transaction failed when processing");
}
}

@ -0,0 +1,64 @@
{
"config": {
"chainId": 2018,
"homesteadBlock": 0,
"daoForkBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"constantinopleFixBlock": 0,
"ethash": {
"fixeddifficulty": 100
}
},
"nonce": "0x42",
"timestamp": "0x0",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x1000000",
"difficulty": "0x10000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"0x0000000000000000000000000000000000001234": {
"comment": "Smart permissioning contract",
"balance": "0",
"code": "aaaa",
"storage": {
// ipv4 values
// ipv4 1 enode high
"0xc4c3a3f99b26e5e534b71d6f33ca6ea5c174decfb16dd7237c60eff9774ef4a4": "0x937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0",
// ipv4 1 enode low
"0xc4c3a3f99b26e5e534b71d6f33ca6ea5c174decfb16dd7237c60eff9774ef4a3": "0x6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012",
// ipv4 1 port and ip together
"0xc4c3a3f99b26e5e534b71d6f33ca6ea5c174decfb16dd7237c60eff9774ef4a5": "0x765f00000000000000000000ffffc0a80001",
// ipv4 2 enode high
"0x329603b541cc3f56680450f275b934abc018b0bdbf0f839d3f00aca66d67b743": "0x6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012",
// ipv4 2 enode low
"0x329603b541cc3f56680450f275b934abc018b0bdbf0f839d3f00aca66d67b744": "0x937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0",
// ipv4 2 port and ip together
"0x329603b541cc3f56680450f275b934abc018b0bdbf0f839d3f00aca66d67b745": "0x766000000000000000000000ffffc0a80001",
// ipv6 values
// ipv6 1 enode high
"0x1ab0d5b7c4004723d5020bf461a01709cbabee4aaf0b414f92d903f5779b33d9": "0x1234000000000000000000000000000000000000000000000000000000000000",
// ipv6 1 enode low
"0x1ab0d5b7c4004723d5020bf461a01709cbabee4aaf0b414f92d903f5779b33da": "0xab61",
// ipv6 1 port followed by ip
"0x1ab0d5b7c4004723d5020bf461a01709cbabee4aaf0b414f92d903f5779b33db": "0x765f00010002000300040005000600070008",
// ipv6 2 enode high
"0x12b025583b6b680e25edb31eb9fd6afae6c4b2c1d8c4c3beb1066be39601abc5": "0x1234000000000000000000000000000000000000000000000000000000000000",
// ipv6 2 enode low
"0x12b025583b6b680e25edb31eb9fd6afae6c4b2c1d8c4c3beb1066be39601abc6": "0x000000000000000000000000000000000000000000000000000000000000ab62",
// ipv6 2 port followed by ip
"0x12b025583b6b680e25edb31eb9fd6afae6c4b2c1d8c4c3beb1066be39601abc7": "0x0000000000000000000000000000766000010002000300040005000600070008"
}
}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

@ -0,0 +1,28 @@
{
"config": {
"chainId": 2018,
"homesteadBlock": 0,
"daoForkBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"constantinopleFixBlock": 0,
"ethash": {
"fixeddifficulty": 100
}
},
"nonce": "0x42",
"timestamp": "0x0",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x1000000",
"difficulty": "0x10000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
Loading…
Cancel
Save